[fix] hide macOS Dock icon when closing to the tray

This commit is contained in:
dijunkun
2026-09-08 00:22:08 +08:00
parent 58225de7ea
commit 0002bcea74
3 changed files with 18 additions and 6 deletions
@@ -1151,6 +1151,7 @@ void GuiApplication::InitializeSystemTray() {
ui_->main->show();
ui_->main->window().set_minimized(false);
#if defined(__APPLE__)
MacActivateWindow(ui_->main->window().appkit_view());
// A window restored from the tray may only obtain its NSWindow after the
// callback returns, so let the regular UI tick configure it.
ui_->main_native_titlebar_attempts = 30;
@@ -1161,16 +1162,12 @@ void GuiApplication::InitializeSystemTray() {
ui_->main->hide();
}
};
auto open_settings = [this] {
auto open_settings = [this, show_window] {
if (!ui_) {
return;
}
ui_->main->show();
ui_->main->window().set_minimized(false);
show_window();
ui_->main->set_settings_open(true);
#if defined(__APPLE__)
ui_->main_native_titlebar_attempts = 30;
#endif
};
auto exit_app = [this] {
exit_ = true;
@@ -17,6 +17,9 @@ namespace crossdesk {
struct MacTrayImpl;
// Activates the app and focuses the restored Slint window's current NSView.
void MacActivateWindow(void* appkit_view);
class MacTray {
public:
MacTray(::SDL_Window* app_window, const std::string& tooltip,
@@ -57,6 +57,8 @@ struct MacTrayImpl {
} else if (app_window) {
SDL_HideWindow(app_window);
}
// Keep running through the menu-bar tray without a Dock application icon.
[NSApp setActivationPolicy:NSApplicationActivationPolicyAccessory];
}
void RemoveTrayIcon() {
@@ -69,6 +71,8 @@ struct MacTrayImpl {
}
void ShowWindow() {
// Restore normal application behavior before showing or focusing a window.
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
if (show_window) {
show_window();
} else if (app_window) {
@@ -142,6 +146,7 @@ struct MacTrayImpl {
}
void OpenSettings() {
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
if (open_settings) {
open_settings();
} else {
@@ -279,6 +284,13 @@ MacTray::MacTray(std::function<void()> show_window,
MacTray::~MacTray() = default;
void MacActivateWindow(void *appkit_view) {
NSView *view = (__bridge NSView *)appkit_view;
NSWindow *window = [view window];
[NSApp activateIgnoringOtherApps:YES];
[window makeKeyAndOrderFront:nil];
}
void MacTray::MinimizeToTray() { impl_->MinimizeToTray(); }
void MacTray::RemoveTrayIcon() { impl_->RemoveTrayIcon(); }