Files
Sprachmittler/App/Settings/SettingsView.swift
T
schihei 41f1cea769 feat(panel): optional reverse direction for target-language text
Add opt-in automatic reverse translation for a two-language pair. autoSwitchTarget defaults on with fallbackTargetLanguage English (or German when the target is English); the saved target and the effective request target are separate. Confident source-equals-target text uses the fallback, the header and a panel banner show the effective direction, and the banner can override the direction for one capture without changing the saved target. The language picker still changes the saved target, and changing the target to the fallback swaps the pair. JSON/HTML/code handling is unchanged.\n\nFilter near-zero NLLanguageRecognizer hypotheses so the existing high-confidence same-language rule works for real text. Add resolver, settings, view-model, detector and persistence tests; document ADR-022, the panel, settings and user guide. Verified: make verify green; German text with target German/fallback English renders German → English with the banner, English text stays English → German, a real click on the override shows German → German, and a new German capture switches back to English.
2026-11-26 17:34:11 +01:00

324 lines
13 KiB
Swift

import SprachmittlerCore
import SwiftUI
/// The Settings window: General, Model, Translation, Privacy and Diagnostics.
@MainActor
struct SettingsView: View {
@Bindable var model: SettingsViewModel
let onOpenDiagnostics: @MainActor () -> Void
let onSaved: @MainActor () -> Void
@State private var draft: AppSettings
@State private var endpointText: String
@State private var saveMessage: String?
@State private var showLanguagePicker = false
init(
model: SettingsViewModel,
onOpenDiagnostics: @escaping @MainActor () -> Void,
onSaved: @escaping @MainActor () -> Void = {}
) {
self.model = model
self.onOpenDiagnostics = onOpenDiagnostics
self.onSaved = onSaved
_draft = State(initialValue: model.draft)
_endpointText = State(initialValue: model.draft.endpoint.absoluteString)
}
var body: some View {
VStack(spacing: 0) {
TabView {
generalTab
.tabItem { Label(String(localized: "General"), systemImage: "gearshape") }
modelTab
.tabItem { Label(String(localized: "Model"), systemImage: "cpu") }
translationTab
.tabItem { Label(String(localized: "Translation"), systemImage: "text.bubble") }
privacyTab
.tabItem { Label(String(localized: "Privacy"), systemImage: "lock") }
diagnosticsTab
.tabItem { Label(String(localized: "Diagnostics"), systemImage: "stethoscope") }
}
.padding(16)
Divider()
HStack {
if let saveMessage {
Text(saveMessage)
.font(.callout)
.foregroundStyle(.red)
} else if let launchError = model.launchError {
Text(launchError)
.font(.callout)
.foregroundStyle(.orange)
}
Spacer()
Button(String(localized: "Reset")) {
model.reset()
draft = model.draft
endpointText = model.draft.endpoint.absoluteString
saveMessage = nil
onSaved()
}
Button(String(localized: "Save")) {
save()
}
.keyboardShortcut("s", modifiers: .command)
.buttonStyle(.borderedProminent)
}
.padding(12)
}
.frame(width: 660, height: 520)
}
private var generalTab: some View {
Form {
Section(String(localized: "Translation")) {
LabeledContent(String(localized: "Target language")) {
Button {
showLanguagePicker.toggle()
} label: {
HStack {
Text(draft.targetLanguageModel.localizedDisplayName)
Image(systemName: "chevron.up.chevron.down")
.font(.caption)
}
}
.popover(isPresented: $showLanguagePicker, arrowEdge: .bottom) {
LanguagePickerView(
selection: Binding(
get: { draft.targetLanguageModel },
set: { draft.targetLanguage = $0.code }
),
recent: draft.recentTargets
)
}
.accessibilityLabel(String(localized: "Target language"))
}
Toggle(String(localized: "Show source preview"), isOn: $draft.showSourcePreview)
Stepper(value: $draft.translationFontSize, in: 12 ... 28) {
LabeledContent(
String(localized: "Translation text size"),
value: "\(draft.translationFontSize) pt"
)
}
.accessibilityLabel(String(localized: "Translation text size"))
Toggle(String(localized: "Render Markdown formatting"), isOn: $draft.renderMarkdown)
.accessibilityLabel(String(localized: "Render Markdown formatting"))
Text(
String(
localized: """
Render bold, italic, headings, lists, code and links when the source is Markdown.
"""
)
)
.font(.caption)
.foregroundStyle(.secondary)
Toggle(String(localized: "Close with Esc without clicking"), isOn: $draft.closeWithEscape)
.accessibilityLabel(String(localized: "Close with Esc without clicking"))
Text(
String(
localized: "The panel takes keyboard focus while it is visible. The source app stays frontmost."
)
)
.font(.caption)
.foregroundStyle(.secondary)
FallbackTargetSettings(
autoSwitchTarget: $draft.autoSwitchTarget,
fallbackTargetLanguage: $draft.fallbackTargetLanguage,
targetLanguage: draft.targetLanguage, recentTargets: draft.recentTargets
)
}
Section(String(localized: "Double copy")) {
LabeledContent(String(localized: "Window: \(draft.doubleCopyWindowMs) ms")) {
Slider(
value: Binding(
get: { Double(draft.doubleCopyWindowMs) },
set: { draft.doubleCopyWindowMs = Int($0) }
),
in: 300 ... 1000,
step: 50
)
}
Text(String(localized: "Two identical copies inside this window trigger a translation."))
.font(.caption)
.foregroundStyle(.secondary)
}
Section(String(localized: "Startup")) {
Toggle(String(localized: "Launch at login"), isOn: $draft.launchAtLogin)
Toggle(String(localized: "Warm up the model on launch"), isOn: $draft.warmUpOnLaunch)
}
}
.formStyle(.grouped)
}
private var modelTab: some View {
Form {
Section(String(localized: "Ollama")) {
TextField(String(localized: "Endpoint"), text: $endpointText)
.accessibilityLabel(String(localized: "Ollama endpoint"))
Toggle(String(localized: "Allow a remote HTTPS endpoint"), isOn: $draft.allowRemoteEndpoint)
Button(String(localized: "Refresh installed models")) {
guard let endpoint = URL(string: endpointText) else {
saveMessage = String(localized: "Enter a valid endpoint URL.")
return
}
draft.endpoint = endpoint
model.draft = draft
Task {
await model.refreshModels()
draft.fastModel = model.detectedFastModel ?? draft.fastModel
}
}
}
Section(String(localized: "Model")) {
Picker(String(localized: "Translation model"), selection: $draft.model) {
ForEach(model.modelChoices, id: \.self) { tag in
Text(tag).tag(tag)
}
}
TextField(
String(localized: "Fast model tag (optional)"),
text: Binding(
get: { draft.fastModel ?? "" },
set: { draft.fastModel = $0.isEmpty ? nil : $0 }
)
)
Picker(String(localized: "Keep model loaded"), selection: $draft.keepModelLoaded) {
Text("5 minutes").tag("5m")
Text("30 minutes").tag("30m")
Text("1 hour").tag("1h")
Text("Unload after each request").tag("0")
}
if let digest = model.selectedDigest {
LabeledContent(String(localized: "Digest"), value: String(digest.prefix(12)))
}
Text(model.licenceNote)
.font(.caption)
.foregroundStyle(.secondary)
}
Section(String(localized: "Context and output")) {
Stepper(
String(localized: "Context: \(draft.numCtx)"),
value: $draft.numCtx,
in: 1024 ... 32768,
step: 1024
)
Stepper(
String(localized: "Maximum output: \(draft.numPredict)"),
value: $draft.numPredict,
in: 256 ... 8192,
step: 256
)
}
}
.formStyle(.grouped)
}
private var translationTab: some View {
Form {
Section(String(localized: "Sampling")) {
LabeledContent(String(localized: "Temperature")) {
TextField("", value: $draft.temperature, format: .number.precision(.fractionLength(0 ... 2)))
.frame(width: 80)
.accessibilityLabel(String(localized: "Temperature"))
}
LabeledContent(String(localized: "Top P")) {
TextField("", value: $draft.topP, format: .number.precision(.fractionLength(0 ... 2)))
.frame(width: 80)
.accessibilityLabel(String(localized: "Top P"))
}
Stepper(String(localized: "Top K: \(draft.topK)"), value: $draft.topK, in: 1 ... 100)
LabeledContent(String(localized: "Repetition penalty")) {
TextField("", value: $draft.repeatPenalty, format: .number.precision(.fractionLength(0 ... 2)))
.frame(width: 80)
.accessibilityLabel(String(localized: "Repetition penalty"))
}
}
Section(String(localized: "Chunking")) {
Stepper(
String(localized: "Latin limit: \(draft.maxCharactersLatin)"),
value: $draft.maxCharactersLatin,
in: 1000 ... 10000,
step: 500
)
Stepper(
String(localized: "Dense-script limit: \(draft.maxCharactersCJK)"),
value: $draft.maxCharactersCJK,
in: 500 ... 5000,
step: 250
)
}
Section(String(localized: "Prompt policy")) {
Text(String(localized: "Default translation is fixed for v1."))
Text(String(localized: "Content-aware prompt selection is decided in M7."))
.font(.caption)
.foregroundStyle(.secondary)
}
}
.formStyle(.grouped)
}
private var privacyTab: some View {
Form {
Section(String(localized: "Local and private")) {
Text(
String(
localized: "Selected text, translations, the clipboard and app names are never written to disk."
)
)
Text(String(localized: "No telemetry, no analytics and no accounts."))
Text(
String(
localized: "The clipboard is read only after a change; the last translation stays in memory."
)
)
}
Section(String(localized: "Permissions")) {
Text(String(localized: "Accessibility is optional and used only for Replace."))
Text(String(localized: "Input Monitoring, Screen Recording, Camera, Microphone and Apple Events"))
Text(String(localized: "are never requested."))
}
}
.formStyle(.grouped)
}
private var diagnosticsTab: some View {
Form {
Section {
Button(String(localized: "Open Diagnostics…")) {
onOpenDiagnostics()
}
Text(String(localized: "Versions, model digests, timings and error kinds only. No text."))
.font(.caption)
.foregroundStyle(.secondary)
}
}
.formStyle(.grouped)
}
private func save() {
guard let endpoint = URL(string: endpointText) else {
saveMessage = String(localized: "Enter a valid endpoint URL.")
return
}
draft.endpoint = endpoint
model.draft = draft
model.save()
if let error = model.saveError {
saveMessage = error
return
}
draft = model.draft
endpointText = draft.endpoint.absoluteString
saveMessage = nil
onSaved()
}
}