diff --git a/src/gliumwindows.rs b/src/gliumwindows.rs index cc9015d17..4d1ba8b65 100644 --- a/src/gliumwindows.rs +++ b/src/gliumwindows.rs @@ -4,6 +4,7 @@ use crate::config::Config; use crate::failure::Error; use crate::font::FontConfiguration; use crate::guicommon::tabs::{Tab, TabId, Tabs}; +use crate::guicommon::window::TerminalWindow; use crate::guiloop::glutinloop::GuiEventLoop; use crate::guiloop::SessionTerminated; use crate::opengl::render::Renderer; @@ -210,6 +211,17 @@ pub struct GliumTerminalWindow { tabs: Tabs, } +impl TerminalWindow for GliumTerminalWindow { + fn get_tabs(&mut self) -> &mut Tabs { + &mut self.tabs + } + + fn set_window_title(&mut self, title: &str) -> Result<(), Error> { + self.host.display.gl_window().set_title(title); + Ok(()) + } +} + impl GliumTerminalWindow { pub fn new( event_loop: &Rc, @@ -321,14 +333,6 @@ impl GliumTerminalWindow { Ok(()) } - pub fn activate_tab(&mut self, tab: usize) -> Result<(), Error> { - let max = self.tabs.len(); - if tab < max { - self.tabs.set_active(tab); - self.update_title(); - } - Ok(()) - } pub fn activate_tab_relative(&mut self, delta: isize) -> Result<(), Error> { let max = self.tabs.len(); let active = self.tabs.get_active_idx() as isize; @@ -337,25 +341,6 @@ impl GliumTerminalWindow { self.activate_tab(tab as usize % max) } - fn update_title(&mut self) { - let num_tabs = self.tabs.len(); - - if num_tabs == 0 { - return; - } - let tab_no = self.tabs.get_active_idx(); - - let terminal = self.tabs.get_active().unwrap().terminal(); - let title = terminal.get_title(); - let window = self.host.display.gl_window(); - - if num_tabs == 1 { - window.set_title(title) - } else { - window.set_title(&format!("[{}/{}] {}", tab_no + 1, num_tabs, title)); - } - } - pub fn window_id(&self) -> glutin::WindowId { self.host.display.gl_window().id() } diff --git a/src/guicommon/mod.rs b/src/guicommon/mod.rs index 4b2ad050f..68d1d4179 100644 --- a/src/guicommon/mod.rs +++ b/src/guicommon/mod.rs @@ -1 +1,2 @@ pub mod tabs; +pub mod window; diff --git a/src/guicommon/window.rs b/src/guicommon/window.rs new file mode 100644 index 000000000..160c71c33 --- /dev/null +++ b/src/guicommon/window.rs @@ -0,0 +1,37 @@ +use crate::guicommon::tabs::Tabs; +use failure::Error; + +pub trait TerminalWindow { + fn get_tabs(&mut self) -> &mut Tabs; + fn set_window_title(&mut self, title: &str) -> Result<(), Error>; + + fn activate_tab(&mut self, tab_idx: usize) -> Result<(), Error> { + let max = self.get_tabs().len(); + if tab_idx < max { + self.get_tabs().set_active(tab_idx); + self.update_title(); + } + Ok(()) + } + + fn update_title(&mut self) { + let num_tabs = self.get_tabs().len(); + + if num_tabs == 0 { + return; + } + let tab_no = self.get_tabs().get_active_idx(); + + let title = { + let terminal = self.get_tabs().get_active().unwrap().terminal(); + terminal.get_title().to_owned() + }; + + if num_tabs == 1 { + self.set_window_title(&title).ok(); + } else { + self.set_window_title(&format!("[{}/{}] {}", tab_no + 1, num_tabs, title)) + .ok(); + } + } +} diff --git a/src/xwindows/xwin.rs b/src/xwindows/xwin.rs index b4161c3bf..937741a1a 100644 --- a/src/xwindows/xwin.rs +++ b/src/xwindows/xwin.rs @@ -5,6 +5,7 @@ use super::{Connection, Window}; use crate::config::Config; use crate::font::FontConfiguration; use crate::guicommon::tabs::{Tab, TabId, Tabs}; +use crate::guicommon::window::TerminalWindow; use crate::guiloop::x11::{GuiEventLoop, WindowId}; use crate::guiloop::SessionTerminated; use crate::opengl::textureatlas::OutOfTextureSpace; @@ -160,6 +161,17 @@ impl<'a> term::TerminalHost for TabHost<'a> { } } +impl TerminalWindow for X11TerminalWindow { + fn get_tabs(&mut self) -> &mut Tabs { + &mut self.tabs + } + + fn set_window_title(&mut self, title: &str) -> Result<(), Error> { + self.host.window.set_title(title); + Ok(()) + } +} + impl X11TerminalWindow { pub fn new( event_loop: &Rc, @@ -525,36 +537,6 @@ impl X11TerminalWindow { Ok(tab_id) } - fn update_title(&mut self) { - let num_tabs = self.tabs.len(); - if num_tabs == 0 { - return; - } - let tab_no = self.tabs.get_active_idx(); - - let terminal = self.tabs.get_active().unwrap().terminal(); - - if num_tabs == 1 { - self.host.window.set_title(terminal.get_title()); - } else { - self.host.window.set_title(&format!( - "[{}/{}] {}", - tab_no + 1, - num_tabs, - terminal.get_title() - )); - } - } - - pub fn activate_tab(&mut self, tab: usize) -> Result<(), Error> { - let max = self.tabs.len(); - if tab < max { - self.tabs.set_active(tab); - self.update_title(); - } - Ok(()) - } - pub fn activate_tab_relative(&mut self, delta: isize) -> Result<(), Error> { let max = self.tabs.len(); let active = self.tabs.get_active_idx() as isize;