From fcff8a195fd277db7138ffbea76992f965b21720 Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Tue, 18 Apr 2023 14:47:04 -0300 Subject: [PATCH] rename HotKey to Shortcut, enhance api --- plugins/global-shortcut/src/lib.rs | 113 ++++++++++++++++++----------- 1 file changed, 72 insertions(+), 41 deletions(-) diff --git a/plugins/global-shortcut/src/lib.rs b/plugins/global-shortcut/src/lib.rs index b0160fd6..eccd9d6e 100644 --- a/plugins/global-shortcut/src/lib.rs +++ b/plugins/global-shortcut/src/lib.rs @@ -4,7 +4,7 @@ use std::{ sync::{Arc, Mutex}, }; -pub use global_hotkey::hotkey::{Code, HotKey, Modifiers}; +pub use global_hotkey::hotkey::{Code, HotKey as Shortcut, Modifiers}; use global_hotkey::{GlobalHotKeyEvent, GlobalHotKeyManager}; use tauri::{ api::ipc::CallbackFn, @@ -18,7 +18,7 @@ pub use error::Error; type Result = std::result::Result; type HotKeyId = u32; -enum HotKeySource { +enum ShortcutSource { Ipc { window: Window, handler: CallbackFn, @@ -26,7 +26,7 @@ enum HotKeySource { Rust, } -impl Clone for HotKeySource { +impl Clone for ShortcutSource { fn clone(&self) -> Self { match self { Self::Ipc { window, handler } => Self::Ipc { @@ -38,24 +38,24 @@ impl Clone for HotKeySource { } } -pub struct Shortcut(HotKey); +pub struct ShortcutWrapper(Shortcut); -impl From for Shortcut { - fn from(value: HotKey) -> Self { +impl From for ShortcutWrapper { + fn from(value: Shortcut) -> Self { Self(value) } } -impl TryFrom<&str> for Shortcut { +impl TryFrom<&str> for ShortcutWrapper { type Error = global_hotkey::Error; fn try_from(value: &str) -> std::result::Result { - HotKey::from_str(value).map(Shortcut) + Shortcut::from_str(value).map(ShortcutWrapper) } } struct RegisteredShortcut { - source: HotKeySource, - shortcut: (HotKey, Option), + source: ShortcutSource, + shortcut: (Shortcut, Option), } pub struct GlobalShortcut { @@ -68,8 +68,8 @@ pub struct GlobalShortcut { impl GlobalShortcut { fn register_internal( &self, - shortcut: (HotKey, Option), - source: HotKeySource, + shortcut: (Shortcut, Option), + source: ShortcutSource, ) -> Result<()> { let id = shortcut.0.id(); acquire_manager(&self.manager)?.register(shortcut.0.clone())?; @@ -80,14 +80,14 @@ impl GlobalShortcut { Ok(()) } - fn register_all_internal)>>( + fn register_all_internal)>>( &self, shortcuts: S, - source: HotKeySource, + source: ShortcutSource, ) -> Result<()> { let hotkeys = shortcuts .into_iter() - .collect::)>>(); + .collect::)>>(); let manager = acquire_manager(&self.manager)?; let mut shortcuts = self.shortcuts.lock().unwrap(); @@ -106,40 +106,61 @@ impl GlobalShortcut { Ok(()) } - pub fn register>(&self, shortcut: S) -> Result<()> + pub fn register>(&self, shortcut: S) -> Result<()> where S::Error: std::error::Error, { - self.register_internal( - ( - shortcut - .try_into() - .map(|s| s.0) - .map_err(|e| Error::GlobalHotkey(e.to_string()))?, - None, - ), - HotKeySource::Rust, - ) + self.register_internal((try_into_shortcut(shortcut)?, None), ShortcutSource::Rust) } - pub fn register_all>(&self, shortcuts: S) -> Result<()> { - self.register_all_internal(shortcuts.into_iter().map(|s| (s, None)), HotKeySource::Rust) + pub fn register_all, S: IntoIterator>( + &self, + shortcuts: S, + ) -> Result<()> + where + T::Error: std::error::Error, + { + let mut s = Vec::new(); + for shortcut in shortcuts { + s.push((try_into_shortcut(shortcut)?, None)); + } + self.register_all_internal(s, ShortcutSource::Rust) } - pub fn unregister(&self, shortcut: HotKey) -> Result<()> { + pub fn unregister>(&self, shortcut: S) -> Result<()> + where + S::Error: std::error::Error, + { acquire_manager(&self.manager)? - .unregister(shortcut) + .unregister(try_into_shortcut(shortcut)?) .map_err(Into::into) } - pub fn unregister_all>(&self, shortcuts: S) -> Result<()> { + pub fn unregister_all, S: IntoIterator>( + &self, + shortcuts: S, + ) -> Result<()> + where + T::Error: std::error::Error, + { + let mut s = Vec::new(); + for shortcut in shortcuts { + s.push(try_into_shortcut(shortcut)?); + } acquire_manager(&self.manager)? - .unregister_all(&shortcuts.into_iter().collect::>()) + .unregister_all(&s) .map_err(Into::into) } - pub fn is_registered(&self, shortcut: HotKey) -> bool { - self.shortcuts.lock().unwrap().contains_key(&shortcut.id()) + pub fn is_registered>(&self, shortcut: S) -> bool + where + S::Error: std::error::Error, + { + if let Ok(shortcut) = try_into_shortcut(shortcut) { + self.shortcuts.lock().unwrap().contains_key(&shortcut.id()) + } else { + false + } } } @@ -161,10 +182,20 @@ fn acquire_manager( .map_err(|e| Error::GlobalHotkey(e.to_string())) } -fn parse_shortcut>(shortcut: S) -> Result { +fn parse_shortcut>(shortcut: S) -> Result { shortcut.as_ref().parse().map_err(Into::into) } +fn try_into_shortcut>(shortcut: S) -> Result +where + S::Error: std::error::Error, +{ + shortcut + .try_into() + .map(|s| s.0) + .map_err(|e| Error::GlobalHotkey(e.to_string())) +} + #[tauri::command] fn register( window: Window, @@ -174,7 +205,7 @@ fn register( ) -> Result<()> { global_shortcut.register_internal( (parse_shortcut(&shortcut)?, Some(shortcut)), - HotKeySource::Ipc { window, handler }, + ShortcutSource::Ipc { window, handler }, ) } @@ -189,7 +220,7 @@ fn register_all( for shortcut in shortcuts { hotkeys.push((parse_shortcut(&shortcut)?, Some(shortcut))); } - global_shortcut.register_all_internal(hotkeys, HotKeySource::Ipc { window, handler }) + global_shortcut.register_all_internal(hotkeys, ShortcutSource::Ipc { window, handler }) } #[tauri::command] @@ -225,7 +256,7 @@ fn is_registered( #[derive(Default)] pub struct Builder { - handler: Option>, + handler: Option>, } impl Builder { @@ -233,7 +264,7 @@ impl Builder { Self::default() } - pub fn with_handler(handler: F) -> Self { + pub fn with_handler(handler: F) -> Self { Self { handler: Some(Box::new(handler)), } @@ -257,7 +288,7 @@ impl Builder { GlobalHotKeyEvent::set_event_handler(Some(move |e: GlobalHotKeyEvent| { if let Some(shortcut) = shortcuts_.lock().unwrap().get(&e.id) { match &shortcut.source { - HotKeySource::Ipc { window, handler } => { + ShortcutSource::Ipc { window, handler } => { let callback_string = tauri::api::ipc::format_callback( *handler, &shortcut.shortcut.1, @@ -265,7 +296,7 @@ impl Builder { .expect("unable to serialize shortcut string to json"); let _ = window.eval(callback_string.as_str()); } - HotKeySource::Rust => { + ShortcutSource::Rust => { if let Some(handler) = &handler { handler(&shortcut.shortcut.0); }