Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
1 task
GuicLuca opened this issue Oct 7, 2024 · 5 comments
Labels
discuss Does this require further discussion before it's dealt with? enhancement Does it add or improve content?

Comments

@GuicLuca
Copy link

GuicLuca commented Oct 7, 2024

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?

  • I want to work on this myself
@GuicLuca GuicLuca added discuss Does this require further discussion before it's dealt with? enhancement Does it add or improve content? labels Oct 7, 2024
@RSS1102
Copy link
Contributor

RSS1102 commented Oct 7, 2024

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

@GuicLuca
Copy link
Author

GuicLuca commented Oct 8, 2024

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
Copy link
Author

GuicLuca commented Oct 9, 2024

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
Copy link
Contributor

RSS1102 commented Oct 9, 2024

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
Copy link
Contributor

RSS1102 commented Oct 9, 2024

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)

2024-10-09.151110.mp4
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(())
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discuss Does this require further discussion before it's dealt with? enhancement Does it add or improve content?
Projects
Status: 🪵 Backlog
Development

No branches or pull requests

2 participants