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.
53 lines
2.0 KiB
Swift
53 lines
2.0 KiB
Swift
import Foundation
|
|
|
|
/// A text replacement implementation. Accessibility direct replacement is preferred; the paste
|
|
/// fallback is the second strategy (`docs/SELECTION_AND_CLIPBOARD.md` §4).
|
|
public protocol TextInserting: Sendable {
|
|
/// Replaces the captured selection with `translation`.
|
|
func replace(
|
|
selection: CapturedText,
|
|
with translation: String,
|
|
target: InsertionTarget
|
|
) async throws -> InsertionOutcome
|
|
}
|
|
|
|
/// Captures the source app and, when Accessibility is trusted, the focused selection at trigger
|
|
/// time. Never reads a full document value.
|
|
@MainActor
|
|
public protocol InsertionTargetProviding {
|
|
/// Returns the target, or nil when no app is frontmost.
|
|
func captureTarget() -> InsertionTarget?
|
|
}
|
|
|
|
/// The safety checks shared by AX and paste insertion.
|
|
public enum InsertionPolicy {
|
|
/// Verifies the frontmost app and, when available, the selected text.
|
|
public static func verify(
|
|
frontmostPID: pid_t,
|
|
target: InsertionTarget,
|
|
selectedText: String?,
|
|
source: String
|
|
) throws {
|
|
guard frontmostPID == target.pid else {
|
|
throw SprachmittlerError.insertionTargetChanged
|
|
}
|
|
guard let selectedText else { return }
|
|
let expected = selectedText
|
|
.replacingOccurrences(of: "\r\n", with: "\n")
|
|
.replacingOccurrences(of: "\r", with: "\n")
|
|
.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
let actual = source
|
|
.replacingOccurrences(of: "\r\n", with: "\n")
|
|
.replacingOccurrences(of: "\r", with: "\n")
|
|
.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
guard expected == actual else {
|
|
throw SprachmittlerError.insertionTargetChanged
|
|
}
|
|
}
|
|
|
|
/// True when the text should not be replaced because a blocking warning exists.
|
|
public static func isBlocked(by warnings: [TranslationWarning]) -> Bool {
|
|
warnings.contains(where: \.blocksReplacement)
|
|
}
|
|
}
|