66
77use 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};
1819use tauri_runtime:: { Error , GlobalShortcutManager , Result , UserEvent } ;
1920#[ cfg( desktop) ]
2021pub 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) ]
4041unsafe 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 ) ]
4454pub 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