tauri icon indicating copy to clipboard operation
tauri copied to clipboard

[bug] The "tray" mouse left-click event cannot be successfully triggered.

Open lin0306 opened this issue 1 year ago • 1 comments

Describe the bug

concrete issue:

When I held down the left mouse button on the system tray for 3 seconds, the event was not triggered as I expected at the moment of pressing, nor was it triggered again at the moment of releasing. Instead, it triggered twice at the moment of releasing.

The log output is as follows:

2025-08-18 14:22:08.692 [INFO easypaste_lib::tray] click tray: Click { id: TrayIconId("1"), position: PhysicalPosition { x: 1916.0, y: 1248.0 }, rect: Rect { position: Physical(PhysicalPosition { x: 1886, y: 1223 }), size: Physical(PhysicalSize { width: 60, height: 60 }) }, button: Left, button_state: Down }
2025-08-18 14:22:08.692 [INFO easypaste_lib::tray] click tray: Click { id: TrayIconId("1"), position: PhysicalPosition { x: 1916.0, y: 1248.0 }, rect: Rect { position: Physical(PhysicalPosition { x: 1886, y: 1223 }), size: Physical(PhysicalSize { width: 60, height: 60 }) }, button: Left, button_state: Up }

The logical code is as follows:

TrayIconBuilder::new()
.on_tray_icon_event(|tray, event| match event {
    TrayIconEvent::Click {
        id: _,
        position: _,
        rect: _,
        button: MouseButton::Left,
        button_state: _,
    } => {
        info!("click tray: {:?}", event)
    },
    _ => {}
})

Reproduction

No response

Expected behavior

No response

Full tauri info output

[✔] Environment
    - OS: Windows 10.0.26100 x86_64 (X64)
    ✔ WebView2: 137.0.3296.68
    ✔ MSVC: Visual Studio 生成工具 2022
    ✔ rustc: 1.86.0 (05f9846f8 2025-03-31)
    ✔ cargo: 1.86.0 (adf9b6ad1 2025-02-28)
    ✔ rustup: 1.28.1 (f9edccde0 2025-03-05)
    ✔ Rust toolchain: stable-x86_64-pc-windows-msvc (default)
    - node: 20.18.0
    - pnpm: 10.12.1
    - yarn: 1.22.22
    - npm: 10.9.1

[-] Packages
    - tauri 🦀: 2.7.0
    - tauri-build 🦀: 2.3.1
    - wry 🦀: 0.52.1, (outdated, latest: 0.53.1)
    - tao 🦀: 0.34.0, (outdated, latest: 0.34.1)
    - @tauri-apps/api : 2.5.0 (outdated, latest: 2.7.0)
    - @tauri-apps/cli : 2.5.0 (outdated, latest: 2.7.1)

[-] Plugins
    - tauri-plugin-global-shortcut 🦀: 2.3.0
    - @tauri-apps/plugin-global-shortcut : 2.2.1 (outdated, latest: 2.3.0)
    - tauri-plugin-shell 🦀: 2.3.0
    - @tauri-apps/plugin-shell : 2.2.1 (outdated, latest: 2.3.0)
    - tauri-plugin-autostart 🦀: 2.5.0
    - @tauri-apps/plugin-autostart : 2.3.0 (outdated, latest: 2.5.0)
    - tauri-plugin-sql 🦀: 2.3.0
    - @tauri-apps/plugin-sql : 2.2.0 (outdated, latest: 2.3.0)
    - tauri-plugin-process 🦀: 2.3.0
    - @tauri-apps/plugin-process : 2.2.1 (outdated, latest: 2.3.0)
    - tauri-plugin-single-instance 🦀: 2.3.2
    - @tauri-apps/plugin-single-instance : not installed!
    - tauri-plugin-updater 🦀: 2.9.0
    - @tauri-apps/plugin-updater : 2.7.1 (outdated, latest: 2.9.0)
    - tauri-plugin-opener 🦀: 2.4.0
    - @tauri-apps/plugin-opener : 2.2.7 (outdated, latest: 2.4.0)
    - tauri-plugin-fs 🦀: 2.4.1
    - @tauri-apps/plugin-fs : 2.3.0 (outdated, latest: 2.4.1)
    - tauri-plugin-log 🦀: 2.6.0
    - @tauri-apps/plugin-log : 2.4.0 (outdated, latest: 2.6.0)
    - tauri-plugin-store 🦀: 2.3.0
    - @tauri-apps/plugin-store : 2.2.0 (outdated, latest: 2.3.0)
    - tauri-plugin-notification 🦀: 2.3.0
    - @tauri-apps/plugin-notification : 2.3.0

[-] App
    - build-type: bundle
    - CSP: unset
    - frontendDist: ../dist
    - devUrl: http://localhost:23000/
    - framework: Vue.js
    - bundler: Rollup

Stack trace


Additional context

No response

lin0306 avatar Aug 18 '25 06:08 lin0306

Reproduced on Windows 11 (build 26300) with tauri dev at c42354b (same core crates as 2.12.6) and tray-icon 0.25.1, both with the icon in the hidden-icons overflow flyout and with the icon shown directly on the taskbar.

Minimal app: a single TrayIconBuilder with no windows, logging a timestamp for every Click event. Holding the left button for 3 s, 3 s and 5 s with the icon on the taskbar:

[4289.021s] Click { button: Left, button_state: Down }
[4289.022s] Click { button: Left, button_state: Up }
[4294.233s] Click { button: Left, button_state: Down }
[4294.233s] Click { button: Left, button_state: Up }
[4301.458s] Click { button: Left, button_state: Down }
[4301.458s] Click { button: Left, button_state: Up }

The overflow flyout gives the same pattern (Down and Up within the same millisecond, at release).

The same happens without tauri or tray-icon (tested in the overflow flyout). A plain Win32 program that registers the icon the same way tray-icon does (NIM_ADD with uCallbackMessage, no NIM_SETVERSION) and logs every callback message gets WM_LBUTTONDOWN only when the button is released:

[  26.504s] callback 0x0201 WM_LBUTTONDOWN
[  26.504s] callback 0x0202 WM_LBUTTONUP
[  31.719s] callback 0x0201 WM_LBUTTONDOWN
[  31.720s] callback 0x0202 WM_LBUTTONUP

With NIM_SETVERSION / NOTIFYICON_VERSION_4 it's the same, with NIN_SELECT arriving together with the other two:

[  19.547s] callback 0x0201 WM_LBUTTONDOWN
[  19.547s] callback 0x0202 WM_LBUTTONUP
[  19.547s] callback 0x0400 NIN_SELECT

On the tray-icon side, tray_proc in src/platform_impl/windows/mod.rs (0.25.1) maps WM_LBUTTONDOWN straight to Click { button_state: Down } and sends it as soon as the callback message arrives (lines 418-424 and 499), so the timestamps above match when the shell delivers the messages.

Win32 test program (trimmed)
const WM_TRAY: u32 = WM_APP + 1;

unsafe extern "system" fn wndproc(hwnd: HWND, msg: u32, wp: WPARAM, lp: LPARAM) -> LRESULT {
  if msg == WM_TRAY {
    let m = (lp as u32) & 0xFFFF;
    if m != WM_MOUSEMOVE {
      let t = unsafe { START.unwrap() }.elapsed().as_secs_f64();
      println!("[{t:>8.3}s] callback 0x{m:04X} {}", name(m));
    }
    return 0;
  }
  unsafe { DefWindowProcW(hwnd, msg, wp, lp) }
}

// in main(), after creating a hidden WS_EX_TOOLWINDOW window:
let mut nid: NOTIFYICONDATAW = std::mem::zeroed();
nid.cbSize = std::mem::size_of::<NOTIFYICONDATAW>() as u32;
nid.hWnd = hwnd;
nid.uID = 1;
nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
nid.uCallbackMessage = WM_TRAY;
nid.hIcon = LoadIconW(std::ptr::null_mut(), IDI_APPLICATION);
println!("NIM_ADD -> {}", Shell_NotifyIconW(NIM_ADD, &nid));
if v4 {
  nid.Anonymous.uVersion = NOTIFYICON_VERSION_4;
  println!("NIM_SETVERSION(4) -> {}", Shell_NotifyIconW(NIM_SETVERSION, &nid));
}

Assertive-xxh avatar Nov 18 '26 06:11 Assertive-xxh