refactor(settings): 改用常规关于选项卡并统一蓝色强调色
This commit is contained in:
@@ -8,96 +8,30 @@ struct AppearanceSettingsView: View {
|
||||
@Bindable var sampling: SamplingPreferences
|
||||
let presentation: SettingsPresentation
|
||||
@Environment(\.locale) private var locale
|
||||
@State private var colorPicker = CustomColorPicker()
|
||||
@State private var selection: SettingsPage? = .appearance
|
||||
@State private var selection = SettingsPage.general
|
||||
|
||||
var body: some View {
|
||||
NavigationSplitView {
|
||||
List(selection: $selection) {
|
||||
ForEach(SettingsPage.allCases) { page in
|
||||
Label(page.title, systemImage: page.symbol).tag(page)
|
||||
}
|
||||
}
|
||||
.listStyle(.sidebar)
|
||||
.navigationSplitViewColumnWidth(min: 180, ideal: 205, max: 240)
|
||||
} detail: {
|
||||
Group {
|
||||
switch selectedPage {
|
||||
case .appearance: appearancePage
|
||||
case .language: languagePage
|
||||
case .monitoring: monitoringPage
|
||||
case .about: aboutPage
|
||||
}
|
||||
}
|
||||
.navigationTitle(selectedPage.title)
|
||||
TabView(selection: $selection) {
|
||||
generalPage
|
||||
.tabItem { Label("settings.general", systemImage: "gearshape") }
|
||||
.tag(SettingsPage.general)
|
||||
aboutPage
|
||||
.tabItem { Label("settings.about", systemImage: "info.circle") }
|
||||
.tag(SettingsPage.about)
|
||||
}
|
||||
.navigationSplitViewStyle(.balanced)
|
||||
.padding(12)
|
||||
.background(SettingsWindowLifecycle(title: AppLocalization.string("settings.title", locale: locale),
|
||||
requestID: presentation.requestID,
|
||||
onClose: { colorPicker.close() }))
|
||||
.onChange(of: preferences.accent) { _, choice in
|
||||
if choice == .custom {
|
||||
Task { @MainActor in
|
||||
await Task.yield()
|
||||
guard preferences.accent == .custom else { return }
|
||||
colorPicker.show(for: preferences)
|
||||
}
|
||||
} else { colorPicker.close() }
|
||||
}
|
||||
.onChange(of: selection) { _, _ in colorPicker.close() }
|
||||
.frame(minWidth: 720, idealWidth: 780, minHeight: 460, idealHeight: 540)
|
||||
requestID: presentation.requestID))
|
||||
.frame(minWidth: 600, idealWidth: 640, minHeight: 420, idealHeight: 460)
|
||||
.modifier(AppearanceStyle(preferences: preferences, refreshContentOnChange: true))
|
||||
}
|
||||
|
||||
private var selectedPage: SettingsPage { selection ?? .appearance }
|
||||
|
||||
private var appearancePage: some View {
|
||||
private var generalPage: some View {
|
||||
Form {
|
||||
Section {
|
||||
LabeledContent("appearance.title") {
|
||||
LiquidAppearancePicker(selection: $preferences.appearance)
|
||||
}
|
||||
LabeledContent("accent.title") {
|
||||
Picker("accent.title", selection: $preferences.accent) {
|
||||
Text("accent.system").tag(AccentChoice.system)
|
||||
Text("accent.blue").tag(AccentChoice.blue)
|
||||
Text("accent.purple").tag(AccentChoice.purple)
|
||||
Text("accent.teal").tag(AccentChoice.teal)
|
||||
Text("accent.green").tag(AccentChoice.green)
|
||||
Text("accent.orange").tag(AccentChoice.orange)
|
||||
Text("accent.pink").tag(AccentChoice.pink)
|
||||
Text("accent.custom").tag(AccentChoice.custom)
|
||||
}
|
||||
.labelsHidden()
|
||||
.fixedSize()
|
||||
}
|
||||
if preferences.accent == .custom {
|
||||
LabeledContent("accent.custom") {
|
||||
CustomColorWell(preferences: preferences, picker: colorPicker)
|
||||
.frame(width: 44, height: 26)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.formStyle(.grouped)
|
||||
.padding(.horizontal, 12)
|
||||
.padding(.top, 6)
|
||||
}
|
||||
|
||||
private var monitoringPage: some View {
|
||||
Form {
|
||||
Section {
|
||||
LabeledContent("sampling.background") {
|
||||
IntervalControl(milliseconds: $sampling.backgroundMilliseconds)
|
||||
}
|
||||
Text("sampling.backgroundHelp").font(.callout).foregroundStyle(.secondary)
|
||||
}
|
||||
}.formStyle(.grouped).padding(.horizontal, 12).padding(.top, 6)
|
||||
}
|
||||
|
||||
private var languagePage: some View {
|
||||
Form {
|
||||
Section {
|
||||
LabeledContent("language.title") {
|
||||
Picker("language.title", selection: $language.language) {
|
||||
Text("language.system").tag(AppLanguage.system)
|
||||
@@ -108,14 +42,15 @@ struct AppearanceSettingsView: View {
|
||||
.labelsHidden()
|
||||
.fixedSize()
|
||||
}
|
||||
Text("language.immediate")
|
||||
.font(.callout)
|
||||
.foregroundStyle(.secondary)
|
||||
}
|
||||
Section {
|
||||
LabeledContent("sampling.background") {
|
||||
IntervalControl(milliseconds: $sampling.backgroundMilliseconds)
|
||||
}
|
||||
Text("sampling.backgroundHelp").font(.callout).foregroundStyle(.secondary)
|
||||
}
|
||||
}
|
||||
.formStyle(.grouped)
|
||||
.padding(.horizontal, 12)
|
||||
.padding(.top, 6)
|
||||
}
|
||||
|
||||
private var aboutPage: some View {
|
||||
@@ -164,7 +99,7 @@ private struct LiquidAppearancePicker: View {
|
||||
HStack(spacing: 4) {
|
||||
ForEach(AppAppearance.allCases) { appearance in
|
||||
Button {
|
||||
// Theme changes must not animate the native split view / Form hierarchy.
|
||||
// Theme changes must not animate the native tab view / Form hierarchy.
|
||||
selection = appearance
|
||||
} label: {
|
||||
Label(appearance.title, systemImage: appearance.symbol)
|
||||
@@ -215,23 +150,6 @@ private extension AppAppearance {
|
||||
}
|
||||
}
|
||||
|
||||
private enum SettingsPage: String, CaseIterable, Identifiable {
|
||||
case appearance, language, monitoring, about
|
||||
var id: String { rawValue }
|
||||
var title: LocalizedStringKey {
|
||||
switch self {
|
||||
case .appearance: "settings.appearance"
|
||||
case .language: "settings.language"
|
||||
case .monitoring: "settings.monitoring"
|
||||
case .about: "settings.about"
|
||||
}
|
||||
}
|
||||
var symbol: String {
|
||||
switch self {
|
||||
case .appearance: "paintpalette"
|
||||
case .language: "globe"
|
||||
case .monitoring: "timer"
|
||||
case .about: "info.circle"
|
||||
}
|
||||
}
|
||||
private enum SettingsPage: Hashable {
|
||||
case general, about
|
||||
}
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
import AppKit
|
||||
import OSLog
|
||||
|
||||
@MainActor final class CustomColorPicker: NSObject {
|
||||
private let logger = Logger(subsystem: "cn.airnan.kymo", category: "color-picker")
|
||||
private static weak var owner: CustomColorPicker?
|
||||
private var preferences: AppearancePreferences?
|
||||
|
||||
func show(for preferences: AppearancePreferences) {
|
||||
logger.info("Custom color panel requested")
|
||||
Self.owner = self
|
||||
self.preferences = preferences
|
||||
let panel = NSColorPanel.shared
|
||||
panel.appearance = preferences.appearance.windowAppearance
|
||||
panel.showsAlpha = false
|
||||
panel.isContinuous = true
|
||||
panel.color = NSColor(srgbRed: preferences.red, green: preferences.green,
|
||||
blue: preferences.blue, alpha: 1)
|
||||
panel.setTarget(self)
|
||||
panel.setAction(#selector(colorChanged(_:)))
|
||||
NSApp.activate()
|
||||
panel.level = .floating
|
||||
panel.makeKeyAndOrderFront(nil)
|
||||
panel.orderFrontRegardless()
|
||||
logger.info("Custom color panel visible: \(panel.isVisible, privacy: .public)")
|
||||
}
|
||||
|
||||
@objc private func colorChanged(_ sender: NSColorPanel) {
|
||||
guard let preferences, let rgb = sender.color.usingColorSpace(.sRGB) else { return }
|
||||
preferences.red = rgb.redComponent
|
||||
preferences.green = rgb.greenComponent
|
||||
preferences.blue = rgb.blueComponent
|
||||
}
|
||||
|
||||
func close() {
|
||||
guard Self.owner === self else { return }
|
||||
NSColorPanel.shared.setTarget(nil)
|
||||
NSColorPanel.shared.setAction(nil)
|
||||
NSColorPanel.shared.close()
|
||||
preferences = nil
|
||||
Self.owner = nil
|
||||
}
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
import SwiftUI
|
||||
import AppKit
|
||||
|
||||
/// A native hit target avoids Form/plain-Button hit-testing ambiguity on the swatch.
|
||||
struct CustomColorWell: NSViewRepresentable {
|
||||
let preferences: AppearancePreferences
|
||||
let picker: CustomColorPicker
|
||||
@Environment(\.locale) private var locale
|
||||
|
||||
func makeNSView(context: Context) -> SwatchButton {
|
||||
let button = SwatchButton()
|
||||
return button
|
||||
}
|
||||
|
||||
func updateNSView(_ button: SwatchButton, context: Context) {
|
||||
button.setAccessibilityLabel(AppLocalization.string("accent.custom", locale: locale))
|
||||
button.open = { picker.show(for: preferences) }
|
||||
button.swatchColor = NSColor(srgbRed: preferences.red, green: preferences.green, blue: preferences.blue, alpha: 1)
|
||||
}
|
||||
|
||||
func sizeThatFits(_ proposal: ProposedViewSize, nsView: SwatchButton, context: Context) -> CGSize? {
|
||||
CGSize(width: 44, height: 26)
|
||||
}
|
||||
|
||||
final class SwatchButton: NSButton {
|
||||
var swatchColor = NSColor.systemBlue { didSet { needsDisplay = true } }
|
||||
override var intrinsicContentSize: NSSize { NSSize(width: 44, height: 26) }
|
||||
|
||||
override func draw(_ dirtyRect: NSRect) {
|
||||
// Draw the actual RGB color inside bounds; no scalable SF Symbol/cell image.
|
||||
let shape = NSBezierPath(roundedRect: bounds.insetBy(dx: 4, dy: 4), xRadius: 4, yRadius: 4)
|
||||
(isHighlighted ? swatchColor.withAlphaComponent(0.7) : swatchColor).setFill()
|
||||
shape.fill()
|
||||
NSColor.separatorColor.setStroke()
|
||||
shape.lineWidth = 1
|
||||
shape.stroke()
|
||||
}
|
||||
|
||||
var open: () -> Void = {}
|
||||
override init(frame frameRect: NSRect) {
|
||||
super.init(frame: frameRect)
|
||||
bezelStyle = .rounded
|
||||
isBordered = false
|
||||
wantsLayer = true
|
||||
layer?.masksToBounds = true
|
||||
title = ""
|
||||
target = self
|
||||
action = #selector(openPicker)
|
||||
}
|
||||
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
|
||||
override func acceptsFirstMouse(for event: NSEvent?) -> Bool { true }
|
||||
@objc func openPicker() { open() }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user