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.
258 lines
9.5 KiB
Swift
258 lines
9.5 KiB
Swift
import AppKit
|
|
import SprachmittlerCore
|
|
import SprachmittlerText
|
|
import SwiftUI
|
|
|
|
extension PanelView {
|
|
private var rendersMarkdown: Bool {
|
|
!model.translation.isEmpty
|
|
&& model.configuration.renderMarkdown
|
|
&& MarkdownPresentation.shouldRender(
|
|
source: model.source?.text ?? "",
|
|
translation: model.translation
|
|
)
|
|
}
|
|
|
|
var translationArea: some View {
|
|
ScrollView {
|
|
translationContent
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
}
|
|
.frame(minHeight: 120, maxHeight: 260)
|
|
.padding(10)
|
|
.background(.background.opacity(0.6), in: RoundedRectangle(cornerRadius: 8))
|
|
.overlay(alignment: .topTrailing) {
|
|
if model.phase == .translating || model.phase == .streaming {
|
|
ProgressView()
|
|
.controlSize(.small)
|
|
.padding(8)
|
|
.accessibilityLabel(String(localized: "Translating"))
|
|
}
|
|
}
|
|
}
|
|
|
|
@ViewBuilder var translationContent: some View {
|
|
if rendersMarkdown {
|
|
MarkdownTranslationView(
|
|
markdown: model.translation,
|
|
fontSize: model.configuration.translationFontSize,
|
|
isRTL: model.effectiveTarget.isRTL
|
|
)
|
|
} else {
|
|
Text(translationText)
|
|
.font(.system(size: CGFloat(model.configuration.translationFontSize)))
|
|
.textSelection(.enabled)
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
.foregroundStyle(model.translation.isEmpty ? .secondary : .primary)
|
|
.environment(
|
|
\.layoutDirection,
|
|
model.effectiveTarget.isRTL ? .rightToLeft : .leftToRight
|
|
)
|
|
.accessibilityLabel(String(localized: "Translation"))
|
|
}
|
|
}
|
|
|
|
var translationText: String {
|
|
if !model.translation.isEmpty {
|
|
return model.translation
|
|
}
|
|
switch model.phase {
|
|
case .translating, .streaming:
|
|
return String(localized: "Translating…")
|
|
case .failed:
|
|
return String(localized: "No translation.")
|
|
case .idle, .ready:
|
|
return String(localized: "No translation yet.")
|
|
}
|
|
}
|
|
|
|
private func routedMessage(from routedSource: ModelLanguage) -> String {
|
|
let source = routedSource.localizedDisplayName
|
|
let target = model.effectiveTarget.localizedDisplayName
|
|
return String(localized: "Detected \(source). Translating to \(target).")
|
|
}
|
|
|
|
@ViewBuilder var warningArea: some View {
|
|
if let routedSource = model.routedSource {
|
|
HStack(alignment: .top, spacing: 8) {
|
|
Image(systemName: "arrow.left.arrow.right.circle.fill")
|
|
.foregroundStyle(.blue)
|
|
.accessibilityHidden(true)
|
|
Text(routedMessage(from: routedSource))
|
|
.font(.callout)
|
|
Spacer()
|
|
Button(String(localized: "Translate to \(model.configuration.target.localizedDisplayName)")) {
|
|
Task { await model.overrideTarget(model.configuration.target) }
|
|
}
|
|
.controlSize(.small)
|
|
}
|
|
.padding(8)
|
|
.background(.blue.opacity(0.08), in: RoundedRectangle(cornerRadius: 8))
|
|
}
|
|
|
|
if model.sameLanguageWarning != nil {
|
|
HStack(alignment: .top, spacing: 8) {
|
|
Image(systemName: "info.circle.fill").foregroundStyle(.blue)
|
|
Text("This text already appears to be in \(model.effectiveTarget.localizedDisplayName).")
|
|
.font(.callout)
|
|
Spacer()
|
|
Button(String(localized: "Translate anyway")) {
|
|
model.dismissSameLanguageWarning()
|
|
}
|
|
.controlSize(.small)
|
|
}
|
|
.padding(8)
|
|
.background(.blue.opacity(0.08), in: RoundedRectangle(cornerRadius: 8))
|
|
}
|
|
|
|
ForEach(Array(visibleWarnings.enumerated()), id: \.offset) { _, warning in
|
|
HStack(alignment: .top, spacing: 8) {
|
|
Image(systemName: warning.isBlocking ? "exclamationmark.triangle.fill" : "exclamationmark.circle")
|
|
.foregroundStyle(warning.isBlocking ? .orange : .secondary)
|
|
Text(warning.panelMessage)
|
|
.font(.callout)
|
|
Spacer()
|
|
}
|
|
.padding(8)
|
|
.background(
|
|
(warning.isBlocking ? Color.orange : Color.secondary)
|
|
.opacity(0.08),
|
|
in: RoundedRectangle(cornerRadius: 8)
|
|
)
|
|
.accessibilityLabel(warning.panelMessage)
|
|
}
|
|
}
|
|
|
|
var visibleWarnings: [TranslationWarning] {
|
|
model.warnings.filter { warning in
|
|
if case .sameLanguage = warning {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
}
|
|
|
|
@ViewBuilder var errorArea: some View {
|
|
if let message = model.errorMessage, model.phase == .failed {
|
|
HStack(alignment: .top, spacing: 8) {
|
|
Image(systemName: "exclamationmark.octagon.fill")
|
|
.foregroundStyle(.red)
|
|
VStack(alignment: .leading, spacing: 6) {
|
|
Text(message)
|
|
.font(.callout)
|
|
if let action = model.recoveryAction {
|
|
recoveryButton(action)
|
|
}
|
|
}
|
|
Spacer()
|
|
}
|
|
.padding(8)
|
|
.background(.red.opacity(0.08), in: RoundedRectangle(cornerRadius: 8))
|
|
.accessibilityLabel(message)
|
|
}
|
|
}
|
|
|
|
@ViewBuilder func recoveryButton(_ action: RecoveryAction) -> some View {
|
|
switch action {
|
|
case .startOllama:
|
|
if let url = URL(string: "https://ollama.com/download") {
|
|
Link(String(localized: "Download Ollama"), destination: url)
|
|
.controlSize(.small)
|
|
}
|
|
case let .pullModel(modelName):
|
|
Button(String(localized: "Copy pull command")) {
|
|
model.copy("ollama pull \(modelName)")
|
|
}
|
|
.controlSize(.small)
|
|
case .openSettings:
|
|
if model.error == .accessibilityNotTrusted, let url = accessibilitySettingsURL {
|
|
Button(String(localized: "Open Accessibility Settings")) {
|
|
NSWorkspace.shared.open(url)
|
|
}
|
|
.controlSize(.small)
|
|
} else {
|
|
Button(String(localized: "Open Settings")) {
|
|
openSettings()
|
|
}
|
|
.controlSize(.small)
|
|
}
|
|
case .copyInstead:
|
|
Button(String(localized: "Copy instead")) {
|
|
model.copyTranslation()
|
|
}
|
|
.controlSize(.small)
|
|
case .shortenText:
|
|
EmptyView()
|
|
case .retry:
|
|
Button(String(localized: "Retry")) {
|
|
Task { await model.retry() }
|
|
}
|
|
.controlSize(.small)
|
|
case .none:
|
|
EmptyView()
|
|
}
|
|
}
|
|
|
|
var accessibilitySettingsURL: URL? {
|
|
URL(string: "x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility")
|
|
}
|
|
|
|
var footer: some View {
|
|
HStack(spacing: 10) {
|
|
Button {
|
|
model.copyTranslation()
|
|
} label: {
|
|
Label(
|
|
model.didCopy ? String(localized: "Copied") : String(localized: "Copy"),
|
|
systemImage: "doc.on.doc"
|
|
)
|
|
}
|
|
.keyboardShortcut("c", modifiers: .command)
|
|
.disabled(!model.canCopy)
|
|
.accessibilityLabel(String(localized: "Copy translation"))
|
|
|
|
Button {
|
|
Task { await model.retry() }
|
|
} label: {
|
|
Label(String(localized: "Retry"), systemImage: "arrow.clockwise")
|
|
}
|
|
.keyboardShortcut("r", modifiers: .command)
|
|
.disabled(model.source == nil)
|
|
.accessibilityLabel(String(localized: "Retry translation"))
|
|
|
|
if let current = model.currentChunk, let total = model.totalChunks, total > 1 {
|
|
Text(String(localized: "Part \(current) of \(total)"))
|
|
.font(.caption)
|
|
.foregroundStyle(.secondary)
|
|
.accessibilityLabel(String(localized: "Part \(current) of \(total)"))
|
|
} else if model.chunkCount > 1 {
|
|
Text(String(localized: "\(model.chunkCount) parts"))
|
|
.font(.caption)
|
|
.foregroundStyle(.secondary)
|
|
.accessibilityLabel(String(localized: "Translated in \(model.chunkCount) parts"))
|
|
}
|
|
|
|
Spacer()
|
|
|
|
Button {
|
|
onClose()
|
|
} label: {
|
|
Text(String(localized: "Close"))
|
|
}
|
|
.keyboardShortcut(.cancelAction)
|
|
.accessibilityLabel(String(localized: "Close panel"))
|
|
|
|
Button {
|
|
Task { await model.replaceSelection() }
|
|
} label: {
|
|
Text(String(localized: "Replace"))
|
|
}
|
|
.keyboardShortcut(.return, modifiers: .command)
|
|
.buttonStyle(.borderedProminent)
|
|
.disabled(!model.canReplace)
|
|
.help(model.replaceUnavailableReason ?? String(localized: "Replace the selection"))
|
|
.accessibilityLabel(String(localized: "Replace selection"))
|
|
}
|
|
}
|
|
}
|