We can now spawn and switch tabs!
This commit is contained in:
1 parent
cabc6681b6
commit
edf44c9c48
3 files changed
+79
-4
No files matched your search
@@ -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<F: FnMut(&mut TerminalWindow) -> 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();
|
||||
|
||||
|
||||
+54
-2
@@ -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(())
|
||||
}
|
||||
}
|
||||
@@ -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(());
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user