From ae9423b9fa43799e82f40f410c1b5ffdf73bbe0e Mon Sep 17 00:00:00 2001 From: Amr Bashir Date: Thu, 20 Jul 2023 16:54:48 +0300 Subject: [PATCH] move menu/tray handlers setup from runtime to tauri --- core/tauri-runtime-wry/src/lib.rs | 18 ------- core/tauri-runtime/src/lib.rs | 4 -- core/tauri/src/app.rs | 88 +++++++++++++++++++------------ core/tauri/src/lib.rs | 7 ++- 4 files changed, 59 insertions(+), 58 deletions(-) diff --git a/core/tauri-runtime-wry/src/lib.rs b/core/tauri-runtime-wry/src/lib.rs index 64f54e09db8d..41c5f8c064c4 100644 --- a/core/tauri-runtime-wry/src/lib.rs +++ b/core/tauri-runtime-wry/src/lib.rs @@ -1740,16 +1740,6 @@ impl Wry { } fn init(event_loop: EventLoop>) -> Result { - let proxy = event_loop.create_proxy(); - menu::MenuEvent::set_event_handler(Some(move |e| { - let _ = proxy.send_event(Message::MenuEvent(e)); - })); - - let proxy = event_loop.create_proxy(); - tray::TrayIconEvent::set_event_handler(Some(move |e| { - let _ = proxy.send_event(Message::TrayIconEvent(e)); - })); - let main_thread_id = current_thread().id(); let web_context = WebContextStore::default(); @@ -2314,14 +2304,6 @@ fn handle_event_loop( callback(RunEvent::Exit); } - Event::UserEvent(Message::MenuEvent(event)) => { - callback(RunEvent::MenuEvent(event)); - } - - Event::UserEvent(Message::TrayIconEvent(event)) => { - callback(RunEvent::TrayIconEvent(event)); - } - Event::UserEvent(Message::Webview(id, WebviewMessage::WebviewEvent(event))) => { if let Some(event) = WindowEventWrapper::from(&event).0 { let windows = windows.borrow(); diff --git a/core/tauri-runtime/src/lib.rs b/core/tauri-runtime/src/lib.rs index 4eca5661e3cd..1c082c65c833 100644 --- a/core/tauri-runtime/src/lib.rs +++ b/core/tauri-runtime/src/lib.rs @@ -183,10 +183,6 @@ pub enum RunEvent { /// /// This event is useful as a place to put your code that should be run after all state-changing events have been handled and you want to do stuff (updating state, performing calculations, etc) that happens as the “main body” of your event loop. MainEventsCleared, - /// An event from a menu item, could be on the window menu bar, application menu bar (on macOS) or tray icon menu. - MenuEvent(menu::MenuEvent), - /// An event from a menu item, could be on the window menu bar, application menu bar (on macOS) or tray icon menu. - TrayIconEvent(tray_icon::TrayIconEvent), /// A custom event defined by the user. UserEvent(T), } diff --git a/core/tauri/src/app.rs b/core/tauri/src/app.rs index 37e9dac99ed0..c6b9d345679a 100644 --- a/core/tauri/src/app.rs +++ b/core/tauri/src/app.rs @@ -36,7 +36,7 @@ use tauri_runtime::{ dpi::{PhysicalPosition, PhysicalSize}, FileDropEvent, }, - RuntimeInitArgs, + EventLoopProxy, RuntimeInitArgs, }; use tauri_utils::PackageInfo; @@ -185,7 +185,10 @@ pub enum RunEvent { impl From for RunEvent { fn from(event: EventLoopMessage) -> Self { - match event {} + match event { + EventLoopMessage::MenuEvent(e) => Self::MenuEvent(e), + EventLoopMessage::TrayIconEvent(e) => Self::TrayIconEvent(e), + } } } @@ -1426,6 +1429,18 @@ impl Builder { menu.init_for_nsapp(); } + // setup menu event handler + let proxy = runtime.create_proxy(); + crate::menu::MenuEvent::set_event_handler(Some(move |e| { + let _ = proxy.send_event(EventLoopMessage::MenuEvent(e)); + })); + + // setup tray event handler + let proxy = runtime.create_proxy(); + crate::tray::TrayIconEvent::set_event_handler(Some(move |e| { + let _ = proxy.send_event(EventLoopMessage::TrayIconEvent(e)); + })); + runtime.set_device_event_filter(self.device_event_filter); let runtime_handle = runtime.handle(); @@ -1616,42 +1631,45 @@ fn on_event_loop_event, RunEvent) + 'static>( } RuntimeRunEvent::Resumed => RunEvent::Resumed, RuntimeRunEvent::MainEventsCleared => RunEvent::MainEventsCleared, - RuntimeRunEvent::MenuEvent(e) => { - for listener in &*app_handle - .manager - .inner - .menu_event_listeners - .lock() - .unwrap() - { - listener(app_handle, e) - } - for (label, listener) in &*app_handle - .manager - .inner - .window_menu_event_listeners - .lock() - .unwrap() - { - if let Some(w) = app_handle.get_window(label) { - listener(&w, e) + RuntimeRunEvent::UserEvent(t) => { + match t { + EventLoopMessage::MenuEvent(e) => { + for listener in &*app_handle + .manager + .inner + .menu_event_listeners + .lock() + .unwrap() + { + listener(app_handle, e) + } + for (label, listener) in &*app_handle + .manager + .inner + .window_menu_event_listeners + .lock() + .unwrap() + { + if let Some(w) = app_handle.get_window(label) { + listener(&w, e) + } + } + } + EventLoopMessage::TrayIconEvent(e) => { + for listener in &*app_handle + .manager + .inner + .tray_event_listeners + .lock() + .unwrap() + { + listener(app_handle, e) + } } } - RunEvent::MenuEvent(e) - } - RuntimeRunEvent::TrayIconEvent(e) => { - for listener in &*app_handle - .manager - .inner - .tray_event_listeners - .lock() - .unwrap() - { - listener(app_handle, e) - } - RunEvent::TrayIconEvent(e) + + t.into() } - RuntimeRunEvent::UserEvent(t) => t.into(), _ => unimplemented!(), }; diff --git a/core/tauri/src/lib.rs b/core/tauri/src/lib.rs index fd4340dab4ee..0aa0ecb83ce2 100644 --- a/core/tauri/src/lib.rs +++ b/core/tauri/src/lib.rs @@ -237,7 +237,12 @@ pub fn log_stdout() { /// The user event type. #[derive(Debug, Clone)] -pub enum EventLoopMessage {} +pub enum EventLoopMessage { + /// An event from a menu item, could be on the window menu bar, application menu bar (on macOS) or tray icon menu. + MenuEvent(menu::MenuEvent), + /// An event from a menu item, could be on the window menu bar, application menu bar (on macOS) or tray icon menu. + TrayIconEvent(tray::TrayIconEvent), +} /// The webview runtime interface. A wrapper around [`runtime::Runtime`] with the proper user event type associated. pub trait Runtime: runtime::Runtime {}