start work on invoking shortcuts from CLI

This commit is contained in:
Joseph Montanaro 2023-09-18 20:13:56 -07:00
parent 1047818fdc
commit 47a3e1cfef

View File

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