tauri-apps / tauri

Build smaller, faster, and more secure desktop and mobile applications with a web frontend.
https://tauri.app
Apache License 2.0
82.68k stars 2.48k forks source link

[bug] Tray menu broken on Linux (memory related) #7648

Closed davide-baldo closed 1 year ago

davide-baldo commented 1 year ago

Describe the bug

On linux when using TrayIconBuilder the menu is not shown upon click (left or right), giving this error:

(api:7589): LIBDBUSMENU-GLIB-WARNING **: 19:23:57.784: About to Show called on an item wihtout submenus.  We're ignoring it.

The menu appears if I leak the menu memory with Box::leak(Box::new(..

Reproduction

This is reproducible on Linux using api example.

  1. go into the API example directory: examples/api/src-tauri
  2. comment /removeswitch-menu match, it has nothing to do with menu visibility, but somehow keeps things working by referencing the menus.
      // "switch-menu" => {
      //   let flag = is_menu1.load(Ordering::Relaxed);
      //   let (menu, tooltip) = if flag {
      //     (menu2.clone(), "Menu 2")
      //   } else {
      //     (menu1.clone(), "Tauri")
      //   };
      //   if let Some(tray) = app.tray_by_id("tray-1") {
      //     let _ = tray.set_menu(Some(menu));
      //     let _ = tray.set_tooltip(Some(tooltip));
      //   }
      //   is_menu1.store(!flag, Ordering::Relaxed);
      // }
  3. Run the API app
  4. try to click the tray icon, it won't show up anything

Expected behavior

The menu should show up upon click of the tray icon.

Platform and versions

~/workspace/github/tauri-apps/tauri/examples/api/src-tauri $ cargo tauri info

[✔] Environment
    - OS: Gentoo Linux 2.13 X64
    ✔ webkit2gtk-4.1: 2.40.2
    ✔ rsvg2: 2.56.1
    ✔ rustc: 1.69.0 (84c898d65 2023-04-16)
    ✔ Cargo: 1.69.0 (6e9a83356 2023-04-12)
    ✔ rustup: 1.0.0
    ✔ Rust toolchain: rustup 1.0.0
    - node: 20.2.0
    - pnpm: 8.6.6
    - npm: 9.6.0

[-] Packages
    - tauri [RUST]: 2.0.0-alpha.10
    - tauri-build [RUST]: 2.0.0-alpha.6
    - wry [RUST]: 0.31.0
    - tao [RUST]: 0.22.0
    - @tauri-apps/api [NPM]: not installed!
    - @tauri-apps/cli [NPM]: 2.0.0-alpha.11

[-] App
    - build-type: bundle
    - CSP: default-src 'self' customprotocol: asset:; font-src https://fonts.gstatic.com; img-src 'self' asset: https://asset.localhost blob: data:; style-src 'unsafe-inline' 'self' https://fonts.googleapis.com; connect-src ipc: https://ipc.localhost
    - distDir: ../dist
    - devPath: http://localhost:5173/
    - framework: Svelte
    - bundler: Vite

Stack trace

No response

Additional context

When leaking the menu memory it shows up, but it doesn't get refreshed when set again with app_handle.tray().unwrap().set_menu()

amrbashir commented 1 year ago

I think this is because the Menu is dropped and so the tray menu won't work which is the expected behavior, so please provide a minimal repro of what your code looks like so I can maybe take a look but you could try to save the Menu in the tauri state store by calling app.manage(menu).

I will go ahead and close the issue but feel free to ping me when you have a repro.