diff --git a/config/src/config.rs b/config/src/config.rs index 6093c9e76d1..a4585cc3f7c 100644 --- a/config/src/config.rs +++ b/config/src/config.rs @@ -94,6 +94,9 @@ pub struct Config { /// The color palette pub colors: Option, + #[dynamic(default)] + pub switch_to_last_active_tab_when_closing_tab: bool, + #[dynamic(default)] pub window_frame: WindowFrameConfig, diff --git a/mux/src/window.rs b/mux/src/window.rs index cc0c617e01f..25bb0cdd497 100644 --- a/mux/src/window.rs +++ b/mux/src/window.rs @@ -128,17 +128,31 @@ impl Window { pub fn remove_by_idx(&mut self, idx: usize) -> Rc { self.invalidate(); let active = self.get_active().map(Rc::clone); - let tab = self.tabs.remove(idx); - self.fixup_active_tab_after_removal(active); - tab + self.do_remove_idx(idx, active) } pub fn remove_by_id(&mut self, id: TabId) { let active = self.get_active().map(Rc::clone); if let Some(idx) = self.idx_by_id(id) { - self.tabs.remove(idx); + self.do_remove_idx(idx, active); + } + } + + fn do_remove_idx(&mut self, idx: usize, active: Option>) -> Rc { + if let (Some(active), Some(removing)) = (&active, self.tabs.get(idx)) { + if active.tab_id() == removing.tab_id() + && config::configuration().switch_to_last_active_tab_when_closing_tab + { + // If we are removing the active tab, switch back to + // the previously active tab + if let Some(last_active) = self.get_last_active_idx() { + self.set_active_without_saving(last_active); + } + } } + let tab = self.tabs.remove(idx); self.fixup_active_tab_after_removal(active); + tab } pub fn get_active(&self) -> Option<&Rc> {