feat(settings): 新增三语即时切换并记录验收与专项边界
This commit is contained in:
@@ -16,17 +16,20 @@ struct kymoApp: App {
|
||||
@State private var metrics = MetricsStore()
|
||||
|
||||
@State private var appearance = AppearancePreferences()
|
||||
@State private var language = LanguagePreferences()
|
||||
|
||||
var body: some Scene {
|
||||
MenuBarExtra {
|
||||
MenuPanelView(metrics: metrics, preferences: appearance)
|
||||
.environment(\.locale, language.locale)
|
||||
} label: {
|
||||
MenuBarLabel(snapshot: metrics.snapshot)
|
||||
}
|
||||
.menuBarExtraStyle(.window)
|
||||
|
||||
Settings {
|
||||
AppearanceSettingsView(preferences: appearance)
|
||||
AppearanceSettingsView(preferences: appearance, language: language)
|
||||
.environment(\.locale, language.locale)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
import Foundation
|
||||
import Observation
|
||||
|
||||
nonisolated enum AppLanguage: String, CaseIterable, Identifiable {
|
||||
case system, simplifiedChinese = "zh-Hans", traditionalChinese = "zh-Hant", english = "en"
|
||||
var id: String { rawValue }
|
||||
static let supported = ["en", "zh-Hans", "zh-Hant"]
|
||||
|
||||
func identifier(preferredLanguages: [String] = Locale.preferredLanguages) -> String {
|
||||
self == .system
|
||||
? Bundle.preferredLocalizations(from: Self.supported, forPreferences: preferredLanguages).first ?? "en"
|
||||
: rawValue
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor @Observable final class LanguagePreferences {
|
||||
@ObservationIgnored private let defaults: UserDefaults
|
||||
var language: AppLanguage { didSet { defaults.set(language.rawValue, forKey: "language") } }
|
||||
var locale: Locale { Locale(identifier: language.identifier()) }
|
||||
|
||||
init(defaults: UserDefaults = .standard) {
|
||||
self.defaults = defaults
|
||||
language = AppLanguage(rawValue: defaults.string(forKey: "language") ?? "") ?? .system
|
||||
}
|
||||
}
|
||||
@@ -4,10 +4,20 @@ import AppKit
|
||||
/// B-stage appearance controls; general/modules/permissions remain D-stage work.
|
||||
struct AppearanceSettingsView: View {
|
||||
@Bindable var preferences: AppearancePreferences
|
||||
@Bindable var language: LanguagePreferences
|
||||
@Environment(\.locale) private var locale
|
||||
@State private var colorPicker = CustomColorPicker()
|
||||
|
||||
var body: some View {
|
||||
Form {
|
||||
Section("settings.language") {
|
||||
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)
|
||||
}
|
||||
}
|
||||
Section("settings.appearance") {
|
||||
Picker("appearance.title", selection: $preferences.appearance) {
|
||||
Text("appearance.system").tag(AppAppearance.system)
|
||||
@@ -35,7 +45,7 @@ struct AppearanceSettingsView: View {
|
||||
}
|
||||
}
|
||||
}
|
||||
.background(SettingsWindowLifecycle(onClose: { colorPicker.close() }))
|
||||
.background(SettingsWindowLifecycle(title: AppLocalization.string("settings.title", locale: locale), onClose: { colorPicker.close() }))
|
||||
.onChange(of: preferences.accent) { _, choice in
|
||||
if choice == .custom {
|
||||
Task { @MainActor in
|
||||
@@ -48,7 +58,7 @@ struct AppearanceSettingsView: View {
|
||||
}
|
||||
.onAppear { NSApp.setActivationPolicy(.regular); NSApp.activate() }
|
||||
.formStyle(.grouped)
|
||||
.frame(width: 420, height: 240)
|
||||
.frame(width: 460, height: 340)
|
||||
.modifier(AppearanceStyle(preferences: preferences))
|
||||
}
|
||||
|
||||
|
||||
@@ -5,14 +5,15 @@ import AppKit
|
||||
struct CustomColorWell: NSViewRepresentable {
|
||||
let preferences: AppearancePreferences
|
||||
let picker: CustomColorPicker
|
||||
@Environment(\.locale) private var locale
|
||||
|
||||
func makeNSView(context: Context) -> SwatchButton {
|
||||
let button = SwatchButton()
|
||||
button.setAccessibilityLabel(String(localized: "accent.custom"))
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -3,14 +3,19 @@ import AppKit
|
||||
|
||||
/// Keep the app discoverable while its settings window is open, including reopen.
|
||||
struct SettingsWindowLifecycle: NSViewRepresentable {
|
||||
var title: String
|
||||
var onClose: () -> Void = {}
|
||||
|
||||
func makeNSView(context: Context) -> WindowObserverView { WindowObserverView() }
|
||||
func updateNSView(_ nsView: WindowObserverView, context: Context) { nsView.onClose = onClose }
|
||||
func updateNSView(_ nsView: WindowObserverView, context: Context) {
|
||||
nsView.onClose = onClose
|
||||
nsView.title = title
|
||||
}
|
||||
|
||||
final class WindowObserverView: NSView {
|
||||
override func hitTest(_ point: NSPoint) -> NSView? { nil }
|
||||
var onClose: () -> Void = {}
|
||||
var title = "" { didSet { window?.title = title } }
|
||||
private var tokens: [NSObjectProtocol] = []
|
||||
|
||||
override func viewDidMoveToWindow() {
|
||||
@@ -18,6 +23,7 @@ struct SettingsWindowLifecycle: NSViewRepresentable {
|
||||
for token in tokens { NotificationCenter.default.removeObserver(token) }
|
||||
tokens.removeAll()
|
||||
guard let window else { return }
|
||||
window.title = title
|
||||
tokens.append(NotificationCenter.default.addObserver(forName: NSWindow.didBecomeKeyNotification,
|
||||
object: window, queue: .main) { _ in
|
||||
MainActor.assumeIsolated {
|
||||
|
||||
@@ -897,6 +897,98 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"settings.language": {
|
||||
"extractionState": "manual",
|
||||
"localizations": {
|
||||
"en": {
|
||||
"stringUnit": {
|
||||
"state": "translated",
|
||||
"value": "Language"
|
||||
}
|
||||
},
|
||||
"zh-Hans": {
|
||||
"stringUnit": {
|
||||
"state": "translated",
|
||||
"value": "语言"
|
||||
}
|
||||
},
|
||||
"zh-Hant": {
|
||||
"stringUnit": {
|
||||
"state": "translated",
|
||||
"value": "語言"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"language.title": {
|
||||
"extractionState": "manual",
|
||||
"localizations": {
|
||||
"en": {
|
||||
"stringUnit": {
|
||||
"state": "translated",
|
||||
"value": "App language"
|
||||
}
|
||||
},
|
||||
"zh-Hans": {
|
||||
"stringUnit": {
|
||||
"state": "translated",
|
||||
"value": "软件语言"
|
||||
}
|
||||
},
|
||||
"zh-Hant": {
|
||||
"stringUnit": {
|
||||
"state": "translated",
|
||||
"value": "App 語言"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"language.system": {
|
||||
"extractionState": "manual",
|
||||
"localizations": {
|
||||
"en": {
|
||||
"stringUnit": {
|
||||
"state": "translated",
|
||||
"value": "Follow system"
|
||||
}
|
||||
},
|
||||
"zh-Hans": {
|
||||
"stringUnit": {
|
||||
"state": "translated",
|
||||
"value": "跟随系统"
|
||||
}
|
||||
},
|
||||
"zh-Hant": {
|
||||
"stringUnit": {
|
||||
"state": "translated",
|
||||
"value": "跟隨系統"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"settings.title": {
|
||||
"extractionState": "manual",
|
||||
"localizations": {
|
||||
"en": {
|
||||
"stringUnit": {
|
||||
"state": "translated",
|
||||
"value": "Kymo Settings"
|
||||
}
|
||||
},
|
||||
"zh-Hans": {
|
||||
"stringUnit": {
|
||||
"state": "translated",
|
||||
"value": "Kymo 设置"
|
||||
}
|
||||
},
|
||||
"zh-Hant": {
|
||||
"stringUnit": {
|
||||
"state": "translated",
|
||||
"value": "Kymo 設定"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"version": "1.0"
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
import Foundation
|
||||
|
||||
/// Native AppKit labels do not consume SwiftUI's locale environment.
|
||||
nonisolated enum AppLocalization {
|
||||
static func string(_ key: String, locale: Locale, bundle: Bundle = .main) -> String {
|
||||
let identifier = Bundle.preferredLocalizations(from: AppLanguage.supported,
|
||||
forPreferences: [locale.identifier]).first ?? "en"
|
||||
guard let path = bundle.path(forResource: identifier, ofType: "lproj"),
|
||||
let localized = Bundle(path: path) else {
|
||||
return bundle.localizedString(forKey: key, value: nil, table: nil)
|
||||
}
|
||||
return localized.localizedString(forKey: key, value: nil, table: nil)
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ import AppKit
|
||||
|
||||
/// A non-activating child window: outside the parent layout, below no other app.
|
||||
struct HoverDetailPresenter<Detail: View>: NSViewRepresentable {
|
||||
@Environment(\.locale) private var locale
|
||||
let isPresented: Bool
|
||||
let detail: Detail
|
||||
let onHover: (Bool) -> Void
|
||||
@@ -10,7 +11,7 @@ struct HoverDetailPresenter<Detail: View>: NSViewRepresentable {
|
||||
|
||||
func makeNSView(context: Context) -> AnchorView { AnchorView() }
|
||||
func updateNSView(_ view: AnchorView, context: Context) {
|
||||
view.configure(presented: isPresented, detail: AnyView(detail), onHover: onHover, onDismiss: onDismiss)
|
||||
view.configure(presented: isPresented, detail: AnyView(detail.environment(\.locale, locale)), onHover: onHover, onDismiss: onDismiss)
|
||||
}
|
||||
static func dismantleNSView(_ view: AnchorView, coordinator: ()) { view.detach() }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user