Files
Sprachmittler/App/Settings/SettingsView.swift
T
schihei 2a601058f4 feat(capture): translate long selections in parts
The capture gate and the chunk size were the same number, so a selection above one chunk was rejected before the pipeline could split it; the Settings sliders moved both thresholds together. Add maximumPartsPerSelection (1-8, default 1, so existing behaviour is unchanged). The capture limit is now part size x parts and travels on TranslationSessionRequest, so the watcher, CapturePolicy and TranslationSession agree for that capture. TranslationStreaming gained an optional per-part callback; the session emits chunkStarted and the panel shows 'Part 2 of 4' while streaming and '2 parts' when finished. textTooLong now points at Settings and offers an Open Settings button. Lower the dense-script part-size maximum to 4000 because 5000 dense characters plus a 4096-token output can overflow the 8192-token context. Tests cover capture limits, watcher limits, pipeline part callbacks, session events and limits, panel progress, settings clamping and migration. Docs updated: ADR-011, TRANSLATION_OUTPUT, SELECTION_AND_CLIPBOARD, ARCHITECTURE, LANGUAGES and README. Verified: make verify green; live 4355-character German text showed Part 1 of 2, Part 2 of 2 and '2 parts'; with one part the panel showed the too-long message and Open Settings.
2026-11-27 20:21:30 +01:00

310 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: "Mother tongue")) {
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: "Mother tongue"))
}
Text(String(localized: "The default target for every language unless a rule matches."))
.font(.caption)
.foregroundStyle(.secondary)
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)
LanguageRulesSettings(routes: $draft.languageRoutes, motherTongue: draft.targetLanguageModel)
}
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"))
}
}
LongSelectionsSettings(draft: $draft)
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()
}
}