import Darwin import Foundation import SprachmittlerCore /// Replaces the selection with Accessibility first and a clipboard paste as fallback. public actor InsertionCoordinator: TextInserting { private let pasteboard: any PasteboardAccessing private let accessibility: any AccessibilityTextEditing private let poster: any EventPosting private let delay: @Sendable (Duration) async throws -> Void private let ownWrite: @MainActor @Sendable () -> Void private let frontmostPID: @MainActor @Sendable () -> pid_t /// Creates the coordinator. public init( pasteboard: any PasteboardAccessing, accessibility: any AccessibilityTextEditing, poster: any EventPosting = CGEventPoster(), delay: @escaping @Sendable (Duration) async throws -> Void = { try await Task.sleep(for: $0) }, ownWrite: @escaping @MainActor @Sendable () -> Void, frontmostPID: @escaping @MainActor @Sendable () -> pid_t ) { self.pasteboard = pasteboard self.accessibility = accessibility self.poster = poster self.delay = delay self.ownWrite = ownWrite self.frontmostPID = frontmostPID } public func replace( selection: CapturedText, with translation: String, target: InsertionTarget ) async throws -> InsertionOutcome { guard target.pid != getpid() else { throw SprachmittlerError.insertionTargetChanged } let pid = await frontmostPID() try InsertionPolicy.verify( frontmostPID: pid, target: target, selectedText: nil, source: selection.text ) guard await accessibility.isTrusted() else { throw SprachmittlerError.accessibilityNotTrusted } if await accessibility.isSecureField(pid: target.pid) { throw SprachmittlerError.accessibilityUnsupported(app: target.bundleID) } if let selected = await accessibility.selectedText(pid: target.pid) { try InsertionPolicy.verify( frontmostPID: pid, target: target, selectedText: selected, source: selection.text ) if await accessibility.setSelectedText(pid: target.pid, to: translation) { return .replaced(using: .accessibility) } } return try await pasteFallback(selection: selection, translation: translation, target: target, pid: pid) } private func pasteFallback( selection: CapturedText, translation: String, target: InsertionTarget, pid: pid_t ) async throws -> InsertionOutcome { // Re-check the selection after a failed AX set before mutating the clipboard. if let selected = await accessibility.selectedText(pid: target.pid) { try InsertionPolicy.verify( frontmostPID: pid, target: target, selectedText: selected, source: selection.text ) } guard let snapshot = await pasteboard.snapshot() else { throw SprachmittlerError.pasteboardUnavailable } let written = await pasteboard.write(translation) await ownWrite() do { guard poster.postPaste(to: target.pid) else { throw SprachmittlerError.accessibilityUnsupported(app: target.bundleID) } try await delay(.milliseconds(300)) guard await pasteboard.restore(snapshot, onlyIfChangeCountMatches: written) else { throw SprachmittlerError.pasteboardRestoreFailed } } catch { // SP-9: restore even when the paste or the settle delay is cancelled/fails. _ = await pasteboard.restore(snapshot, onlyIfChangeCountMatches: written) throw error } return .replaced(using: .paste) } /// Whether Accessibility is currently available; the panel uses this for hints. public func isAccessibilityTrusted() async -> Bool { await accessibility.isTrusted() } }