feat: add back tauri::Builder::on_menu_event (#11228)

* feat: add back `tauri::Builder::on_menu_event`

* fix build

* fix build and fmt

* fix mobile
This commit is contained in:
Amr Bashir 2024-10-09 17:09:42 +03:00 committed by GitHub
parent 04fd3a7db5
commit 1d3f51e100
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 41 additions and 1 deletions

View File

@ -0,0 +1,5 @@
---
"tauri": "patch:feat"
---
Add `tauri::Builder::on_menu_event`.

View File

@ -1224,6 +1224,10 @@ pub struct Builder<R: Runtime> {
#[cfg(desktop)]
menu: Option<Box<dyn FnOnce(&AppHandle<R>) -> crate::Result<Menu<R>> + Send>>,
/// Menu event listeners for any menu event.
#[cfg(desktop)]
menu_event_listeners: Vec<GlobalMenuEventListener<AppHandle<R>>>,
/// Enable macOS default menu creation.
#[allow(unused)]
enable_macos_default_menu: bool,
@ -1293,6 +1297,8 @@ impl<R: Runtime> Builder<R> {
state: StateManager::new(),
#[cfg(desktop)]
menu: None,
#[cfg(desktop)]
menu_event_listeners: Vec::new(),
enable_macos_default_menu: true,
window_event_listeners: Vec::new(),
webview_event_listeners: Vec::new(),
@ -1615,6 +1621,29 @@ tauri::Builder::default()
self
}
/// Registers an event handler for any menu event.
///
/// # Examples
/// ```
/// use tauri::menu::*;
///
/// tauri::Builder::default()
/// .on_menu_event(|app, event| {
/// if event.id() == "quit" {
/// app.exit(0);
/// }
/// });
/// ```
#[must_use]
#[cfg(desktop)]
pub fn on_menu_event<F: Fn(&AppHandle<R>, MenuEvent) + Send + Sync + 'static>(
mut self,
f: F,
) -> Self {
self.menu_event_listeners.push(Box::new(f));
self
}
/// Enable or disable the default menu on macOS. Enabled by default.
///
/// # Examples
@ -1820,6 +1849,8 @@ tauri::Builder::default()
self.on_page_load,
self.uri_scheme_protocols,
self.state,
#[cfg(desktop)]
self.menu_event_listeners,
self.window_event_listeners,
self.webview_event_listeners,
#[cfg(desktop)]

View File

@ -589,6 +589,7 @@ mod tests {
Default::default(),
Default::default(),
Default::default(),
Default::default(),
"".into(),
crate::generate_invoke_key().unwrap(),
);
@ -702,6 +703,7 @@ mod tests {
Default::default(),
Default::default(),
Default::default(),
Default::default(),
"".into(),
crate::generate_invoke_key().unwrap(),
);

View File

@ -248,6 +248,7 @@ impl<R: Runtime> AppManager<R> {
on_page_load: Option<Arc<OnPageLoad<R>>>,
uri_scheme_protocols: HashMap<String, Arc<webview::UriSchemeProtocol<R>>>,
state: StateManager,
#[cfg(desktop)] menu_event_listener: Vec<crate::app::GlobalMenuEventListener<AppHandle<R>>>,
window_event_listeners: Vec<GlobalWindowEventListener<R>>,
webiew_event_listeners: Vec<GlobalWebviewEventListener<R>>,
#[cfg(desktop)] window_menu_event_listeners: HashMap<
@ -290,7 +291,7 @@ impl<R: Runtime> AppManager<R> {
menu: menu::MenuManager {
menus: Default::default(),
menu: Default::default(),
global_event_listeners: Default::default(),
global_event_listeners: Mutex::new(menu_event_listener),
event_listeners: Mutex::new(window_menu_event_listeners),
},
plugins: Mutex::new(plugins),
@ -730,6 +731,7 @@ mod test {
Default::default(),
Default::default(),
Default::default(),
Default::default(),
"".into(),
crate::generate_invoke_key().unwrap(),
);