move TabHost to guicommon
This commit is contained in:
1 parent
3d7a313e3f
commit
68fc054872
3 files changed
+100
-208
No files matched your search
+9
-111
@@ -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<Host>,
|
||||
}
|
||||
|
||||
struct Host {
|
||||
event_loop: Rc<GuiEventLoop>,
|
||||
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<Hyperlink>) {
|
||||
match open::that(link.uri()) {
|
||||
Ok(_) => {}
|
||||
Err(err) => eprintln!("failed to open {}: {:?}", link.uri(), err),
|
||||
}
|
||||
}
|
||||
|
||||
fn get_clipboard(&mut self) -> Result<String, Error> {
|
||||
self.host.get_clipboard()
|
||||
}
|
||||
|
||||
fn set_clipboard(&mut self, clip: Option<String>) -> 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<Host>,
|
||||
event_loop: Rc<GuiEventLoop>,
|
||||
@@ -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()?;
|
||||
}
|
||||
|
||||
@@ -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<F: 'static + Fn(&mut TerminalWindow) -> Result<(), Error>>(&self, func: F);
|
||||
@@ -60,3 +63,84 @@ impl<H: HostHelper> DerefMut for HostImpl<H> {
|
||||
&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<H>,
|
||||
}
|
||||
|
||||
impl<'a, H: HostHelper> TabHost<'a, H> {
|
||||
pub fn new(pty: &'a mut MasterPty, host: &'a mut HostImpl<H>) -> 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<Hyperlink>) {
|
||||
match open::that(link.uri()) {
|
||||
Ok(_) => {}
|
||||
Err(err) => eprintln!("failed to open {}: {:?}", link.uri(), err),
|
||||
}
|
||||
}
|
||||
|
||||
fn get_clipboard(&mut self) -> Result<String, Error> {
|
||||
self.host.get_clipboard()
|
||||
}
|
||||
|
||||
fn set_clipboard(&mut self, clip: Option<String>) -> 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)
|
||||
})
|
||||
}
|
||||
}
|
||||
+7
-97
@@ -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<Host>,
|
||||
}
|
||||
|
||||
/// 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<Hyperlink>) {
|
||||
match open::that(link.uri()) {
|
||||
Ok(_) => {}
|
||||
Err(err) => eprintln!("failed to open {}: {:?}", link.uri(), err),
|
||||
}
|
||||
}
|
||||
|
||||
fn get_clipboard(&mut self) -> Result<String, Error> {
|
||||
self.host.get_clipboard()
|
||||
}
|
||||
|
||||
fn set_clipboard(&mut self, clip: Option<String>) -> 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),
|
||||
)?;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user