Files
Kymo/kymo/UI/Components/AppearanceStyle.swift
T

96 lines
4.0 KiB
Swift

import SwiftUI
import AppKit
import Observation
extension AppAppearance {
func resolved(systemIsDark: Bool) -> AppAppearance {
self == .system ? (systemIsDark ? .dark : .light) : self
}
var windowAppearance: NSAppearance? {
switch self {
case .system: nil
case .light: NSAppearance(named: .aqua)
case .dark: NSAppearance(named: .darkAqua)
}
}
}
struct AppearanceStyle: ViewModifier {
let preferences: AppearancePreferences
var refreshContentOnChange = false
@State private var system: SystemAppearanceObserver?
private var resolved: AppAppearance { preferences.appearance.resolved(systemIsDark: system?.isDark ?? SystemAppearanceObserver.isDark(globalDomain: UserDefaults.standard.persistentDomain(forName: UserDefaults.globalDomain))) }
private var scheme: ColorScheme { resolved == .dark ? .dark : .light }
func body(content: Content) -> some View {
content
// Settings native hosts can retain their previous native appearance.
// Keep state in the owning view, but replace the hosted subtree on a resolved theme change.
.id(refreshContentOnChange ? resolved : nil)
.background(WindowAppearanceBridge(choice: resolved))
.preferredColorScheme(scheme)
.environment(\.colorScheme, scheme)
.tint(.blue)
.onAppear { if system == nil { system = SystemAppearanceObserver() } }
}
}
/// Apply the resolved light/dark choice to native hosts and child windows.
/// AppearanceStyle observes the system independently and reapplies its resolved choice.
private struct WindowAppearanceBridge: NSViewRepresentable {
let choice: AppAppearance
func makeNSView(context: Context) -> AppearanceView { AppearanceView() }
func updateNSView(_ view: AppearanceView, context: Context) { view.choice = choice }
final class AppearanceView: NSView {
override func hitTest(_ point: NSPoint) -> NSView? { nil }
var choice = AppAppearance.system { didSet { apply() } }
override func viewDidMoveToWindow() { super.viewDidMoveToWindow(); apply() }
private func apply() {
WindowAppearance.apply(choice, to: window)
}
}
}
@MainActor enum WindowAppearance {
static func apply(_ choice: AppAppearance, to window: NSWindow?) {
let appearance = choice.windowAppearance
if window?.appearance?.name != appearance?.name { window?.appearance = appearance }
for child in window?.childWindows ?? [] where child.appearance?.name != appearance?.name {
child.appearance = appearance
}
}
}
/// System source is independent of the color scheme inherited by this window.
@MainActor @Observable final class SystemAppearanceObserver {
private(set) var isDark = false
@ObservationIgnored private var observation: NSKeyValueObservation?
@ObservationIgnored private var themeToken: NSObjectProtocol?
init() {
refresh()
observation = NSApp.observe(\.effectiveAppearance, options: [.new]) { [weak self] _, _ in
guard let self else { return }
Task { @MainActor in self.refresh() }
}
themeToken = DistributedNotificationCenter.default().addObserver(
forName: Notification.Name("AppleInterfaceThemeChangedNotification"), object: nil, queue: .main
) { [weak self] _ in
guard let self else { return }
Task { @MainActor in self.refresh() }
}
}
private func refresh() {
// Read the global domain, not this app's effective window appearance.
isDark = Self.isDark(globalDomain: UserDefaults.standard.persistentDomain(forName: UserDefaults.globalDomain))
}
static func isDark(globalDomain: [String: Any]?) -> Bool {
(globalDomain?["AppleInterfaceStyle"] as? String)?.caseInsensitiveCompare("Dark") == .orderedSame
}
deinit { if let themeToken { DistributedNotificationCenter.default().removeObserver(themeToken) } }
}