tauri-apps / tauri

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

[bug] tauri::tray::TrayIconBuilder.on_tray_icon_event seems broken for MacOS Applications #11413

Open Czxck001 opened 1 month ago

Czxck001 commented 1 month ago

Describe the bug

It seems on_tray_icon_event can only receive TrayIconEvent::Enter, TrayIconEvent::Move and TrayIconEvent::Leave when mouse cursor is hovering on the tray icon, but won't be able to receive TrayIconEvent::Click when the user clicks on the icon.

Reproduction

Just create a blank tauri app and change the lib.rs as the following:

use tauri::tray::{MouseButton, MouseButtonState, TrayIcon, TrayIconEvent};

#[tauri::command]
fn greet(name: &str) -> String {
    format!("Hello, {}! You've been greeted from Rust!", name)
}

#[cfg_attr(mobile, tauri::mobile_entry_point)]
pub fn run() {
    tauri::Builder::default()
        .plugin(tauri_plugin_shell::init())
        .invoke_handler(tauri::generate_handler![greet])
        .setup(|app| {
            tauri::tray::TrayIconBuilder::new()
            .on_tray_icon_event(
                |icon: &TrayIcon, event| {
                println!("Tray icon event received {:?}", event);
                match event {
                TrayIconEvent::Click {
                    button: MouseButton::Left,
                    button_state: MouseButtonState::Up,
                    position,
                    ..
                } => {
                    println!("Tray icon clicked");
                }
                _ => {}
                }
                }
            ).build(app)?;
            Ok(())
        })
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

Expected behavior

The program should print TrayIconEvent::Click to the console when user clicks the icon, and be able to match the Click event accordingly, and print Tray icon clicked. However, as mentioned, it appears only to receive and print Enter, Move and Leave:

Tray icon event received Enter { id: TrayIconId("2"), position: PhysicalPosition { x: 3868.3203125, y: 6.171875 }, rect: Rect { position: Physical(PhysicalPosition { x: 3848, y: 0 }), size: Physical(PhysicalSize { width: 32, height: 48 }) } }
Tray icon event received Move { id: TrayIconId("2"), position: PhysicalPosition { x: 3868.3203125, y: 6.171875 }, rect: Rect { position: Physical(PhysicalPosition { x: 3848, y: 0 }), size: Physical(PhysicalSize { width: 32, height: 48 }) } }
Tray icon event received Leave { id: TrayIconId("2"), position: PhysicalPosition { x: 3813.1328125, y: 6.171875 }, rect: Rect { position: Physical(PhysicalPosition { x: 3848, y: 0 }), size: Physical(PhysicalSize { width: 32, height: 48 }) } }
Tray icon event received Enter { id: TrayIconId("2"), position: PhysicalPosition { x: 3867.234375, y: 8.375 }, rect: Rect { position: Physical(PhysicalPosition { x: 3848, y: 0 }), size: Physical(PhysicalSize { width: 32, height: 48 }) } }
Tray icon event received Move { id: TrayIconId("2"), position: PhysicalPosition { x: 3867.234375, y: 8.375 }, rect: Rect { position: Physical(PhysicalPosition { x: 3848, y: 0 }), size: Physical(PhysicalSize { width: 32, height: 48 }) } }
Tray icon event received Leave { id: TrayIconId("2"), position: PhysicalPosition { x: 3887.7421875, y: 8.375 }, rect: Rect { position: Physical(PhysicalPosition { x: 3848, y: 0 }), size: Physical(PhysicalSize { width: 32, height: 48 }) } }

Full tauri info output

> tauri2-test@0.1.0 tauri
> tauri info

[✔] Environment
    - OS: Mac OS 15.0.0 arm64 (X64)
    ✔ Xcode Command Line Tools: installed
    ✔ rustc: 1.81.0 (eeb90cda1 2024-09-04)
    ✔ cargo: 1.81.0 (2dbb1af80 2024-08-20)
    ✔ rustup: 1.27.1 (54dd3d00f 2024-04-24)
    ✔ Rust toolchain: stable-aarch64-apple-darwin (default)
    - node: 19.8.1
    - yarn: 1.22.19
    - npm: 9.5.1

[-] Packages
    - tauri 🦀: 2.0.4
    - tauri-build 🦀: 2.0.1
    - wry 🦀: 0.46.2
    - tao 🦀: 0.30.3
    - @tauri-apps/api : 2.0.2
    - @tauri-apps/cli : 2.0.3

[-] Plugins
    - tauri-plugin-shell 🦀: 2.0.1
    - @tauri-apps/plugin-shell : 2.0.0

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

Stack trace

N/A

Additional context

No response

FabianLars commented 3 days ago

idk why i got notified for this issue today (deleted message?) but on my machine (m2 pro 15.1) your code works fine actually.

could this be a problem with macos 15.0 maybe? i don't think i've tried it when i was on that version. It wouldn't be the first time that a macos update broke parts of the tray icon.

nicolaspapp commented 3 days ago

Yes, that was me. I was having a similar issue, but in my case it was because I had registered the on_tray_icon_event twice.