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.configuration.target.localizedDisplayName)") .font(.caption) .foregroundStyle(.secondary) } } .allowsHitTesting(false) .accessibilityElement(children: .combine) .accessibilityLabel(String(localized: "Move panel")) Spacer().allowsHitTesting(false) 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")) } .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")) } }