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

add helper for calling from TabHost -> TerminalWindow

This commit is contained in:
Wez Furlong 2019-02-23 00:35:49 -08:00
parent 26d7ed9cbf
commit 826a0df380
2 changed files with 34 additions and 80 deletions

View File

@ -200,10 +200,10 @@ impl GuiEventLoop {
/// Run a function with access to the mutable version of the window with
/// the specified window id
pub fn with_window<F: FnMut(&mut TerminalWindow) -> Result<(), Error>>(
pub fn with_window<F: FnOnce(&mut TerminalWindow) -> Result<(), Error>>(
&self,
window_id: WindowId,
mut func: F,
func: F,
) -> Result<(), Error> {
let mut windows = self.windows.borrow_mut();

View File

@ -115,6 +115,23 @@ pub struct TerminalWindow {
tabs: Tabs,
}
impl<'a> TabHost<'a> {
fn with_window<F: 'static + Fn(&mut TerminalWindow) -> 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
.core
.spawn(futures::future::poll_fn(move || {
events
.with_window(window_id, &func)
.map(futures::Async::Ready)
.map_err(|_| ())
}));
}
}
impl<'a> term::TerminalHost for TabHost<'a> {
fn writer(&mut self) -> &mut Write {
&mut self.pty
@ -146,21 +163,10 @@ impl<'a> term::TerminalHost for TabHost<'a> {
}
fn set_title(&mut self, _title: &str) {
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
.with_window(window_id, |win| {
win.update_title();
Ok(())
})
.map(futures::Async::Ready)
.map_err(|_| ())
}));
self.with_window(move |win| {
win.update_title();
Ok(())
})
}
fn new_window(&mut self) {
@ -196,81 +202,29 @@ impl<'a> term::TerminalHost for TabHost<'a> {
}
fn activate_tab(&mut self, tab: usize) {
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
.with_window(window_id, |win| win.activate_tab(tab))
.map(futures::Async::Ready)
.map_err(|_| ())
}));
self.with_window(move |win| win.activate_tab(tab))
}
fn activate_tab_relative(&mut self, tab: isize) {
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
.with_window(window_id, |win| win.activate_tab_relative(tab))
.map(futures::Async::Ready)
.map_err(|_| ())
}));
self.with_window(move |win| win.activate_tab_relative(tab))
}
fn increase_font_size(&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
.with_window(window_id, |win| {
let scale = win.fonts.get_font_scale();
win.scaling_changed(Some(scale * 1.1))
})
.map(futures::Async::Ready)
.map_err(|_| ())
}));
self.with_window(move |win| {
let scale = win.fonts.get_font_scale();
win.scaling_changed(Some(scale * 1.1))
})
}
fn decrease_font_size(&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
.with_window(window_id, |win| {
let scale = win.fonts.get_font_scale();
win.scaling_changed(Some(scale * 0.9))
})
.map(futures::Async::Ready)
.map_err(|_| ())
}));
self.with_window(move |win| {
let scale = win.fonts.get_font_scale();
win.scaling_changed(Some(scale * 0.9))
})
}
fn reset_font_size(&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
.with_window(window_id, |win| win.scaling_changed(Some(1.0)))
.map(futures::Async::Ready)
.map_err(|_| ())
}));
self.with_window(move |win| win.scaling_changed(Some(1.0)))
}
}