actually apply item changes

This commit is contained in:
Lucas Nogueira 2024-08-14 19:21:06 -03:00
parent a9cc84dbdf
commit 66abc2199c
No known key found for this signature in database
GPG Key ID: 7C32FCA95C8C95D7
2 changed files with 25 additions and 5 deletions

View File

@ -3,6 +3,3 @@
---
Ensure system tray is created when the event loop is ready. Menu item modifications are not applied unless it is ready.
If you need to modify the menu items immediately after creating a tray in the setup hook,
either directly configure the menu item change when creating the menu or move the code when `RunEvent::Ready` is fired.
See https://docs.rs/tauri/latest/tauri/struct.App.html#method.run for more information.

View File

@ -195,8 +195,31 @@ impl<T: UserEvent> TrayHandle for SystemTrayHandle<T> {
fn update_item(&self, id: u16, update: MenuUpdate) -> Result<()> {
if !self.context.is_event_loop_ready.load(Ordering::Relaxed) {
if let Some(_pending) = &mut *self.pending.0.borrow_mut() {
// do nothing - menu items not available yet
if let Some(pending) = &mut *self.pending.0.borrow_mut() {
if let Some(menu) = &mut pending.menu {
for item in &mut menu.items {
if let SystemTrayMenuEntry::CustomItem(item) = item {
if item.id == id {
match update {
MenuUpdate::SetEnabled(enabled) => {
item.enabled = enabled;
}
MenuUpdate::SetTitle(title) => {
item.title = title;
}
MenuUpdate::SetSelected(selected) => {
item.selected = selected;
}
#[cfg(target_os = "macos")]
MenuUpdate::SetNativeImage(img) => {
item.native_image.replace(img);
}
}
break;
}
}
}
}
return Ok(());
}
}