Skip to content

Commit b41b57e

Browse files
authored
fix(core): avoid panics in global shortcut, closes #7105 (#7136)
1 parent 647800c commit b41b57e

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'tauri': 'patch'
3+
'tauri-runtime-wry': 'patch'
4+
---
5+
6+
Fix panics when registering an invalid global shortcuts or checking it is registered and return proper errors instead.

core/tauri-runtime-wry/src/global_shortcut.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
77
use std::{
88
collections::HashMap,
9+
error::Error as StdError,
910
fmt,
1011
sync::{
1112
mpsc::{channel, Sender},
@@ -18,7 +19,7 @@ use crate::{getter, Context, Message};
1819
use tauri_runtime::{Error, GlobalShortcutManager, Result, UserEvent};
1920
#[cfg(desktop)]
2021
pub use wry::application::{
21-
accelerator::{Accelerator, AcceleratorId},
22+
accelerator::{Accelerator, AcceleratorId, AcceleratorParseError},
2223
global_shortcut::{GlobalShortcut, ShortcutManager as WryShortcutManager},
2324
};
2425

@@ -39,6 +40,15 @@ pub struct GlobalShortcutWrapper(GlobalShortcut);
3940
#[allow(clippy::non_send_fields_in_send_ty)]
4041
unsafe impl Send for GlobalShortcutWrapper {}
4142

43+
#[derive(Debug, Clone)]
44+
struct AcceleratorParseErrorWrapper(AcceleratorParseError);
45+
impl fmt::Display for AcceleratorParseErrorWrapper {
46+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47+
write!(f, "{}", self.0)
48+
}
49+
}
50+
impl StdError for AcceleratorParseErrorWrapper {}
51+
4252
/// Wrapper around [`WryShortcutManager`].
4353
#[derive(Clone)]
4454
pub struct GlobalShortcutManagerHandle<T: UserEvent> {
@@ -67,14 +77,21 @@ impl<T: UserEvent> GlobalShortcutManager for GlobalShortcutManagerHandle<T> {
6777
self,
6878
rx,
6979
Message::GlobalShortcut(GlobalShortcutMessage::IsRegistered(
70-
accelerator.parse().expect("invalid accelerator"),
80+
accelerator
81+
.parse()
82+
.map_err(|e: AcceleratorParseError| Error::GlobalShortcut(Box::new(
83+
AcceleratorParseErrorWrapper(e)
84+
)))?,
7185
tx
7286
))
7387
)
7488
}
7589

7690
fn register<F: Fn() + Send + 'static>(&mut self, accelerator: &str, handler: F) -> Result<()> {
77-
let wry_accelerator: Accelerator = accelerator.parse().expect("invalid accelerator");
91+
let wry_accelerator: Accelerator =
92+
accelerator.parse().map_err(|e: AcceleratorParseError| {
93+
Error::GlobalShortcut(Box::new(AcceleratorParseErrorWrapper(e)))
94+
})?;
7895
let id = wry_accelerator.clone().id();
7996
let (tx, rx) = channel();
8097
let shortcut = getter!(

0 commit comments

Comments
 (0)