upgrade to tauri 2.0 beta
This commit is contained in:
@ -1,11 +1,16 @@
|
||||
use serde::{Serialize, Deserialize};
|
||||
|
||||
use tauri::{
|
||||
GlobalShortcutManager,
|
||||
AppHandle,
|
||||
Manager,
|
||||
async_runtime as rt,
|
||||
};
|
||||
|
||||
use tauri_plugin_global_shortcut::{
|
||||
GlobalShortcutExt,
|
||||
Error as ShortcutError,
|
||||
};
|
||||
|
||||
use crate::app::APP;
|
||||
use crate::config::HotkeysConfig;
|
||||
use crate::errors::*;
|
||||
@ -21,38 +26,62 @@ pub enum ShortcutAction {
|
||||
|
||||
pub fn exec_shortcut(action: ShortcutAction) {
|
||||
match action {
|
||||
ShortcutAction::LaunchTerminal => launch_terminal(),
|
||||
ShortcutAction::ShowWindow => {
|
||||
let app = APP.get().unwrap();
|
||||
app.get_window("main")
|
||||
.ok_or("Couldn't find application main window")
|
||||
.map(|w| w.show().error_popup("Failed to show window"))
|
||||
.error_popup("Failed to show window");
|
||||
},
|
||||
ShortcutAction::LaunchTerminal => {
|
||||
rt::spawn(async {
|
||||
terminal::launch(false).await.error_popup("Failed to launch terminal");
|
||||
});
|
||||
show_window(app);
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub fn register_hotkeys(hotkeys: &HotkeysConfig) -> tauri::Result<()> {
|
||||
fn show_window(app: &AppHandle) {
|
||||
let handle = app.clone();
|
||||
rt::spawn(async move {
|
||||
handle.get_webview_window("main")
|
||||
.ok_or("Couldn't find application main window")
|
||||
.error_popup_passthrough("Failed to show window").await?
|
||||
.show()
|
||||
.error_popup("Failed to show window").await;
|
||||
Ok::<(), ()>(())
|
||||
});
|
||||
|
||||
// app.get_webview_window("main") // Option<Window>
|
||||
// .ok_or("Couldn't find application main window") // Result<Window, &'static str>
|
||||
// .map(|w| w.show().error_popup("Failed to show window"))
|
||||
// .error_popup("Failed to show window");
|
||||
}
|
||||
|
||||
|
||||
fn launch_terminal() {
|
||||
rt::spawn(async {
|
||||
terminal::launch(false)
|
||||
.await
|
||||
.error_popup("Failed to launch terminal")
|
||||
.await;
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
pub fn register_hotkeys(hotkeys: &HotkeysConfig) -> Result<(), ShortcutError> {
|
||||
let app = APP.get().unwrap();
|
||||
let mut manager = app.global_shortcut_manager();
|
||||
manager.unregister_all()?;
|
||||
let shortcuts = app.global_shortcut();
|
||||
shortcuts.unregister_all([
|
||||
hotkeys.show_window.keys.as_str(),
|
||||
hotkeys.launch_terminal.keys.as_str(),
|
||||
])?;
|
||||
|
||||
if hotkeys.show_window.enabled {
|
||||
manager.register(
|
||||
&hotkeys.show_window.keys,
|
||||
|| exec_shortcut(ShortcutAction::ShowWindow)
|
||||
shortcuts.on_shortcut(
|
||||
hotkeys.show_window.keys.as_str(),
|
||||
|app, _shortcut, _event| show_window(app)
|
||||
)?;
|
||||
}
|
||||
|
||||
if hotkeys.launch_terminal.enabled {
|
||||
manager.register(
|
||||
&hotkeys.launch_terminal.keys,
|
||||
|| exec_shortcut(ShortcutAction::LaunchTerminal)
|
||||
shortcuts.on_shortcut(
|
||||
hotkeys.launch_terminal.keys.as_str(),
|
||||
|_app, _shortcut, _event| launch_terminal()
|
||||
)?;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user