tauri-apps / tauri-docs

The source for all Tauri project documentation.
https://tauri.app
MIT License
835 stars 634 forks source link

[request] V2 Documentation doesn't show anymore how to manipulate TrayIcon menu item (like update title or changing icon at runtime) #2811

Open GuicLuca opened 1 month ago

GuicLuca commented 1 month ago

Question you want answered

How can i update an TrayIcon menu item title/logo at runtime.

Where did you look for an answer?

First I searched on the v2.0 documentation site on the system tray page. Then I moved to the migration page to check if there is some explanation to "how I should update the v1.x code". Not getting any info, I decided to check on internet external website/videos/tutorials. Finally, I ended my search here, on GitHub issues.

Page URL

https://v2.tauri.app/plugin/system-tray

Additional context

No response

Are you willing to work on this yourself?

RSS1102 commented 1 month ago

Is this what you need? https://tauri.app/plugin/system-tray/#change-the-tray-icon

GuicLuca commented 1 month ago

Is this what you need? https://tauri.app/plugin/system-tray/#change-the-tray-icon

Hey, I was about to try your previous response code haha ^^ No, this section of the documentation explains how to change the global tray icon. In my case, I need to update a MenuItem from the tray menu.

I'll try to do your previous response and rebuild the whole menu each time it's needed. ;) I'll tell you if it's works 👍

PS: Do you know why the feature of getting a tray-menu item was removed from the 1.x version to the 2.0? (I'm curious)

GuicLuca commented 1 month ago

Hello, your suggestion (that was to rebuild the whole tray menu when an update is needed) works perfectly, but I can't stop thinking that the V1 system was a bit easier and more efficient ^^

Thank you very much for the answer. 👌

RSS1102 commented 1 month ago

Hello, your suggestion (that was to rebuild the whole tray menu when an update is needed) works perfectly, but I can't stop thinking that the V1 system was a bit easier and more efficient ^^

Thank you very much for the answer. 👌

Or maybe there is a better way, but I just don't know it yet? You may need more exploration if you want to pursue the ultimate.

RSS1102 commented 1 month ago

Maybe you should try this, it works for me when changing menu item.

(The menu of the video and the item of the code are not consistent)

https://github.com/user-attachments/assets/ee80cad8-d12c-4509-b271-5b211ce382f9

use tauri::{
    menu::{Menu, MenuItem},
    tray::{MouseButton, MouseButtonState, TrayIconBuilder, TrayIconEvent},
    App,
};

pub fn system_tray_menu(app: &mut App) -> Result<(), tauri::Error> {

    let quit = MenuItem::with_id(app, "quit", "Quit", true, None::<&str>)?;
    let switch= MenuItem::with_id(app, "switch", "switch", true, None::<&str>)?;

     // There is two item here
    let menu = Menu::with_items(app, &[&switch, &quit])?;

     // There is only one item here
     let new_menu = Menu::with_items(app, &[&switch])?;

    let tray_menu = TrayIconBuilder::with_id("tray")
        .menu(&menu)
        .menu_on_left_click(false)
        .icon(app.default_window_icon().unwrap().clone())
        .build(app)?;

    tray_menu.on_tray_icon_event(|tray, event| match event {
        TrayIconEvent::Click {
            button: MouseButton::Left,
            button_state: MouseButtonState::Up,
            ..
        } => {
            let app = tray.app_handle();
            restore_and_focus_window(app, "main");
        }
        _ => {}
    });

    let tray_menu_clone = tray_menu.clone();

    tray_menu.on_menu_event(move |app, event| match event.id.as_ref() {
        "switch" => {
            match tray_menu_clone.set_menu(Some(new_menu.clone())) {
                Ok(_) => {}
                Err(e) => {
                    eprintln!("Error setting menu: {:?}", e);
                }
            }
        }
        "quit" => {
            app.exit(0);
        }
        _ => {}
    });

    Ok(())
}