KMMainViewController+UI.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. let info : [AnyHashable : Any] = notification.userInfo ?? [:]
  12. if info.keys.contains(KMPreference.viewZoomScaleTypeKey) {
  13. self.selectZoom(KMPreferenceManager.shared.viewZoomScaleType)
  14. }
  15. if notification.name.rawValue == "pdfViewDocumentDidLoaded" || info.keys.contains(KMPreference.viewPageDisplayTypeKey) {
  16. }
  17. if info.keys.contains(KMPreference.displayBackgroundNormalColorKey) || notification.name.rawValue == "pdfViewDocumentDidLoaded" {
  18. self.listView.backgroundColor = KMPreferenceManager.shared.displayBackgroundNormalColor
  19. self.listView.layoutDocumentView()
  20. }
  21. if (info.keys.contains(KMPreference.generalAuthorNameKey)) { // 作者名称
  22. CPDFKitConfig.sharedInstance().setAnnotationAuthor((info[KMPreference.generalAuthorNameKey] as! String))
  23. }
  24. if (info.keys.contains(KMPreference.highlightLinksKey)) {
  25. let hlLink = info[KMPreference.highlightLinksKey] as? Bool
  26. CPDFKitConfig.sharedInstance().setEnableLinkFieldHighlight(hlLink == nil ? false : hlLink!)
  27. self.listView.setNeedsDisplayForVisiblePages()
  28. }
  29. if info.keys.contains(KMPreference.thumbPageSizeKey) {
  30. self.leftSideViewController.refreshUIOfThumbnailIfNeed(preference: true)
  31. }
  32. if info.keys.contains(KMPreference.thumbSnapshotSizeKey) {
  33. self.leftSideViewController.refreshUIOfSnapshotIfNeed(preference: true)
  34. }
  35. if info.keys.contains(KMPreference.outlineFontSizeKey) {
  36. self.leftSideViewController.updateTableFont()
  37. }
  38. if info.keys.contains(KMPreference.greekThresholdKey) {
  39. let value = KMPreference.shared.greekThreshold.cgFloat
  40. self.listView.setGreekingThreshold(value)
  41. }
  42. if info.keys.contains(KMPreference.antiAliasTextKey) {
  43. self.listView.setShouldAntiAlias(KMPreference.shared.antiAliasText)
  44. self.listView.applyDefaultInterpolationQuality()
  45. }
  46. if info.keys.contains(KMPreference.editPDFPopWindowShowKey) {
  47. let show = KMPreference.shared.editPDFPopWindowIsShow
  48. }
  49. }
  50. func convertNotesUsingPDFDocument(_ pdfDocument: CPDFDocument, callback: (()->Void)? = nil) {
  51. guard let doc = self.listView.document else {
  52. return
  53. }
  54. DispatchQueue.main.async {
  55. self.beginProgressSheet(withMessage: "", maxValue: 0)
  56. let count = pdfDocument.pageCount
  57. DispatchQueue.global().async {
  58. self.model.addAnnotations.removeAll()
  59. self.model.removeAnnotations.removeAll()
  60. for i in 0..<count {
  61. let page = pdfDocument.page(at: i)
  62. for annotation in page?.annotations ?? [] {
  63. var newAnnotation: CPDFAnnotation?
  64. if let inkAnnotation = annotation as? CPDFInkAnnotation, inkAnnotation.contents.hasPrefix("<?xml version=\"1.0\" encoding=\"utf-8\"?>") {
  65. let table = KMTableAnnotation(KMNoteBounds: inkAnnotation.bounds, document: doc)
  66. table.border = inkAnnotation.border
  67. table.color = inkAnnotation.color
  68. table.createForm(withList: inkAnnotation.contents, andPaths: inkAnnotation.bezierPaths())
  69. table.updateAppearanceInk(withIsAdd: false)
  70. table.contents = annotation.contents
  71. newAnnotation = table
  72. }
  73. if let newAnnotation = newAnnotation {
  74. self.model.addAnnotations.append(newAnnotation)
  75. self.model.removeAnnotations.append(annotation)
  76. }
  77. }
  78. }
  79. DispatchQueue.main.async {
  80. for i in 0..<self.model.addAnnotations.count {
  81. let newAnnotation = self.model.addAnnotations[i]
  82. let annotation = self.model.removeAnnotations[i]
  83. let page = annotation.page
  84. // this is only to make sure markup annotations generate the lineRects, for thread safety
  85. self.listView.addAnnotation(with: newAnnotation, to: page)
  86. self.listView.remove(annotation)
  87. if newAnnotation.contents != nil {
  88. if newAnnotation.contents.count == 0 {
  89. newAnnotation.autoUpdateString()
  90. }
  91. }
  92. }
  93. self.dismissProgressSheet()
  94. self.listView.undoManager?.removeAllActions()
  95. self.undoManager?.removeAllActions()
  96. // 清空数据
  97. self.model.addAnnotations.removeAll()
  98. self.model.removeAnnotations.removeAll()
  99. callback?()
  100. }
  101. }
  102. }
  103. }
  104. // MARK: - KMInterfaceThemeChangedProtocol
  105. override func interfaceThemeDidChanged(_ appearance: NSAppearance.Name) {
  106. super.interfaceThemeDidChanged(appearance)
  107. self.leftSideViewController.interfaceThemeDidChanged(appearance)
  108. self.pdfEditController?.interfaceThemeDidChanged(appearance)
  109. }
  110. }
  111. // MARK: - KMLeftSideViewControllerDelegate
  112. extension KMMainViewController: KMLeftSideViewControllerDelegate {
  113. func controlStateChange(_ obj: KMLeftSideViewController, show: Bool) {
  114. if show {
  115. self.openLeftPane()
  116. } else {
  117. self.closeLeftPane()
  118. }
  119. }
  120. func enterEditMode(_ obj: KMLeftSideViewController, _ pages: [Int]) {
  121. }
  122. func searchAction(searchString: String,isCase:Bool) {
  123. self.search(searchString: searchString,isCase: isCase)
  124. }
  125. func controller(controller: KMLeftSideViewController, itemClick item: Any?, itemKey: KMItemKey, params: Any?) {
  126. if (itemKey == .print) { // 打印
  127. if params is NSImage {
  128. KMPrintWindowController.cpdf_printImage(image: params as! NSImage)
  129. return
  130. }
  131. var pageRange: KMPrintPageRange = KMPrintPageRange(type: .allPage, selectPages: [], reversePrintOrder: false)
  132. if (params != nil) {
  133. pageRange.type = .custom
  134. pageRange.selectPages = params as! [Int]
  135. }
  136. self.showPrintWindow(pageRange: pageRange)
  137. } else if (itemKey == .cut || itemKey == .paste || itemKey == .delete || itemKey == .leftRotate || itemKey == .rightRotate) {
  138. self.recordIsPDFDocumentEdited(type: itemKey.toSubscribeWaterMarkType())
  139. } else if itemKey == .pageEdit {
  140. } else if itemKey == .demote { // 大纲降级
  141. guard let currentOutline: CPDFOutline = controller.tocOutlineView.km.clickedItem() else {
  142. return
  143. }
  144. self.listView.demote(outline: currentOutline)
  145. } else if itemKey == .promote { // 大纲升级
  146. guard let currentOutline: CPDFOutline = controller.tocOutlineView.km.clickedItem() else {
  147. return
  148. }
  149. self.listView.promote(outline: currentOutline)
  150. } else if itemKey == .tts {
  151. FMTrackEventManager.defaultManager.trackEvent(event: "SubTbr_PageDisplay", withProperties: ["SubTbr_Btn": "Btn_SubTbr_PageDisplay_TTS"])
  152. self.listView.annotationType = .unkown
  153. self.showTTSWindow()
  154. }
  155. }
  156. func controller(controller: KMLeftSideViewController, bookMarkDidChange bookMarks: [KMBookmarkItem]) {
  157. self.needSave = true
  158. self.recordIsPDFDocumentEdited()
  159. }
  160. func controller(controller: KMLeftSideViewController, rotateType: KMRotateType) {
  161. if rotateType == .clockwise {
  162. self.menuItemAction_rotateRight((Any).self)
  163. } else if rotateType == .anticlockwise {
  164. self.menuItemAction_rotateLeft((Any).self)
  165. }
  166. }
  167. func controller(controller: KMLeftSideViewController, listViewSelectionDidChange object: Any?, info: [String : Any]?) {
  168. if controller.thumbnailTableView.isEqual(to: object) {
  169. let row = controller.thumbnailTableView.selectedRow
  170. let curIndex = self.listView.currentPageIndex
  171. if (row != -1 && row != curIndex) {
  172. self.listView.go(toPageIndex: row, animated: true)
  173. }
  174. }
  175. }
  176. }
  177. //MARK: - ReadModel
  178. extension KMMainViewController {
  179. @objc func addOutLineItemAction() {
  180. let labelString = "\(KMLocalizedString("Page")) \((self.listView.currentPageIndex ?? 0) + 1)"
  181. let label = self.listView.currentSelection?.string() ?? labelString
  182. let dest = self.listView.currentDestination
  183. if let row = self.leftSideViewController.selectedRowIndexes().last {
  184. if let ol = self.leftSideViewController.tocOutlineView.item(atRow: row) as? CPDFOutline {
  185. _ = self.listView.addOutline(for: ol.parent, label: label, dest: dest, at: ol.index+1)
  186. } else {
  187. let idx = self.listView.document?.outlineRoot()?.numberOfChildren ?? 0
  188. _ = self.listView.addOutlineForRoot(label: label, dest:dest, at: idx)
  189. }
  190. } else {
  191. let idx = self.listView.document?.outlineRoot()?.numberOfChildren ?? 0
  192. _ = self.listView.addOutlineForRoot(label: label, dest:dest, at: idx)
  193. }
  194. }
  195. @objc func lookUpAction() {
  196. let label = self.listView.currentSelection?.string() ?? ""
  197. }
  198. @objc func searchBaiduAction() {
  199. let label = self.listView.currentSelection?.string() ?? ""
  200. let query = label.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? ""
  201. if let url = URL(string: "https://www.baidu.com/s?wd=\(query)") {
  202. NSWorkspace.shared.open(url)
  203. }
  204. }
  205. @objc func showInfoInFinder() {
  206. }
  207. @objc func NextPageAction() {
  208. }
  209. @objc func PreviousPageAction() {
  210. }
  211. @objc func TranslateItemAction() {
  212. // 获取选中的文本
  213. if let selection = self.listView.currentSelection?.string() {
  214. // 进行翻译
  215. let escapedText = selection.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
  216. let urlStr = "https://translate.google.com/?sl=auto&amp;tl=zh-CN&amp;text=\(escapedText)"
  217. if let url = URL(string: urlStr) {
  218. NSWorkspace.shared.open(url)
  219. }
  220. } else {
  221. print("No text selected.")
  222. }
  223. }
  224. @objc func AutoScrollItemAction() {
  225. //增加判断,如果是正在滚动,就停止,否则就开始滚动
  226. self.listView.autoFlow()
  227. }
  228. @objc func cutAction(sender: NSMenuItem) {
  229. }
  230. @objc func deleteAction(sender: NSMenuItem) {
  231. }
  232. @objc func ColorsItemAction(sender: NSMenuItem) {
  233. }
  234. @objc func LinesItemAction(sender: NSMenuItem) {
  235. }
  236. @objc func EditNoteItemAction(sender: NSMenuItem) {
  237. }
  238. @objc func AITranslateItemAction(sender: NSMenuItem) {
  239. self.showAITypeChooseView(aiConfigType: .translate)
  240. }
  241. @objc func AIProofreadItemAction(sender: NSMenuItem) {
  242. self.showAITypeChooseView(aiConfigType: .proofreading)
  243. }
  244. @objc func AIRewriteItemAction(sender: NSMenuItem) {
  245. self.showAITypeChooseView(aiConfigType: .reWriting)
  246. }
  247. }
  248. // MARK: - KMSnapshotWindowControllerDelegate
  249. extension KMMainViewController: KMSnapshotWindowControllerDelegate {
  250. func snapshotControllerWillClose(_ controller: KMSnapshotWindowController) {
  251. self.leftSideViewController.snapshotControllerWillClose(controller)
  252. }
  253. func snapshotController(_ controller: KMSnapshotWindowController, miniaturizedRect isMiniaturize: Bool) -> NSRect {
  254. if isMiniaturize && self.interactionMode != .presentation {
  255. if self.interactionMode != .legacyFullScreen && self.model.leftPanelOpen == false {
  256. self.toggleLeftPane()
  257. } else if self.interactionMode == .legacyFullScreen {
  258. }
  259. var row = NSNotFound
  260. for (i,sp) in self.leftSideViewController.snapshots.enumerated() {
  261. if controller.isEqual(sp.windowC) {
  262. row = i
  263. }
  264. }
  265. if (row != NSNotFound) {
  266. if self.leftSideViewController.type.methodType != .snapshot {
  267. self.leftSideViewController.leftView.segmentedControl.selectedSegment = 5
  268. } else {
  269. self.leftSideViewController.refreshUIOfSnapshotIfNeed()
  270. }
  271. self.leftSideViewController.snapshotTableView.scrollRowToVisible(row)
  272. }
  273. }
  274. return self.leftSideViewController.snapshotController(controller, miniaturizedRect: isMiniaturize)
  275. }
  276. func snapshotControllerDidFinishSetup(_ controller: KMSnapshotWindowController) {
  277. self.leftSideViewController.snapshotControllerDidFinishSetup(controller)
  278. }
  279. }
  280. // MARK: - CPDFDocumentDelegate
  281. extension KMMainViewController: CPDFDocumentDelegate {
  282. func documentDidBeginDocumentFind(_ document: CPDFDocument!) {
  283. self.leftSideViewController.documentDidBeginFind()
  284. }
  285. func documentDidEndDocumentFind(_ document: CPDFDocument!) {
  286. self.leftSideViewController.documentDidEndFind()
  287. }
  288. }