ahkohd/tauri-macos-spotlight-example

System tray not working in built version of Spotlight/NSPanel example

allachetan opened this issue · 5 comments

The app runs fine with the "run dev" command, but quits immediately after opening the built version of the app. I suspect there might be a bug in the init_spotlight_window function. I have only added a basic implementation of the system tray from the Tauri docs shown below (no other additions to the codebase).

My macos version is: 13.6.6 (intel). However I tested this on my arm mac and it also did not work.

Code:

#![cfg_attr(
    all(not(debug_assertions), target_os = "windows"),
    windows_subsystem = "windows"
)]
mod spotlight;
use tauri::{SystemTray, CustomMenuItem, SystemTrayMenu, SystemTrayMenuItem};

fn main() {
    let quit = CustomMenuItem::new("quit".to_string(), "Quit");
    let hide = CustomMenuItem::new("hide".to_string(), "Hide");
    let tray_menu = SystemTrayMenu::new()
        .add_item(quit)
        .add_native_item(SystemTrayMenuItem::Separator)
        .add_item(hide);
    let system_tray = SystemTray::new().with_menu(tray_menu);

    tauri::Builder::default()
        .invoke_handler(tauri::generate_handler![
            spotlight::init_spotlight_window,
            spotlight::show_spotlight,
            spotlight::hide_spotlight
        ])
        .manage(spotlight::State::default())
        .system_tray(system_tray)
        .setup(move |app| {
            // Set activation poicy to Accessory to prevent the app icon from showing on the dock
            app.set_activation_policy(tauri::ActivationPolicy::Accessory);
            Ok(())
        })
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}

Any help with this would be greatly appreciated :). In init_spotlight_window code seems to be able to create the panel without issue. So the issue seems to be within the following code inside init_spotlight_window:

// Set panel above the main menu window level
panel.set_level(NSMainMenuWindowLevel + 1);

// Set panel to auto hide when it resigns key
panel.set_auto_hide(true);

// Ensure that the panel can display over the top of fullscreen apps
panel.set_collection_behaviour(
    NSWindowCollectionBehavior::NSWindowCollectionBehaviorTransient
        | NSWindowCollectionBehavior::NSWindowCollectionBehaviorMoveToActiveSpace
        | NSWindowCollectionBehavior::NSWindowCollectionBehaviorFullScreenAuxiliary,
);

// Ensures panel does not activate
panel.set_style_mask(NSWindowStyleMaskNonActivatingPanel);

// Setup delegate for an NSPanel to listen for window resign key and hide the panel
let delegate = RawNSPanelDelegate::new();
delegate.set_panel_(panel.clone());
panel.set_delegate(Some(delegate));

I have tested a production build of this repository's example, and it works perfectly.

Can you please provide a GitHub repository containing a minimal reproduction of your project? I would like to test it on my end and debug the issue for you.

You can also check this https://github.com/ahkohd/tauri-macos-menubar-app-example template on building a menubar application with a tray icon using the tauri-nspanel plugin.

You can get more information about why your app crashed by running the binary of the app in terminal and inspecting the standard output.

gh.mp4

Right-click on your production build "App.app", On the context menu, click on "Show Package Contents"

Thank you for your response. I will try debugging later tonight or this week with your method, thank you for showing me!

I have created a repo with the minimal implementation here: https://github.com/allachetan/tauri-macos-spotlight-with-tray. I tried running the build command again, but same issue of an immediate crash after attempting to open the app even though it works fine with the run dev command.

Update: I was not able to get it working with this codebase, however I was able to get the solution I was looking for follwing your repo https://github.com/ahkohd/tauri-macos-menubar-app-example using the tauri nspanel plugin.