Files
SnapGo/activation_policy_darwin.go
mamamiyear 85d5fcc722 build: silence duplicate -lobjc linker warning on Xcode 15+
Add -Wl,-no_warn_duplicate_libraries to the darwin cgo LDFLAGS and allow
it through the cgo flag allowlist via CGO_LDFLAGS_ALLOW in the build
scripts, so the ld-prime duplicate -lobjc note no longer clutters build
output.
2026-07-07 14:16:24 +08:00

78 lines
2.4 KiB
Go

//go:build darwin
package main
/*
#cgo CFLAGS: -x objective-c -fobjc-arc
#cgo LDFLAGS: -framework AppKit
// Silence the "ignoring duplicate libraries: '-lobjc'" note emitted by the
// Xcode 15+ linker (ld-prime): cgo links -lobjc for our Objective-C code and
// the Go darwin runtime pulls it in as well, which the new linker flags as a
// duplicate. Suppressing it here keeps the build output clean; the flag is a
// no-op on the legacy ld64 path.
#cgo LDFLAGS: -Wl,-no_warn_duplicate_libraries
#include <dispatch/dispatch.h>
#import <AppKit/AppKit.h>
static id snipDidHideObserver = nil;
static void snipSetActivationPolicy(bool regular, bool activate) {
void (^work)(void) = ^{
[NSApp setActivationPolicy: regular
? NSApplicationActivationPolicyRegular
: NSApplicationActivationPolicyAccessory];
if (activate) {
[NSApp unhide:nil];
[NSApp activateIgnoringOtherApps:YES];
}
};
if ([NSThread isMainThread]) {
work();
} else {
dispatch_sync(dispatch_get_main_queue(), work);
}
}
static void snipInstallActivationPolicyHooks(void) {
void (^work)(void) = ^{
if (snipDidHideObserver != nil) {
return;
}
snipDidHideObserver = [[NSNotificationCenter defaultCenter]
addObserverForName:NSApplicationDidHideNotification
object:NSApp
queue:[NSOperationQueue mainQueue]
usingBlock:^(__unused NSNotification *note) {
[NSApp setActivationPolicy:NSApplicationActivationPolicyAccessory];
}];
};
if ([NSThread isMainThread]) {
work();
} else {
dispatch_sync(dispatch_get_main_queue(), work);
}
}
*/
import "C"
// installActivationPolicyHooks observes the native hide path used by Wails'
// HideWindowOnClose on macOS. Closing Settings calls `[NSApp hide:nil]`,
// which does not reliably pass through Wails' Go OnBeforeClose hook.
func installActivationPolicyHooks() {
C.snipInstallActivationPolicyHooks()
}
// showDockIcon makes the app behave like a normal foreground macOS app while
// Settings is visible.
func showDockIcon(activate bool) {
C.snipSetActivationPolicy(true, C.bool(activate))
}
// hideDockIcon returns the app to menu-bar-agent mode: no persistent Dock
// icon, while the status-bar item remains available.
func hideDockIcon() {
C.snipSetActivationPolicy(false, false)
}