From c2f854c3719cd87dda797e68593157638983116d Mon Sep 17 00:00:00 2001 From: hugeblank Date: Tue, 26 Sep 2023 14:09:57 -0700 Subject: [PATCH 1/2] Add ctrl+q/ctrl+w keybinds Does what title says. Lifts event handler logic from paste image support (#167). Includes some commented out logic for compatibility with systray (#166) when/if merged. Requires TRAY_LABEL be exposed as public, however. --- src-tauri/Cargo.toml | 2 +- src-tauri/src/main.rs | 19 +++++++++++++++---- src-tauri/src/shortcuts.rs | 25 +++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 src-tauri/src/shortcuts.rs diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index a7f089b..036b3b2 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -17,7 +17,7 @@ tauri-build = { version = "1.2.1", features = [] } [dependencies] serde_json = "1.0.91" serde = { version = "1.0.152", features = ["derive"] } -tauri = { version = "1.2.3", features = ["api-all", "devtools", "updater"] } +tauri = { version = "1.2.3", features = ["api-all", "devtools", "global-shortcut", "updater"] } [features] # by default Tauri runs in production mode diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index eee64e1..350a6d2 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -5,6 +5,7 @@ #[cfg(target_os = "macos")] mod menu; +mod shortcuts; fn main() { let builder = tauri::Builder::default(); @@ -12,7 +13,17 @@ fn main() { #[cfg(target_os = "macos")] let builder = builder.menu(menu::menu()); - builder - .run(tauri::generate_context!()) - .expect("error while running tauri application"); -} \ No newline at end of file + builder + .build(tauri::generate_context!()) + .expect("error while building tauri application") + .run(run_event_handler) +} + +fn run_event_handler(app: &tauri::AppHandle, event: tauri::RunEvent) { + match event { + tauri::RunEvent::Ready => { + shortcuts::ready_event_handler(app); + } + _ => {} + } +} diff --git a/src-tauri/src/shortcuts.rs b/src-tauri/src/shortcuts.rs new file mode 100644 index 0000000..1dbb4bd --- /dev/null +++ b/src-tauri/src/shortcuts.rs @@ -0,0 +1,25 @@ +use tauri::{ GlobalShortcutManager, Manager }; + +//use crate::tray; + +pub fn ready_event_handler( + app: &tauri::AppHandle +) { + let manager = app.global_shortcut_manager(); + manager.clone().register("CmdOrCtrl+w", hide_closure(app)).unwrap(); + manager.clone().register("CmdOrCtrl+q", close_closure(app)).unwrap(); +} + +fn hide_closure(app: &tauri::AppHandle) -> impl Fn() { + let window = app.get_window("main").unwrap(); + //let app = app.clone(); + return move || { + window.hide().unwrap(); + //tray::toggle_window_state(window.clone(), app.tray_handle_by_id(tray::TRAY_LABEL).unwrap()) + } +} + +fn close_closure(app: &tauri::AppHandle) -> impl Fn() { + let window = app.get_window("main").unwrap(); + return move || window.close().unwrap(); +} \ No newline at end of file From 3e1913b48bf643afef65e752b104ab91dde64be7 Mon Sep 17 00:00:00 2001 From: hugeblank Date: Tue, 26 Sep 2023 14:15:34 -0700 Subject: [PATCH 2/2] Comment out ctrl+w to uncomment when #166 is merged. --- src-tauri/src/shortcuts.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src-tauri/src/shortcuts.rs b/src-tauri/src/shortcuts.rs index 1dbb4bd..a60df01 100644 --- a/src-tauri/src/shortcuts.rs +++ b/src-tauri/src/shortcuts.rs @@ -6,18 +6,20 @@ pub fn ready_event_handler( app: &tauri::AppHandle ) { let manager = app.global_shortcut_manager(); - manager.clone().register("CmdOrCtrl+w", hide_closure(app)).unwrap(); + //manager.clone().register("CmdOrCtrl+w", hide_closure(app)).unwrap(); manager.clone().register("CmdOrCtrl+q", close_closure(app)).unwrap(); } +/* fn hide_closure(app: &tauri::AppHandle) -> impl Fn() { let window = app.get_window("main").unwrap(); - //let app = app.clone(); + let app = app.clone(); return move || { window.hide().unwrap(); - //tray::toggle_window_state(window.clone(), app.tray_handle_by_id(tray::TRAY_LABEL).unwrap()) + tray::toggle_window_state(window.clone(), app.tray_handle_by_id(tray::TRAY_LABEL).unwrap()) } } +*/ fn close_closure(app: &tauri::AppHandle) -> impl Fn() { let window = app.get_window("main").unwrap();