KMMainViewController+UI.swift 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. self.pdfEditController?.interfaceThemeDidChanged(appearance)
  70. }
  71. }
  72. //MARK: - ReadModel
  73. extension KMMainViewController {
  74. @objc func addOutLineItemAction() {
  75. }
  76. @objc func lookUpAction() {
  77. let label = self.listView.currentSelection?.string() ?? ""
  78. }
  79. @objc func searchBaiduAction() {
  80. let label = self.listView.currentSelection?.string() ?? ""
  81. let query = label.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? ""
  82. if let url = URL(string: "https://www.baidu.com/s?wd=\(query)") {
  83. NSWorkspace.shared.open(url)
  84. }
  85. }
  86. @objc func showInfoInFinder() {
  87. }
  88. @objc func NextPageAction() {
  89. }
  90. @objc func PreviousPageAction() {
  91. }
  92. @objc func TranslateItemAction() {
  93. // 获取选中的文本
  94. if let selection = self.listView.currentSelection?.string() {
  95. // 进行翻译
  96. let escapedText = selection.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
  97. let urlStr = "https://translate.google.com/?sl=auto&amp;tl=zh-CN&amp;text=\(escapedText)"
  98. if let url = URL(string: urlStr) {
  99. NSWorkspace.shared.open(url)
  100. }
  101. } else {
  102. print("No text selected.")
  103. }
  104. }
  105. @objc func AutoScrollItemAction() {
  106. //增加判断,如果是正在滚动,就停止,否则就开始滚动
  107. self.listView.autoFlow()
  108. }
  109. @objc func cutAction(sender: NSMenuItem) {
  110. }
  111. @objc func deleteAction(sender: NSMenuItem) {
  112. }
  113. @objc func ColorsItemAction(sender: NSMenuItem) {
  114. }
  115. @objc func LinesItemAction(sender: NSMenuItem) {
  116. }
  117. @objc func EditNoteItemAction(sender: NSMenuItem) {
  118. }
  119. @objc func AITranslateItemAction(sender: NSMenuItem) {
  120. self.showAITypeChooseView(aiConfigType: .translate)
  121. }
  122. @objc func AIProofreadItemAction(sender: NSMenuItem) {
  123. self.showAITypeChooseView(aiConfigType: .proofreading)
  124. }
  125. @objc func AIRewriteItemAction(sender: NSMenuItem) {
  126. self.showAITypeChooseView(aiConfigType: .reWriting)
  127. }
  128. }
  129. // MARK: - KMSnapshotWindowControllerDelegate
  130. extension KMMainViewController: KMSnapshotWindowControllerDelegate {
  131. func snapshotControllerWillClose(_ controller: KMSnapshotWindowController) {
  132. }
  133. func snapshotController(_ controller: KMSnapshotWindowController, miniaturizedRect isMiniaturize: Bool) -> NSRect {
  134. return CGRectZero
  135. }
  136. func snapshotControllerDidFinishSetup(_ controller: KMSnapshotWindowController) {
  137. }
  138. }
  139. // MARK: - CPDFDocumentDelegate
  140. extension KMMainViewController: CPDFDocumentDelegate {
  141. func documentDidBeginDocumentFind(_ document: CPDFDocument!) {
  142. }
  143. func documentDidEndDocumentFind(_ document: CPDFDocument!) {
  144. }
  145. }