make keybinds configurable
This commit is contained in:
@ -6,7 +6,6 @@ use is_terminal::IsTerminal;
|
||||
use serde::{Serialize, Deserialize};
|
||||
use sqlx::SqlitePool;
|
||||
use tauri::{
|
||||
AppHandle,
|
||||
Manager,
|
||||
GlobalShortcutManager,
|
||||
async_runtime as rt,
|
||||
@ -27,10 +26,17 @@ pub struct TermConfig {
|
||||
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
|
||||
pub struct HotkeyConfig {
|
||||
pub struct HotkeysConfig {
|
||||
// tauri uses strings to represent keybinds, so we will as well
|
||||
pub show_window: String,
|
||||
pub launch_terminal: String,
|
||||
pub show_window: Hotkey,
|
||||
pub launch_terminal: Hotkey,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
|
||||
pub struct Hotkey {
|
||||
pub keys: String,
|
||||
pub enabled: bool,
|
||||
}
|
||||
|
||||
|
||||
@ -49,7 +55,7 @@ pub struct AppConfig {
|
||||
#[serde(default = "default_term_config")]
|
||||
pub terminal: TermConfig,
|
||||
#[serde(default = "default_hotkey_config")]
|
||||
pub hotkeys: HotkeyConfig,
|
||||
pub hotkeys: HotkeysConfig,
|
||||
}
|
||||
|
||||
|
||||
@ -187,42 +193,46 @@ fn default_term_config() -> TermConfig {
|
||||
}
|
||||
|
||||
|
||||
fn default_hotkey_config() -> HotkeyConfig {
|
||||
HotkeyConfig {
|
||||
show_window: "alt+shift+C".into(),
|
||||
launch_terminal: "alt+shift+T".into(),
|
||||
fn default_hotkey_config() -> HotkeysConfig {
|
||||
HotkeysConfig {
|
||||
show_window: Hotkey {keys: "alt+shift+C".into(), enabled: true},
|
||||
launch_terminal: Hotkey {keys: "alt+shift+T".into(), enabled: true},
|
||||
}
|
||||
}
|
||||
|
||||
// note: will panic if called before APP is set
|
||||
pub fn register_hotkeys(hotkeys: &HotkeyConfig) -> tauri::Result<()> {
|
||||
pub fn register_hotkeys(hotkeys: &HotkeysConfig) -> tauri::Result<()> {
|
||||
let app = crate::app::APP.get().unwrap();
|
||||
|
||||
let mut manager = app.global_shortcut_manager();
|
||||
manager.unregister_all()?;
|
||||
|
||||
let h = app.app_handle();
|
||||
manager.register(
|
||||
&hotkeys.show_window,
|
||||
move || {
|
||||
h.get_window("main")
|
||||
.map(|w| w.show().error_popup("Failed to show"))
|
||||
.ok_or(HandlerError::NoMainWindow)
|
||||
.error_popup("No main window");
|
||||
},
|
||||
)?;
|
||||
if hotkeys.show_window.enabled {
|
||||
let handle = app.app_handle();
|
||||
manager.register(
|
||||
&hotkeys.show_window.keys,
|
||||
move || {
|
||||
handle.get_window("main")
|
||||
.map(|w| w.show().error_popup("Failed to show"))
|
||||
.ok_or(HandlerError::NoMainWindow)
|
||||
.error_popup("No main window");
|
||||
},
|
||||
)?;
|
||||
}
|
||||
|
||||
// register() doesn't take an async fn, so we have to use spawn
|
||||
manager.register(
|
||||
&hotkeys.launch_terminal,
|
||||
|| {
|
||||
rt::spawn(async {
|
||||
crate::terminal::launch(false)
|
||||
.await
|
||||
.error_popup("Failed to launch");
|
||||
});
|
||||
}
|
||||
)?;
|
||||
if hotkeys.launch_terminal.enabled {
|
||||
// register() doesn't take an async fn, so we have to use spawn
|
||||
manager.register(
|
||||
&hotkeys.launch_terminal.keys,
|
||||
|| {
|
||||
rt::spawn(async {
|
||||
crate::terminal::launch(false)
|
||||
.await
|
||||
.error_popup("Failed to launch");
|
||||
});
|
||||
}
|
||||
)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -71,7 +71,9 @@ impl AppState {
|
||||
sv.rebind(new_config.listen_addr, new_config.listen_port).await?;
|
||||
}
|
||||
// re-register hotkeys if necessary
|
||||
if new_config.hotkeys != live_config.hotkeys {
|
||||
if new_config.hotkeys.show_window != live_config.hotkeys.show_window
|
||||
|| new_config.hotkeys.launch_terminal != live_config.hotkeys.launch_terminal
|
||||
{
|
||||
config::register_hotkeys(&new_config.hotkeys)?;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user