[bug] When tray menu is replaced while it's opened an old menu stays on the screen
Describe the bug
I'm creating this issue in addition to our recent talk on Twitter: https://twitter.com/TauriApps/status/1709573174269002065.
If the tray menu is opened during its change (tray_handle().set_menu()), an old menu stays visible. Users have to reopen the menu to see the changes.
Here is the video of the sample repo:
https://github.com/tauri-apps/tauri/assets/306080/3fdbd407-9787-4568-8bb5-b3559607c00f
Reproduction
I've made a repo: https://github.com/vovayatsyuk/tauri-dynamic-tray-menu
Here is the code I use:
use tauri::{CustomMenuItem, SystemTray, SystemTrayMenu};
fn main() {
let tray_menu = SystemTrayMenu::new();
let system_tray = SystemTray::new().with_menu(tray_menu);
tauri::Builder::default()
.setup(|app| {
let handle = app.handle();
std::thread::spawn(move || {
let mut i = 0;
loop {
std::thread::sleep(std::time::Duration::from_millis(1000));
i += 1;
let tray_menu = SystemTrayMenu::new().add_item(
CustomMenuItem::new("item".to_string(), format!("Item {}", i))
);
handle.tray_handle().set_menu(tray_menu).unwrap();
}
});
Ok(())
})
.system_tray(system_tray)
.invoke_handler(tauri::generate_handler![greet])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
Expected behavior
I expect that the old tray menu will be closed and a new one will be opened in its place.
Platform and versions
[✔] Environment
- OS: Mac OS 14.0.0 X64
✔ Xcode Command Line Tools: installed
✔ rustc: 1.72.1 (d5c2e9c34 2023-09-13)
✔ Cargo: 1.72.1 (103a7ff2e 2023-08-15)
✔ rustup: 1.26.0 (5af9b9484 2023-04-05)
✔ Rust toolchain: stable-x86_64-apple-darwin (default)
- node: 20.4.0
- yarn: 1.22.19
- npm: 9.7.2
[-] Packages
- tauri [RUST]: 1.5.1
- tauri-build [RUST]: 1.5.0
- wry [RUST]: 0.24.4
- tao [RUST]: 0.16.4
- tauri-cli [RUST]: not installed!
- @tauri-apps/api [NPM]: not installed!
- @tauri-apps/cli [NPM]: 1.5.1
[-] App
- build-type: bundle
- CSP: unset
- distDir: ../src
- devPath: ../src
Stack trace
No response
Additional context
No response
I really don't know what the best behavior would be here, it feels weird to replace the menu while it is open, because we would need to close the active menu and then show another menu which is not a good UX for the user IMO.
Also do you have an idea how it works in other frameworks like electron?
I agree this is a weird from UX perspective. It would be much nicer to have the ability to add/remove specific items instead of rebuilding whole menu.
Electron closes the old menu and reopens the new one. This is what it looks like on Ubuntu (you should notice flickering caused by closing animation):
https://github.com/tauri-apps/tauri/assets/306080/a43f327c-6772-49a5-ae2d-b0b1001292fe
I agree this is a weird from UX perspective. It would be much nicer to have the ability to add/remove specific items instead of rebuilding whole menu.
Yeah, this is available in tauri 2.0-alpha, and there are no plans to backport it to v1.
Electron closes the old menu and reopens the new one. This is what it looks like on Ubuntu (you should notice flickering caused by closing animation):
Even though this looks like a bad UX, I think we still need to comply
I will move this over to muda so it can be fixed in for v2
I've just discovered that Ubuntu updates the tray menu automatically. Here is what my sample repo looks like on ubuntu (It doesn't flicker like an electron app):
https://github.com/tauri-apps/muda/assets/306080/5a88da98-e6df-4cb2-b604-eb283964708f