import AppKit import Foundation import SprachmittlerCore /// The Services entry point (`docs/SELECTION_AND_CLIPBOARD.md` ยง6). /// /// The system supplies the pasteboard, so this path needs no Accessibility permission and no /// polling. `AppDelegate` installs this object as `NSApp.servicesProvider`. /// /// `@unchecked Sendable` is safe: the only stored value is an immutable `@Sendable` closure, and /// AppKit invokes the selector on the main thread. public final class ServicesProvider: NSObject, @unchecked Sendable { /// The captured-text handler. The app hops to the main actor itself. public typealias Handler = @Sendable (CapturedText) -> Void private let handler: Handler /// Creates a provider. public init(handler: @escaping Handler) { self.handler = handler super.init() } /// The `NSMessage = translateSelection` selector from Info.plist. @objc @MainActor public func translateSelection( _ pasteboard: NSPasteboard, userData: String, error: AutoreleasingUnsafeMutablePointer ) { guard !SystemPasteboard.containsOwnWriteMarkers(in: pasteboard), let text = pasteboard.string(forType: .string), !text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { error.pointee = "No text was selected." as NSString return } handler(CapturedText(text: text, capturedAt: Date(), source: .services)) } }