diff --git a/.changes/core-global-shortcut-panics.md b/.changes/core-global-shortcut-panics.md new file mode 100644 index 000000000..8f6eafe22 --- /dev/null +++ b/.changes/core-global-shortcut-panics.md @@ -0,0 +1,6 @@ +--- +'tauri': 'patch' +'tauri-runtime-wry': 'patch' +--- + +Fix panics when registering an invalid global shortcuts or checking it is registered and return proper errors instead. diff --git a/core/tauri-runtime-wry/src/global_shortcut.rs b/core/tauri-runtime-wry/src/global_shortcut.rs index 17d30d29d..4b3bbb61c 100644 --- a/core/tauri-runtime-wry/src/global_shortcut.rs +++ b/core/tauri-runtime-wry/src/global_shortcut.rs @@ -6,6 +6,7 @@ use std::{ collections::HashMap, + error::Error as StdError, fmt, sync::{ mpsc::{channel, Sender}, @@ -18,7 +19,7 @@ use crate::{getter, Context, Message}; use tauri_runtime::{Error, GlobalShortcutManager, Result, UserEvent}; #[cfg(desktop)] pub use wry::application::{ - accelerator::{Accelerator, AcceleratorId}, + accelerator::{Accelerator, AcceleratorId, AcceleratorParseError}, global_shortcut::{GlobalShortcut, ShortcutManager as WryShortcutManager}, }; @@ -39,6 +40,15 @@ pub struct GlobalShortcutWrapper(GlobalShortcut); #[allow(clippy::non_send_fields_in_send_ty)] unsafe impl Send for GlobalShortcutWrapper {} +#[derive(Debug, Clone)] +struct AcceleratorParseErrorWrapper(AcceleratorParseError); +impl fmt::Display for AcceleratorParseErrorWrapper { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.0) + } +} +impl StdError for AcceleratorParseErrorWrapper {} + /// Wrapper around [`WryShortcutManager`]. #[derive(Clone)] pub struct GlobalShortcutManagerHandle { @@ -67,14 +77,21 @@ impl GlobalShortcutManager for GlobalShortcutManagerHandle { self, rx, Message::GlobalShortcut(GlobalShortcutMessage::IsRegistered( - accelerator.parse().expect("invalid accelerator"), + accelerator + .parse() + .map_err(|e: AcceleratorParseError| Error::GlobalShortcut(Box::new( + AcceleratorParseErrorWrapper(e) + )))?, tx )) ) } fn register(&mut self, accelerator: &str, handler: F) -> Result<()> { - let wry_accelerator: Accelerator = accelerator.parse().expect("invalid accelerator"); + let wry_accelerator: Accelerator = + accelerator.parse().map_err(|e: AcceleratorParseError| { + Error::GlobalShortcut(Box::new(AcceleratorParseErrorWrapper(e))) + })?; let id = wry_accelerator.clone().id(); let (tx, rx) = channel(); let shortcut = getter!(