I'm creating macOS menu bar app in SwiftUI and I want to add settings. Currently I have two problems:
- SettingsLink does open settings app, but if it's already open then it won't bring it to front
- When I open settings via SettingsLink, how do I handle showing app icon on dock?
As for the second point, I've tried setting Application is agent in Info.plist and changing NSApp.setActivationPolicy() depending if Settings window is open. However, I couldn't figure how to listen to the Settings window open/closed state.
import SwiftUI
@main
struct TestApp: App {
@NSApplicationDelegateAdaptor(AppDelegate.self) var delegate
var body: some Scene {
Settings {
Text("123")
}
MenuBarExtra("Test", systemImage: "star.fill")
{
SettingsLink { Text("Open settings") }
}
}
}
final class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_: Notification) {
NSApp.setActivationPolicy(.regular)
}
}