How to open Settings from menu bar app and show app icon in dock?

63 views Asked by At

I'm creating macOS menu bar app in SwiftUI and I want to add settings. Currently I have two problems:

  1. SettingsLink does open settings app, but if it's already open then it won't bring it to front
  2. 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)
    }
}
0

There are 0 answers