tauri-apps / tauri

Build smaller, faster, and more secure desktop applications with a web frontend.
https://tauri.app
Apache License 2.0
81.95k stars 2.46k forks source link

[bug] Buggy behaviors of `SystemTrayEvent` on macOS (fail to set tray menu & fail to get callback) #7364

Closed XOR-op closed 1 year ago

XOR-op commented 1 year ago

Describe the bug

When trying to call set_menu with tray_handle in on_system_tray_event callback, tauri's behavior is weird. Let's break into two part:

  1. Right after first menu initialization (the initialization works as expected),
    • SystemTrayEvent::LeftClick: event handled successfully, set_menu returns Ok(), but in fact failed to replace tray menu
    • SystemTrayEvent::MenuItemClick: event handled successfully, set_menu returns Ok(), successfully replace tray menu
  2. After successful set_menu by SystemTrayEvent::MenuItemClick:
    • SystemTrayEvent::LeftClick: event not handled, #5842 has similar bug report
    • SystemTrayEvent::MenuItemClick: event handled successfully, set_menu returns Ok(), successfully replace tray menu

The RightClick seems to have similar bugs. I'm not sure if these two buggy behaviors are related, but I believe they may have the same root cause. I tested this bug on both 1.4.1 and 2.0.0-alpha-10.

Reproduction

Minimal repo in https://github.com/XOR-op/tauri-bug-reproduction

Expected behavior

  1. Successful replacement of tray menu. If anything is wrong, it should not return Ok(); and it should work at most cases.
  2. Get proper event handling after tray menu replacement if this bug has the same root cause with #5842

Platform and versions

- OS: Mac OS 13.4.0 X64
    ✔ Xcode Command Line Tools: installed
    ✔ rustc: 1.70.0 (90c541806 2023-05-31)
    ✔ Cargo: 1.70.0 (ec8a8a0ca 2023-04-25)
    ✔ Rust toolchain: stable-x86_64-apple-darwin
- tauri: 1.4.1 (also tested on 2.0.0-alpha-10)

Stack trace

No response

Additional context

No response

pewsheen commented 1 year ago

Here's something I found so far for the LeftClick issue:

In tao, we delegate the menu to tray_target while building SystemTray

https://github.com/tauri-apps/tao/blob/731e39755a2ecca9e82a09feab0dc19498080a34/src/platform_impl/macos/system_tray.rs#L91-L98

However, when we set a new menu, we didn't delegate it to tray_target; I guess this would cause some issues.

So I track into the set_menu. I tried getting tray_target from the button and let the new menu set delegate to it. The LeftClick event is emitted again in an ugly way lol

  pub fn set_menu(&mut self, tray_menu: &Menu) {
    unsafe {
      self.ns_status_bar.setMenu_(tray_menu.menu);
      let button: id = self.ns_status_bar.button();
      let tray_target: id = msg_send![button, target];
      let () = msg_send![tray_menu.menu, setDelegate: tray_target];
    }
  }

I'm not sure if this is the root cause or just some lucky guess. If we decide to fix like this way, we might need to store the tray_target somewhere in SystemTray struct?

amrbashir commented 1 year ago

@pewsheen yeah the set_menu function is not correct at all, please open a PR and we will discuss it more there