-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
global_shortcut.rs
106 lines (97 loc) · 3.05 KB
/
global_shortcut.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
use super::InvokeContext;
use crate::{api::ipc::CallbackFn, Runtime};
use serde::Deserialize;
use tauri_macros::{module_command_handler, CommandModule};
#[cfg(global_shortcut_all)]
use crate::runtime::GlobalShortcutManager;
/// The API descriptor.
#[derive(Deserialize, CommandModule)]
#[serde(tag = "cmd", rename_all = "camelCase")]
pub enum Cmd {
/// Register a global shortcut.
Register {
shortcut: String,
handler: CallbackFn,
},
/// Register a list of global shortcuts.
RegisterAll {
shortcuts: Vec<String>,
handler: CallbackFn,
},
/// Unregister a global shortcut.
Unregister { shortcut: String },
/// Unregisters all registered shortcuts.
UnregisterAll,
/// Determines whether the given hotkey is registered or not.
IsRegistered { shortcut: String },
}
impl Cmd {
#[module_command_handler(global_shortcut_all, "globalShortcut > all")]
fn register<R: Runtime>(
context: InvokeContext<R>,
shortcut: String,
handler: CallbackFn,
) -> crate::Result<()> {
let mut manager = context.window.app_handle.global_shortcut_manager();
register_shortcut(context.window, &mut manager, shortcut, handler)?;
Ok(())
}
#[module_command_handler(global_shortcut_all, "globalShortcut > all")]
fn register_all<R: Runtime>(
context: InvokeContext<R>,
shortcuts: Vec<String>,
handler: CallbackFn,
) -> crate::Result<()> {
let mut manager = context.window.app_handle.global_shortcut_manager();
for shortcut in shortcuts {
register_shortcut(context.window.clone(), &mut manager, shortcut, handler)?;
}
Ok(())
}
#[module_command_handler(global_shortcut_all, "globalShortcut > all")]
fn unregister<R: Runtime>(context: InvokeContext<R>, shortcut: String) -> crate::Result<()> {
context
.window
.app_handle
.global_shortcut_manager()
.unregister(&shortcut)?;
Ok(())
}
#[module_command_handler(global_shortcut_all, "globalShortcut > all")]
fn unregister_all<R: Runtime>(context: InvokeContext<R>) -> crate::Result<()> {
context
.window
.app_handle
.global_shortcut_manager()
.unregister_all()?;
Ok(())
}
#[module_command_handler(global_shortcut_all, "globalShortcut > all")]
fn is_registered<R: Runtime>(context: InvokeContext<R>, shortcut: String) -> crate::Result<bool> {
Ok(
context
.window
.app_handle
.global_shortcut_manager()
.is_registered(&shortcut)?,
)
}
}
#[cfg(global_shortcut_all)]
fn register_shortcut<R: Runtime>(
window: crate::Window<R>,
manager: &mut R::GlobalShortcutManager,
shortcut: String,
handler: CallbackFn,
) -> crate::Result<()> {
let accelerator = shortcut.clone();
manager.register(&shortcut, move || {
let callback_string = crate::api::ipc::format_callback(handler, &accelerator)
.expect("unable to serialize shortcut string to json");
let _ = window.eval(callback_string.as_str());
})?;
Ok(())
}