Files
schihei 940abcc996 feat(settings): mother-tongue language routing table
Replace the single reverse-direction pair with a general routing table. The saved target is the mother tongue and the default target for every source; languageRoutes is an empty-by-default, source-unique list of source-to-target rules (for example Chinese -> English, Traditional Chinese -> English, German -> English). Rules apply only when the source is confidently detected; otherwise the mother tongue is used. Same-language rules are dropped.\n\nThe panel shows the effective direction and a banner explaining a rule, with a one-capture override back to the mother tongue; the panel picker is now a session-only override. Persistent rules are edited in Settings. The old autoSwitchTarget/fallbackTargetLanguage keys are ignored, so the default after migration is everything -> mother tongue.\n\nAdd LanguageRoute, generalize the direction policy and tests, add the rules UI, and update ADR-022, architecture, translation output, user guide, README and progress. Verified: make verify green; empty rules translate French -> German, a Chinese -> English rule produces Chinese (Simplified) -> English with the banner, and the rules table renders in Settings.
2026-11-26 18:31:45 +01:00

132 lines
5.2 KiB
Swift

import AppKit
import SprachmittlerCore
import SprachmittlerText
import SwiftUI
/// The translation panel view. It never activates the app.
struct PanelView: View {
@Bindable var model: PanelViewModel
let onClose: @MainActor () -> Void
@Environment(\.openSettings) var openSettings
@State private var showLanguagePicker = false
@State private var showSource = true
var body: some View {
VStack(alignment: .leading, spacing: 0) {
header
VStack(alignment: .leading, spacing: 10) {
if model.configuration.showSourcePreview, let source = model.source {
sourcePreview(source)
}
translationArea
warningArea
errorArea
footer
}
.padding(.horizontal, 16)
.padding(.top, 10)
.padding(.bottom, 16)
}
.frame(width: 540)
.background(.regularMaterial, in: RoundedRectangle(cornerRadius: 14, style: .continuous))
.clipShape(RoundedRectangle(cornerRadius: 14, style: .continuous))
.overlay(
RoundedRectangle(cornerRadius: 14, style: .continuous)
.strokeBorder(.separator, lineWidth: 1)
)
.ignoresSafeArea(.container, edges: .top)
.onAppear { showSource = model.configuration.showSourcePreview }
.onChange(of: model.configuration.showSourcePreview) { _, newValue in
showSource = newValue
}
.onExitCommand(perform: onClose)
.accessibilityElement(children: .contain)
.accessibilityLabel(String(localized: "Translation panel"))
}
private var header: some View {
ZStack {
WindowDragArea()
Color.primary.opacity(0.04).allowsHitTesting(false)
HStack(spacing: 10) {
HStack(spacing: 10) {
Image(systemName: "line.3.horizontal")
.font(.system(size: 12, weight: .semibold))
.foregroundStyle(.secondary)
.frame(width: 24, height: 32)
.background(.quaternary, in: RoundedRectangle(cornerRadius: 7))
Image(systemName: "character.bubble")
.font(.title2)
.foregroundStyle(.tint)
VStack(alignment: .leading, spacing: 1) {
Text(String(localized: "Translate"))
.font(.headline)
Text("\(sourceLanguageLabel) → \(model.effectiveTarget.localizedDisplayName)")
.font(.caption)
.foregroundStyle(.secondary)
}
}
.allowsHitTesting(false)
.accessibilityElement(children: .combine)
.accessibilityLabel(String(localized: "Move panel"))
Spacer().allowsHitTesting(false)
Button {
showLanguagePicker.toggle()
} label: {
Label(model.effectiveTarget.localizedDisplayName, systemImage: "globe")
.labelStyle(.titleAndIcon)
}
.accessibilityLabel(String(localized: "Target language"))
.popover(isPresented: $showLanguagePicker, arrowEdge: .bottom) {
LanguagePickerView(
selection: Binding(
get: { model.effectiveTarget },
set: { language in
Task { await model.overrideTarget(language) }
}
),
recent: model.configuration.recentTargets
)
}
Button {
openSettings()
} label: {
Image(systemName: "gearshape")
}
.accessibilityLabel(String(localized: "Settings"))
}
.padding(.horizontal, 16)
}
.frame(height: 56)
.frame(maxWidth: .infinity)
.help(String(localized: "Drag to move panel"))
}
private var sourceLanguageLabel: String {
guard let code = model.sourceLanguage, let language = LanguageCatalogue.resolve(code) else {
return String(localized: "Detected language")
}
return language.localizedDisplayName
}
private func sourcePreview(_ source: CapturedText) -> some View {
DisclosureGroup(isExpanded: $showSource) {
ScrollView {
Text(source.text)
.font(.callout)
.textSelection(.enabled)
.frame(maxWidth: .infinity, alignment: .leading)
}
.frame(maxHeight: 100)
} label: {
Text(showSource ? String(localized: "Source") : String(localized: "Show source"))
.font(.caption.weight(.semibold))
.foregroundStyle(.secondary)
}
.help(showSource ? String(localized: "Hide source") : String(localized: "Show source"))
.accessibilityLabel(String(localized: "Source text"))
}
}