diff --git a/src/mux/mod.rs b/src/mux/mod.rs index e9a58124e..f62c2789e 100644 --- a/src/mux/mod.rs +++ b/src/mux/mod.rs @@ -201,10 +201,16 @@ impl Mux { pub fn remove_tab(&self, tab_id: TabId) { debug!("removing tab {}", tab_id); self.tabs.borrow_mut().remove(&tab_id); + self.prune_dead_windows(); + } + + pub fn prune_dead_windows(&self) { + let live_tab_ids: Vec = self.tabs.borrow().keys().cloned().collect(); let mut windows = self.windows.borrow_mut(); let mut dead_windows = vec![]; for (window_id, win) in windows.iter_mut() { - if win.remove_by_id(tab_id) && win.is_empty() { + win.prune_dead_tabs(&live_tab_ids); + if win.is_empty() { dead_windows.push(*window_id); } } @@ -273,6 +279,15 @@ impl Mux { pub fn iter_domains(&self) -> Vec> { self.domains.borrow().values().cloned().collect() } + + pub fn domain_was_detached(&self, domain: DomainId) { + self.tabs + .borrow_mut() + .retain(|_tab_id, tab| tab.domain_id() != domain); + // Ideally we'd do this here, but that seems to cause problems + // at the moment: + // self.prune_dead_windows(); + } } #[derive(Debug, Fail)] diff --git a/src/mux/window.rs b/src/mux/window.rs index cafc0c2ae..4a3219a3b 100644 --- a/src/mux/window.rs +++ b/src/mux/window.rs @@ -85,4 +85,25 @@ impl Window { pub fn iter(&self) -> impl Iterator> { self.tabs.iter() } + + pub fn prune_dead_tabs(&mut self, live_tab_ids: &[TabId]) { + let dead: Vec = self + .tabs + .iter() + .filter_map(|tab| { + if live_tab_ids + .iter() + .find(|&&id| id == tab.tab_id()) + .is_none() + { + Some(tab.tab_id()) + } else { + None + } + }) + .collect(); + for tab_id in dead { + self.remove_by_id(tab_id); + } + } } diff --git a/src/server/domain.rs b/src/server/domain.rs index 0ad2d5f24..0a18f7ee7 100644 --- a/src/server/domain.rs +++ b/src/server/domain.rs @@ -99,6 +99,8 @@ impl ClientDomain { pub fn perform_detach(&self) { log::error!("detached domain {}", self.local_domain_id); self.inner.borrow_mut().take(); + let mux = Mux::get().unwrap(); + mux.domain_was_detached(self.local_domain_id); } pub fn remote_to_local_tab_id(&self, remote_tab_id: TabId) -> Option {