feat(menubar): 实现趋势面板外侧详情与三语外观设置
接入有界趋势、平滑滚动、菜单式外侧详情、底部操作与三语文案,装配独立外观设置和 Dock 生命周期。 合并跟随系统外观刷新、自选颜色入口与色块绘制修复,保留用户确认的配色联动、重启保存及滚动验收。 多场景视觉与长期资源验证边界仍记录于 docs。
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension AppearancePreferences {
|
||||
var tintColor: Color {
|
||||
switch accent {
|
||||
case .system: .accentColor
|
||||
case .blue: .blue
|
||||
case .purple: .purple
|
||||
case .teal: .teal
|
||||
case .green: .green
|
||||
case .orange: .orange
|
||||
case .pink: .pink
|
||||
case .custom: Color(red: red, green: green, blue: blue)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct AppearanceStyle: ViewModifier {
|
||||
let preferences: AppearancePreferences
|
||||
@State private var system = SystemAppearanceObserver()
|
||||
private var resolved: AppAppearance { preferences.appearance.resolved(systemIsDark: system.isDark) }
|
||||
private var scheme: ColorScheme { resolved == .dark ? .dark : .light }
|
||||
func body(content: Content) -> some View {
|
||||
content
|
||||
.background(WindowAppearanceBridge(choice: resolved))
|
||||
.preferredColorScheme(scheme)
|
||||
.environment(\.colorScheme, scheme)
|
||||
.tint(preferences.tintColor)
|
||||
}
|
||||
}
|
||||
|
||||
/// Explicitly clear the NSWindow override when returning to system appearance.
|
||||
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) } }
|
||||
}
|
||||
Reference in New Issue
Block a user