diff --git a/mux/src/activity.rs b/mux/src/activity.rs index 504d17240..2760a9e9a 100644 --- a/mux/src/activity.rs +++ b/mux/src/activity.rs @@ -1,4 +1,5 @@ //! Keeps track of the number of user-initiated activities +use crate::Mux; use std::sync::atomic::{AtomicUsize, Ordering}; static COUNT: AtomicUsize = AtomicUsize::new(0); @@ -25,5 +26,11 @@ impl Activity { impl Drop for Activity { fn drop(&mut self) { COUNT.fetch_sub(1, Ordering::SeqCst); + + promise::spawn::spawn_into_main_thread(async move { + let mux = Mux::get().unwrap(); + mux.prune_dead_windows(); + }) + .detach(); } } diff --git a/mux/src/lib.rs b/mux/src/lib.rs index 78337b234..b1fa03a85 100644 --- a/mux/src/lib.rs +++ b/mux/src/lib.rs @@ -41,6 +41,7 @@ pub enum MuxNotification { pane_id: PaneId, alert: wezterm_term::Alert, }, + Empty, } static SUB_ID: AtomicUsize = AtomicUsize::new(0); @@ -184,6 +185,11 @@ fn read_from_pane_pty(pane_id: PaneId, banner: Option, mut reader: Box { promise::spawn::spawn_into_main_thread(async move { @@ -454,6 +460,10 @@ impl Mux { log::trace!("window {} is dead", window_id); self.remove_window_internal(window_id); } + + if self.is_empty() { + self.notify(MuxNotification::Empty); + } } pub fn kill_window(&self, window_id: WindowId) { diff --git a/wezterm-gui/src/frontend.rs b/wezterm-gui/src/frontend.rs index 1cfe708c0..6fc0aef55 100644 --- a/wezterm-gui/src/frontend.rs +++ b/wezterm-gui/src/frontend.rs @@ -66,6 +66,12 @@ impl GuiFrontEnd { pane_id: _, alert: Alert::TitleMaybeChanged, } => {} + MuxNotification::Empty => { + if mux::activity::Activity::count() == 0 { + log::trace!("Mux is now empty, terminate gui"); + Connection::get().unwrap().terminate_message_loop(); + } + } } true } else { @@ -76,17 +82,6 @@ impl GuiFrontEnd { } pub fn run_forever(&self) -> anyhow::Result<()> { - self.connection - .schedule_timer(std::time::Duration::from_millis(200), move || { - if mux::activity::Activity::count() == 0 { - let mux = Mux::get().unwrap(); - mux.prune_dead_windows(); - if mux.is_empty() { - Connection::get().unwrap().terminate_message_loop(); - } - } - }); - self.connection.run_message_loop() } } diff --git a/wezterm-mux-server-impl/src/dispatch.rs b/wezterm-mux-server-impl/src/dispatch.rs index 15aad8b9f..7d8ed5433 100644 --- a/wezterm-mux-server-impl/src/dispatch.rs +++ b/wezterm-mux-server-impl/src/dispatch.rs @@ -86,6 +86,7 @@ where handler.schedule_pane_push(pane_id); } Ok(Item::Notif(MuxNotification::WindowCreated(_window_id))) => {} + Ok(Item::Notif(MuxNotification::Empty)) => {} Err(err) => { log::error!("process_async Err {}", err); return Ok(());