diff --git a/Cargo.toml b/Cargo.toml index 3521b9ef4..3fc6fbfe0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ base91 = { path = "base91" } bitflags = "1.0" clipboard = "0.5" dirs = "1.0" +downcast-rs = "1.0" euclid = "0.19" pretty_env_logger = "0.3" failure = "0.1" diff --git a/src/frontend/glium/glutinloop.rs b/src/frontend/glium/glutinloop.rs index eed659601..d2e054514 100644 --- a/src/frontend/glium/glutinloop.rs +++ b/src/frontend/glium/glutinloop.rs @@ -2,7 +2,7 @@ use crate::config::Config; use crate::font::FontConfiguration; use crate::frontend::glium::window::GliumTerminalWindow; use crate::frontend::guicommon::window::TerminalWindow; -use crate::frontend::FrontEnd; +use crate::frontend::{front_end, FrontEnd}; use crate::mux::tab::Tab; use crate::mux::{Mux, SessionTerminated}; use failure::{bail, Error}; @@ -38,10 +38,13 @@ struct GuiSender { impl GuiSender { pub fn send(&self, what: SpawnFunc) -> Result<(), Error> { - match GuiEventLoop::get() { + match front_end() { // If we can get a handle on the GuiEventLoop then we // are in the gui thread and can queue up func directly. - Some(event_loop) => event_loop.spawn_func(what), + Some(front_end) => match front_end.downcast_ref::() { + Some(f) => f.event_loop.spawn_func(what), + None => bail!("front_end was not a GlutinFrontEnd!?"), + }, // Otherwise, send it through the bounded channel, // which may block us None => match self.tx.send(what) { @@ -104,14 +107,9 @@ pub struct GlutinFrontEnd { event_loop: Rc, } -thread_local! { - static GLUTIN_EVENT_LOOP: RefCell>> = RefCell::new(None); -} - impl GlutinFrontEnd { pub fn try_new(mux: &Rc) -> Result, Error> { let event_loop = Rc::new(GuiEventLoop::new(mux)?); - GLUTIN_EVENT_LOOP.with(|f| *f.borrow_mut() = Some(Rc::clone(&event_loop))); Ok(Rc::new(Self { event_loop })) } } @@ -184,16 +182,6 @@ impl GuiEventLoop { }) } - pub fn get() -> Option> { - let mut res = None; - GLUTIN_EVENT_LOOP.with(|f| { - if let Some(me) = &*f.borrow() { - res = Some(Rc::clone(me)); - } - }); - res - } - fn spawn_func(&self, func: SpawnFunc) { self.gui_thread_sends.borrow_mut().push_back(func); } @@ -214,8 +202,11 @@ impl GuiEventLoop { tx: self.gui_tx.clone(), }, move || { - let myself = Self::get().expect("to be called on gui thread"); - let mut windows = myself.windows.borrow_mut(); + let front_end = front_end().expect("to be called on gui thread"); + let front_end = front_end + .downcast_ref::() + .expect("front_end to be GlutinFrontEnd"); + let mut windows = front_end.event_loop.windows.borrow_mut(); if let Some(window) = windows.by_id.get_mut(&window_id) { func(window) } else { @@ -272,8 +263,12 @@ impl GuiEventLoop { tx: self.gui_tx.clone(), }, move || { - let events = Self::get().expect("to be called on gui thread"); - let mut windows = events.windows.borrow_mut(); + let front_end = front_end().expect("to be called on gui thread"); + let front_end = front_end + .downcast_ref::() + .expect("front_end to be GlutinFrontEnd"); + + let mut windows = front_end.event_loop.windows.borrow_mut(); windows.by_id.remove(&window_id); Ok(()) diff --git a/src/frontend/mod.rs b/src/frontend/mod.rs index 9cb5c40d9..6406d9a8a 100644 --- a/src/frontend/mod.rs +++ b/src/frontend/mod.rs @@ -2,6 +2,7 @@ use crate::config::Config; use crate::font::FontConfiguration; use crate::mux::tab::Tab; use crate::mux::Mux; +use downcast_rs::{impl_downcast, Downcast}; use failure::{format_err, Error}; use lazy_static::lazy_static; use promise::Executor; @@ -102,7 +103,7 @@ impl std::str::FromStr for FrontEndSelection { } } -pub trait FrontEnd { +pub trait FrontEnd: Downcast { /// Run the event loop. Does not return until there is either a fatal /// error, or until there are no more windows left to manage. fn run_forever(&self) -> Result<(), Error>; @@ -116,3 +117,4 @@ pub trait FrontEnd { fn gui_executor(&self) -> Box; } +impl_downcast!(FrontEnd);