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

113 lines
3.5 KiB
Swift

import AppKit
import Combine
import os
import SprachmittlerCore
/// Lifecycle glue: composition root, Services provider, watcher, panel and windows.
@MainActor
final class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject {
let environment: AppEnvironment
private let loggers = Loggers(subsystem: Bundle.main.bundleIdentifier ?? "Sprachmittler")
@Published private(set) var menuState = StatusMenuState(
targetCode: "",
hasTranslation: false,
canReplace: false
)
private lazy var onboardingWindow = OnboardingWindowController(
model: environment.onboarding,
onCopyCommand: { [weak self] text in self?.environment.copyText(text) },
onFinished: { [weak self] in
self?.environment.settingsDidChange()
self?.refreshMenuState()
}
)
private lazy var diagnosticsWindow = DiagnosticsWindowController(
model: environment.diagnosticsModel,
onCopy: { [weak self] text in self?.environment.copyText(text) }
)
override init() {
environment = AppEnvironment()
super.init()
refreshMenuState()
}
func applicationDidFinishLaunching(_ notification: Notification) {
loggers.logger(.session).notice("Sprachmittler launched")
environment.onStateChange = { [weak self] in self?.refreshMenuState() }
environment.start { [weak self] captured in
self?.handle(captured)
}
NSApp.servicesProvider = environment.servicesProvider
NSUpdateDynamicServices()
if !environment.settingsStore.current().hasCompletedOnboarding {
showOnboarding()
}
}
func applicationWillTerminate(_ notification: Notification) {
environment.stop()
}
/// Menu action: translate the current clipboard text.
func captureClipboardText() {
environment.captureClipboardText()
}
/// Menu action: change the target language.
func selectTarget(_ language: TargetLanguage) {
environment.selectTarget(language)
refreshMenuState()
}
/// Menu action: copy the last translation.
func copyLastTranslation() {
environment.copyLastTranslation()
}
/// Menu action: replace the last selection with the last translation.
func replaceLastTranslation() {
environment.replaceLastTranslation()
}
/// Menu action: open setup.
func showOnboarding() {
onboardingWindow.show()
}
/// Menu action: open diagnostics.
func showDiagnostics() {
diagnosticsWindow.show()
}
/// Menu action: About and licence notice.
func showAbout() {
NSApp.activate(ignoringOtherApps: true)
NSApp.orderFrontStandardAboutPanel(options: [
.applicationName: "Sprachmittler",
.credits: NSAttributedString(
string: "Local translation with Hy-MT3. No text leaves this Mac.\n"
+ "Hy-MT3 is Apache-2.0 per the model card; Sprachmittler bundles no weights."
),
])
}
private func handle(_ captured: CapturedText) {
loggers.logger(.capture).notice(
"Captured \(captured.text.count) characters from \(captured.source.rawValue, privacy: .public)"
)
refreshMenuState()
}
private func refreshMenuState() {
menuState = StatusMenuState(
targetCode: environment.currentTarget.code,
hasTranslation: environment.lastTranslation != nil,
canReplace: environment.canReplaceLast
)
}
}