OCRFileView.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. import Cocoa
  2. import UniformTypeIdentifiers
  3. import Vision
  4. // MARK: - OCR Service
  5. enum OCRError: LocalizedError {
  6. case invalidImage
  7. case recognitionFailed
  8. case noTextFound
  9. var errorDescription: String? {
  10. switch self {
  11. case .invalidImage:
  12. return "The selected file could not be opened as an image."
  13. case .recognitionFailed:
  14. return "Text recognition failed. Please try another image."
  15. case .noTextFound:
  16. return "No text was found in this image."
  17. }
  18. }
  19. }
  20. enum OCRFileService {
  21. private static let photoExtensions: Set<String> = [
  22. "jpg", "jpeg", "png", "heic", "heif", "gif", "tiff", "tif", "bmp", "webp",
  23. ]
  24. static func present(from window: NSWindow?) {
  25. let panel = NSOpenPanel()
  26. panel.title = "OCR File"
  27. panel.message = "Select an image to extract text from."
  28. panel.prompt = "Select Image"
  29. panel.canChooseDirectories = false
  30. panel.canChooseFiles = true
  31. panel.allowsMultipleSelection = false
  32. panel.allowsOtherFileTypes = false
  33. panel.allowedContentTypes = photoExtensions.compactMap { UTType(filenameExtension: $0) }
  34. panel.directoryURL = FileManager.default.urls(for: .picturesDirectory, in: .userDomainMask).first
  35. let completion: (NSApplication.ModalResponse) -> Void = { response in
  36. guard response == .OK, let url = panel.url else { return }
  37. let hostWindow = window ?? NSApp.keyWindow
  38. guard let viewController = hostWindow?.contentViewController as? ViewController else { return }
  39. viewController.presentOCRFile(imageURL: url)
  40. }
  41. if let window = window ?? NSApp.keyWindow {
  42. panel.beginSheetModal(for: window, completionHandler: completion)
  43. } else if panel.runModal() == .OK {
  44. completion(.OK)
  45. }
  46. }
  47. static func recognizeText(from url: URL) async throws -> String {
  48. let accessed = url.startAccessingSecurityScopedResource()
  49. defer {
  50. if accessed { url.stopAccessingSecurityScopedResource() }
  51. }
  52. guard let image = NSImage(contentsOf: url),
  53. let cgImage = image.cgImage(forProposedRect: nil, context: nil, hints: nil) else {
  54. throw OCRError.invalidImage
  55. }
  56. return try await recognizeText(in: cgImage)
  57. }
  58. private static func recognizeText(in cgImage: CGImage) async throws -> String {
  59. try await withCheckedThrowingContinuation { continuation in
  60. let request = VNRecognizeTextRequest { request, error in
  61. if let error {
  62. continuation.resume(throwing: error)
  63. return
  64. }
  65. guard let observations = request.results as? [VNRecognizedTextObservation] else {
  66. continuation.resume(throwing: OCRError.recognitionFailed)
  67. return
  68. }
  69. let lines = observations.compactMap { $0.topCandidates(1).first?.string }
  70. let text = lines.joined(separator: "\n").trimmingCharacters(in: .whitespacesAndNewlines)
  71. guard !text.isEmpty else {
  72. continuation.resume(throwing: OCRError.noTextFound)
  73. return
  74. }
  75. continuation.resume(returning: text)
  76. }
  77. request.recognitionLevel = .accurate
  78. request.usesLanguageCorrection = true
  79. let handler = VNImageRequestHandler(cgImage: cgImage, options: [:])
  80. do {
  81. try handler.perform([request])
  82. } catch {
  83. continuation.resume(throwing: error)
  84. }
  85. }
  86. }
  87. }
  88. // MARK: - Overlay
  89. final class OCRFileOverlayView: NSView, AppearanceRefreshable {
  90. var onDismiss: (() -> Void)?
  91. private let imageURL: URL
  92. private let backButton = OCRFileToolbarButton(symbolName: "chevron.left", accessibilityLabel: "Back")
  93. private let titleLabel = NSTextField(labelWithString: "OCR File")
  94. private let textContainer = NSView()
  95. private let textScrollView = NSScrollView()
  96. private let textView = NSTextView()
  97. private let loadingIndicator = NSProgressIndicator()
  98. private let statusLabel = NSTextField(labelWithString: "Extracting text…")
  99. private let printButton = OCRFilePrintButton()
  100. init(imageURL: URL) {
  101. self.imageURL = imageURL
  102. super.init(frame: .zero)
  103. translatesAutoresizingMaskIntoConstraints = false
  104. setup()
  105. refreshAppearance()
  106. NotificationCenter.default.addObserver(
  107. self,
  108. selector: #selector(appearanceDidChange),
  109. name: .appearanceDidChange,
  110. object: nil
  111. )
  112. recognizeText()
  113. }
  114. deinit {
  115. NotificationCenter.default.removeObserver(self)
  116. }
  117. @objc private func appearanceDidChange() {
  118. refreshAppearance()
  119. }
  120. @available(*, unavailable)
  121. required init?(coder: NSCoder) { nil }
  122. func refreshAppearance() {
  123. layer?.backgroundColor = AppTheme.background.cgColor
  124. titleLabel.textColor = AppTheme.textPrimary
  125. textContainer.layer?.backgroundColor = AppTheme.cardBackground.cgColor
  126. textContainer.layer?.borderColor = AppTheme.paywallBorder.cgColor
  127. statusLabel.textColor = AppTheme.textSecondary
  128. backButton.refreshAppearance()
  129. printButton.refreshAppearance()
  130. }
  131. func present(in parent: NSView) {
  132. guard superview == nil else { return }
  133. parent.addSubview(self)
  134. NSLayoutConstraint.activate([
  135. leadingAnchor.constraint(equalTo: parent.leadingAnchor),
  136. trailingAnchor.constraint(equalTo: parent.trailingAnchor),
  137. topAnchor.constraint(equalTo: parent.topAnchor),
  138. bottomAnchor.constraint(equalTo: parent.bottomAnchor),
  139. ])
  140. alphaValue = 0
  141. NSAnimationContext.runAnimationGroup { context in
  142. context.duration = 0.2
  143. animator().alphaValue = 1
  144. }
  145. }
  146. func dismiss(animated: Bool = true) {
  147. let remove = { [weak self] in
  148. self?.removeFromSuperview()
  149. self?.onDismiss?()
  150. }
  151. guard animated else {
  152. remove()
  153. return
  154. }
  155. NSAnimationContext.runAnimationGroup({ context in
  156. context.duration = 0.15
  157. animator().alphaValue = 0
  158. }, completionHandler: remove)
  159. }
  160. private func setup() {
  161. wantsLayer = true
  162. titleLabel.font = AppTheme.semiboldFont(size: 18)
  163. titleLabel.alignment = .center
  164. titleLabel.translatesAutoresizingMaskIntoConstraints = false
  165. textContainer.wantsLayer = true
  166. textContainer.layer?.cornerRadius = AppTheme.contentPanelCornerRadius
  167. textContainer.layer?.borderWidth = 1.5
  168. textContainer.applyCardShadow()
  169. textContainer.translatesAutoresizingMaskIntoConstraints = false
  170. textScrollView.translatesAutoresizingMaskIntoConstraints = false
  171. textScrollView.hasVerticalScroller = true
  172. textScrollView.hasHorizontalScroller = false
  173. textScrollView.autohidesScrollers = true
  174. textScrollView.borderType = .noBorder
  175. textScrollView.drawsBackground = false
  176. textView.isRichText = false
  177. textView.isEditable = true
  178. textView.isSelectable = true
  179. textView.drawsBackground = false
  180. textView.textColor = AppTheme.textPrimary
  181. textView.font = AppTheme.regularFont(size: 14)
  182. textView.textContainerInset = NSSize(width: 16, height: 16)
  183. textView.isAutomaticQuoteSubstitutionEnabled = true
  184. textView.isAutomaticDashSubstitutionEnabled = true
  185. textView.isAutomaticTextReplacementEnabled = true
  186. textView.isHidden = true
  187. textScrollView.documentView = textView
  188. loadingIndicator.style = .spinning
  189. loadingIndicator.controlSize = .regular
  190. loadingIndicator.translatesAutoresizingMaskIntoConstraints = false
  191. loadingIndicator.startAnimation(nil)
  192. statusLabel.font = AppTheme.regularFont(size: 14)
  193. statusLabel.alignment = .center
  194. statusLabel.translatesAutoresizingMaskIntoConstraints = false
  195. backButton.translatesAutoresizingMaskIntoConstraints = false
  196. printButton.translatesAutoresizingMaskIntoConstraints = false
  197. printButton.isEnabled = false
  198. backButton.onClick = { [weak self] in self?.dismiss() }
  199. printButton.onClick = { [weak self] in self?.printText() }
  200. addSubview(backButton)
  201. addSubview(titleLabel)
  202. addSubview(textContainer)
  203. textContainer.addSubview(textScrollView)
  204. textContainer.addSubview(loadingIndicator)
  205. textContainer.addSubview(statusLabel)
  206. addSubview(printButton)
  207. NSLayoutConstraint.activate([
  208. backButton.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 24),
  209. backButton.topAnchor.constraint(equalTo: topAnchor, constant: 20),
  210. backButton.widthAnchor.constraint(equalToConstant: 40),
  211. backButton.heightAnchor.constraint(equalToConstant: 40),
  212. titleLabel.centerXAnchor.constraint(equalTo: centerXAnchor),
  213. titleLabel.centerYAnchor.constraint(equalTo: backButton.centerYAnchor),
  214. textContainer.leadingAnchor.constraint(equalTo: leadingAnchor, constant: AppTheme.contentPanelInset),
  215. textContainer.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -AppTheme.contentPanelInset),
  216. textContainer.topAnchor.constraint(equalTo: backButton.bottomAnchor, constant: 28),
  217. textContainer.bottomAnchor.constraint(equalTo: printButton.topAnchor, constant: -28),
  218. textScrollView.leadingAnchor.constraint(equalTo: textContainer.leadingAnchor, constant: 12),
  219. textScrollView.trailingAnchor.constraint(equalTo: textContainer.trailingAnchor, constant: -12),
  220. textScrollView.topAnchor.constraint(equalTo: textContainer.topAnchor, constant: 12),
  221. textScrollView.bottomAnchor.constraint(equalTo: textContainer.bottomAnchor, constant: -12),
  222. loadingIndicator.centerXAnchor.constraint(equalTo: textContainer.centerXAnchor),
  223. loadingIndicator.centerYAnchor.constraint(equalTo: textContainer.centerYAnchor, constant: -12),
  224. statusLabel.topAnchor.constraint(equalTo: loadingIndicator.bottomAnchor, constant: 12),
  225. statusLabel.leadingAnchor.constraint(equalTo: textContainer.leadingAnchor, constant: 24),
  226. statusLabel.trailingAnchor.constraint(equalTo: textContainer.trailingAnchor, constant: -24),
  227. printButton.centerXAnchor.constraint(equalTo: centerXAnchor),
  228. printButton.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -28),
  229. printButton.widthAnchor.constraint(greaterThanOrEqualToConstant: 220),
  230. ])
  231. }
  232. private func recognizeText() {
  233. Task { @MainActor in
  234. do {
  235. let text = try await OCRFileService.recognizeText(from: imageURL)
  236. showRecognizedText(text)
  237. } catch {
  238. showRecognitionError(error)
  239. }
  240. }
  241. }
  242. private func showRecognizedText(_ text: String) {
  243. loadingIndicator.stopAnimation(nil)
  244. loadingIndicator.isHidden = true
  245. statusLabel.isHidden = true
  246. textView.string = text
  247. textView.isHidden = false
  248. printButton.isEnabled = true
  249. window?.makeFirstResponder(textView)
  250. }
  251. private func showRecognitionError(_ error: Error) {
  252. loadingIndicator.stopAnimation(nil)
  253. loadingIndicator.isHidden = true
  254. statusLabel.isHidden = true
  255. let alert = NSAlert()
  256. alert.messageText = "OCR Failed"
  257. alert.informativeText = error.localizedDescription
  258. alert.alertStyle = .warning
  259. alert.addButton(withTitle: "OK")
  260. alert.beginSheetModal(for: window ?? NSApp.keyWindow ?? NSWindow()) { [weak self] _ in
  261. self?.dismiss()
  262. }
  263. }
  264. private func printText() {
  265. PrintService.printText(textView.string, title: "OCR File", from: window)
  266. }
  267. }
  268. // MARK: - Toolbar Button
  269. private final class OCRFileToolbarButton: NSControl, AppearanceRefreshable {
  270. var onClick: (() -> Void)?
  271. private let symbolName: String
  272. private let iconView = NSImageView()
  273. init(symbolName: String, accessibilityLabel: String) {
  274. self.symbolName = symbolName
  275. super.init(frame: .zero)
  276. toolTip = accessibilityLabel
  277. wantsLayer = true
  278. layer?.cornerRadius = 12
  279. if let image = NSImage(systemSymbolName: symbolName, accessibilityDescription: accessibilityLabel) {
  280. let config = NSImage.SymbolConfiguration(pointSize: 15, weight: .semibold)
  281. iconView.image = image.withSymbolConfiguration(config)
  282. }
  283. iconView.translatesAutoresizingMaskIntoConstraints = false
  284. addSubview(iconView)
  285. NSLayoutConstraint.activate([
  286. iconView.centerXAnchor.constraint(equalTo: centerXAnchor),
  287. iconView.centerYAnchor.constraint(equalTo: centerYAnchor),
  288. ])
  289. refreshAppearance()
  290. }
  291. @available(*, unavailable)
  292. required init?(coder: NSCoder) { nil }
  293. func refreshAppearance() {
  294. layer?.backgroundColor = AppTheme.elevatedBackground.cgColor
  295. layer?.borderColor = AppTheme.paywallBorder.cgColor
  296. layer?.borderWidth = 1.5
  297. iconView.contentTintColor = AppTheme.textPrimary
  298. }
  299. override func mouseUp(with event: NSEvent) {
  300. guard bounds.contains(convert(event.locationInWindow, from: nil)) else { return }
  301. onClick?()
  302. }
  303. override func resetCursorRects() {
  304. addCursorRect(bounds, cursor: .pointingHand)
  305. }
  306. }
  307. // MARK: - Print Button
  308. private final class OCRFilePrintButton: NSControl, AppearanceRefreshable {
  309. var onClick: (() -> Void)?
  310. private let titleLabel = NSTextField(labelWithString: "Print Text")
  311. private let iconView = NSImageView()
  312. private var hoverTracker: HoverTracker?
  313. private var isHovered = false
  314. private var isEnabledState = true
  315. override var isEnabled: Bool {
  316. get { isEnabledState }
  317. set {
  318. isEnabledState = newValue
  319. alphaValue = newValue ? 1 : 0.45
  320. }
  321. }
  322. init() {
  323. super.init(frame: .zero)
  324. wantsLayer = true
  325. layer?.cornerRadius = 22
  326. titleLabel.font = AppTheme.semiboldFont(size: 16)
  327. titleLabel.textColor = .white
  328. titleLabel.translatesAutoresizingMaskIntoConstraints = false
  329. if let image = NSImage(systemSymbolName: "printer.fill", accessibilityDescription: "Print") {
  330. let config = NSImage.SymbolConfiguration(pointSize: 14, weight: .semibold)
  331. iconView.image = image.withSymbolConfiguration(config)
  332. }
  333. iconView.contentTintColor = .white
  334. iconView.translatesAutoresizingMaskIntoConstraints = false
  335. addSubview(titleLabel)
  336. addSubview(iconView)
  337. NSLayoutConstraint.activate([
  338. heightAnchor.constraint(equalToConstant: 52),
  339. titleLabel.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 28),
  340. titleLabel.centerYAnchor.constraint(equalTo: centerYAnchor),
  341. iconView.leadingAnchor.constraint(equalTo: titleLabel.trailingAnchor, constant: 10),
  342. iconView.centerYAnchor.constraint(equalTo: centerYAnchor),
  343. iconView.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -28),
  344. iconView.widthAnchor.constraint(equalToConstant: 20),
  345. iconView.heightAnchor.constraint(equalToConstant: 20),
  346. ])
  347. hoverTracker = HoverTracker(view: self) { [weak self] hovering in
  348. self?.setHovered(hovering)
  349. }
  350. refreshAppearance()
  351. }
  352. @available(*, unavailable)
  353. required init?(coder: NSCoder) { nil }
  354. func refreshAppearance() {
  355. let color = isHovered
  356. ? AppTheme.blue.blended(withFraction: 0.15, of: .black) ?? AppTheme.blue
  357. : AppTheme.blue
  358. layer?.backgroundColor = color.cgColor
  359. titleLabel.textColor = .white
  360. iconView.contentTintColor = .white
  361. }
  362. private func setHovered(_ hovering: Bool) {
  363. guard isEnabledState else { return }
  364. isHovered = hovering
  365. animateHover {
  366. let color = hovering
  367. ? AppTheme.blue.blended(withFraction: 0.15, of: .black) ?? AppTheme.blue
  368. : AppTheme.blue
  369. layer?.backgroundColor = color.cgColor
  370. layer?.transform = hovering
  371. ? CATransform3DMakeScale(1.03, 1.03, 1)
  372. : CATransform3DIdentity
  373. }
  374. }
  375. override func mouseUp(with event: NSEvent) {
  376. guard isEnabledState else { return }
  377. guard bounds.contains(convert(event.locationInWindow, from: nil)) else { return }
  378. onClick?()
  379. }
  380. override func resetCursorRects() {
  381. guard isEnabledState else { return }
  382. addCursorRect(bounds, cursor: .pointingHand)
  383. }
  384. }