feat(global-shortcut): pass app handle to the closure, closes #657 (#658)

* feat(global-shortcut): pass app handle to the closure, closes #657

* Update global-shortcut-app-handle.md
pull/260/merge
Amr Bashir 2 years ago committed by GitHub
parent 28ec084e56
commit c2103c91bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,5 @@
---
"global-shortcut": "patch"
---
**Breaking Change**: Changed `Builder::with_handler` closure to take `&AppHandle` as the first argument and the shortcut as the second argument.

@ -33,7 +33,7 @@ mod error;
pub use error::Error; pub use error::Error;
type Result<T> = std::result::Result<T, Error>; type Result<T> = std::result::Result<T, Error>;
type HotKeyId = u32; type HotKeyId = u32;
type HandlerFn = Box<dyn Fn(&Shortcut) + Send + Sync + 'static>; type HandlerFn<R> = Box<dyn Fn(&AppHandle<R>, &Shortcut) + Send + Sync + 'static>;
enum ShortcutSource { enum ShortcutSource {
Ipc(Channel), Ipc(Channel),
@ -268,23 +268,32 @@ fn is_registered<R: Runtime>(
Ok(global_shortcut.is_registered(parse_shortcut(shortcut)?)) Ok(global_shortcut.is_registered(parse_shortcut(shortcut)?))
} }
#[derive(Default)] pub struct Builder<R: Runtime> {
pub struct Builder { handler: Option<HandlerFn<R>>,
handler: Option<HandlerFn>,
} }
impl Builder { impl<R: Runtime> Default for Builder<R> {
fn default() -> Self {
Self {
handler: Default::default(),
}
}
}
impl<R: Runtime> Builder<R> {
pub fn new() -> Self { pub fn new() -> Self {
Self::default() Self::default()
} }
pub fn with_handler<F: Fn(&Shortcut) + Send + Sync + 'static>(handler: F) -> Self { pub fn with_handler<F: Fn(&AppHandle<R>, &Shortcut) + Send + Sync + 'static>(
handler: F,
) -> Self {
Self { Self {
handler: Some(Box::new(handler)), handler: Some(Box::new(handler)),
} }
} }
pub fn build<R: Runtime>(self) -> TauriPlugin<R> { pub fn build(self) -> TauriPlugin<R> {
let handler = self.handler; let handler = self.handler;
PluginBuilder::new("globalShortcut") PluginBuilder::new("globalShortcut")
.js_init_script(include_str!("api-iife.js").to_string()) .js_init_script(include_str!("api-iife.js").to_string())
@ -300,6 +309,7 @@ impl Builder {
Arc::new(Mutex::new(HashMap::<HotKeyId, RegisteredShortcut>::new())); Arc::new(Mutex::new(HashMap::<HotKeyId, RegisteredShortcut>::new()));
let shortcuts_ = shortcuts.clone(); let shortcuts_ = shortcuts.clone();
let app_handle = app.clone();
GlobalHotKeyEvent::set_event_handler(Some(move |e: GlobalHotKeyEvent| { GlobalHotKeyEvent::set_event_handler(Some(move |e: GlobalHotKeyEvent| {
if let Some(shortcut) = shortcuts_.lock().unwrap().get(&e.id) { if let Some(shortcut) = shortcuts_.lock().unwrap().get(&e.id) {
match &shortcut.source { match &shortcut.source {
@ -308,7 +318,7 @@ impl Builder {
} }
ShortcutSource::Rust => { ShortcutSource::Rust => {
if let Some(handler) = &handler { if let Some(handler) = &handler {
handler(&shortcut.shortcut.0); handler(&app_handle, &shortcut.shortcut.0);
} }
} }
} }

Loading…
Cancel
Save