import AppKit import SprachmittlerCore import SwiftUI /// The first-run setup window. struct OnboardingView: View { @Bindable var model: OnboardingViewModel let onCopyCommand: @MainActor (String) -> Void let onFinished: @MainActor () -> Void @State private var showLanguagePicker = false @State private var errorMessage: String? var body: some View { VStack(alignment: .leading, spacing: 16) { VStack(alignment: .leading, spacing: 4) { Text(String(localized: "Set up Sprachmittler")) .font(.title2.bold()) Text(String(localized: "Local translation with Ollama. Nothing leaves this Mac.")) .foregroundStyle(.secondary) } checkRow( title: String(localized: "Ollama"), state: model.ollama, detail: ollamaDetail ) { Button(String(localized: "Check again")) { Task { await model.refresh() } } .controlSize(.small) if case .failed = model.ollama, let url = URL(string: "https://ollama.com/download") { Link(String(localized: "Download Ollama"), destination: url) .controlSize(.small) } } checkRow( title: String(localized: "Hy-MT3 model"), state: model.model, detail: modelDetail ) { Button(String(localized: "Refresh")) { Task { await model.refresh() } } .controlSize(.small) if case .failed = model.model { Button(String(localized: "Copy pull command")) { onCopyCommand(model.pullCommand) } .controlSize(.small) } } VStack(alignment: .leading, spacing: 6) { Text(String(localized: "Target language")) .font(.headline) Button { showLanguagePicker.toggle() } label: { HStack { Text(model.target.localizedDisplayName) Image(systemName: "chevron.up.chevron.down") .font(.caption) } } .popover(isPresented: $showLanguagePicker, arrowEdge: .bottom) { LanguagePickerView( selection: Binding( get: { model.target }, set: { model.setTarget($0) } ) ) } .accessibilityLabel(String(localized: "Target language")) } VStack(alignment: .leading, spacing: 6) { Text(String(localized: "Accessibility (optional)")) .font(.headline) Text(String(localized: "Only Replace needs it. Double copy, translation and Copy work without it.")) .font(.caption) .foregroundStyle(.secondary) HStack { if model.accessibilityTrusted { Label(String(localized: "Granted"), systemImage: "checkmark.circle.fill") .foregroundStyle(.green) } else { Button(String(localized: "Enable Accessibility")) { model.requestAccessibility() } Button(String(localized: "Open System Settings")) { model.openAccessibilitySettings() } } } } if let errorMessage { Text(errorMessage) .font(.callout) .foregroundStyle(.red) } Spacer() HStack { Button(String(localized: "Check again")) { Task { await model.refresh() } } Spacer() Button(String(localized: "Done")) { do { try model.complete() onFinished() } catch { errorMessage = String(localized: "Setup could not be saved.") } } .keyboardShortcut(.defaultAction) .buttonStyle(.borderedProminent) .disabled(!model.canFinish) } } .padding(24) .frame(width: 540, height: 560) .task { await model.refresh() } } private var ollamaDetail: String? { switch model.ollama { case let .ok(version): String(localized: "Running, version \(version)") case let .failed(message): message case .checking: String(localized: "Checking…") case .idle: nil } } private var modelDetail: String? { switch model.model { case let .ok(tags): tags case let .failed(message): message case .checking: String(localized: "Checking…") case .idle: nil } } private func checkRow( title: String, state: OnboardingCheck, detail: String?, @ViewBuilder actions: () -> some View ) -> some View { HStack(alignment: .top, spacing: 10) { Image(systemName: icon(for: state)) .foregroundStyle(color(for: state)) .font(.title3) VStack(alignment: .leading, spacing: 2) { Text(title).font(.headline) if let detail { Text(detail) .font(.callout) .foregroundStyle(.secondary) } } Spacer() HStack(spacing: 8) { actions() } } .padding(10) .background(.background.opacity(0.6), in: RoundedRectangle(cornerRadius: 10)) } private func icon(for state: OnboardingCheck) -> String { switch state { case .ok: "checkmark.circle.fill" case .failed: "xmark.octagon.fill" case .checking: "clock" case .idle: "circle" } } private func color(for state: OnboardingCheck) -> Color { switch state { case .ok: .green case .failed: .red case .checking: .secondary case .idle: .secondary } } } /// The onboarding window controller. @MainActor final class OnboardingWindowController { private let window: NSWindow init( model: OnboardingViewModel, onCopyCommand: @escaping @MainActor (String) -> Void, onFinished: @escaping @MainActor () -> Void ) { let window = NSWindow( contentRect: NSRect(x: 0, y: 0, width: 540, height: 560), styleMask: [.titled, .closable], backing: .buffered, defer: false ) self.window = window window.title = String(localized: "Set up Sprachmittler") window.isReleasedWhenClosed = false window.contentViewController = NSHostingController( rootView: OnboardingView( model: model, onCopyCommand: onCopyCommand, onFinished: { [weak window] in window?.close() onFinished() } ) ) window.center() } func show() { NSApp.activate(ignoringOtherApps: true) window.makeKeyAndOrderFront(nil) } }