feat(settings): 重构液态玻璃设置并更新品牌图标
This commit is contained in:
@@ -4,18 +4,25 @@ 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() {
|
||||
@@ -24,6 +31,17 @@ struct SettingsWindowLifecycle: NSViewRepresentable {
|
||||
tokens.removeAll()
|
||||
guard let window else { return }
|
||||
window.title = title
|
||||
window.titlebarAppearsTransparent = true
|
||||
window.isOpaque = false
|
||||
window.backgroundColor = .clear
|
||||
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 {
|
||||
@@ -33,12 +51,36 @@ struct SettingsWindowLifecycle: NSViewRepresentable {
|
||||
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)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
deinit { for token in tokens { NotificationCenter.default.removeObserver(token) } }
|
||||
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) } }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user