Files
schihei a78572adc3 feat(m4-m6): session, panel, Replace, settings and onboarding
M4: TranslationSession actor with epochs/one-in-flight streaming, PanelViewModel, non-activating NSPanel, streaming panel UI, searchable/recent target languages, warnings/errors and Copy/Retry/Replace actions.

M5: InsertionCoordinator with target capture, AX direct replacement, secure-field/own-PID refusal, frontmost and selected-text verification, snapshot/⌘V/restore fallback, cancellation-safe restore and blocking-warning gate.

M6: versioned AppSettings/SettingsStore, onboarding for Ollama/model/target/Accessibility, General/Model/Translation/Privacy/Diagnostics settings, launch-at-login via SMAppService, status menu (target, copy/replace last, setup, diagnostics, about), text-free diagnostics and first-run setup.

Docs: PROGRESS M4–M6, ARCHITECTURE §10–§12, ADR-019/020, SELECTION_AND_CLIPBOARD and TESTING. Manual 10-app matrices, real AX/paste delivery, VoiceOver and focus checks remain open.

Verified: make verify green (format, strict lint, scripts, privacy, Debug+Release signatures, 217 package tests); app launches and a two-copy clipboard smoke test opens the panel.
2026-11-26 14:03:16 +01:00

232 lines
7.6 KiB
Swift

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)
}
}