add hotkeys to show window and launch terminal
This commit is contained in:
1 parent
c98a065587
commit
5685948608
5 files changed
+69
-1
No files matched your search
@@ -5,6 +5,12 @@ use auto_launch::AutoLaunchBuilder;
|
||||
use is_terminal::IsTerminal;
|
||||
use serde::{Serialize, Deserialize};
|
||||
use sqlx::SqlitePool;
|
||||
use tauri::{
|
||||
AppHandle,
|
||||
Manager,
|
||||
GlobalShortcutManager,
|
||||
async_runtime as rt,
|
||||
};
|
||||
|
||||
use crate::errors::*;
|
||||
|
||||
@@ -20,6 +26,14 @@ pub struct TermConfig {
|
||||
}
|
||||
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
|
||||
pub struct HotkeyConfig {
|
||||
// tauri uses strings to represent keybinds, so we will as well
|
||||
pub show_window: String,
|
||||
pub launch_terminal: String,
|
||||
}
|
||||
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
pub struct AppConfig {
|
||||
#[serde(default = "default_listen_addr")]
|
||||
@@ -34,6 +48,8 @@ pub struct AppConfig {
|
||||
pub start_on_login: bool,
|
||||
#[serde(default = "default_term_config")]
|
||||
pub terminal: TermConfig,
|
||||
#[serde(default = "default_hotkey_config")]
|
||||
pub hotkeys: HotkeyConfig,
|
||||
}
|
||||
|
||||
|
||||
@@ -46,6 +62,7 @@ impl Default for AppConfig {
|
||||
start_minimized: default_start_minimized(),
|
||||
start_on_login: default_start_on_login(),
|
||||
terminal: default_term_config(),
|
||||
hotkeys: default_hotkey_config(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -170,6 +187,47 @@ fn default_term_config() -> TermConfig {
|
||||
}
|
||||
|
||||
|
||||
fn default_hotkey_config() -> HotkeyConfig {
|
||||
HotkeyConfig {
|
||||
show_window: "alt+shift+C".into(),
|
||||
launch_terminal: "alt+shift+T".into(),
|
||||
}
|
||||
}
|
||||
|
||||
// note: will panic if called before APP is set
|
||||
pub fn register_hotkeys(hotkeys: &HotkeyConfig) -> 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");
|
||||
},
|
||||
)?;
|
||||
|
||||
// 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");
|
||||
});
|
||||
}
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
fn default_listen_addr() -> Ipv4Addr { Ipv4Addr::LOCALHOST }
|
||||
fn default_rehide_ms() -> u64 { 1000 }
|
||||
// start minimized and on login only in production mode
|
||||
|
||||
Reference in new issue
Block a user