diff --git a/src/guiloop/glutinloop.rs b/src/guiloop/glutinloop.rs index 49e8a7bbe..3dccb4d2f 100644 --- a/src/guiloop/glutinloop.rs +++ b/src/guiloop/glutinloop.rs @@ -136,10 +136,8 @@ impl GuiSystem for GlutinGuiSystem { Box::new(self.event_loop.poll_tx.clone()) } - fn gui_executor(&self) -> Arc { - Arc::new(GlutinGuiExecutor { - tx: self.event_loop.gui_tx.clone(), - }) + fn gui_executor(&self) -> Arc { + self.event_loop.gui_executor() } fn run_forever(&self) -> Result<(), Error> { @@ -222,8 +220,15 @@ impl GuiEventLoop { res } + fn gui_executor(&self) -> Arc { + Arc::new(GlutinGuiExecutor { + tx: self.gui_tx.clone(), + }) + } + pub fn register_tab(&self, tab: &Rc) -> Result<(), Error> { - self.mux.add_tab(Box::new(self.poll_tx.clone()), tab) + self.mux + .add_tab(self.gui_executor(), Box::new(self.poll_tx.clone()), tab) } fn do_spawn_new_window( @@ -233,7 +238,7 @@ impl GuiEventLoop { ) -> Result<(), Error> { let tab = spawn_tab(&config, None)?; let sender = Box::new(self.poll_tx.clone()); - self.mux.add_tab(sender, &tab)?; + self.mux.add_tab(self.gui_executor(), sender, &tab)?; let events = Self::get().expect("to be called on gui thread"); let window = GliumTerminalWindow::new(&events, &fonts, &config, &tab)?; diff --git a/src/guiloop/mod.rs b/src/guiloop/mod.rs index 9884e082d..191e082cd 100644 --- a/src/guiloop/mod.rs +++ b/src/guiloop/mod.rs @@ -97,7 +97,7 @@ pub trait GuiSystem { fn pty_sender(&self) -> Box; - fn gui_executor(&self) -> Arc; + fn gui_executor(&self) -> Arc; } pub mod glutinloop; diff --git a/src/main.rs b/src/main.rs index dbb09797e..96b55fe3f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -172,7 +172,7 @@ fn spawn_window( fontconfig: &Rc, ) -> Result<(), Error> { let tab = spawn_tab(config, cmd)?; - mux.add_tab(gui.pty_sender(), &tab)?; + mux.add_tab(gui.gui_executor(), gui.pty_sender(), &tab)?; gui.spawn_new_window(config, &fontconfig, &tab) } diff --git a/src/mux/mod.rs b/src/mux/mod.rs index dd938f221..c284722ec 100644 --- a/src/mux/mod.rs +++ b/src/mux/mod.rs @@ -1,10 +1,12 @@ use crate::futurecore::Spawner; use crate::guicommon::tabs::{Tab, TabId}; use failure::Error; +use promise::Executor; use std::cell::RefCell; use std::collections::HashMap; use std::io::Read; use std::rc::Rc; +use std::sync::Arc; use std::thread; use term::TerminalHost; use termwiz::hyperlink::Hyperlink; @@ -28,6 +30,7 @@ pub trait PtyEventSender: Send { } fn read_from_tab_pty( + executor: Arc, spawner: Spawner, sender: Box, tab_id: TabId, @@ -107,17 +110,27 @@ impl Mux { self.tabs.borrow().get(&tab_id).map(Rc::clone) } - pub fn add_tab(&self, sender: Box, tab: &Rc) -> Result<(), Error> { + pub fn add_tab( + &self, + executor: Arc, + sender: Box, + tab: &Rc, + ) -> Result<(), Error> { self.tabs.borrow_mut().insert(tab.tab_id(), Rc::clone(tab)); let reader = tab.reader()?; let tab_id = tab.tab_id(); let spawner = self.spawner.borrow().as_ref().unwrap().clone(); - thread::spawn(move || read_from_tab_pty(spawner, sender, tab_id, reader)); + thread::spawn(move || read_from_tab_pty(executor, spawner, sender, tab_id, reader)); Ok(()) } + pub fn remove_tab(&self, tab_id: TabId) { + eprintln!("removing tab {}", tab_id); + self.tabs.borrow_mut().remove(&tab_id); + } + pub fn process_pty_event(&self, event: PtyEvent) -> Result<(), Error> { match event { PtyEvent::Data { tab_id, data } => { @@ -134,7 +147,7 @@ impl Mux { // The fact that we woke up is enough to trigger each // window to check for termination eprintln!("tab {} terminated", tab_id); - self.tabs.borrow_mut().remove(&tab_id); + self.remove_tab(tab_id); } } Ok(())