From edf44c9c480eaead35133106ebaa96c628174fa2 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 4 Mar 2018 16:46:14 -0800 Subject: [PATCH] We can now spawn and switch tabs! --- src/guiloop/x11.rs | 19 +++++++++++++ src/xwindows/xwin.rs | 56 +++++++++++++++++++++++++++++++++++++-- term/src/terminalstate.rs | 8 ++++-- 3 files changed, 79 insertions(+), 4 deletions(-) diff --git a/src/guiloop/x11.rs b/src/guiloop/x11.rs index cb5c3f0ba..6e8e105f7 100644 --- a/src/guiloop/x11.rs +++ b/src/guiloop/x11.rs @@ -174,6 +174,25 @@ impl GuiEventLoop { } } + /// Run a function with access to the mutable version of the window with + /// the specified window id + pub fn with_window Result<(), Error>>( + &self, + window_id: WindowId, + mut func: F, + ) -> Result<(), Error> { + let mut windows = self.windows.borrow_mut(); + + let window = windows + .by_id + .get_mut(&window_id) + .ok_or_else(|| format_err!("no window_id {:?} in the windows_by_id map", window_id))?; + + func(window) + } + + /// Spawn a new tab in the specified window. This method registers + /// the returned pty fd as a TabEntry so that events are wired up. pub fn spawn_tab(&self, window_id: WindowId) -> Result<(), Error> { let mut windows = self.windows.borrow_mut(); diff --git a/src/xwindows/xwin.rs b/src/xwindows/xwin.rs index 03a856489..679c8d8aa 100644 --- a/src/xwindows/xwin.rs +++ b/src/xwindows/xwin.rs @@ -45,6 +45,12 @@ impl Tabs { &self.tabs[self.active] } + fn set_active(&mut self, idx: usize) { + assert!(idx < self.tabs.len()); + self.active = idx; + self.tabs[idx].terminal.borrow_mut().make_all_lines_dirty(); + } + fn get_for_fd(&self, fd: RawFd) -> Option<&Tab> { for t in &self.tabs { if t.pty.borrow().as_raw_fd() == fd { @@ -66,8 +72,9 @@ impl Tabs { fn remove_tab_for_fd(&mut self, fd: RawFd) { if let Some(idx) = self.index_for_fd(fd) { self.tabs.remove(idx); - if self.active == idx && idx >= self.tabs.len() { - self.active = self.tabs.len() - 1; + let len = self.tabs.len(); + if self.active == idx && idx >= len { + self.set_active(len - 1); } } } @@ -156,6 +163,36 @@ impl<'a> term::TerminalHost for TabHost<'a> { .map_err(|_| ()) })); } + + 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(|_| ()) + })); + } + + 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(|_| ()) + })); + } } impl TerminalWindow { @@ -500,4 +537,19 @@ impl TerminalWindow { Ok(fd) } + + pub fn activate_tab(&mut self, tab: usize) -> Result<(), Error> { + let max = self.tabs.tabs.len(); + if tab < max { + self.tabs.set_active(tab); + } + Ok(()) + } + + pub fn activate_tab_relative(&mut self, delta: isize) -> Result<(), Error> { + let max = self.tabs.tabs.len(); + let tab = (self.tabs.active as isize + delta) as usize % max; + self.tabs.set_active(tab); + Ok(()) + } } diff --git a/term/src/terminalstate.rs b/term/src/terminalstate.rs index ec59dd06f..680792931 100644 --- a/term/src/terminalstate.rs +++ b/term/src/terminalstate.rs @@ -620,11 +620,15 @@ impl TerminalState { host.new_tab(); return Ok(()); } - if mods == (KeyModifiers::SUPER | KeyModifiers::SHIFT) && key == KeyCode::Char('[') { + if mods == (KeyModifiers::SUPER | KeyModifiers::SHIFT) + && (key == KeyCode::Char('[') || key == KeyCode::Char('{')) + { host.activate_tab_relative(-1); return Ok(()); } - if mods == (KeyModifiers::SUPER | KeyModifiers::SHIFT) && key == KeyCode::Char(']') { + if mods == (KeyModifiers::SUPER | KeyModifiers::SHIFT) + && (key == KeyCode::Char(']') || key == KeyCode::Char('}')) + { host.activate_tab_relative(1); return Ok(()); }