-
Notifications
You must be signed in to change notification settings - Fork 662
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
Comments
Is this what you need? |
Hey, I was about to try your previous response code haha ^^ I'll try to do your previous response and rebuild the whole menu each time it's needed. ;) 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) |
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. |
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.mp4use 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(())
} |
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?
The text was updated successfully, but these errors were encountered: