Skip to content

Commit

Permalink
fix(core): avoid panics in global shortcut, closes #7105 (#7136)
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir authored Jun 5, 2023
1 parent 647800c commit b41b57e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .changes/core-global-shortcut-panics.md
Original file line number Diff line number Diff line change
@@ -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.
23 changes: 20 additions & 3 deletions core/tauri-runtime-wry/src/global_shortcut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use std::{
collections::HashMap,
error::Error as StdError,
fmt,
sync::{
mpsc::{channel, Sender},
Expand All @@ -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},
};

Expand All @@ -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<T: UserEvent> {
Expand Down Expand Up @@ -67,14 +77,21 @@ impl<T: UserEvent> GlobalShortcutManager for GlobalShortcutManagerHandle<T> {
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<F: Fn() + Send + 'static>(&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!(
Expand Down

0 comments on commit b41b57e

Please sign in to comment.