Repository navigation
Expand file tree
/
Copy pathServiceProvider.swift
More file actions
45 lines (40 loc) · 1.68 KB
/
Copy pathServiceProvider.swift
File metadata and controls
45 lines (40 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import AppKit
/// Entry point for the macOS Services menu.
///
/// The selector MUST match `NSMessage` in Info.plist. For `NSMessage =
/// "readSelectedText"`, the runtime invokes `readSelectedText:userData:error:`,
/// which maps to the Swift method below. Do not rename one without the other.
final class ServiceProvider: NSObject {
@objc func readSelectedText(
_ pboard: NSPasteboard,
userData: String?,
error: AutoreleasingUnsafeMutablePointer<NSString>?
) {
guard let text = pboard.string(forType: .string), !text.isEmpty else {
Log.service.error("Service fired but pasteboard held no plain text")
error?.pointee = "No selectable text was provided." as NSString
return
}
Log.service.info("Service fired: received \(text.count) chars")
Task { @MainActor in
AppDelegate.shared?.handleSelectedText(text)
}
}
/// Second Services entry ("코드 해설"). NSMessage = "explainSelectedText" maps
/// to this selector. Explains the selected code, then speaks it end-to-end.
@objc func explainSelectedText(
_ pboard: NSPasteboard,
userData: String?,
error: AutoreleasingUnsafeMutablePointer<NSString>?
) {
guard let text = pboard.string(forType: .string), !text.isEmpty else {
Log.service.error("Explain service fired but pasteboard held no plain text")
error?.pointee = "No selectable text was provided." as NSString
return
}
Log.service.info("Explain service fired: received \(text.count) chars")
Task { @MainActor in
AppDelegate.shared?.handleCodeText(text)
}
}
}