diff options
| author | Ogulcan Celik <ogulcancelik@gmail.com> | 2026-03-29 19:54:52 +0300 |
|---|---|---|
| committer | Ogulcan Celik <ogulcancelik@gmail.com> | 2026-03-29 19:54:52 +0300 |
| commit | bf6b6d779a63d8952d8db0a7de5daa3454984620 (patch) | |
| tree | 06776548463ce0b70177f85ace997d6e33f07533 /src/workspace.rs | |
| parent | a5847a8039d2754dd2908a13e4625b31f7404d9a (diff) | |
feat(api): use compact public workspace and pane ids
Diffstat (limited to 'src/workspace.rs')
| -rw-r--r-- | src/workspace.rs | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/workspace.rs b/src/workspace.rs index 78992a73..ff8e302f 100644 --- a/src/workspace.rs +++ b/src/workspace.rs @@ -17,6 +17,10 @@ pub struct Workspace { /// Identity source for this workspace. pub root_pane: PaneId, pub layout: TileLayout, + /// Stable-ish public pane numbers within this workspace. + /// New panes append at the end; closing a pane compacts higher numbers down. + pub public_pane_numbers: HashMap<PaneId, usize>, + pub(crate) next_public_pane_number: usize, /// Pane state — always present, testable without PTYs. pub panes: HashMap<PaneId, PaneState>, /// Pane runtimes — only present in production (empty in tests). @@ -37,6 +41,8 @@ impl Workspace { let mut panes = HashMap::new(); panes.insert(root_id, PaneState::new()); + let mut public_pane_numbers = HashMap::new(); + public_pane_numbers.insert(root_id, 1); let mut runtimes = HashMap::new(); runtimes.insert(root_id, runtime); @@ -45,6 +51,8 @@ impl Workspace { custom_name: None, root_pane: root_id, layout, + public_pane_numbers, + next_public_pane_number: 2, panes, runtimes, zoomed: false, @@ -65,6 +73,9 @@ impl Workspace { cwd.unwrap_or_else(|| std::env::current_dir().unwrap_or_else(|_| "/".into())); let runtime = PaneRuntime::spawn(new_id, rows, cols, actual_cwd, self.events.clone())?; self.panes.insert(new_id, PaneState::new()); + self.public_pane_numbers + .insert(new_id, self.next_public_pane_number); + self.next_public_pane_number += 1; self.runtimes.insert(new_id, runtime); self.zoomed = false; Ok(new_id) @@ -94,6 +105,14 @@ impl Workspace { self.layout.focus_pane(prev_focus); } + if let Some(removed_number) = self.public_pane_numbers.remove(&pane_id) { + for number in self.public_pane_numbers.values_mut() { + if *number > removed_number { + *number -= 1; + } + } + self.next_public_pane_number = self.public_pane_numbers.len() + 1; + } self.panes.remove(&pane_id); self.runtimes.remove(&pane_id); self.zoomed = false; @@ -110,6 +129,10 @@ impl Workspace { self.layout.pane_ids().into_iter().find(|id| *id != closing) } + pub fn public_pane_number(&self, pane_id: PaneId) -> Option<usize> { + self.public_pane_numbers.get(&pane_id).copied() + } + /// Get the runtime for the focused pane. pub fn focused_runtime(&self) -> Option<&PaneRuntime> { self.runtimes.get(&self.layout.focused()) @@ -251,10 +274,14 @@ impl Workspace { let (layout, root_id) = TileLayout::new(); let mut panes = HashMap::new(); panes.insert(root_id, PaneState::new()); + let mut public_pane_numbers = HashMap::new(); + public_pane_numbers.insert(root_id, 1); Self { custom_name: Some(name.to_string()), root_pane: root_id, layout, + public_pane_numbers, + next_public_pane_number: 2, panes, runtimes: HashMap::new(), zoomed: false, @@ -266,6 +293,9 @@ impl Workspace { pub fn test_split(&mut self, direction: Direction) -> PaneId { let new_id = self.layout.split_focused(direction); self.panes.insert(new_id, PaneState::new()); + self.public_pane_numbers + .insert(new_id, self.next_public_pane_number); + self.next_public_pane_number += 1; new_id } } |
