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(()) +}