tauri-apps / tray-icon

Tray icons for Desktop Applications.
Apache License 2.0
228 stars 33 forks source link

How to flash the tray icon? #124

Closed nongyehong closed 7 months ago

nongyehong commented 8 months ago

How to flash the icon of the system tray, using two images alternately seems not to achieve this effect

amrbashir commented 7 months ago

There is no APIs to flash the tray icon.

Alternating between two images should work though, can you provide a minimal repro and information about your OS?

nongyehong commented 7 months ago
#[tauri::command]
pub fn tray_blink(
    app: AppHandle,
    state: tauri::State<TrayState>,
    is_run: bool,
    ms: Option<u64>,
    icon_path_1: Option<String>,
    icon_path_2: Option<String>,
) {

    match state.id.lock().as_deref_mut().map(|x| x.as_mut()) {
        Ok(Some(v)) => v.abort(),
        _ => (),
    }

    if !is_run {
        return;
    }

    *state.id.lock().unwrap() = Some(tokio::spawn(async move {
        let path1 = &icon_path_1.unwrap();
        let path2 = &icon_path_2.unwrap();
        let mut count = 0;

        loop {
            tokio::time::sleep(std::time::Duration::from_millis(ms.unwrap_or_else(|| 500))).await;
            count += 1;

            let path = if count % 2 == 0 {
                path2
            } else {
                path1
            };

            app
                .tray_handle()
                .set_icon(tauri::Icon::File(std::path::PathBuf::from(path)))
                .unwrap();
        }
    }));
    }

Full tauri info output

[✔] Environment
    - OS: Windows 10.0.26080 X64
    ✔ WebView2: 122.0.2365.92
    ✔ MSVC: Visual Studio Community 2022
    ✔ rustc: 1.77.0 (aedd173a2 2024-03-17)
    ✔ cargo: 1.77.0 (3fe68eabf 2024-02-29)
    ✔ rustup: 1.27.0 (bbb9276d2 2024-03-08)
    ✔ Rust toolchain: stable-x86_64-pc-windows-msvc (default)
    - node: 20.10.0
    - pnpm: 8.15.4
    - yarn: 1.22.21
    - npm: 10.2.3

[-] Packages
    - tauri [RUST]: 1.6.1
    - tauri-build [RUST]: 1.5.0
    - wry [RUST]: 0.24.6
    - tao [RUST]: 0.16.5
    - @tauri-apps/api [NPM]: 1.5.3
    - @tauri-apps/cli [NPM]: 1.5.11

[-] App
    - build-type: bundle
    - CSP: unset
    - distDir: ../dist
    - devPath: http://localhost:6130/
    - framework: Vue.js
    - bundler: Rollup
nongyehong commented 7 months ago

https://github.com/tauri-apps/tray-icon/assets/87641407/fdc543bc-be76-469f-bd2a-d67efc4f01a6

an icon flashes, similar to wechat

amrbashir commented 7 months ago

I am not sure which icon is yours in the video you showed, but you should be able to simulate what wechat is doing by alternating between a transparent icon and your actual app icon.

I should also note that you're using tauri v1 and this crate is only used in v2.