fix: preserve focus when closing a non-focused workspace (#1877)

refs #1328
This commit is contained in:
Yi-An Lai 2026-07-26 04:25:46 -07:00 committed by GitHub
parent e536bd8bd9
commit 165dca453d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 26 additions and 0 deletions

View File

@ -1697,6 +1697,10 @@ impl AppState {
crate::logging::workspace_closed(&workspace_id);
}
}
let active_workspace_id = self
.active
.and_then(|idx| self.workspaces.get(idx))
.map(|ws| ws.id.clone());
self.remove_plugin_pane_records(pane_ids);
for idx in close_indices.iter().rev() {
self.workspaces.remove(*idx);
@ -1709,6 +1713,12 @@ impl AppState {
self.tab_scroll = 0;
self.tab_scroll_follow_active = true;
} else {
// Keep focus on the previously focused workspace
if let Some(id) = active_workspace_id {
if let Some(idx) = self.workspaces.iter().position(|ws| ws.id == id) {
self.selected = idx;
}
}
if self.selected >= self.workspaces.len() {
self.selected = self.workspaces.len() - 1;
}
@ -4619,6 +4629,22 @@ mod tests {
assert_eq!(state.active, Some(0));
}
#[test]
fn close_non_focused_workspace_keeps_focus() {
let mut state = app_with_workspaces(&["a", "b", "c"]);
state.selected = 1;
state.active = Some(0);
state.close_selected_workspace();
assert_eq!(state.workspaces.len(), 2);
assert_eq!(state.workspaces[0].display_name(), "a");
assert_eq!(state.workspaces[1].display_name(), "c");
assert_eq!(state.selected, 0);
assert_eq!(state.active, Some(0));
state.assert_invariants_for_test();
}
#[test]
fn pane_died_last_pane_removes_workspace() {
let mut state = app_with_workspaces(&["a", "b"]);