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] No `LeftClick` are passed to `on_system_tray_event` #7283

Closed qwfy closed 1 year ago

qwfy commented 1 year ago

Describe the bug

When I left/right/double click the tray icon, I expect the corresponding branch in the handler registered with on_system_tray_event to be called, but it doesn't, instead, the tray's menu pops up, when I click on any menu item, the MenuItemClick branch is called.


    let quit = CustomMenuItem::new("quit".to_string(), "Quit");
    let hide = CustomMenuItem::new("hide".to_string(), "Hide");
    let tray_menu = SystemTrayMenu::new()
      .add_item(quit)
      .add_native_item(SystemTrayMenuItem::Separator)
      .add_item(hide);
    let mut tray = SystemTray::new()
        .with_icon(
            Icon::File("icons/icon.png".into())
        )
        .with_menu(tray_menu);

    tauri::Builder::default()
        .invoke_handler(tauri::generate_handler![greet])
        .system_tray(tray)
        .on_system_tray_event(|app, event: SystemTrayEvent| match event {
          SystemTrayEvent::MenuItemClick {..} => {
              println!("-----------------menu");
              let window = app.get_window("main").unwrap();
              window.show().unwrap();
              window.set_focus().unwrap();
          },
          SystemTrayEvent::RightClick {..} => {
              println!("-----------------right");
              let window = app.get_window("main").unwrap();
              window.show().unwrap();
              window.set_focus().unwrap();
          },
          SystemTrayEvent::LeftClick {..} => {
              println!("-----------------left");
              let window = app.get_window("main").unwrap();
              window.show().unwrap();
              window.set_focus().unwrap();
          },
          SystemTrayEvent::DoubleClick {..} => {
              println!("-----------------double");
              let window = app.get_window("main").unwrap();
              window.show().unwrap();
              window.set_focus().unwrap();
          },
          _event => {
              println!("----------------- unknown");
            let window = app.get_window("main").unwrap();
              window.show().unwrap();
              window.set_focus().unwrap();
          }
        })

Reproduction

Setup:

  1. Create a new project according to the official doc
  2. Follow https://tauri.app/v1/guides/features/system-tray/#listening-to-system-tray-events

incase the doc changes:

use tauri::{CustomMenuItem, SystemTray, SystemTrayMenu, SystemTrayEvent};
use tauri::Manager;

fn main() {
  let tray_menu = SystemTrayMenu::new(); // insert the menu items here
  tauri::Builder::default()
    .system_tray(SystemTray::new().with_menu(tray_menu))
    .on_system_tray_event(|app, event| match event {
      SystemTrayEvent::LeftClick {
        position: _,
        size: _,
        ..
      } => {
        println!("system tray received a left click");
      }
      SystemTrayEvent::RightClick {
        position: _,
        size: _,
        ..
      } => {
        println!("system tray received a right click");
      }
      SystemTrayEvent::DoubleClick {
        position: _,
        size: _,
        ..
      } => {
        println!("system tray received a double click");
      }
      SystemTrayEvent::MenuItemClick { id, .. } => {
        match id.as_str() {
          "quit" => {
            std::process::exit(0);
          }
          "hide" => {
            let window = app.get_window("main").unwrap();
            window.hide().unwrap();
          }
          _ => {}
        }
      }
      _ => {}
    })
    .run(tauri::generate_context!())
    .expect("error while running tauri application");
}

Expected behavior

See description.

Platform and versions

OS:

Operating System: Red Hat Enterprise Linux 8.8
KDE Plasma Version: 5.24.7
KDE Frameworks Version: 5.96.0
Qt Version: 5.15.3
Graphics Platform: X11

libappindicator-gtk3-devel
libappindicator-gtk3
[✔] Environment
    - OS: Red Hat Enterprise Linux 8.8.0 X64
    ✔ webkit2gtk-4.0: 2.38.5
    ✔ rsvg2: 2.42.7
    ✔ rustc: 1.68.0 (2c8cc3432 2023-03-06)
    ✔ Cargo: 1.68.0 (115f34552 2023-02-26)
    ✔ rustup: 1.25.2 (17db695f1 2023-02-01)
    ✔ Rust toolchain: stable-x86_64-unknown-linux-gnu (environment override by RUSTUP_TOOLCHAIN)
    - node: 16.19.1
    - yarn: 1.22.19
    - npm: 8.19.3

[-] Packages
    - tauri [RUST]: 1.4.1
    - tauri-build [RUST]: 1.4.0
    - wry [RUST]: 0.24.3
    - tao [RUST]: 0.16.2
    - @tauri-apps/api [NPM]: 1.4.0
    - @tauri-apps/cli [NPM]: 1.4.0

[-] App
    - build-type: bundle
    - CSP: unset
    - distDir: ../dist
    - devPath: http://localhost:1420/
    - framework: React
    - bundler: Vite

### Stack trace

```text
Not a crash.

Additional context

No response

FabianLars commented 1 year ago

This is expected, the library we use on Linux (libappindicator) doesn't support click events but it's also mentioned in the event variants (not quite obvious i know): grafik https://docs.rs/tauri/latest/tauri/enum.SystemTrayEvent.html#variant.LeftClick