KMLeftSideViewController+Action.swift 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. //
  2. // KMLeftSideViewController+Action.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/12/22.
  6. //
  7. import Foundation
  8. // MARK: - Action
  9. extension KMLeftSideViewController {
  10. @IBAction func leftSideViewMoreButtonAction(_ sender: AnyObject?) {
  11. guard let button = sender as? NSButton else {
  12. NSSound.beep()
  13. return
  14. }
  15. let tag = button.tag
  16. if (tag == 300) {
  17. var selectedRow = 0
  18. if (self.snapshotTableView.selectedRow >= 0) {
  19. selectedRow = self.snapshotTableView.selectedRow;
  20. } else {
  21. return
  22. }
  23. let model = self.snapshots[selectedRow]
  24. let controller = model.windowC
  25. let menu = NSMenu()
  26. let itemExport = menu.addItem(title: KMLocalizedString("Export", "Menu item title"), action: nil, target: self)
  27. let subMenu = NSMenu()
  28. var t = subMenu.addItem(title: KMLocalizedString("PNG", "Menu item title"), action: #selector(menuItemClick_ExportPNG), target: self)
  29. t?.representedObject = controller
  30. t = subMenu.addItem(title: KMLocalizedString("JPG", "Menu item title"), action: #selector(menuItemClick_ExportJPG), target: self)
  31. t?.representedObject = controller
  32. t = subMenu.addItem(title: KMLocalizedString("PDF", "Menu item title"), action: #selector(menuItemClick_ExportPDF), target: self)
  33. t?.representedObject = controller
  34. itemExport?.submenu = subMenu
  35. let itemPrint = menu.addItem(title: KMLocalizedString("Print", "Menu item title"), action: #selector(menuItemClick_Print), target: self)
  36. itemPrint?.representedObject = controller
  37. menu.addItem(.separator())
  38. let itemSelectAll = menu.addItem(title: KMLocalizedString("Select All", "Menu item title"), action: #selector(menuItemClick_SelectAll), target: self)
  39. itemSelectAll?.representedObject = controller
  40. menu.addItem(.separator())
  41. let itemDeleteAllSnapshot = menu.addItem(title: KMLocalizedString("Delete All Snapshots", "Menu item title"), action: #selector(deleteAllSnapshot), target: self)
  42. itemDeleteAllSnapshot?.representedObject = controller
  43. if let data = NSApp.currentEvent {
  44. NSMenu.popUpContextMenu(menu, with: data, for: button)
  45. }
  46. } else if (tag == 302) {
  47. let menu = NSMenu()
  48. let expandAllCommentsItem = menu.addItem(title: KMLocalizedString("Expand All", nil), action: #selector(toc_expandAllComments), target: self)
  49. expandAllCommentsItem?.representedObject = self.tocOutlineView
  50. let foldAllCommentsItem = menu.addItem(title: KMLocalizedString("Collapse All", nil), action: #selector(toc_foldAllComments), target: self)
  51. expandAllCommentsItem?.representedObject = self.tocOutlineView
  52. let item = self.outlineRoot()
  53. var num = 0
  54. for i in 0 ..< Int(item?.numberOfChildren ?? 0) {
  55. let outline = item?.child(at: UInt(i))
  56. if self.tocOutlineView.isItemExpanded(outline) {
  57. num += 1
  58. }
  59. }
  60. if let cnt = item?.numberOfChildren, cnt > 0 && num == 0 {
  61. self.tocType = .fold
  62. } else if let cnt = item?.numberOfChildren, cnt > 0 && num == cnt {
  63. self.tocType = .unfold
  64. } else {
  65. self.tocType = .none
  66. }
  67. if (self.tocType == .unfold) {
  68. expandAllCommentsItem?.state = .on
  69. foldAllCommentsItem?.state = .off
  70. } else if (self.tocType == .fold) {
  71. expandAllCommentsItem?.state = .off
  72. foldAllCommentsItem?.state = .on
  73. } else {
  74. expandAllCommentsItem?.state = .off
  75. foldAllCommentsItem?.state = .off
  76. }
  77. let removeEntryItem = menu.addItem(title: KMLocalizedString("Remove All Outlines", nil), action: #selector(leftSideEmptyAnnotationClick_DeleteOutline), target: self)
  78. removeEntryItem?.representedObject = self.tocOutlineView
  79. if let data = NSApp.currentEvent {
  80. NSMenu.popUpContextMenu(menu, with: data, for: button)
  81. }
  82. } else if (tag == 304) {
  83. let menu = NSMenu()
  84. let object = KMPopupMenuObject()
  85. object.menuTag = 1001
  86. menu.delegate = object
  87. object.enterControllerCallback = { [weak self] isEnter in
  88. if (isEnter) {
  89. self?.moreButtonLayer?.isHidden = false
  90. } else {
  91. self?.moreButtonLayer?.isHidden = true
  92. }
  93. }
  94. let expandAllCommentsItem = menu.addItem(title: KMLocalizedString("Expand All", nil), action: #selector(note_expandAllComments), target: self)
  95. expandAllCommentsItem?.representedObject = self.noteOutlineView
  96. let foldAllCommentsItem = menu.addItem(title: KMLocalizedString("Collapse All", nil), action: #selector(note_foldAllComments), target: self)
  97. foldAllCommentsItem?.representedObject = self.noteOutlineView
  98. if (self.foldType == .unfold) {
  99. expandAllCommentsItem?.state = .on
  100. foldAllCommentsItem?.state = .off
  101. } else if (self.foldType == .fold) {
  102. expandAllCommentsItem?.state = .off
  103. foldAllCommentsItem?.state = .on
  104. } else {
  105. expandAllCommentsItem?.state = .off
  106. foldAllCommentsItem?.state = .off
  107. }
  108. let showAnnotationItem = menu.addItem(title: KMLocalizedString("Show Note", nil), action: nil, target: self)
  109. let subMenu = NSMenu()
  110. var t = subMenu.addItem(title: KMLocalizedString("Page", nil), action: #selector(noteShowNoteAction), target: self)
  111. let pageKey = self.noteTypeDict[Self.Key.noteFilterPage] as? Bool ?? false
  112. if pageKey {
  113. t?.state = .off
  114. } else {
  115. t?.state = .on
  116. }
  117. t?.representedObject = self.noteOutlineView
  118. t?.tag = 101
  119. t = subMenu.addItem(title: KMLocalizedString("Time", nil) , action: #selector(noteShowNoteAction), target: self)
  120. let timeKey = self.noteTypeDict[Self.Key.noteFilterTime] as? Bool ?? false
  121. if timeKey {
  122. t?.state = .off
  123. } else {
  124. t?.state = .on
  125. }
  126. t?.representedObject = self.noteOutlineView
  127. t?.tag = 102
  128. t = subMenu.addItem(title: KMLocalizedString("Author", nil) , action: #selector(noteShowNoteAction), target: self)
  129. let authorKey = self.noteTypeDict[Self.Key.noteFilterAuther] as? Bool ?? false
  130. if authorKey {
  131. t?.state = .off
  132. } else {
  133. t?.state = .on
  134. }
  135. t?.representedObject = self.noteOutlineView
  136. t?.tag = 103
  137. showAnnotationItem?.submenu = subMenu
  138. menu.addItem(.separator())
  139. let exportAnnotationsItem = menu.addItem(title: NSLocalizedString("Export Annotations…", comment: ""), action: nil, target: self)
  140. let subMenu2 = NSMenu()
  141. var t2 = subMenu2.addItem(title: NSLocalizedString("PDF", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  142. t2?.representedObject = self.noteOutlineView
  143. t2?.tag = 0
  144. t2 = subMenu2.addItem(title: NSLocalizedString("PDF Bundle", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  145. t2?.representedObject = self.noteOutlineView
  146. t2?.tag = 1
  147. t2 = subMenu2.addItem(title: NSLocalizedString("PDF Reader Pro Edition Notes", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  148. t2?.representedObject = self.noteOutlineView
  149. t2?.tag = 2
  150. t2 = subMenu2.addItem(title: NSLocalizedString("Notes as Text", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  151. t2?.representedObject = self.noteOutlineView
  152. t2?.tag = 3
  153. t2 = subMenu2.addItem(title: NSLocalizedString("Notes as RTF", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  154. t2?.representedObject = self.noteOutlineView
  155. t2?.tag = 4
  156. t2 = subMenu2.addItem(title: NSLocalizedString("Notes as RTFD", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  157. t2?.representedObject = self.noteOutlineView
  158. t2?.tag = 5
  159. t2 = subMenu2.addItem(title: NSLocalizedString("Notes as FDF", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  160. t2?.representedObject = self.noteOutlineView
  161. t2?.tag = 6
  162. exportAnnotationsItem?.submenu = subMenu2
  163. menu.addItem(.separator())
  164. let removeAllAnnotationsItem = menu.addItem(title: NSLocalizedString("Remove All Annotations", comment: ""), action: #selector(leftSideEmptyAnnotationClick_DeleteAnnotation), target: self)
  165. removeAllAnnotationsItem?.representedObject = self.noteOutlineView
  166. if let data = NSApp.currentEvent {
  167. NSMenu.popUpContextMenu(menu, with: data, for: button, with: nil)
  168. }
  169. }
  170. }
  171. func searchFieldChangeAction(_ editContent: String) {
  172. if self.findState == .note {
  173. self.updateNoteFilterPredicate()
  174. } else {
  175. self.updateSnapshotFilterPredicate()
  176. }
  177. if editContent.count > 0 {
  178. let findPboard = NSPasteboard(name: .find)
  179. findPboard.clearContents()
  180. findPboard.writeObjects([editContent as NSPasteboardWriting])
  181. }
  182. }
  183. @IBAction func search(_ sender: NSSearchField) {
  184. if sender.stringValue.isEmpty {
  185. self.applySearchTableHeader("")
  186. }
  187. self.delegate?.searchAction?(searchString: sender.stringValue, isCase: self.mwcFlags.caseInsensitiveSearch == 1)
  188. }
  189. // RefreshUI
  190. public func refreshUIForAddAnnotation(annos: [CPDFAnnotation]?, page: CPDFPage?) {
  191. self.updateThumbnail(at: Int(page?.pageIndex() ?? 0))
  192. self.note_reloadDataIfNeed()
  193. }
  194. public func refreshUIForAnnoAttributeDidChange(_ anno: CPDFAnnotation?, attributes: [String : Any]?) {
  195. if let data = anno {
  196. self.note_reloadDataForAnnoIfNeed(anno: data)
  197. }
  198. }
  199. public func refreshUIForDocumentChanged() {
  200. if self.type.methodType == .Thumbnail {
  201. self.resetThumbnails(ks: false)
  202. } else if self.type.methodType == .Outline {
  203. Task { @MainActor in
  204. self.tocOutlineView.reloadData()
  205. }
  206. } else if self.type.methodType == .Annotation {
  207. self.note_reloadDataIfNeed()
  208. } else if self.type.methodType == .snapshot {
  209. self.reloadSnapshotDataIfNeed()
  210. } else if self.type.methodType == .Search {
  211. if self.searchField.stringValue.isEmpty == false {
  212. self.search(self.searchField)
  213. }
  214. }
  215. }
  216. }
  217. // MARK: - Double Action
  218. extension KMLeftSideViewController {
  219. @objc func toggleSelectedSnapshots(_ sender: AnyObject?) {
  220. let indexs = self.snapshotTableView.selectedRowIndexes
  221. if indexs.isEmpty {
  222. return
  223. }
  224. let model = self.snapshots[indexs.last!]
  225. let windowC = model.windowC
  226. if let data = windowC?.window?.isVisible, data {
  227. windowC?.miniaturize()
  228. } else {
  229. windowC?.deminiaturize()
  230. }
  231. var rowIndexSet = IndexSet()
  232. let row = self.snapshotTableView.selectedRow
  233. if row >= 0 && row < self.snapshots.count {
  234. rowIndexSet.insert(row)
  235. }
  236. var columnIndexSet = IndexSet()
  237. columnIndexSet.insert(0)
  238. self.snapshotTableView.reloadData(forRowIndexes: rowIndexSet, columnIndexes: columnIndexSet)
  239. }
  240. // MARK: - KMInterfaceThemeChangedProtocol
  241. override func interfaceThemeDidChanged(_ appearance: NSAppearance.Name) {
  242. super.interfaceThemeDidChanged(appearance)
  243. self.updateViewColor()
  244. self.leftView.interfaceThemeDidChanged(appearance)
  245. }
  246. }
  247. // MARK: - NSMenuDelegate
  248. extension KMLeftSideViewController: NSMenuDelegate {
  249. func menuNeedsUpdate(_ menu: NSMenu) {
  250. menu.removeAllItems()
  251. if menu.isEqual(to: self.tocOutlineView.menu) {
  252. self.outlineListMenu(menu)
  253. return
  254. }
  255. var item: NSMenuItem?
  256. if menu.isEqual(to: self.thumbnailTableView.menu) {
  257. let row = self.thumbnailTableView.clickedRow
  258. if self.pdfDocument()?.documentURL == nil || self.thumbnailTableView.selectedRowIndexes.contains(row) == false{
  259. return
  260. }
  261. let isLocked = self.isLocked()
  262. let pageCount = self.pageCount()
  263. if (row != -1 && isLocked == false) {
  264. if(self.thumbnailTableView.selectedRowIndexes.count == pageCount) {
  265. item = menu.addItem(title: KMLocalizedString("Cut", "Menu item title"), action: nil, target: self)
  266. } else {
  267. item = menu.addItem(title: KMLocalizedString("Cut", "Menu item title"), action: #selector(cutPage), target: self)
  268. }
  269. item?.representedObject = IndexSet(integer: row)
  270. item = menu.addItem(title: KMLocalizedString("Copy", "Menu item title"), action: #selector(copyPage), target: self)
  271. item?.representedObject = IndexSet(integer: row)
  272. if (self.copyPages.count > 0) {
  273. item = menu.addItem(title: KMLocalizedString("Paste", "Menu item title"), action: #selector(pastePage), target: self)
  274. }else{
  275. item = menu.addItem(title: KMLocalizedString("Paste", "Menu item title"), action: nil, target: self)
  276. }
  277. item?.representedObject = IndexSet(integer: row)
  278. menu.addItem(.separator())
  279. if(self.thumbnailTableView.selectedRowIndexes.count == pageCount) {
  280. item = menu.addItem(title: KMLocalizedString("Delete", "Menu item title"), action: nil, target: self)
  281. } else {
  282. item = menu.addItem(title: KMLocalizedString("Delete", "Menu item title"), action: #selector(deletePage), target: self)
  283. }
  284. item?.representedObject = IndexSet(integer: row)
  285. menu.addItem(.separator())
  286. item = menu.addItem(title: KMLocalizedString("Rotate", "Menu item title"), action: #selector(rotatePageMenuAction), target: self)
  287. item?.representedObject = IndexSet(integer: row)
  288. item = menu.addItem(title: KMLocalizedString("Insert", "Menu item title"), action: nil, target: self)
  289. let subMenu = NSMenu()
  290. _ = subMenu.addItem(title: KMLocalizedString("Blank Page", "Menu item title"), action: #selector(quickInsert), target: self)
  291. _ = subMenu.addItem(title: KMLocalizedString("Blank Page - Custom...", "Menu item title"), action: #selector(insert), target: self)
  292. _ = subMenu.addItem(title: KMLocalizedString("From PDF", "Menu item title"), action: #selector(insertPDF), target: self)
  293. item?.submenu = subMenu
  294. item?.representedObject = IndexSet(integer: row)
  295. item = menu.addItem(title: KMLocalizedString("Extract", "Menu item title"), action: #selector(extractPage), target: self)
  296. item?.representedObject = IndexSet(integer: row)
  297. item = menu.addItem(title: KMLocalizedString("Page Edit", "Menu item title"), action: #selector(pageEdit), target: self)
  298. menu.addItem(.separator())
  299. var displayStr = ""
  300. if (self.isDisplayPageSize) {
  301. displayStr = KMLocalizedString("Hide Page Size", "Menu item title")
  302. } else {
  303. displayStr = KMLocalizedString("Display Page Size", "Menu item title")
  304. }
  305. item = menu.addItem(title: displayStr, action: #selector(displayPageSize), target: self)
  306. item?.representedObject = IndexSet(integer: row)
  307. if let doct = self.pdfDocument() {
  308. item = menu.addItem(title: KMLocalizedString("Share", "Menu item title"), action: nil, target: self)
  309. var tName = self.fileNameWithSelectedPages(self.thumbnailTableView.selectedRowIndexes)
  310. if (tName.count > 50) {
  311. tName = tName.substring(to: 50)
  312. }
  313. item?.submenu = NSSharingServicePicker.menu(forSharingItems: [doct.documentURL as Any], subjectContext: tName, withTarget: self, selector: #selector(sharePage), serviceDelegate: nil)
  314. }
  315. }
  316. } else if menu.isEqual(to: self.findTableView.menu) {
  317. var rowIndexes = self.findTableView.selectedRowIndexes
  318. let row = self.findTableView.clickedRow
  319. if (row != -1) {
  320. if rowIndexes.contains(row) == false {
  321. rowIndexes = IndexSet(integer: row)
  322. }
  323. var selections: [CPDFSelection] = []
  324. for (i, data) in self.searchResults.enumerated() {
  325. if rowIndexes.contains(i) {
  326. selections.append(data.selection)
  327. }
  328. }
  329. let hideNotes = self.hideNotes()
  330. let allowsNotes = self.allowsNotes()
  331. if hideNotes == false && allowsNotes {
  332. item = menu.addItem(withTitle: KMLocalizedString("Add New Circle", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.circle.rawValue)
  333. item?.representedObject = selections
  334. item = menu.addItem(withTitle: KMLocalizedString("Add New Rectangle", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.square.rawValue)
  335. item?.representedObject = selections
  336. item = menu.addItem(withTitle: KMLocalizedString("Add New Highlight", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.highlight.rawValue)
  337. item?.representedObject = selections
  338. item = menu.addItem(withTitle: KMLocalizedString("Add New Underline", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.underline.rawValue)
  339. item?.representedObject = selections
  340. item = menu.addItem(withTitle: KMLocalizedString("Add New Strikethrough", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.strikeOut.rawValue)
  341. item?.representedObject = selections
  342. }
  343. }
  344. } else if menu.isEqual(to: self.groupedFindTableView.menu) {
  345. var rowIndexes = self.groupedFindTableView.selectedRowIndexes
  346. let row = self.groupedFindTableView.clickedRow
  347. if (row != -1) {
  348. if rowIndexes.contains(row) == false {
  349. rowIndexes = IndexSet(integer: row)
  350. }
  351. // NSArray *selections = [[[leftSideController.groupedFindArrayController arrangedObjects] objectsAtIndexes:rowIndexes] valueForKeyPath:@"@unionOfArrays.matches"];
  352. var selections: [CPDFSelection] = []
  353. for (i, data) in self.groupSearchResults.enumerated() {
  354. if rowIndexes.contains(i) {
  355. for searchM in data.datas {
  356. selections.append(searchM.selection)
  357. }
  358. }
  359. }
  360. item = menu.addItem(title: KMLocalizedString("Select", "Menu item title"), action: #selector(selectSelections), target: self)
  361. item?.representedObject = selections
  362. menu.addItem(.separator())
  363. let hideNotes = self.hideNotes()
  364. let allowsNotes = self.allowsNotes()
  365. if hideNotes == false && allowsNotes {
  366. item = menu.addItem(withTitle: KMLocalizedString("Add New Circle", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.circle.rawValue)
  367. item?.representedObject = selections
  368. item = menu.addItem(withTitle: KMLocalizedString("Add New Rectangle", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.square.rawValue)
  369. item?.representedObject = selections
  370. item = menu.addItem(withTitle: KMLocalizedString("Add New Highlight", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.highlight.rawValue)
  371. item?.representedObject = selections
  372. item = menu.addItem(withTitle: KMLocalizedString("Add New Underline", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.underline.rawValue)
  373. item?.representedObject = selections
  374. item = menu.addItem(withTitle: KMLocalizedString("Add New Strikethrough", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.strikeOut.rawValue)
  375. item?.representedObject = selections
  376. }
  377. }
  378. } else if menu.isEqual(to: self.snapshotTableView.menu) {
  379. let row = self.snapshotTableView.clickedRow
  380. if (row != -1) {
  381. let model = self.snapshots[row]
  382. let controller = model.windowC
  383. if let data = controller?.window?.isVisible, data {
  384. item = menu.addItem(title: KMLocalizedString("Hide", "Menu item title"), action: #selector(hideSnapshot), target: self)
  385. item?.representedObject = controller
  386. } else {
  387. item = menu.addItem(title: KMLocalizedString("Show", "Menu item title"), action: #selector(showSnapshot), target: self)
  388. item?.representedObject = controller
  389. }
  390. menu.addItem(.separator())
  391. item = menu.addItem(title: KMLocalizedString("Export", "Menu item title"), action: nil, target: self)
  392. let subMenu = NSMenu()
  393. var t = subMenu.addItem(title: KMLocalizedString("PNG", "Menu item title"), action: #selector(menuItemClick_ExportPNG), target: self)
  394. t?.representedObject = controller
  395. t = subMenu.addItem(title: KMLocalizedString("JPG", "Menu item title"), action: #selector(menuItemClick_ExportJPG), target: self)
  396. t?.representedObject = controller
  397. t = subMenu.addItem(title: KMLocalizedString("PDF", "Menu item title"), action: #selector(menuItemClick_ExportPDF), target: self)
  398. t?.representedObject = controller
  399. item?.submenu = subMenu
  400. item = menu.addItem(title: KMLocalizedString("Print", "Menu item title"), action: #selector(menuItemClick_Print), target: self)
  401. item?.representedObject = controller
  402. menu.addItem(.separator())
  403. item = menu.addItem(title: KMLocalizedString("Copy", "Menu item title"), action: #selector(menuItemClick_Copy), target: self)
  404. item?.representedObject = controller
  405. menu.addItem(.separator())
  406. item = menu.addItem(title: KMLocalizedString("Delete", "Menu item title"), action: #selector(deleteSnapshot), target: self)
  407. item?.representedObject = controller
  408. item = menu.addItem(title: KMLocalizedString("Delete All Snapshots", "Menu item title"), action: #selector(deleteAllSnapshot), target: self)
  409. item?.representedObject = controller
  410. }
  411. } else if menu.isEqual(to: self.noteOutlineView.menu) {
  412. var items: NSArray?
  413. var rowIndexes = self.noteOutlineView.selectedRowIndexes
  414. let row = self.noteOutlineView.clickedRow
  415. if (row != -1) {
  416. if rowIndexes.contains(row) == false {
  417. rowIndexes = IndexSet(integer: row)
  418. }
  419. items = self.noteOutlineView.itemsAtRowIndexes(rowIndexes) as NSArray
  420. // PDFAnnotation *foldNote = (PDFAnnotation *)notes[row];
  421. // let foldNote = self.allAnnotations[row]
  422. guard let foldNote = self.fetchNote(for: row) else {
  423. return
  424. }
  425. var isFold = true
  426. if self.allFoldNotes.contains(foldNote) {
  427. isFold = false
  428. }
  429. item = menu.addItem(title: KMLocalizedString("Expand", nil), action: #selector(unfoldNoteAction), target: self)
  430. if (isFold) {
  431. item?.state = .off
  432. } else {
  433. item?.state = .on
  434. }
  435. item?.representedObject = items
  436. item = menu.addItem(title: KMLocalizedString("Collapse", nil), action: #selector(foldNoteAction), target: self)
  437. if (isFold) {
  438. item?.state = .on
  439. } else {
  440. item?.state = .off
  441. }
  442. item?.representedObject = items
  443. menu.addItem(.separator())
  444. let hideNotes = self.hideNotes()
  445. if hideNotes == false && (items?.count ?? 0) == 1 {
  446. let annotation = self.noteItems(items!).lastObject as? CPDFAnnotation
  447. if let data = annotation?.isEditable(), data {
  448. if annotation?.type == nil {
  449. let isNote = annotation?.isNote() ?? false
  450. if isNote {
  451. // [NSLocalizedString(@"Edit", @"Menu item title") stringByAppendingEllipsis]
  452. item = menu.addItem(title: KMLocalizedString("Edit", "Menu item title"), action: #selector(editNoteTextFromTable), target: self)
  453. item?.representedObject = annotation
  454. }
  455. } else if let data = self.noteOutlineView.tableColumn(withIdentifier: NSUserInterfaceItemIdentifier("note"))?.isHidden, data {
  456. // [NSLocalizedString(@"Edit", @"Menu item title") stringByAppendingEllipsis]
  457. item = menu.addItem(title: KMLocalizedString("Edit", "Menu item title"), action: #selector(editThisAnnotation), target: self)
  458. item?.representedObject = annotation
  459. } else {
  460. item = menu.addItem(title: KMLocalizedString("Edit", "Menu item title"), action: #selector(editNoteFromTable), target: self)
  461. item?.representedObject = annotation
  462. item = menu.addItem(title: KMLocalizedString("Edit", "Menu item title"), action: #selector(editThisAnnotation), target: self)
  463. item?.representedObject = annotation
  464. item?.keyEquivalentModifierMask = [.option]
  465. item?.isAlternate = true
  466. }
  467. }
  468. }
  469. if menu.numberOfItems > 0 {
  470. item = menu.addItem(title: NSLocalizedString("Export Annotations…", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  471. let subMenu = NSMenu()
  472. item?.submenu = subMenu
  473. item = subMenu.addItem(title: NSLocalizedString("PDF", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  474. item?.tag = 0
  475. item = subMenu.addItem(title: NSLocalizedString("PDF Bundle", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  476. item?.tag = 1
  477. item = subMenu.addItem(title: NSLocalizedString("PDF Reader Pro Edition Notes", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  478. item?.tag = 2
  479. item = subMenu.addItem(title: NSLocalizedString("Notes as Texts", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  480. item?.tag = 3
  481. item = subMenu.addItem(title: NSLocalizedString("Notes as RTF", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  482. item?.tag = 4
  483. item = subMenu.addItem(title: NSLocalizedString("Notes as RTFD", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  484. item?.tag = 5
  485. item = subMenu.addItem(title: NSLocalizedString("Notes as FDF", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  486. item?.tag = 6
  487. menu.addItem(.separator())
  488. if self.outlineView(self.noteOutlineView, canDeleteItems: items as? [Any] ?? []) {
  489. item = menu.addItem(title: KMLocalizedString("Delete", "Menu item title"), action: #selector(deleteNotes), target: self)
  490. item?.representedObject = items
  491. }
  492. item = menu.addItem(title: NSLocalizedString("Remove All Annotations", tableName: "MainMenu", comment: "Menu item title"), action: #selector(removeAllAnnotations), target: self)
  493. }
  494. } else {
  495. let subMenu = NSMenu()
  496. item?.submenu = subMenu
  497. item = subMenu.addItem(title: NSLocalizedString("PDF", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  498. item?.tag = 0
  499. item = subMenu.addItem(title: NSLocalizedString("PDF Bundle", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  500. item?.tag = 1
  501. item = subMenu.addItem(title: NSLocalizedString("PDF Reader Pro Edition Notes", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  502. item?.tag = 2
  503. item = subMenu.addItem(title: NSLocalizedString("Notes as Texts", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  504. item?.tag = 3
  505. item = subMenu.addItem(title: NSLocalizedString("Notes as RTF", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  506. item?.tag = 4
  507. item = subMenu.addItem(title: NSLocalizedString("Notes as RTFD", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  508. item?.tag = 5
  509. item = subMenu.addItem(title: NSLocalizedString("Notes as FDF", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  510. item?.tag = 6
  511. item = menu.addItem(title: NSLocalizedString("Remove All Annotations", tableName: "MainMenu", comment: "Menu item title"), action: #selector(removeAllAnnotations), target: self)
  512. }
  513. }
  514. }
  515. }
  516. // MARK: - NSMenuItemValidation
  517. extension KMLeftSideViewController: NSMenuItemValidation {
  518. func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  519. let isLocked = self.isLocked()
  520. if isLocked {
  521. return false
  522. }
  523. let action = menuItem.action
  524. if (action == #selector(toggleCaseInsensitiveSearch)) {
  525. let state = KMDataManager.ud_integer(forKey: SKCaseInsensitiveSearchKey)
  526. menuItem.state = state == 1 ? .on : .off
  527. return true
  528. } else if (action == #selector(toggleWholeWordSearch)) {
  529. menuItem.state = self.mwcFlags.wholeWordSearch == 1 ? .on : .off
  530. return true
  531. } else if action == #selector(toggleCaseInsensitiveNoteSearch) {
  532. menuItem.state = self.caseInsensitiveNoteSearch ? .on : .off
  533. return true
  534. }
  535. if (action == #selector(outlineContextMenuItemClicked_AddEntry) ||
  536. action == #selector(outlineContextMenuItemClicked_AddChildEntry) ||
  537. action == #selector(outlineContextMenuItemClicked_AddAuntEntry) ||
  538. action == #selector(outlineContextMenuItemClicked_RemoveEntry) ||
  539. action == #selector(outlineContextMenuItemClicked_Edit) ||
  540. action == #selector(outlineContextMenuItemClicked_SetDestination) ||
  541. action == #selector(outlineContextMenuItemClicked_Rename) ||
  542. action == #selector(outlineContextMenuItemClicked_Promote) ||
  543. action == #selector(outlineContextMenuItemClicked_Demote)) {
  544. return self.outlineListValidateMenuItem(menuItem)
  545. } else if action == #selector(toggleOutlineCaseInsensitiveSearch) {
  546. menuItem.state = self.outlineIgnoreCaseFlag ? .on : .off
  547. return true
  548. }
  549. // } else if (action == @selector(note_expandAllComments:) ||
  550. // action == @selector(note_foldAllComments:) ||
  551. // action == @selector(exportAnnotationNotes:) ||
  552. // action == @selector(leftSideEmptyAnnotationClick_DeleteAnnotation:) ||
  553. // action == @selector(removeAllAnnotations:)) {
  554. // if (@available(macOS 10.13, *)) {
  555. // if (notes.count == 0) {
  556. // return NO;
  557. // } else {
  558. // return YES;
  559. // }
  560. // } else {
  561. // if (action == @selector(note_expandAllComments:) ||
  562. // action == @selector(note_foldAllComments:)) {
  563. // return NO;
  564. // }
  565. // }
  566. // } else
  567. if (action == #selector(unfoldNoteAction) ||
  568. action == #selector(foldNoteAction)) {
  569. let row = self.noteOutlineView.clickedRow
  570. // NSArray *noteArr = [rightSideController.noteArrayController arrangedObjects];
  571. // PDFAnnotation *foldNote = (PDFAnnotation *)noteArr[row];
  572. // let foldNote = self.allAnnotations[row]
  573. let foldNote = self.fetchNote(for: row)
  574. // SKNPDFAnnotationNote
  575. if foldNote is CPDFMarkupAnnotation || foldNote is CPDFTextAnnotation {
  576. return true
  577. } else {
  578. return false
  579. }
  580. } else if (action == #selector(menuItemClick_ExportPNG) ||
  581. action == #selector(menuItemClick_ExportJPG) ||
  582. action == #selector(menuItemClick_ExportPDF)) {
  583. if (self.snapshotTableView.selectedRow == -1 ) {
  584. return false
  585. } else {
  586. return true
  587. }
  588. }
  589. // else if (action == @selector(editNoteFromTable:)) {
  590. // NSInteger row = [rightSideController.noteOutlineView clickedRow];
  591. // NSArray * noteArr = [rightSideController.noteArrayController arrangedObjects];
  592. // PDFAnnotation *foldNote = (PDFAnnotation *)noteArr[row];
  593. // if (@available(macOS 10.13, *)) {
  594. // if ([foldNote.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeSignature]) {
  595. // return NO;
  596. // }
  597. // }
  598. // if ([foldNote isKindOfClass:[PDFAnnotationStamp class]] ||
  599. // [foldNote isKindOfClass:[KMAnnotationStamp class]]) {
  600. // return NO;
  601. // } else {
  602. // return YES;
  603. // }
  604. // }
  605. else if (action == #selector(menuItemClick_SelectAll)) {
  606. menuItem.state = self.snapshotListIsAllSelected() ? .on : .off
  607. return true
  608. }
  609. // else if (action == @selector(leftSideEmptyAnnotationClick_DeleteOutline:)) {
  610. // PDFOutline * item = [[pdfView document] outlineRoot];
  611. // if (self.isSearchOutlineMode) {
  612. // NSInteger num = 0;
  613. // for (NSUInteger i = 0; i < [item numberOfChildren]; i++) {
  614. // PDFOutline *outline = [item childAtIndex:i];
  615. // if ([self hasContainString:self.leftSideController.outlineSearchField.stringValue rootOutline:outline]) {
  616. // num ++;
  617. // }
  618. // }
  619. // if (num > 0)
  620. // return YES;
  621. // else
  622. // return NO;
  623. // } else {
  624. // if ([item numberOfChildren] > 0)
  625. // return YES;
  626. // else
  627. // return NO;
  628. // }
  629. // }
  630. return true
  631. }
  632. }
  633. // MARK: - NSPopoverDelegate
  634. extension KMLeftSideViewController: NSPopoverDelegate {
  635. func popoverWillClose(_ notification: Notification) {
  636. guard let popover = notification.object as? NSPopover else {
  637. return
  638. }
  639. if let vc = popover.contentViewController as? KMOutlineEditViewController {
  640. self.editOutlineUI(vc)
  641. self.tocOutlineView.reloadData()
  642. }
  643. }
  644. }
  645. // MARK: - NSSearchFieldDelegate
  646. extension KMLeftSideViewController: NSSearchFieldDelegate {
  647. func controlTextDidChange(_ obj: Notification) {
  648. if self.outlineSearchField.isEqual(to: obj.object) {
  649. if (self.outlineSearchField.stringValue.isEmpty == false) {
  650. self.isSearchOutlineMode = true
  651. self.tocOutlineView.reloadData()
  652. self.tocOutlineView.expandItem(nil, expandChildren: true)
  653. self.tocType = .unfold
  654. } else {
  655. self.isSearchOutlineMode = false
  656. self.showSearchOutlineBlankState(false)
  657. self.tocOutlineView.reloadData()
  658. }
  659. self.outlineAddButton.isEnabled = !self.isSearchOutlineMode
  660. } else if self.snapshotSearchField.isEqual(to: obj.object) {
  661. let searchString = self.snapshotSearchField.stringValue
  662. // NSPredicate *filterPredicate = nil;
  663. // if ([searchString length] > 0) {
  664. // NSExpression *lhs = [NSExpression expressionForConstantValue:searchString];
  665. // NSExpression *rhs = [NSExpression expressionForKeyPath:@"string"];
  666. // NSUInteger options = NSDiacriticInsensitivePredicateOption;
  667. // if (mwcFlags.caseInsensitiveNoteSearch)
  668. // options |= NSCaseInsensitivePredicateOption;
  669. // filterPredicate = [NSComparisonPredicate predicateWithLeftExpression:lhs rightExpression:rhs modifier:NSDirectPredicateModifier type:NSInPredicateOperatorType options:options];
  670. // }
  671. // [rightSideController.snapshotArrayController setFilterPredicate:filterPredicate];
  672. // NSArray * snapshots = [rightSideController.snapshotArrayController arrangedObjects];
  673. self.searchSnapshots.removeAll()
  674. if searchString.isEmpty {
  675. self.isSearchSnapshotMode = false
  676. self.searchSnapshots = self.snapshots.filter({ model in
  677. let data = model.windowC?.string.contains(searchString) ?? false
  678. return data
  679. })
  680. } else {
  681. self.isSearchSnapshotMode = true
  682. }
  683. var snapshots = self.searchSnapshots
  684. if self.isSearchSnapshotMode == false {
  685. snapshots = self.snapshots
  686. }
  687. Task { @MainActor in
  688. self.updataLeftSideSnapView()
  689. self.snapshotTableView.reloadData()
  690. }
  691. if (snapshots.count > 0) {
  692. self.leftSideEmptyVC.outlineSearchView.removeFromSuperview()
  693. } else {
  694. let view = self.snapshotTableView.enclosingScrollView
  695. let viewFrmae = view?.frame ?? .zero
  696. let emptyVcSize = self.leftSideEmptyVC.outlineSearchView.frame.size
  697. self.leftSideEmptyVC.outlineSearchView.frame = NSMakeRect((viewFrmae.size.width-emptyVcSize.width)/2.0,(viewFrmae.size.height-emptyVcSize.height)/2.0, emptyVcSize.width, emptyVcSize.height)
  698. self.leftSideEmptyVC.outlineSearchView.autoresizingMask = [.minXMargin, .maxXMargin, .minYMargin, .maxYMargin]
  699. self.snapshotTableView.enclosingScrollView?.documentView?.addSubview(self.leftSideEmptyVC.outlineSearchView)
  700. }
  701. } else if self.noteSearchField.isEqual(to: obj.object) {
  702. if let isEmpty = self.noteSearchField?.stringValue.isEmpty {
  703. self.noteSearchMode = !isEmpty
  704. } else {
  705. self.noteSearchMode = false
  706. }
  707. }
  708. }
  709. }
  710. // MARK: - NSTextFieldDelegate
  711. extension KMLeftSideViewController: NSTextFieldDelegate {
  712. func controlTextDidBeginEditing(_ obj: Notification) {
  713. if self.noteOutlineView.isEqual(to: obj.object) {
  714. // if (mwcFlags.isEditingTable == NO && mwcFlags.isEditingPDF == NO)
  715. // [[self document] objectDidBeginEditing:self];
  716. // mwcFlags.isEditingTable = YES;
  717. }
  718. }
  719. func controlTextDidEndEditing(_ obj: Notification) {
  720. if self.noteOutlineView.isEqual(to: obj.object) {
  721. // if (mwcFlags.isEditingTable && mwcFlags.isEditingPDF == NO)
  722. // [[self document] objectDidEndEditing:self];
  723. // mwcFlags.isEditingTable = NO;
  724. } else if let data = self.renamePDFOutlineTextField?.isEqual(to: obj.object), data {
  725. let tf = obj.object as! NSTextField
  726. if let ol = self.renamePDFOutline {
  727. if self.isRenameNoteOutline == false {
  728. if tf.stringValue == ol.label {
  729. return
  730. }
  731. }
  732. self.renamePDFOutline(ol, label: tf.stringValue)
  733. self.updateSelectRowHeight()
  734. self.tocOutlineView.reloadData()
  735. }
  736. }
  737. }
  738. }
  739. // MARK: - Menu Item Actions
  740. extension KMLeftSideViewController {
  741. @objc func toggleCaseInsensitiveNoteSearch(_ sender: AnyObject?) {
  742. self.caseInsensitiveNoteSearch = !self.caseInsensitiveNoteSearch
  743. if self.searchField.stringValue.isEmpty == false {
  744. self.searchNotes(self.searchField)
  745. }
  746. KMDataManager.ud_set(self.caseInsensitiveNoteSearch, forKey: SKCaseInsensitiveNoteSearchKey)
  747. }
  748. @objc func searchNotes(_ sender: AnyObject?) {
  749. self.noteSearchMode = false
  750. if self.findState == .note {
  751. if self.noteSearchField.isEqual(to: sender) {
  752. self.noteSearchMode = true
  753. }
  754. self.updateNoteFilterPredicate()
  755. } else {
  756. self.updateSnapshotFilterPredicate()
  757. }
  758. let textfield = sender as? NSSearchField
  759. if let data = textfield?.stringValue.isEmpty, data == false {
  760. let findPboard = NSPasteboard(name: .find)
  761. findPboard.clearContents()
  762. // findPboard.writeObjects([textfield!.stringValue])
  763. }
  764. }
  765. func updateSnapshotFilterPredicate() {
  766. let searchString = self.snapshotSearchField.stringValue
  767. self.searchSnapshots.removeAll()
  768. if self.findState == .snapshot && searchString.isEmpty == false {
  769. self.searchSnapshots = self.snapshots.filter({ model in
  770. let data = model.windowC?.string.contains(searchString) ?? false
  771. return data
  772. })
  773. }
  774. Task { @MainActor in
  775. self.updataLeftSideSnapView()
  776. self.snapshotTableView.reloadData()
  777. }
  778. }
  779. func snapshotListIsAllSelected() -> Bool {
  780. if self.snapshots.isEmpty {
  781. return false
  782. }
  783. for model in self.snapshots {
  784. if model.isSelected == false {
  785. return false
  786. }
  787. }
  788. return true
  789. }
  790. }