...nope. Still all sorts of weirdness with the above suggestions.
NSWindow.willCloseNotification
triggers when mousing through the system menu items for the app. That needs to go.
.onSubmit
does not actually trigger for SettingsLink
. That needs to go.
Something that seems to almost work consistently is to add this library: https://github.com/orchetect/SettingsAccess
SettingsLink {
Text("Settings")
} preAction: {
NSApp.activate(ignoringOtherApps: true)
} postAction: {
NSApp.activate(ignoringOtherApps: true)
}
This also keeps the .onReceive
for NSWindow.didBecomeMainNotification
, but with an addition:
Settings {
Text("123")
.onReceive(NotificationCenter.default.publisher(for: NSWindow.didBecomeMainNotification)) { newValue in
NSApp.activate(ignoringOtherApps: true)
NSApp.setActivationPolicy(.regular)
NSApp.windows.first?.orderFrontRegardless()
}
}
For making the app disappear from the dock upon closing the last window, using the AppDelegate as follows seems to do the trick. Unclear as of yet if there are any side effects:
final class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_: Notification) {
NSApp.setActivationPolicy(.accessory)
}
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
NSApp.setActivationPolicy(.accessory)
NSApp.deactivate()
return false
}
}