tauri-apps/tauri

[bug] TrayIcon mouse events don't work if the icon was created from async command

qu1ck opened this issue · 0 comments

qu1ck commented

Describe the bug

After upgrading my app to tauri v2 I could not make the tray icon behave as it did in v1 at all. It would not react to mouse at all, not even context menu would show up. Debugging showed that events were not firing.

After hours of trying things in a dummy create-tauri-app to isolate the issue I noticed that if the tray icon is created in a synchronous command it works as expected, but if the command is async then the icon will be created but it does not react to mouse at all.

This was not an issue in tauri v1.

Reproduction

Use npm create tauri-app@latest and modify the greet command like this

// Learn more about Tauri commands at https://tauri.app/develop/calling-rust/
#[tauri::command]
async fn greet(name: &str, app: AppHandle) -> String {
    tauri::tray::TrayIconBuilder::with_id("TRAY")
        .icon(app.default_window_icon().unwrap().clone())
        .on_tray_icon_event(|_icon, event| {
            println!("Tray icon event received {:?}", event);
            match event {
                TrayIconEvent::Click {
                    button: MouseButton::Left,
                    button_state: MouseButtonState::Up,
                    ..
                } => {
                    println!("Tray icon clicked");
                }
                _ => {}
            }
        })
        .on_menu_event(|_app, _event| {})
        .build(&app)
        .ok();
    format!("Hello, {}! You've been greeted from Rust!", name)
}

Notice that the icon ignores mouse.
But if you drop the async then it works as expected.

Expected behavior

Icon works properly with both sync and async commands.

Full tauri info output

[✘] Environment
    - OS: Windows 10.0.19045 x86_64 (X64)
    ✔ WebView2: 129.0.2792.89
    ✘ Couldn't detect any Visual Studio or VS Build Tools instance with MSVC and SDK components. Download from https://aka.ms/vs/17/release/vs_BuildTools.exe
    ✔ rustc: 1.80.0 (051478957 2024-07-21)
    ✔ cargo: 1.80.0 (376290515 2024-07-16)
    ✔ rustup: 1.27.1 (54dd3d00f 2024-04-24)
    ✔ Rust toolchain: stable-x86_64-pc-windows-msvc (default)
    - node: 16.14.2
    - pnpm: 8.6.2
    - npm: 8.5.0

[-] Packages
    - tauri 🦀: 2.0.6      
    - tauri-build 🦀: 2.0.2
    - wry 🦀: 0.46.3
    - tao 🦀: 0.30.3
    - tauri-cli 🦀: 1.3.1
    - @tauri-apps/api : 2.0.0 (outdated, latest: 2.0.3)
    - @tauri-apps/cli : 2.0.3 (outdated, latest: 2.0.4)

[-] Plugins
    - tauri-plugin-notification 🦀: 2.0.1
    - @tauri-apps/plugin-notification : 2.0.0
    - tauri-plugin-dialog 🦀: 2.0.3
    - @tauri-apps/plugin-dialog : 2.0.1
    - tauri-plugin-fs 🦀: 2.0.3
    - @tauri-apps/plugin-fs : 2.0.1
    - tauri-plugin-cli 🦀: 2.0.1
    - @tauri-apps/plugin-cli : 2.0.0
    - tauri-plugin-shell 🦀: 2.0.2
    - @tauri-apps/plugin-shell : 2.0.1

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

Stack trace

No response

Additional context

This may be windows specific.