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

166 lines
6.4 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 selection = SettingsPage.general
var body: some View {
Group {
switch selection {
case .general: generalPage
case .about: aboutPage
}
}
.padding(12)
.toolbar {
ToolbarItem(placement: .principal) {
Picker("settings.title", selection: $selection) {
Text("settings.general").tag(SettingsPage.general)
Text("settings.about").tag(SettingsPage.about)
}
.pickerStyle(.segmented)
.labelsHidden()
.controlSize(.large)
.frame(width: 180)
}
}
.background(SettingsWindowLifecycle(title: AppLocalization.string("settings.title", locale: locale),
requestID: presentation.requestID))
.frame(minWidth: 600, idealWidth: 640, minHeight: 420, idealHeight: 460)
.modifier(AppearanceStyle(preferences: preferences, refreshContentOnChange: true))
}
private var generalPage: some View {
Form {
Section {
LabeledContent("appearance.title") {
LiquidAppearancePicker(selection: $preferences.appearance)
}
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()
}
}
Section {
LabeledContent("sampling.background") {
IntervalControl(milliseconds: $sampling.backgroundMilliseconds)
}
Text("sampling.backgroundHelp").font(.callout).foregroundStyle(.secondary)
}
}
.formStyle(.grouped)
}
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 {
// Theme changes must not animate the native tab view / Form hierarchy.
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)
}
.animation(reduceMotion ? nil : .smooth(duration: 0.24), value: selection)
.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: Hashable {
case general, about
}