import AppKit import SprachmittlerCore import SwiftUI /// The translation panel view. It never activates the app. struct PanelView: View { @Bindable var model: PanelViewModel let onClose: @MainActor () -> Void @Environment(\.openSettings) private var openSettings @State private var showLanguagePicker = false @State private var showSource = true var body: some View { VStack(alignment: .leading, spacing: 10) { header if model.configuration.showSourcePreview, let source = model.source { sourcePreview(source) } translationArea warningArea errorArea footer } .padding(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) ) .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 { HStack(spacing: 10) { ZStack(alignment: .leading) { WindowDragArea() 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.configuration.target.localizedDisplayName)") .font(.caption) .foregroundStyle(.secondary) } } .allowsHitTesting(false) } .frame(height: 44) .contentShape(Rectangle()) .help(String(localized: "Drag to move panel")) .accessibilityLabel(String(localized: "Move panel")) Spacer() Button { showLanguagePicker.toggle() } label: { Label(model.configuration.target.localizedDisplayName, systemImage: "globe") .labelStyle(.titleAndIcon) } .accessibilityLabel(String(localized: "Target language")) .popover(isPresented: $showLanguagePicker, arrowEdge: .bottom) { LanguagePickerView( selection: Binding( get: { model.configuration.target }, set: { language in Task { await model.setTarget(language) } } ), recent: model.configuration.recentTargets ) } Button { openSettings() } label: { Image(systemName: "gearshape") } .accessibilityLabel(String(localized: "Settings")) } } 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")) } private var translationArea: some View { ScrollView { 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.configuration.target.isRTL ? .rightToLeft : .leftToRight ) .accessibilityLabel(String(localized: "Translation")) } .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")) } } } private 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.") } } @ViewBuilder private var warningArea: some View { 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.configuration.target.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) } } private var visibleWarnings: [TranslationWarning] { model.warnings.filter { warning in if case .sameLanguage = warning { return false } return true } } @ViewBuilder private 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 private 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() } } private var accessibilitySettingsURL: URL? { URL(string: "x-apple.systempreferences:com.apple.preference.security?Privacy_Accessibility") } private 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")) 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")) } } }