Files
Kymo/kymo/Features/Settings/AppearanceSettingsView.swift
T

242 lines
9.2 KiB
Swift

import SwiftUI
import AppKit
/// Settings navigation owns presentation only; persisted preferences remain in Core.
struct AppearanceSettingsView: View {
@Bindable var preferences: AppearancePreferences
@Bindable var language: LanguagePreferences
@Bindable var sampling: SamplingPreferences
let presentation: SettingsPresentation
@Environment(\.locale) private var locale
@State private var colorPicker = CustomColorPicker()
@State private var selection: SettingsPage? = .appearance
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)
}
.navigationSplitViewStyle(.balanced)
.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)
.modifier(AppearanceStyle(preferences: preferences))
}
private var selectedPage: SettingsPage { selection ?? .appearance }
private var appearancePage: 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)
Text(verbatim: "简体中文").tag(AppLanguage.simplifiedChinese)
Text(verbatim: "繁體中文").tag(AppLanguage.traditionalChinese)
Text(verbatim: "English").tag(AppLanguage.english)
}
.labelsHidden()
.fixedSize()
}
Text("language.immediate")
.font(.callout)
.foregroundStyle(.secondary)
}
}
.formStyle(.grouped)
.padding(.horizontal, 12)
.padding(.top, 6)
}
private var aboutPage: some View {
Form {
Section {
HStack(alignment: .top, spacing: 18) {
Image("BrandLogo")
.resizable()
.scaledToFit()
.frame(width: 88, height: 88)
.accessibilityHidden(true)
VStack(alignment: .leading, spacing: 7) {
Text(verbatim: "Kymo").font(.title2.bold())
Text("about.version \(version)")
.font(.callout)
.foregroundStyle(.secondary)
Text("about.description")
.foregroundStyle(.secondary)
.fixedSize(horizontal: false, vertical: true)
}
Spacer(minLength: 0)
}
.padding(.vertical, 8)
}
}
.formStyle(.grouped)
.padding(.horizontal, 12)
.padding(.top, 6)
}
private var version: String {
let bundle = Bundle.main
let version = bundle.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String ?? "--"
let build = bundle.object(forInfoDictionaryKey: "CFBundleVersion") as? String ?? "--"
return "\(version) (\(build))"
}
}
private struct LiquidAppearancePicker: View {
@Binding var selection: AppAppearance
@Environment(\.accessibilityReduceMotion) private var reduceMotion
@Namespace private var selectionNamespace
var body: some View {
GlassEffectContainer(spacing: 10) {
HStack(spacing: 4) {
ForEach(AppAppearance.allCases) { appearance in
Button {
if reduceMotion {
selection = appearance
} else {
withAnimation(.smooth(duration: 0.24)) {
selection = appearance
}
}
} label: {
Label(appearance.title, systemImage: appearance.symbol)
.labelStyle(.titleAndIcon)
.font(.system(size: 13, weight: .medium))
.foregroundStyle(selection == appearance ? .primary : .secondary)
.padding(.horizontal, 11)
.frame(minWidth: 108, minHeight: 34)
.contentShape(.capsule)
.background {
if selection == appearance {
Capsule()
.fill(.clear)
.glassEffect(.regular.interactive(), in: .capsule)
.glassEffectID("appearance-selection", in: selectionNamespace)
.glassEffectTransition(.matchedGeometry)
}
}
}
.buttonStyle(.plain)
.accessibilityAddTraits(selection == appearance ? .isSelected : [])
}
}
.padding(4)
.glassEffect(.regular, in: .capsule)
}
.accessibilityElement(children: .contain)
.accessibilityLabel(Text("appearance.title"))
}
}
private extension AppAppearance {
var title: LocalizedStringKey {
switch self {
case .system: "appearance.system"
case .light: "appearance.light"
case .dark: "appearance.dark"
}
}
var symbol: String {
switch self {
case .system: "circle.lefthalf.filled"
case .light: "sun.max.fill"
case .dark: "moon.fill"
}
}
}
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"
}
}
}