From 3d7a313e3fbc50c63ed70568620766345bfc57a5 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 24 Feb 2019 09:02:49 -0800 Subject: [PATCH] refactor to make TabHost impls look similar --- src/gliumwindows.rs | 152 +++++++++++++++++++------------------- src/guicommon/host.rs | 5 ++ src/guiloop/glutinloop.rs | 2 +- src/guiloop/x11.rs | 2 +- src/xwindows/xwin.rs | 123 +++++++++++++++--------------- 5 files changed, 146 insertions(+), 138 deletions(-) diff --git a/src/gliumwindows.rs b/src/gliumwindows.rs index 965cabacf..6ea85a955 100644 --- a/src/gliumwindows.rs +++ b/src/gliumwindows.rs @@ -43,7 +43,54 @@ struct Host { fonts: Rc, } -impl HostHelper for Host {} +impl HostHelper for Host { + fn with_window Result<(), Error>>(&self, func: F) { + GuiEventLoop::with_window(&self.event_loop, self.display.gl_window().id(), func); + } + + fn new_window(&mut self) { + let event_loop = Rc::clone(&self.event_loop); + let config = Rc::clone(&self.config); + let fonts = Rc::clone(&self.fonts); + self.event_loop.spawn_fn(move || { + let (terminal, master, child, fonts) = spawn_window_impl(None, &config, &fonts)?; + let window = + GliumTerminalWindow::new(&event_loop, terminal, master, child, &fonts, &config)?; + + event_loop.add_window(window) + }); + } + fn new_tab(&mut self) { + self.with_window(|win| win.spawn_tab().map(|_| ())); + } + + fn toggle_full_screen(&mut self) { + if let Some(pos) = self.is_fullscreen.take() { + let window = self.display.gl_window(); + // Use simple fullscreen mode on macos, as wez personally + // prefers the faster transition to/from this mode than + // the Lion+ slow transition to a new Space. This could + // be made into a config option if someone really wanted + // that behavior. + #[cfg(target_os = "macos")] + window.set_simple_fullscreen(false); + #[cfg(not(target_os = "macos"))] + window.set_fullscreen(None); + window.set_position(pos); + } else { + // We use our own idea of the position because get_position() + // appears to only return the initial position of the window + // on Linux. + self.is_fullscreen = self.window_position.take(); + + let window = self.display.gl_window(); + #[cfg(target_os = "macos")] + window.set_simple_fullscreen(true); + #[cfg(not(target_os = "macos"))] + window.set_fullscreen(Some(window.get_current_monitor())); + } + } +} impl<'a> term::TerminalHost for TabHost<'a> { fn writer(&mut self) -> &mut Write { @@ -65,103 +112,52 @@ impl<'a> term::TerminalHost for TabHost<'a> { } fn set_title(&mut self, _title: &str) { - // activate_tab_relative calls Terminal::update_title() - // in the appropriate context - self.activate_tab_relative(0); + self.host.with_window(move |win| { + win.update_title(); + Ok(()) + }) } fn toggle_full_screen(&mut self) { - if let Some(pos) = self.host.is_fullscreen.take() { - let window = self.host.display.gl_window(); - // Use simple fullscreen mode on macos, as wez personally - // prefers the faster transition to/from this mode than - // the Lion+ slow transition to a new Space. This could - // be made into a config option if someone really wanted - // that behavior. - #[cfg(target_os = "macos")] - window.set_simple_fullscreen(false); - #[cfg(not(target_os = "macos"))] - window.set_fullscreen(None); - window.set_position(pos); - } else { - // We use our own idea of the position because get_position() - // appears to only return the initial position of the window - // on Linux. - self.host.is_fullscreen = self.host.window_position.take(); - - let window = self.host.display.gl_window(); - #[cfg(target_os = "macos")] - window.set_simple_fullscreen(true); - #[cfg(not(target_os = "macos"))] - window.set_fullscreen(Some(window.get_current_monitor())); - } - } - - fn new_window(&mut self) { - let event_loop = Rc::clone(&self.host.event_loop); - let config = Rc::clone(&self.host.config); - let fonts = Rc::clone(&self.host.fonts); - self.host.event_loop.spawn_fn(move || { - let (terminal, master, child, fonts) = spawn_window_impl(None, &config, &fonts)?; - let window = - GliumTerminalWindow::new(&event_loop, terminal, master, child, &fonts, &config)?; - - event_loop.add_window(window) - }); + self.host.toggle_full_screen(); } fn new_tab(&mut self) { - GuiEventLoop::with_window( - &self.host.event_loop, - self.host.display.gl_window().id(), - |win| win.spawn_tab().map(|_| ()), - ); + self.host.new_tab(); + } + fn new_window(&mut self) { + self.host.new_window(); } fn activate_tab(&mut self, tab: usize) { - GuiEventLoop::with_window( - &self.host.event_loop, - self.host.display.gl_window().id(), - move |win| win.activate_tab(tab), - ); + self.host.with_window(move |win| win.activate_tab(tab)); } fn activate_tab_relative(&mut self, tab: isize) { - GuiEventLoop::with_window( - &self.host.event_loop, - self.host.display.gl_window().id(), - move |win| win.activate_tab_relative(tab), - ); + self.host + .with_window(move |win| win.activate_tab_relative(tab)); } - fn increase_font_size(&mut self) { - GuiEventLoop::with_window( - &self.host.event_loop, - self.host.display.gl_window().id(), - |win| { - let scale = win.fonts.get_font_scale(); - win.scaling_changed(Some(scale * 1.1), None, win.width, win.height) - }, - ); + self.host.with_window(move |win| { + let scale = win.fonts().get_font_scale(); + let dims = win.get_dimensions(); + win.scaling_changed(Some(scale * 1.1), None, dims.width, dims.height) + }) } fn decrease_font_size(&mut self) { - GuiEventLoop::with_window( - &self.host.event_loop, - self.host.display.gl_window().id(), - |win| { - let scale = win.fonts.get_font_scale(); - win.scaling_changed(Some(scale * 0.9), None, win.width, win.height) - }, - ); + self.host.with_window(move |win| { + let scale = win.fonts().get_font_scale(); + let dims = win.get_dimensions(); + win.scaling_changed(Some(scale * 0.9), None, dims.width, dims.height) + }) } fn reset_font_size(&mut self) { - GuiEventLoop::with_window( - &self.host.event_loop, - self.host.display.gl_window().id(), - |win| win.scaling_changed(Some(1.0), None, win.width, win.height), - ); + self.host.with_window(move |win| { + let dims = win.get_dimensions(); + win.scaling_changed(Some(1.0), None, dims.width, dims.height) + }) } } diff --git a/src/guicommon/host.rs b/src/guicommon/host.rs index bdf1aef14..78ba2e1df 100644 --- a/src/guicommon/host.rs +++ b/src/guicommon/host.rs @@ -1,8 +1,13 @@ +use super::window::TerminalWindow; use clipboard::{ClipboardContext, ClipboardProvider}; use failure::Error; use std::ops::{Deref, DerefMut}; pub trait HostHelper { + fn with_window Result<(), Error>>(&self, func: F); + fn new_tab(&mut self); + fn new_window(&mut self); + fn toggle_full_screen(&mut self); } pub struct HostImpl { diff --git a/src/guiloop/glutinloop.rs b/src/guiloop/glutinloop.rs index 3ee301bf3..c1d17f192 100644 --- a/src/guiloop/glutinloop.rs +++ b/src/guiloop/glutinloop.rs @@ -187,7 +187,7 @@ impl GuiEventLoop { }) } - pub fn with_window Result<(), Error>>( + pub fn with_window Result<(), Error>>( events: &Rc, window_id: WindowId, func: F, diff --git a/src/guiloop/x11.rs b/src/guiloop/x11.rs index c75f72fe2..03e8a71d4 100644 --- a/src/guiloop/x11.rs +++ b/src/guiloop/x11.rs @@ -200,7 +200,7 @@ impl GuiEventLoop { /// Run a function with access to the mutable version of the window with /// the specified window id - pub fn with_window Result<(), Error>>( + pub fn with_window Result<(), Error>>( &self, window_id: WindowId, func: F, diff --git a/src/xwindows/xwin.rs b/src/xwindows/xwin.rs index 9c96e4036..f0b7b24a2 100644 --- a/src/xwindows/xwin.rs +++ b/src/xwindows/xwin.rs @@ -35,27 +35,12 @@ struct Host { config: Rc, } -impl HostHelper for Host {} +impl HostHelper for Host { + fn with_window Result<(), Error>>(&self, func: F) { + let events = Rc::clone(&self.event_loop); + let window_id = self.window.window.window_id; -pub struct X11TerminalWindow { - host: HostImpl, - conn: Rc, - fonts: Rc, - renderer: Renderer, - width: u16, - height: u16, - cell_height: usize, - cell_width: usize, - tabs: Tabs, -} - -impl<'a> TabHost<'a> { - fn with_window Result<(), Error>>(&self, func: F) { - let events = Rc::clone(&self.host.event_loop); - let window_id = self.host.window.window.window_id; - - self.host - .event_loop + self.event_loop .core .spawn(futures::future::poll_fn(move || { events @@ -64,6 +49,48 @@ impl<'a> TabHost<'a> { .map_err(|_| ()) })); } + fn new_window(&mut self) { + let event_loop = Rc::clone(&self.event_loop); + let events = Rc::clone(&self.event_loop); + let config = Rc::clone(&self.config); + let fonts = Rc::clone(&self.fonts); + + self.event_loop + .core + .spawn(futures::future::poll_fn(move || { + events + .spawn_window(&event_loop, &config, &fonts) + .map(futures::Async::Ready) + .map_err(|_| ()) + })); + } + + fn new_tab(&mut self) { + let events = Rc::clone(&self.event_loop); + let window_id = self.window.window.window_id; + + self.event_loop + .core + .spawn(futures::future::poll_fn(move || { + events + .spawn_tab(window_id) + .map(futures::Async::Ready) + .map_err(|_| ()) + })); + } + + fn toggle_full_screen(&mut self) {} +} + +pub struct X11TerminalWindow { + host: HostImpl, + conn: Rc, + renderer: Renderer, + width: u16, + height: u16, + cell_height: usize, + cell_width: usize, + tabs: Tabs, } impl<'a> term::TerminalHost for TabHost<'a> { @@ -87,68 +114,49 @@ impl<'a> term::TerminalHost for TabHost<'a> { } fn set_title(&mut self, _title: &str) { - self.with_window(move |win| { + self.host.with_window(move |win| { win.update_title(); Ok(()) }) } fn new_window(&mut self) { - let event_loop = Rc::clone(&self.host.event_loop); - let events = Rc::clone(&self.host.event_loop); - let config = Rc::clone(&self.host.config); - let fonts = Rc::clone(&self.host.fonts); - - self.host - .event_loop - .core - .spawn(futures::future::poll_fn(move || { - events - .spawn_window(&event_loop, &config, &fonts) - .map(futures::Async::Ready) - .map_err(|_| ()) - })); + self.host.new_window(); } - fn new_tab(&mut self) { - let events = Rc::clone(&self.host.event_loop); - let window_id = self.host.window.window.window_id; - - self.host - .event_loop - .core - .spawn(futures::future::poll_fn(move || { - events - .spawn_tab(window_id) - .map(futures::Async::Ready) - .map_err(|_| ()) - })); + self.host.new_tab(); } fn activate_tab(&mut self, tab: usize) { - self.with_window(move |win| win.activate_tab(tab)) + self.host.with_window(move |win| win.activate_tab(tab)) } fn activate_tab_relative(&mut self, tab: isize) { - self.with_window(move |win| win.activate_tab_relative(tab)) + self.host + .with_window(move |win| win.activate_tab_relative(tab)) } fn increase_font_size(&mut self) { - self.with_window(move |win| { - let scale = win.fonts.get_font_scale(); - win.scaling_changed(Some(scale * 1.1), None, win.width, win.height) + self.host.with_window(move |win| { + let scale = win.fonts().get_font_scale(); + let dims = win.get_dimensions(); + win.scaling_changed(Some(scale * 1.1), None, dims.width, dims.height) }) } fn decrease_font_size(&mut self) { - self.with_window(move |win| { - let scale = win.fonts.get_font_scale(); - win.scaling_changed(Some(scale * 0.9), None, win.width, win.height) + self.host.with_window(move |win| { + let scale = win.fonts().get_font_scale(); + let dims = win.get_dimensions(); + win.scaling_changed(Some(scale * 0.9), None, dims.width, dims.height) }) } fn reset_font_size(&mut self) { - self.with_window(move |win| win.scaling_changed(Some(1.0), None, win.width, win.height)) + self.host.with_window(move |win| { + let dims = win.get_dimensions(); + win.scaling_changed(Some(1.0), None, dims.width, dims.height) + }) } } @@ -276,7 +284,6 @@ impl X11TerminalWindow { host, renderer, conn: Rc::clone(&event_loop.conn), - fonts: Rc::clone(&fonts), width, height, cell_height,