@@ -10,7 +10,7 @@ use crate::{
1010 flavors:: wry:: Wry ,
1111 manager:: { Args , WindowManager } ,
1212 tag:: Tag ,
13- webview:: { CustomProtocol , WebviewAttributes , WindowBuilder } ,
13+ webview:: { CustomProtocol , Menu , MenuItemId , WebviewAttributes , WindowBuilder } ,
1414 window:: PendingWindow ,
1515 Dispatch , Runtime ,
1616 } ,
@@ -23,6 +23,26 @@ use std::{collections::HashMap, sync::Arc};
2323#[ cfg( feature = "updater" ) ]
2424use crate :: updater;
2525
26+ pub ( crate ) type GlobalMenuEventListener < P > = Box < dyn Fn ( WindowMenuEvent < P > ) + Send + Sync > ;
27+
28+ /// A menu event that was triggered on a window.
29+ pub struct WindowMenuEvent < P : Params > {
30+ pub ( crate ) menu_item_id : MenuItemId ,
31+ pub ( crate ) window : Window < P > ,
32+ }
33+
34+ impl < P : Params > WindowMenuEvent < P > {
35+ /// The menu item id.
36+ pub fn menu_item_id ( & self ) -> MenuItemId {
37+ self . menu_item_id
38+ }
39+
40+ /// The window that the menu belongs to.
41+ pub fn window ( & self ) -> & Window < P > {
42+ & self . window
43+ }
44+ }
45+
2646/// A handle to the currently running application.
2747///
2848/// This type implements [`Manager`] which allows for manipulation of global application items.
@@ -154,6 +174,12 @@ where
154174
155175 /// App state.
156176 state : StateManager ,
177+
178+ /// The menu set to all windows.
179+ menu : Vec < Menu > ,
180+
181+ /// Menu event handlers that listens to all windows.
182+ menu_event_listeners : Vec < GlobalMenuEventListener < Args < E , L , A , R > > > ,
157183}
158184
159185impl < E , L , A , R > Builder < E , L , A , R >
@@ -173,6 +199,8 @@ where
173199 plugins : PluginStore :: default ( ) ,
174200 uri_scheme_protocols : Default :: default ( ) ,
175201 state : StateManager :: new ( ) ,
202+ menu : Vec :: new ( ) ,
203+ menu_event_listeners : Vec :: new ( ) ,
176204 }
177205 }
178206
@@ -286,6 +314,21 @@ where
286314 self
287315 }
288316
317+ /// Sets the menu to use on all windows.
318+ pub fn menu ( mut self , menu : Vec < Menu > ) -> Self {
319+ self . menu = menu;
320+ self
321+ }
322+
323+ /// Registers a menu event handler for all windows.
324+ pub fn on_menu_event < F : Fn ( WindowMenuEvent < Args < E , L , A , R > > ) + Send + Sync + ' static > (
325+ mut self ,
326+ handler : F ,
327+ ) -> Self {
328+ self . menu_event_listeners . push ( Box :: new ( handler) ) ;
329+ self
330+ }
331+
289332 /// Registers a URI scheme protocol available to all webviews.
290333 /// Leverages [setURLSchemeHandler](https://developer.apple.com/documentation/webkit/wkwebviewconfiguration/2875766-seturlschemehandler) on macOS,
291334 /// [AddWebResourceRequestedFilter](https://docs.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2.addwebresourcerequestedfilter?view=webview2-dotnet-1.0.774.44) on Windows
@@ -321,6 +364,8 @@ where
321364 self . on_page_load ,
322365 self . uri_scheme_protocols ,
323366 self . state ,
367+ self . menu ,
368+ self . menu_event_listeners ,
324369 ) ;
325370
326371 // set up all the windows defined in the config
0 commit comments