From 68fc0548722a6f889f32b69e37ad63c482d888cb Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 24 Feb 2019 09:17:28 -0800 Subject: [PATCH] move TabHost to guicommon --- src/gliumwindows.rs | 120 ++++-------------------------------------- src/guicommon/host.rs | 84 +++++++++++++++++++++++++++++ src/xwindows/xwin.rs | 104 +++--------------------------------- 3 files changed, 100 insertions(+), 208 deletions(-) diff --git a/src/gliumwindows.rs b/src/gliumwindows.rs index 6ea85a955..250d9057b 100644 --- a/src/gliumwindows.rs +++ b/src/gliumwindows.rs @@ -3,7 +3,7 @@ use crate::config::Config; use crate::failure::Error; use crate::font::FontConfiguration; -use crate::guicommon::host::{HostHelper, HostImpl}; +use crate::guicommon::host::{HostHelper, HostImpl, TabHost}; use crate::guicommon::tabs::{Tab, TabId, Tabs}; use crate::guicommon::window::{Dimensions, TerminalWindow}; use crate::guiloop::glutinloop::GuiEventLoop; @@ -14,24 +14,14 @@ use glium; use glium::glutin::dpi::{LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize}; use glium::glutin::{self, ElementState, MouseCursor}; use std::cell::RefMut; -use std::io::Write; use std::rc::Rc; use term::KeyCode; use term::KeyModifiers; use term::{self, Terminal}; use term::{MouseButton, MouseEventKind}; -use termwiz::hyperlink::Hyperlink; #[cfg(target_os = "macos")] use winit::os::macos::WindowExt; -/// Implements `TerminalHost` for a Tab. -/// `TabHost` instances are short lived and borrow references to -/// other state. -struct TabHost<'a> { - pty: &'a mut MasterPty, - host: &'a mut HostImpl, -} - struct Host { event_loop: Rc, display: glium::Display, @@ -92,75 +82,6 @@ impl HostHelper for Host { } } -impl<'a> term::TerminalHost for TabHost<'a> { - fn writer(&mut self) -> &mut Write { - &mut self.pty - } - fn click_link(&mut self, link: &Rc) { - match open::that(link.uri()) { - Ok(_) => {} - Err(err) => eprintln!("failed to open {}: {:?}", link.uri(), err), - } - } - - fn get_clipboard(&mut self) -> Result { - self.host.get_clipboard() - } - - fn set_clipboard(&mut self, clip: Option) -> Result<(), Error> { - self.host.set_clipboard(clip) - } - - fn set_title(&mut self, _title: &str) { - self.host.with_window(move |win| { - win.update_title(); - Ok(()) - }) - } - - fn toggle_full_screen(&mut self) { - self.host.toggle_full_screen(); - } - - fn new_tab(&mut self) { - self.host.new_tab(); - } - fn new_window(&mut self) { - self.host.new_window(); - } - - fn activate_tab(&mut self, tab: usize) { - self.host.with_window(move |win| win.activate_tab(tab)); - } - - fn activate_tab_relative(&mut self, tab: isize) { - self.host - .with_window(move |win| win.activate_tab_relative(tab)); - } - fn increase_font_size(&mut self) { - 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.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.host.with_window(move |win| { - let dims = win.get_dimensions(); - win.scaling_changed(Some(1.0), None, dims.width, dims.height) - }) - } -} - pub struct GliumTerminalWindow { host: HostImpl, event_loop: Rc, @@ -358,13 +279,8 @@ impl GliumTerminalWindow { pub fn process_data_read_from_pty(&mut self, data: &[u8], tab_id: usize) -> Result<(), Error> { let tab = self.tabs.get_by_id(tab_id)?; - tab.terminal().advance_bytes( - data, - &mut TabHost { - pty: &mut *tab.pty(), - host: &mut self.host, - }, - ); + tab.terminal() + .advance_bytes(data, &mut TabHost::new(&mut tab.pty(), &mut self.host)); Ok(()) } @@ -414,10 +330,7 @@ impl GliumTerminalWindow { y: (y as usize / self.cell_height) as i64, modifiers: Self::decode_modifiers(modifiers), }, - &mut TabHost { - pty: &mut *tab.pty(), - host: &mut self.host, - }, + &mut TabHost::new(&mut tab.pty(), &mut self.host), )?; // Deliberately not forcing a paint on mouse move as it // makes selection feel sluggish @@ -463,10 +376,7 @@ impl GliumTerminalWindow { y: (self.last_mouse_coords.y as usize / self.cell_height) as i64, modifiers: Self::decode_modifiers(modifiers), }, - &mut TabHost { - pty: &mut *tab.pty(), - host: &mut self.host, - }, + &mut TabHost::new(&mut tab.pty(), &mut self.host), )?; self.paint_if_needed()?; @@ -523,10 +433,7 @@ impl GliumTerminalWindow { y: (self.last_mouse_coords.y as usize / self.cell_height) as i64, modifiers: Self::decode_modifiers(modifiers), }, - &mut TabHost { - pty: &mut *tab.pty(), - host: &mut self.host, - }, + &mut TabHost::new(&mut tab.pty(), &mut self.host), )?; } self.paint_if_needed()?; @@ -744,19 +651,13 @@ impl GliumTerminalWindow { ElementState::Pressed => tab.terminal().key_down( key, mods, - &mut TabHost { - pty: &mut *tab.pty(), - host: &mut self.host, - }, + &mut TabHost::new(&mut tab.pty(), &mut self.host), )?, ElementState::Released => tab.terminal().key_up( key, mods, - &mut TabHost { - pty: &mut *tab.pty(), - host: &mut self.host, - }, + &mut TabHost::new(&mut tab.pty(), &mut self.host), )?, } } else { @@ -828,10 +729,7 @@ impl GliumTerminalWindow { tab.terminal().key_down( KeyCode::Char(c), self.last_modifiers, - &mut TabHost { - pty: &mut *tab.pty(), - host: &mut self.host, - }, + &mut TabHost::new(&mut tab.pty(), &mut self.host), )?; self.paint_if_needed()?; } diff --git a/src/guicommon/host.rs b/src/guicommon/host.rs index 78ba2e1df..ce9916954 100644 --- a/src/guicommon/host.rs +++ b/src/guicommon/host.rs @@ -1,7 +1,10 @@ use super::window::TerminalWindow; +use crate::MasterPty; use clipboard::{ClipboardContext, ClipboardProvider}; use failure::Error; use std::ops::{Deref, DerefMut}; +use std::rc::Rc; +use termwiz::hyperlink::Hyperlink; pub trait HostHelper { fn with_window Result<(), Error>>(&self, func: F); @@ -60,3 +63,84 @@ impl DerefMut for HostImpl { &mut self.helper } } + +/// Implements `TerminalHost` for a Tab. +/// `TabHost` instances are short lived and borrow references to +/// other state. +pub struct TabHost<'a, H: HostHelper> { + pty: &'a mut MasterPty, + host: &'a mut HostImpl, +} + +impl<'a, H: HostHelper> TabHost<'a, H> { + pub fn new(pty: &'a mut MasterPty, host: &'a mut HostImpl) -> Self { + Self { pty, host } + } +} + +impl<'a, H: HostHelper> term::TerminalHost for TabHost<'a, H> { + fn writer(&mut self) -> &mut std::io::Write { + &mut self.pty + } + + fn click_link(&mut self, link: &Rc) { + match open::that(link.uri()) { + Ok(_) => {} + Err(err) => eprintln!("failed to open {}: {:?}", link.uri(), err), + } + } + + fn get_clipboard(&mut self) -> Result { + self.host.get_clipboard() + } + + fn set_clipboard(&mut self, clip: Option) -> Result<(), Error> { + self.host.set_clipboard(clip) + } + + fn set_title(&mut self, _title: &str) { + self.host.with_window(move |win| { + win.update_title(); + Ok(()) + }) + } + + fn new_window(&mut self) { + self.host.new_window(); + } + fn new_tab(&mut self) { + self.host.new_tab(); + } + + fn activate_tab(&mut self, tab: usize) { + self.host.with_window(move |win| win.activate_tab(tab)) + } + + fn activate_tab_relative(&mut self, tab: isize) { + self.host + .with_window(move |win| win.activate_tab_relative(tab)) + } + + fn increase_font_size(&mut self) { + 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.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.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/xwindows/xwin.rs b/src/xwindows/xwin.rs index f0b7b24a2..30b3c051c 100644 --- a/src/xwindows/xwin.rs +++ b/src/xwindows/xwin.rs @@ -4,7 +4,7 @@ use super::xkeysyms; use super::{Connection, Window}; use crate::config::Config; use crate::font::FontConfiguration; -use crate::guicommon::host::{HostHelper, HostImpl}; +use crate::guicommon::host::{HostHelper, HostImpl, TabHost}; use crate::guicommon::tabs::{Tab, TabId, Tabs}; use crate::guicommon::window::{Dimensions, TerminalWindow}; use crate::guiloop::x11::{GuiEventLoop, WindowId}; @@ -13,20 +13,11 @@ use crate::MasterPty; use failure::Error; use futures; use std::cell::RefMut; -use std::io::{self, Read, Write}; +use std::io::{self, Read}; use std::rc::Rc; use term::{self, KeyCode, KeyModifiers, MouseButton, MouseEvent, MouseEventKind}; -use termwiz::hyperlink::Hyperlink; use xcb; -/// Implements `TerminalHost` for a Tab. -/// `TabHost` instances are short lived and borrow references to -/// other state. -struct TabHost<'a> { - pty: &'a mut MasterPty, - host: &'a mut HostImpl, -} - /// Holds most of the information we need to implement `TerminalHost` struct Host { window: Window, @@ -93,73 +84,6 @@ pub struct X11TerminalWindow { tabs: Tabs, } -impl<'a> term::TerminalHost for TabHost<'a> { - fn writer(&mut self) -> &mut Write { - &mut self.pty - } - - fn click_link(&mut self, link: &Rc) { - match open::that(link.uri()) { - Ok(_) => {} - Err(err) => eprintln!("failed to open {}: {:?}", link.uri(), err), - } - } - - fn get_clipboard(&mut self) -> Result { - self.host.get_clipboard() - } - - fn set_clipboard(&mut self, clip: Option) -> Result<(), Error> { - self.host.set_clipboard(clip) - } - - fn set_title(&mut self, _title: &str) { - self.host.with_window(move |win| { - win.update_title(); - Ok(()) - }) - } - - fn new_window(&mut self) { - self.host.new_window(); - } - fn new_tab(&mut self) { - self.host.new_tab(); - } - - fn activate_tab(&mut self, tab: usize) { - self.host.with_window(move |win| win.activate_tab(tab)) - } - - fn activate_tab_relative(&mut self, tab: isize) { - self.host - .with_window(move |win| win.activate_tab_relative(tab)) - } - - fn increase_font_size(&mut self) { - 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.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.host.with_window(move |win| { - let dims = win.get_dimensions(); - win.scaling_changed(Some(1.0), None, dims.width, dims.height) - }) - } -} - impl TerminalWindow for X11TerminalWindow { fn get_tabs(&self) -> &Tabs { &self.tabs @@ -311,10 +235,7 @@ impl X11TerminalWindow { Ok(size) => { tab.terminal().advance_bytes( &buf[0..size], - &mut TabHost { - pty: &mut *tab.pty(), - host: &mut self.host, - }, + &mut TabHost::new(&mut tab.pty(), &mut self.host), ); } Err(err) => { @@ -336,13 +257,8 @@ impl X11TerminalWindow { Some(tab) => tab, None => return Ok(()), }; - tab.terminal().mouse_event( - event, - &mut TabHost { - pty: &mut *tab.pty(), - host: &mut self.host, - }, - )?; + tab.terminal() + .mouse_event(event, &mut TabHost::new(&mut tab.pty(), &mut self.host))?; Ok(()) } @@ -367,10 +283,7 @@ impl X11TerminalWindow { tab.terminal().key_down( code, mods, - &mut TabHost { - pty: &mut *tab.pty(), - host: &mut self.host, - }, + &mut TabHost::new(&mut tab.pty(), &mut self.host), )?; } } @@ -384,10 +297,7 @@ impl X11TerminalWindow { tab.terminal().key_up( code, mods, - &mut TabHost { - pty: &mut *tab.pty(), - host: &mut self.host, - }, + &mut TabHost::new(&mut tab.pty(), &mut self.host), )?; } }