From 47a3e1cfef589b96f4d4769c839a3e0084369168 Mon Sep 17 00:00:00 2001 From: Joseph Montanaro Date: Mon, 18 Sep 2023 20:13:56 -0700 Subject: [PATCH] start work on invoking shortcuts from CLI --- src-tauri/src/shortcuts.rs | 51 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src-tauri/src/shortcuts.rs diff --git a/src-tauri/src/shortcuts.rs b/src-tauri/src/shortcuts.rs new file mode 100644 index 0000000..c40d758 --- /dev/null +++ b/src-tauri/src/shortcuts.rs @@ -0,0 +1,51 @@ +use serde::{Serialize, Deserialize}; + +use tauri::{ + AppHandle, + Manager, +}; + +use crate::app::APP; +use crate::config::HotkeysConfig; +use crate::terminal; + + +#[derive(Debug, Serialize, Deserialize)] +pub enum ShortcutAction { + ShowWindow, + LaunchTerminal, +} + + +pub fn exec_shortcut(action: ShortcutAction) { + match action { + ShowWindow => { + let app = APP.get().unwrap(); + app.get_window("main").map(|w| w.show()); + }, + LaunchTerminal => terminal::launch(false), + } +} + + +pub fn register_hotkeys(hotkeys: &HotkeysConfig) -> tauri::Result<()> { + let app = APP.get().unwrap(); + let mut manager = app.global_shortcut_manager(); + manager.unregister_all()?; + + if hotkeys.show_window.enabled { + manager.register( + hotkeys.show_window.keys, + || exec_shortcut(ShortcutAction::ShowWindow) + )?; + } + + if hotkeys.launch_terminal.enabled { + manager.register( + &hotkeys.launch_terminal.keys, + || exec_shortcut(ShortcutAction::LaunchTerminal) + )?; + } + + Ok(()) +}