From 754f8166b5c4c4a59688d8efbba00a2c4195ab21 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Fri, 14 Feb 2020 08:49:15 -0800 Subject: [PATCH] Add HideApplication and QuitApplication key assignments ``` [[keys]] key = "q" mods = "CMD" action = "QuitApplication" ``` refs: https://github.com/wez/wezterm/issues/150 --- docs/changelog.markdown | 5 +++++ src/config/keys.rs | 4 ++++ src/frontend/gui/termwindow.rs | 8 ++++++++ src/keyassignment.rs | 5 +++++ window/src/connection.rs | 5 +++++ window/src/os/macos/connection.rs | 6 ++++++ 6 files changed, 33 insertions(+) diff --git a/docs/changelog.markdown b/docs/changelog.markdown index 77ba6c4eb..72052dd21 100644 --- a/docs/changelog.markdown +++ b/docs/changelog.markdown @@ -14,6 +14,11 @@ brief notes about them may accumulate here. size of new terminal windows * Added `hide_tab_bar_if_only_one_tab = true` config option to hide the tab bar when the window contains only a single tab. +* Added `HideApplication` key action (defaults to `CMD-H` on macOS only) which + hides the wezterm application. This is macOS specific. +* Added `QuitApplication` key action which causes the gui loop to terminate + and the application to exit. This is not bound by default, but you may + choose to assign it to something like `CMD-Q`. ### 20200202-181957-765184e5 diff --git a/src/config/keys.rs b/src/config/keys.rs index 7f0f90d77..f409623dc 100644 --- a/src/config/keys.rs +++ b/src/config/keys.rs @@ -38,6 +38,8 @@ impl std::convert::TryInto for &Key { KeyAction::Copy => KeyAssignment::Copy, KeyAction::Paste => KeyAssignment::Paste, KeyAction::Hide => KeyAssignment::Hide, + KeyAction::HideApplication => KeyAssignment::HideApplication, + KeyAction::QuitApplication => KeyAssignment::QuitApplication, KeyAction::Show => KeyAssignment::Show, KeyAction::IncreaseFontSize => KeyAssignment::IncreaseFontSize, KeyAction::DecreaseFontSize => KeyAssignment::DecreaseFontSize, @@ -103,6 +105,8 @@ pub enum KeyAction { SendString, Nop, Hide, + HideApplication, + QuitApplication, Show, CloseCurrentTab, ReloadConfiguration, diff --git a/src/frontend/gui/termwindow.rs b/src/frontend/gui/termwindow.rs index f9fdae107..873652f94 100644 --- a/src/frontend/gui/termwindow.rs +++ b/src/frontend/gui/termwindow.rs @@ -1250,6 +1250,14 @@ impl TermWindow { MoveTabRelative(n) => self.move_tab_relative(*n)?, ScrollByPage(n) => self.scroll_by_page(*n)?, ShowTabNavigator => self.show_tab_navigator(), + HideApplication => { + let con = Connection::get().expect("call on gui thread"); + con.hide_application(); + } + QuitApplication => { + let con = Connection::get().expect("call on gui thread"); + con.terminate_message_loop(); + } }; Ok(()) } diff --git a/src/keyassignment.rs b/src/keyassignment.rs index 1304cf0f9..f4351f309 100644 --- a/src/keyassignment.rs +++ b/src/keyassignment.rs @@ -39,6 +39,8 @@ pub enum KeyAssignment { MoveTab(usize), ScrollByPage(isize), ShowTabNavigator, + HideApplication, + QuitApplication, } pub struct KeyMap(HashMap<(KeyCode, KeyModifiers), KeyAssignment>); @@ -150,6 +152,9 @@ impl KeyMap { [KeyModifiers::ALT, KeyCode::Char('9'), ShowTabNavigator], ); + #[cfg(target_os = "macos")] + m!([KeyModifiers::SUPER, KeyCode::Char('h'), HideApplication],); + Self(map) } diff --git a/window/src/connection.rs b/window/src/connection.rs index d48facaab..bd6106678 100644 --- a/window/src/connection.rs +++ b/window/src/connection.rs @@ -28,6 +28,11 @@ pub trait ConnectionOps { fn terminate_message_loop(&self); fn run_message_loop(&self) -> Fallible<()>; + /// Hide the application. + /// This actions hides all of the windows of the application and switches + /// focus away from it. + fn hide_application(&self) {} + // TODO: return a handle that can be used to cancel the timer fn schedule_timer(&self, interval: std::time::Duration, callback: F); } diff --git a/window/src/os/macos/connection.rs b/window/src/os/macos/connection.rs index 0a2957d81..e4e02b62a 100644 --- a/window/src/os/macos/connection.rs +++ b/window/src/os/macos/connection.rs @@ -85,6 +85,12 @@ impl ConnectionOps for Connection { Ok(()) } + fn hide_application(&self) { + unsafe { + let () = msg_send![self.ns_app, hide: self.ns_app]; + } + } + fn schedule_timer(&self, interval: std::time::Duration, callback: F) { let secs_f64 = (interval.as_secs() as f64) + (f64::from(interval.subsec_nanos()) / 1_000_000_000_f64);