Skip to content

Commit

Permalink
move menu/tray handlers setup from runtime to tauri
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir committed Jul 20, 2023
1 parent 0063216 commit ae9423b
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 58 deletions.
18 changes: 0 additions & 18 deletions core/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1740,16 +1740,6 @@ impl<T: UserEvent> Wry<T> {
}

fn init(event_loop: EventLoop<Message<T>>) -> Result<Self> {
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();

Expand Down Expand Up @@ -2314,14 +2304,6 @@ fn handle_event_loop<T: UserEvent>(
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();
Expand Down
4 changes: 0 additions & 4 deletions core/tauri-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,6 @@ pub enum RunEvent<T: UserEvent> {
///
/// 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),
}
Expand Down
88 changes: 53 additions & 35 deletions core/tauri/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use tauri_runtime::{
dpi::{PhysicalPosition, PhysicalSize},
FileDropEvent,
},
RuntimeInitArgs,
EventLoopProxy, RuntimeInitArgs,
};
use tauri_utils::PackageInfo;

Expand Down Expand Up @@ -185,7 +185,10 @@ pub enum RunEvent {

impl From<EventLoopMessage> for RunEvent {
fn from(event: EventLoopMessage) -> Self {
match event {}
match event {
EventLoopMessage::MenuEvent(e) => Self::MenuEvent(e),
EventLoopMessage::TrayIconEvent(e) => Self::TrayIconEvent(e),
}
}
}

Expand Down Expand Up @@ -1426,6 +1429,18 @@ impl<R: Runtime> Builder<R> {
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();
Expand Down Expand Up @@ -1616,42 +1631,45 @@ fn on_event_loop_event<R: Runtime, F: FnMut(&AppHandle<R>, 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!(),
};

Expand Down
7 changes: 6 additions & 1 deletion core/tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<EventLoopMessage> {}
Expand Down

0 comments on commit ae9423b

Please sign in to comment.