84 lines
3.6 KiB
Swift
84 lines
3.6 KiB
Swift
import SwiftUI
|
|
import AppKit
|
|
|
|
/// Keep the app discoverable while its settings window is open, including reopen.
|
|
struct SettingsWindowLifecycle: NSViewRepresentable {
|
|
var title: String
|
|
var requestID: UInt64 = 0
|
|
var onClose: () -> Void = {}
|
|
|
|
func makeNSView(context: Context) -> WindowObserverView { WindowObserverView() }
|
|
func updateNSView(_ nsView: WindowObserverView, context: Context) {
|
|
nsView.onClose = onClose
|
|
nsView.title = title
|
|
nsView.requestID = requestID
|
|
}
|
|
|
|
final class WindowObserverView: NSView {
|
|
override func hitTest(_ point: NSPoint) -> NSView? { nil }
|
|
var onClose: () -> Void = {}
|
|
var title = "" { didSet { window?.title = title } }
|
|
var requestID: UInt64 = 0 {
|
|
didSet { if oldValue != requestID { schedulePresentation() } }
|
|
}
|
|
private var presentationTask: Task<Void, Never>?
|
|
private var needsPresentation = false
|
|
private var tokens: [NSObjectProtocol] = []
|
|
|
|
override func viewDidMoveToWindow() {
|
|
super.viewDidMoveToWindow()
|
|
for token in tokens { NotificationCenter.default.removeObserver(token) }
|
|
tokens.removeAll()
|
|
guard let window else { return }
|
|
window.title = title
|
|
schedulePresentation()
|
|
tokens.append(NotificationCenter.default.addObserver(forName: NSApplication.didBecomeActiveNotification,
|
|
object: NSApp, queue: .main) { [weak self] _ in
|
|
MainActor.assumeIsolated {
|
|
guard let self, self.needsPresentation else { return }
|
|
self.presentWindow()
|
|
}
|
|
})
|
|
tokens.append(NotificationCenter.default.addObserver(forName: NSWindow.didBecomeKeyNotification,
|
|
object: window, queue: .main) { _ in
|
|
MainActor.assumeIsolated {
|
|
if NSApp.activationPolicy() != .regular { NSApp.setActivationPolicy(.regular) }
|
|
}
|
|
})
|
|
tokens.append(NotificationCenter.default.addObserver(forName: NSWindow.willCloseNotification,
|
|
object: window, queue: .main) { [weak self] _ in
|
|
MainActor.assumeIsolated {
|
|
self?.presentationTask?.cancel()
|
|
self?.needsPresentation = false
|
|
self?.onClose()
|
|
NSApp.setActivationPolicy(.accessory)
|
|
}
|
|
})
|
|
}
|
|
|
|
private func schedulePresentation() {
|
|
guard window != nil else { return }
|
|
needsPresentation = true
|
|
NSApp.setActivationPolicy(.regular)
|
|
presentationTask?.cancel()
|
|
presentationTask = Task { @MainActor [weak self] in
|
|
// Allow accessory → regular and SwiftUI's openSettings ordering to settle.
|
|
await Task.yield()
|
|
guard !Task.isCancelled, let self, self.needsPresentation else { return }
|
|
self.presentWindow()
|
|
}
|
|
}
|
|
|
|
private func presentWindow() {
|
|
guard let window else { return }
|
|
if window.isMiniaturized { window.deminiaturize(nil) }
|
|
NSApp.activate()
|
|
window.makeKeyAndOrderFront(nil)
|
|
window.orderFrontRegardless()
|
|
if NSApp.isActive { needsPresentation = false }
|
|
}
|
|
|
|
deinit { presentationTask?.cancel(); for token in tokens { NotificationCenter.default.removeObserver(token) } }
|
|
}
|
|
}
|