From adbe545d44ccf22e1890c92eb64368a14fa50c97 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Tue, 13 Apr 2021 08:18:52 -0700 Subject: [PATCH] fix window vanishing when a tab is closed We weren't fixing up the active tab position correctly after removing a tab. I think this bug crept in around the ActivateLastTab changes. We now try harder to set active idx back to the tab that was active prior to the remove, and ensure that we set the active index to something within range if it was the active tab that was removed. Refs: https://github.com/wez/wezterm/issues/690 --- docs/changelog.md | 1 + mux/src/lib.rs | 1 + mux/src/window.rs | 32 ++++++++++++++++++++++++++------ 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/docs/changelog.md b/docs/changelog.md index 08d8a4058..25c356264 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -24,6 +24,7 @@ As features stabilize some brief notes about them will accumulate here. * Improved font IO performance and memory usage on all platforms * New [window:toast_notification](config/lua/window/toast_notification.md) method for showing desktop notifications. [#619](https://github.com/wez/wezterm/issues/619) * Fixed: half-pixel gaps in ligatured/double-wide glyphs depending on the font size [#614](https://github.com/wez/wezterm/issues/614) +* Fixed: Window could vanish if a tab closed while the rightmost tab was active(!) [#690](https://github.com/wez/wezterm/issues/690) ### 20210405-110924-a5bb5be8 diff --git a/mux/src/lib.rs b/mux/src/lib.rs index 4853d8aa9..78337b234 100644 --- a/mux/src/lib.rs +++ b/mux/src/lib.rs @@ -451,6 +451,7 @@ impl Mux { } for window_id in dead_windows { + log::trace!("window {} is dead", window_id); self.remove_window_internal(window_id); } } diff --git a/mux/src/window.rs b/mux/src/window.rs index e164628dc..0f0648354 100644 --- a/mux/src/window.rs +++ b/mux/src/window.rs @@ -93,18 +93,35 @@ impl Window { None } + fn fixup_active_tab_after_removal(&mut self, active: Option>) { + let len = self.tabs.len(); + if let Some(active) = active { + for (idx, tab) in self.tabs.iter().enumerate() { + if tab.tab_id() == active.tab_id() { + self.set_active_without_saving(idx); + return; + } + } + } + + if len > 0 && self.active >= len { + self.set_active_without_saving(len - 1); + } + } + pub fn remove_by_idx(&mut self, idx: usize) -> Rc { self.invalidated = true; - self.tabs.remove(idx) + let active = self.get_active().map(Rc::clone); + let tab = self.tabs.remove(idx); + self.fixup_active_tab_after_removal(active); + tab } pub fn remove_by_id(&mut self, id: TabId) -> bool { if let Some(idx) = self.idx_by_id(id) { + let active = self.get_active().map(Rc::clone); self.tabs.remove(idx); - let len = self.tabs.len(); - if len > 0 && self.active == idx && idx >= len { - self.set_active_without_saving(len - 1); - } + self.fixup_active_tab_after_removal(active); true } else { false @@ -172,13 +189,15 @@ impl Window { invalidated = true; } if tab.is_dead() { - return Some(tab.tab_id()); + Some(tab.tab_id()) } else { None } }) .collect(); + for tab_id in dead { + log::trace!("Window::prune_dead_tabs: tab_id {} is dead", tab_id); self.remove_by_id(tab_id); invalidated = true; } @@ -199,6 +218,7 @@ impl Window { }) .collect(); for tab_id in dead { + log::trace!("Window::prune_dead_tabs: (live) tab_id {} is dead", tab_id); self.remove_by_id(tab_id); }