From 000c5eddac7176b1b35e3cb9a1d2850355729876 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sat, 8 Jun 2019 10:25:59 -0700 Subject: [PATCH] Add CloseCurrentTab key binding Added CMD-W as a default binding for this action. refs: https://github.com/wez/wezterm/pull/26 --- README.md | 2 ++ src/config.rs | 2 ++ src/frontend/glium/window.rs | 12 +----------- src/frontend/guicommon/host.rs | 18 ++++++++++++++++++ src/frontend/guicommon/window.rs | 4 +++- 5 files changed, 26 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 22bdbfdc7..c310dca4d 100644 --- a/README.md +++ b/README.md @@ -144,6 +144,7 @@ The default key bindings are: | `SUPER` | `0` | `ResetFontSize` | | `CTRL` | `0` | `ResetFontSize` | | `SUPER` | `t` | `SpawnTab` | +| `SUPER` | `w` | `CloseCurrentTab` | | `SUPER` | `1` | `ActivateTab(0)` | | `SUPER` | `2` | `ActivateTab(1)` | | `SUPER` | `3` | `ActivateTab(2)` | @@ -217,6 +218,7 @@ specified via the `arg` key; see examples below. | `Nop` | Does nothing. This is useful to disable a default key assignment. | | `Hide` | Hides the current window | | `Show` | Shows the current window | +| `CloseCurrentTab` | Equivalent to clicking the `x` on the window title bar to close it: Closes the current tab. If that was the last tab, closes that window. If that was the last window, wezterm terminates. | Example: diff --git a/src/config.rs b/src/config.rs index 41b82363b..c321b0521 100644 --- a/src/config.rs +++ b/src/config.rs @@ -110,6 +110,7 @@ impl std::convert::TryInto for &Key { KeyAction::DecreaseFontSize => KeyAssignment::DecreaseFontSize, KeyAction::ResetFontSize => KeyAssignment::ResetFontSize, KeyAction::Nop => KeyAssignment::Nop, + KeyAction::CloseCurrentTab => KeyAssignment::CloseCurrentTab, KeyAction::ActivateTab => KeyAssignment::ActivateTab( self.arg .as_ref() @@ -148,6 +149,7 @@ pub enum KeyAction { Nop, Hide, Show, + CloseCurrentTab, } fn de_keycode<'de, D>(deserializer: D) -> Result diff --git a/src/frontend/glium/window.rs b/src/frontend/glium/window.rs index 4bcc9e1de..daef78de1 100644 --- a/src/frontend/glium/window.rs +++ b/src/frontend/glium/window.rs @@ -747,17 +747,7 @@ impl GliumTerminalWindow { event: WindowEvent::CloseRequested, .. } => { - let mux = Mux::get().unwrap(); - let tab = match mux.get_active_tab_for_window(self.get_mux_window_id()) { - Some(tab) => tab, - None => return Ok(()), - }; - mux.remove_tab(tab.tab_id()); - if let Some(mut win) = mux.get_window_mut(self.get_mux_window_id()) { - win.remove_by_id(tab.tab_id()); - } - // Explicit drop of mux to satisfy the borrow checker - drop(mux); + self.host.close_current_tab(); } _ => {} } diff --git a/src/frontend/guicommon/host.rs b/src/frontend/guicommon/host.rs index 2b47418bb..6ecc2c98a 100644 --- a/src/frontend/guicommon/host.rs +++ b/src/frontend/guicommon/host.rs @@ -31,6 +31,7 @@ pub enum KeyAssignment { Nop, Hide, Show, + CloseCurrentTab, } pub trait HostHelper { @@ -129,6 +130,7 @@ fn key_bindings() -> KeyMap { [KeyModifiers::CTRL, KeyCode::Char('0'), ResetFontSize], // Tab navigation and management [KeyModifiers::SUPER, KeyCode::Char('t'), SpawnTab], + [KeyModifiers::SUPER, KeyCode::Char('w'), CloseCurrentTab], [KeyModifiers::SUPER, KeyCode::Char('1'), ActivateTab(0)], [KeyModifiers::SUPER, KeyCode::Char('2'), ActivateTab(1)], [KeyModifiers::SUPER, KeyCode::Char('3'), ActivateTab(2)], @@ -241,6 +243,7 @@ impl HostImpl { SendString(s) => tab.writer().write_all(s.as_bytes())?, Hide => self.hide_window(), Show => self.show_window(), + CloseCurrentTab => self.close_current_tab(), Nop => {} } Ok(()) @@ -291,6 +294,21 @@ impl HostImpl { }) } + pub fn close_current_tab(&mut self) { + self.with_window(move |win| { + let mux = Mux::get().unwrap(); + let tab = match mux.get_active_tab_for_window(win.get_mux_window_id()) { + Some(tab) => tab, + None => return Ok(()), + }; + mux.remove_tab(tab.tab_id()); + if let Some(mut win) = mux.get_window_mut(win.get_mux_window_id()) { + win.remove_by_id(tab.tab_id()); + } + win.activate_tab_relative(0) + }); + } + pub fn hide_window(&mut self) { self.with_window(move |win| { win.hide_window(); diff --git a/src/frontend/guicommon/window.rs b/src/frontend/guicommon/window.rs index 289347674..1b1dd99db 100644 --- a/src/frontend/guicommon/window.rs +++ b/src/frontend/guicommon/window.rs @@ -5,7 +5,7 @@ use crate::mux::window::WindowId; use crate::mux::Mux; use crate::opengl::render::Renderer; use crate::opengl::textureatlas::OutOfTextureSpace; -use failure::{format_err, Error}; +use failure::{ensure, format_err, Error}; use glium; use portable_pty::PtySize; use std::rc::Rc; @@ -71,6 +71,8 @@ pub trait TerminalWindow { .ok_or_else(|| format_err!("no such window"))?; let max = window.len(); + ensure!(max > 0, "no more tabs"); + let active = window.get_active_idx() as isize; let tab = active + delta; let tab = if tab < 0 { max as isize + tab } else { tab };