feat: add global_shortcut module
This commit is contained in:
parent
e638b5c289
commit
db6a72cbdd
4 changed files with 179 additions and 13 deletions
24
Cargo.toml
24
Cargo.toml
|
@ -1,35 +1,35 @@
|
|||
[package]
|
||||
edition = "2021"
|
||||
name = "tauri-sys"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
serde-wasm-bindgen = "0.4.3"
|
||||
js-sys = "0.3.59"
|
||||
serde = { version = "1.0.140", features = ["derive"] }
|
||||
wasm-bindgen = { version = "0.2.82", features = ["serde_json"] }
|
||||
wasm-bindgen-futures = "0.4.32"
|
||||
url = { version = "2.3.1", optional = true, features = ["serde"] }
|
||||
thiserror = "1.0.37"
|
||||
semver = { version = "1.0.14", optional = true, features = ["serde"] }
|
||||
js-sys = "0.3.59"
|
||||
log = "0.4.17"
|
||||
semver = {version = "1.0.14", optional = true, features = ["serde"]}
|
||||
serde = {version = "1.0.140", features = ["derive"]}
|
||||
serde-wasm-bindgen = "0.4.3"
|
||||
thiserror = "1.0.37"
|
||||
url = {version = "2.3.1", optional = true, features = ["serde"]}
|
||||
wasm-bindgen = {version = "0.2.82", features = ["serde_json"]}
|
||||
wasm-bindgen-futures = "0.4.32"
|
||||
|
||||
[dev-dependencies]
|
||||
tauri-sys = {path = ".", features = ["all"]}
|
||||
wasm-bindgen-test = "0.3.33"
|
||||
tauri-sys = { path = ".", features = ["all"] }
|
||||
# tauri = "1.1.1"
|
||||
|
||||
[package.metadata.docs.rs]
|
||||
all-features = true
|
||||
|
||||
[features]
|
||||
all = ["app", "clipboard", "event", "mocks", "tauri", "window", "process", "dialog", "os", "notification", "path", "updater"]
|
||||
all = ["app", "clipboard", "event", "mocks", "tauri", "window", "process", "dialog", "os", "notification", "path", "updater", "global_shortcut"]
|
||||
app = ["dep:semver"]
|
||||
clipboard = []
|
||||
dialog = []
|
||||
event = []
|
||||
global_shortcut = []
|
||||
mocks = []
|
||||
tauri = ["dep:url"]
|
||||
window = []
|
||||
|
|
|
@ -58,7 +58,7 @@ These API bindings are not completely on-par with `@tauri-apps/api` yet, but her
|
|||
- [x] `dialog`
|
||||
- [x] `event`
|
||||
- [ ] `fs`
|
||||
- [ ] `global_shortcut`
|
||||
- [x] `global_shortcut`
|
||||
- [ ] `http`
|
||||
- [x] `mocks`
|
||||
- [x] `notification`
|
||||
|
|
164
src/global_shortcut.rs
Normal file
164
src/global_shortcut.rs
Normal file
|
@ -0,0 +1,164 @@
|
|||
//! Register global shortcuts.
|
||||
//!
|
||||
//! The APIs must be added to tauri.allowlist.globalShortcut in tauri.conf.json:
|
||||
//!
|
||||
//! ```json
|
||||
//! {
|
||||
//! "tauri": {
|
||||
//! "allowlist": {
|
||||
//! "globalShortcut": {
|
||||
//! "all": true // enable all global shortcut APIs
|
||||
//! }
|
||||
//! }
|
||||
//! }
|
||||
//! }
|
||||
//! ```
|
||||
//! It is recommended to allowlist only the APIs you use for optimal bundle size and security.
|
||||
|
||||
use wasm_bindgen::{prelude::Closure, JsValue};
|
||||
|
||||
/// Determines whether the given shortcut is registered by this application or not.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust,no_run
|
||||
/// use tauri_sys::global_shortcut::is_registered;
|
||||
///
|
||||
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
/// let registered = is_registered("CommandOrControl+P").await?;
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
pub async fn is_registered(shortcut: &str) -> crate::Result<bool> {
|
||||
let raw = inner::isRegistered(shortcut).await?;
|
||||
|
||||
Ok(serde_wasm_bindgen::from_value(raw)?)
|
||||
}
|
||||
|
||||
/// Register a global shortcut.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust,no_run
|
||||
/// use tauri_sys::global_shortcut::register;
|
||||
/// use web_sys::console;
|
||||
///
|
||||
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
/// register("CommandOrControl+Shift+C", |_| {
|
||||
/// console::log_1(&"Shortcut triggered".into());
|
||||
/// }).await?;
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
pub async fn register<H>(shortcut: &str, mut handler: H) -> crate::Result<()>
|
||||
where
|
||||
H: FnMut(&str) + 'static,
|
||||
{
|
||||
let closure = Closure::<dyn FnMut(JsValue)>::new(move |raw: JsValue| {
|
||||
let raw = raw.as_string().unwrap();
|
||||
(handler)(raw.as_str())
|
||||
});
|
||||
|
||||
inner::register(shortcut, &closure).await?;
|
||||
|
||||
closure.forget();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Register a collection of global shortcuts.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust,no_run
|
||||
/// use tauri_sys::global_shortcut::register_all;
|
||||
///
|
||||
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
/// let registered = register_all(["CommandOrControl+Shift+C", "Ctrl+Alt+F12"], |shortcut| {
|
||||
/// console::log_1(&format!("Shortcut {} triggered", shortcut).into());
|
||||
/// }).await?;
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
pub async fn register_all<'a, I, H>(shortcuts: I, mut handler: H) -> crate::Result<()>
|
||||
where
|
||||
I: IntoIterator<Item = &'a str>,
|
||||
H: FnMut(&str) + 'static,
|
||||
{
|
||||
let shortcuts = shortcuts.into_iter().map(JsValue::from_str).collect();
|
||||
|
||||
let closure = Closure::<dyn FnMut(JsValue)>::new(move |raw: JsValue| {
|
||||
let raw = raw.as_string().unwrap();
|
||||
(handler)(raw.as_str())
|
||||
});
|
||||
|
||||
inner::registerAll(shortcuts, &closure).await?;
|
||||
|
||||
closure.forget();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Unregister a global shortcut.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust,no_run
|
||||
/// use tauri_sys::global_shortcut::unregister;
|
||||
///
|
||||
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
/// unregister("CmdOrControl+Space").await?;
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
pub async fn unregister(shortcut: &str) -> crate::Result<()> {
|
||||
inner::unregister(shortcut).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Unregisters all shortcuts registered by the application.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust,no_run
|
||||
/// use tauri_sys::global_shortcut::unregister_all;
|
||||
///
|
||||
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
/// unregister_all().await?;
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
pub async fn unregister_all() -> crate::Result<()> {
|
||||
inner::unregisterAll().await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
mod inner {
|
||||
use js_sys::Array;
|
||||
use wasm_bindgen::{
|
||||
prelude::{wasm_bindgen, Closure},
|
||||
JsValue,
|
||||
};
|
||||
|
||||
#[wasm_bindgen(module = "/src/globalShortcut.js")]
|
||||
extern "C" {
|
||||
#[wasm_bindgen(catch)]
|
||||
pub async fn isRegistered(shortcut: &str) -> Result<JsValue, JsValue>;
|
||||
#[wasm_bindgen(catch)]
|
||||
pub async fn register(
|
||||
shortcut: &str,
|
||||
handler: &Closure<dyn FnMut(JsValue)>,
|
||||
) -> Result<(), JsValue>;
|
||||
#[wasm_bindgen(catch)]
|
||||
pub async fn registerAll(
|
||||
shortcuts: Array,
|
||||
handler: &Closure<dyn FnMut(JsValue)>,
|
||||
) -> Result<(), JsValue>;
|
||||
#[wasm_bindgen(catch)]
|
||||
pub async fn unregister(shortcut: &str) -> Result<(), JsValue>;
|
||||
#[wasm_bindgen(catch)]
|
||||
pub async fn unregisterAll() -> Result<(), JsValue>;
|
||||
}
|
||||
}
|
|
@ -8,6 +8,8 @@ pub mod clipboard;
|
|||
pub mod dialog;
|
||||
#[cfg(feature = "event")]
|
||||
pub mod event;
|
||||
#[cfg(feature = "global_shortcut")]
|
||||
pub mod global_shortcut;
|
||||
#[cfg(feature = "mocks")]
|
||||
pub mod mocks;
|
||||
#[cfg(feature = "notification")]
|
||||
|
|
Loading…
Add table
Reference in a new issue