KMMainViewController+UI.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //
  2. // KMMainViewController+UI.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2022/12/15.
  6. //
  7. import Foundation
  8. extension KMMainViewController {
  9. //通知
  10. func preferenceDidChangeNotification(notification:Notification) {
  11. }
  12. func convertNotesUsingPDFDocument(_ pdfDocument: CPDFDocument, callback: (()->Void)? = nil) {
  13. guard let doc = self.listView.document else {
  14. return
  15. }
  16. DispatchQueue.main.async {
  17. self.beginProgressSheet(withMessage: "", maxValue: 0)
  18. let count = pdfDocument.pageCount
  19. DispatchQueue.global().async {
  20. self.model.addAnnotations.removeAll()
  21. self.model.removeAnnotations.removeAll()
  22. for i in 0..<count {
  23. let page = pdfDocument.page(at: i)
  24. for annotation in page?.annotations ?? [] {
  25. var newAnnotation: CPDFAnnotation?
  26. if let inkAnnotation = annotation as? CPDFInkAnnotation, inkAnnotation.contents.hasPrefix("<?xml version=\"1.0\" encoding=\"utf-8\"?>") {
  27. let table = KMTableAnnotation(KMNoteBounds: inkAnnotation.bounds, document: doc)
  28. table.border = inkAnnotation.border
  29. table.color = inkAnnotation.color
  30. table.createForm(withList: inkAnnotation.contents, andPaths: inkAnnotation.bezierPaths())
  31. table.updateAppearanceInk(withIsAdd: false)
  32. table.contents = annotation.contents
  33. newAnnotation = table
  34. }
  35. if let newAnnotation = newAnnotation {
  36. self.model.addAnnotations.append(newAnnotation)
  37. self.model.removeAnnotations.append(annotation)
  38. }
  39. }
  40. }
  41. DispatchQueue.main.async {
  42. for i in 0..<self.model.addAnnotations.count {
  43. let newAnnotation = self.model.addAnnotations[i]
  44. let annotation = self.model.removeAnnotations[i]
  45. let page = annotation.page
  46. // this is only to make sure markup annotations generate the lineRects, for thread safety
  47. self.listView.addAnnotation(with: newAnnotation, to: page)
  48. self.listView.remove(annotation)
  49. if newAnnotation.contents != nil {
  50. if newAnnotation.contents.count == 0 {
  51. newAnnotation.autoUpdateString()
  52. }
  53. }
  54. }
  55. self.dismissProgressSheet()
  56. self.listView.undoManager?.removeAllActions()
  57. self.undoManager?.removeAllActions()
  58. // 清空数据
  59. self.model.addAnnotations.removeAll()
  60. self.model.removeAnnotations.removeAll()
  61. callback?()
  62. }
  63. }
  64. }
  65. }
  66. // MARK: - KMInterfaceThemeChangedProtocol
  67. override func interfaceThemeDidChanged(_ appearance: NSAppearance.Name) {
  68. super.interfaceThemeDidChanged(appearance)
  69. }
  70. }
  71. //MARK: - ReadModel
  72. extension KMMainViewController {
  73. @objc func addOutLineItemAction() {
  74. }
  75. @objc func lookUpAction() {
  76. let label = self.listView.currentSelection?.string() ?? ""
  77. }
  78. @objc func searchBaiduAction() {
  79. let label = self.listView.currentSelection?.string() ?? ""
  80. let query = label.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? ""
  81. if let url = URL(string: "https://www.baidu.com/s?wd=\(query)") {
  82. NSWorkspace.shared.open(url)
  83. }
  84. }
  85. @objc func showInfoInFinder() {
  86. }
  87. @objc func NextPageAction() {
  88. }
  89. @objc func PreviousPageAction() {
  90. }
  91. @objc func TranslateItemAction() {
  92. // 获取选中的文本
  93. if let selection = self.listView.currentSelection?.string() {
  94. // 进行翻译
  95. let escapedText = selection.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
  96. let urlStr = "https://translate.google.com/?sl=auto&amp;tl=zh-CN&amp;text=\(escapedText)"
  97. if let url = URL(string: urlStr) {
  98. NSWorkspace.shared.open(url)
  99. }
  100. } else {
  101. print("No text selected.")
  102. }
  103. }
  104. @objc func AutoScrollItemAction() {
  105. //增加判断,如果是正在滚动,就停止,否则就开始滚动
  106. self.listView.autoFlow()
  107. }
  108. @objc func cutAction(sender: NSMenuItem) {
  109. }
  110. @objc func deleteAction(sender: NSMenuItem) {
  111. }
  112. @objc func ColorsItemAction(sender: NSMenuItem) {
  113. }
  114. @objc func LinesItemAction(sender: NSMenuItem) {
  115. }
  116. @objc func EditNoteItemAction(sender: NSMenuItem) {
  117. }
  118. @objc func AITranslateItemAction(sender: NSMenuItem) {
  119. self.showAITypeChooseView(aiConfigType: .translate)
  120. }
  121. @objc func AIProofreadItemAction(sender: NSMenuItem) {
  122. self.showAITypeChooseView(aiConfigType: .proofreading)
  123. }
  124. @objc func AIRewriteItemAction(sender: NSMenuItem) {
  125. self.showAITypeChooseView(aiConfigType: .reWriting)
  126. }
  127. }
  128. // MARK: - KMSnapshotWindowControllerDelegate
  129. extension KMMainViewController: KMSnapshotWindowControllerDelegate {
  130. func snapshotControllerWillClose(_ controller: KMSnapshotWindowController) {
  131. }
  132. func snapshotController(_ controller: KMSnapshotWindowController, miniaturizedRect isMiniaturize: Bool) -> NSRect {
  133. return CGRectZero
  134. }
  135. func snapshotControllerDidFinishSetup(_ controller: KMSnapshotWindowController) {
  136. }
  137. }
  138. // MARK: - CPDFDocumentDelegate
  139. extension KMMainViewController: CPDFDocumentDelegate {
  140. func documentDidBeginDocumentFind(_ document: CPDFDocument!) {
  141. }
  142. func documentDidEndDocumentFind(_ document: CPDFDocument!) {
  143. }
  144. }