refactor(global-shortcut): enhance un/register to accept an array, remove un/registerAll (#1117)

* refactor(shell): enhance `un/register` to accept an array, remove `un/registerAll`

closes #1101

* Update lib.rs

* remove permissions, cleanup docs

* bring back unregister_all

* fmt

* fix build

* bundle

---------

Co-authored-by: Lucas Nogueira <lucas@tauri.app>
This commit is contained in:
Amr Bashir
2024-07-08 19:20:00 +03:00
committed by GitHub
co-authored by Lucas Nogueira
parent a66549329c
commit 381a466db3
9 changed files with 101 additions and 123 deletions
+33 -43
View File
@@ -29,7 +29,7 @@ use serde::Serialize;
use tauri::{
ipc::Channel,
plugin::{Builder as PluginBuilder, TauriPlugin},
AppHandle, Manager, Runtime, State, Window,
AppHandle, Manager, Runtime, State,
};
mod error;
@@ -84,7 +84,7 @@ impl<R: Runtime> GlobalShortcut<R> {
Ok(())
}
fn register_all_internal<S, F>(&self, shortcuts: S, handler: Option<F>) -> Result<()>
fn register_multiple_internal<S, F>(&self, shortcuts: S, handler: Option<F>) -> Result<()>
where
S: IntoIterator<Item = Shortcut>,
F: Fn(&AppHandle<R>, &Shortcut, ShortcutEvent) + Send + Sync + 'static,
@@ -107,7 +107,9 @@ impl<R: Runtime> GlobalShortcut<R> {
Ok(())
}
}
impl<R: Runtime> GlobalShortcut<R> {
/// Register a shortcut.
pub fn register<S>(&self, shortcut: S) -> Result<()>
where
@@ -131,7 +133,7 @@ impl<R: Runtime> GlobalShortcut<R> {
}
/// Register multiple shortcuts.
pub fn register_all<S, T>(&self, shortcuts: S) -> Result<()>
pub fn register_multiple<S, T>(&self, shortcuts: S) -> Result<()>
where
S: IntoIterator<Item = T>,
T: TryInto<ShortcutWrapper>,
@@ -141,11 +143,11 @@ impl<R: Runtime> GlobalShortcut<R> {
for shortcut in shortcuts {
s.push(try_into_shortcut(shortcut)?);
}
self.register_all_internal(s, None::<fn(&AppHandle<R>, &Shortcut, ShortcutEvent)>)
self.register_multiple_internal(s, None::<fn(&AppHandle<R>, &Shortcut, ShortcutEvent)>)
}
/// Register multiple shortcuts with a handler.
pub fn on_all_shortcuts<S, T, F>(&self, shortcuts: S, handler: F) -> Result<()>
pub fn on_shortcuts<S, T, F>(&self, shortcuts: S, handler: F) -> Result<()>
where
S: IntoIterator<Item = T>,
T: TryInto<ShortcutWrapper>,
@@ -156,9 +158,10 @@ impl<R: Runtime> GlobalShortcut<R> {
for shortcut in shortcuts {
s.push(try_into_shortcut(shortcut)?);
}
self.register_all_internal(s, Some(handler))
self.register_multiple_internal(s, Some(handler))
}
/// Unregister a shortcut
pub fn unregister<S: TryInto<ShortcutWrapper>>(&self, shortcut: S) -> Result<()>
where
S::Error: std::error::Error,
@@ -169,7 +172,8 @@ impl<R: Runtime> GlobalShortcut<R> {
Ok(())
}
pub fn unregister_all<T: TryInto<ShortcutWrapper>, S: IntoIterator<Item = T>>(
/// Unregister multiple shortcuts.
pub fn unregister_multiple<T: TryInto<ShortcutWrapper>, S: IntoIterator<Item = T>>(
&self,
shortcuts: S,
) -> Result<()>
@@ -191,6 +195,16 @@ impl<R: Runtime> GlobalShortcut<R> {
Ok(())
}
/// Unregister all registered shortcuts.
pub fn unregister_all(&self) -> Result<()> {
let mut shortcuts = self.shortcuts.lock().unwrap();
let hotkeys = std::mem::take(&mut *shortcuts);
let hotkeys = hotkeys.values().map(|s| s.shortcut).collect::<Vec<_>>();
self.manager
.unregister_all(hotkeys.as_slice())
.map_err(Into::into)
}
/// Determines whether the given shortcut is registered by this application or not.
///
/// If the shortcut is registered by another application, it will still return `false`.
@@ -239,29 +253,7 @@ struct ShortcutJsEvent {
#[tauri::command]
fn register<R: Runtime>(
_window: Window<R>,
global_shortcut: State<'_, GlobalShortcut<R>>,
shortcut: String,
handler: Channel,
) -> Result<()> {
global_shortcut.register_internal(
parse_shortcut(shortcut)?,
Some(
move |_app: &AppHandle<R>, shortcut: &Shortcut, e: ShortcutEvent| {
let js_event = ShortcutJsEvent {
id: e.id,
state: e.state,
shortcut: shortcut.into_string(),
};
let _ = handler.send(js_event);
},
),
)
}
#[tauri::command]
fn register_all<R: Runtime>(
_window: Window<R>,
_app: AppHandle<R>,
global_shortcut: State<'_, GlobalShortcut<R>>,
shortcuts: Vec<String>,
handler: Channel,
@@ -275,7 +267,7 @@ fn register_all<R: Runtime>(
hotkeys.push(hotkey);
}
global_shortcut.register_all_internal(
global_shortcut.register_multiple_internal(
hotkeys,
Some(
move |_app: &AppHandle<R>, shortcut: &Shortcut, e: ShortcutEvent| {
@@ -292,15 +284,6 @@ fn register_all<R: Runtime>(
#[tauri::command]
fn unregister<R: Runtime>(
_app: AppHandle<R>,
global_shortcut: State<'_, GlobalShortcut<R>>,
shortcut: String,
) -> Result<()> {
global_shortcut.unregister(parse_shortcut(shortcut)?)
}
#[tauri::command]
fn unregister_all<R: Runtime>(
_app: AppHandle<R>,
global_shortcut: State<'_, GlobalShortcut<R>>,
shortcuts: Vec<String>,
@@ -309,7 +292,15 @@ fn unregister_all<R: Runtime>(
for shortcut in shortcuts {
hotkeys.push(parse_shortcut(&shortcut)?);
}
global_shortcut.unregister_all(hotkeys)
global_shortcut.unregister_multiple(hotkeys)
}
#[tauri::command]
fn unregister_all<R: Runtime>(
_app: AppHandle<R>,
global_shortcut: State<'_, GlobalShortcut<R>>,
) -> Result<()> {
global_shortcut.unregister_all()
}
#[tauri::command]
@@ -379,10 +370,9 @@ impl<R: Runtime> Builder<R> {
PluginBuilder::new("global-shortcut")
.invoke_handler(tauri::generate_handler![
register,
register_all,
unregister,
unregister_all,
is_registered
is_registered,
])
.setup(move |app, _api| {
let manager = GlobalHotKeyManager::new()?;