1
1
mirror of https://github.com/wez/wezterm.git synced 2024-12-25 06:12:16 +03:00

refactor to make TabHost impls look similar

This commit is contained in:
Wez Furlong 2019-02-24 09:02:49 -08:00
parent 5b84b6dfc9
commit 3d7a313e3f
5 changed files with 146 additions and 138 deletions

View File

@ -43,7 +43,54 @@ struct Host {
fonts: Rc<FontConfiguration>,
}
impl HostHelper for Host {}
impl HostHelper for Host {
fn with_window<F: 'static + Fn(&mut TerminalWindow) -> 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)
})
}
}

View File

@ -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<F: 'static + Fn(&mut TerminalWindow) -> Result<(), Error>>(&self, func: F);
fn new_tab(&mut self);
fn new_window(&mut self);
fn toggle_full_screen(&mut self);
}
pub struct HostImpl<H: HostHelper> {

View File

@ -187,7 +187,7 @@ impl GuiEventLoop {
})
}
pub fn with_window<F: 'static + Fn(&mut GliumTerminalWindow) -> Result<(), Error>>(
pub fn with_window<F: 'static + Fn(&mut TerminalWindow) -> Result<(), Error>>(
events: &Rc<Self>,
window_id: WindowId,
func: F,

View File

@ -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<F: FnOnce(&mut X11TerminalWindow) -> Result<(), Error>>(
pub fn with_window<F: FnOnce(&mut TerminalWindow) -> Result<(), Error>>(
&self,
window_id: WindowId,
func: F,

View File

@ -35,27 +35,12 @@ struct Host {
config: Rc<Config>,
}
impl HostHelper for Host {}
impl HostHelper for Host {
fn with_window<F: 'static + Fn(&mut TerminalWindow) -> 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<Host>,
conn: Rc<Connection>,
fonts: Rc<FontConfiguration>,
renderer: Renderer,
width: u16,
height: u16,
cell_height: usize,
cell_width: usize,
tabs: Tabs,
}
impl<'a> TabHost<'a> {
fn with_window<F: 'static + Fn(&mut X11TerminalWindow) -> 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<Host>,
conn: Rc<Connection>,
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,