KMLeftSideViewController+Action.swift 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  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. }
  200. // MARK: - Double Action
  201. extension KMLeftSideViewController {
  202. @objc func toggleSelectedSnapshots(_ sender: AnyObject?) {
  203. let indexs = self.snapshotTableView.selectedRowIndexes
  204. if indexs.isEmpty {
  205. return
  206. }
  207. let model = self.snapshots[indexs.last!]
  208. let windowC = model.windowC
  209. if let data = windowC?.window?.isVisible, data {
  210. windowC?.miniaturize()
  211. } else {
  212. windowC?.deminiaturize()
  213. }
  214. var rowIndexSet = IndexSet()
  215. let row = self.snapshotTableView.selectedRow
  216. if row >= 0 && row < self.snapshots.count {
  217. rowIndexSet.insert(row)
  218. }
  219. var columnIndexSet = IndexSet()
  220. columnIndexSet.insert(0)
  221. self.snapshotTableView.reloadData(forRowIndexes: rowIndexSet, columnIndexes: columnIndexSet)
  222. }
  223. // MARK: - KMInterfaceThemeChangedProtocol
  224. override func interfaceThemeDidChanged(_ appearance: NSAppearance.Name) {
  225. super.interfaceThemeDidChanged(appearance)
  226. self.updateViewColor()
  227. self.leftView.interfaceThemeDidChanged(appearance)
  228. }
  229. }
  230. // MARK: - NSMenuDelegate
  231. extension KMLeftSideViewController: NSMenuDelegate {
  232. func menuNeedsUpdate(_ menu: NSMenu) {
  233. menu.removeAllItems()
  234. if menu.isEqual(to: self.tocOutlineView.menu) {
  235. self.outlineListMenu(menu)
  236. return
  237. }
  238. var item: NSMenuItem?
  239. if menu.isEqual(to: self.thumbnailTableView.menu) {
  240. let row = self.thumbnailTableView.clickedRow
  241. if self.pdfDocument()?.documentURL == nil || self.thumbnailTableView.selectedRowIndexes.contains(row) == false{
  242. return
  243. }
  244. let isLocked = self.isLocked()
  245. let pageCount = self.pageCount()
  246. if (row != -1 && isLocked == false) {
  247. if(self.thumbnailTableView.selectedRowIndexes.count == pageCount) {
  248. item = menu.addItem(title: KMLocalizedString("Cut", "Menu item title"), action: nil, target: self)
  249. } else {
  250. item = menu.addItem(title: KMLocalizedString("Cut", "Menu item title"), action: #selector(cutPage), target: self)
  251. }
  252. item?.representedObject = IndexSet(integer: row)
  253. item = menu.addItem(title: KMLocalizedString("Copy", "Menu item title"), action: #selector(copyPage), target: self)
  254. item?.representedObject = IndexSet(integer: row)
  255. if (self.copyPages.count > 0) {
  256. item = menu.addItem(title: KMLocalizedString("Paste", "Menu item title"), action: #selector(pastePage), target: self)
  257. }else{
  258. item = menu.addItem(title: KMLocalizedString("Paste", "Menu item title"), action: nil, target: self)
  259. }
  260. item?.representedObject = IndexSet(integer: row)
  261. menu.addItem(.separator())
  262. if(self.thumbnailTableView.selectedRowIndexes.count == pageCount) {
  263. item = menu.addItem(title: KMLocalizedString("Delete", "Menu item title"), action: nil, target: self)
  264. } else {
  265. item = menu.addItem(title: KMLocalizedString("Delete", "Menu item title"), action: #selector(deletePage), target: self)
  266. }
  267. item?.representedObject = IndexSet(integer: row)
  268. menu.addItem(.separator())
  269. item = menu.addItem(title: KMLocalizedString("Rotate", "Menu item title"), action: #selector(rotatePageMenuAction), target: self)
  270. item?.representedObject = IndexSet(integer: row)
  271. item = menu.addItem(title: KMLocalizedString("Insert", "Menu item title"), action: nil, target: self)
  272. let subMenu = NSMenu()
  273. _ = subMenu.addItem(title: KMLocalizedString("Blank Page", "Menu item title"), action: #selector(quickInsert), target: self)
  274. _ = subMenu.addItem(title: KMLocalizedString("Blank Page - Custom...", "Menu item title"), action: #selector(insert), target: self)
  275. _ = subMenu.addItem(title: KMLocalizedString("From PDF", "Menu item title"), action: #selector(insertPDF), target: self)
  276. item?.submenu = subMenu
  277. item?.representedObject = IndexSet(integer: row)
  278. item = menu.addItem(title: KMLocalizedString("Extract", "Menu item title"), action: #selector(extractPage), target: self)
  279. item?.representedObject = IndexSet(integer: row)
  280. item = menu.addItem(title: KMLocalizedString("Page Edit", "Menu item title"), action: #selector(pageEdit), target: self)
  281. menu.addItem(.separator())
  282. var displayStr = ""
  283. if (self.isDisplayPageSize) {
  284. displayStr = KMLocalizedString("Hide Page Size", "Menu item title")
  285. } else {
  286. displayStr = KMLocalizedString("Display Page Size", "Menu item title")
  287. }
  288. item = menu.addItem(title: displayStr, action: #selector(displayPageSize), target: self)
  289. item?.representedObject = IndexSet(integer: row)
  290. if let doct = self.pdfDocument() {
  291. item = menu.addItem(title: KMLocalizedString("Share", "Menu item title"), action: nil, target: self)
  292. var tName = self.fileNameWithSelectedPages(self.thumbnailTableView.selectedRowIndexes)
  293. if (tName.count > 50) {
  294. tName = tName.substring(to: 50)
  295. }
  296. item?.submenu = NSSharingServicePicker.menu(forSharingItems: [doct.documentURL as Any], subjectContext: tName, withTarget: self, selector: #selector(sharePage), serviceDelegate: nil)
  297. }
  298. }
  299. } else if menu.isEqual(to: self.findTableView.menu) {
  300. var rowIndexes = self.findTableView.selectedRowIndexes
  301. let row = self.findTableView.clickedRow
  302. if (row != -1) {
  303. if rowIndexes.contains(row) == false {
  304. rowIndexes = IndexSet(integer: row)
  305. }
  306. var selections: [CPDFSelection] = []
  307. for (i, data) in self.searchResults.enumerated() {
  308. if rowIndexes.contains(i) {
  309. selections.append(data.selection)
  310. }
  311. }
  312. let hideNotes = self.hideNotes()
  313. let allowsNotes = self.allowsNotes()
  314. if hideNotes == false && allowsNotes {
  315. item = menu.addItem(withTitle: KMLocalizedString("Add New Circle", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.circle.rawValue)
  316. item?.representedObject = selections
  317. item = menu.addItem(withTitle: KMLocalizedString("Add New Rectangle", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.square.rawValue)
  318. item?.representedObject = selections
  319. item = menu.addItem(withTitle: KMLocalizedString("Add New Highlight", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.highlight.rawValue)
  320. item?.representedObject = selections
  321. item = menu.addItem(withTitle: KMLocalizedString("Add New Underline", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.underline.rawValue)
  322. item?.representedObject = selections
  323. item = menu.addItem(withTitle: KMLocalizedString("Add New Strikethrough", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.strikeOut.rawValue)
  324. item?.representedObject = selections
  325. }
  326. }
  327. } else if menu.isEqual(to: self.groupedFindTableView.menu) {
  328. var rowIndexes = self.groupedFindTableView.selectedRowIndexes
  329. let row = self.groupedFindTableView.clickedRow
  330. if (row != -1) {
  331. if rowIndexes.contains(row) == false {
  332. rowIndexes = IndexSet(integer: row)
  333. }
  334. // NSArray *selections = [[[leftSideController.groupedFindArrayController arrangedObjects] objectsAtIndexes:rowIndexes] valueForKeyPath:@"@unionOfArrays.matches"];
  335. var selections: [CPDFSelection] = []
  336. for (i, data) in self.groupSearchResults.enumerated() {
  337. if rowIndexes.contains(i) {
  338. for searchM in data.datas {
  339. selections.append(searchM.selection)
  340. }
  341. }
  342. }
  343. item = menu.addItem(title: KMLocalizedString("Select", "Menu item title"), action: #selector(selectSelections), target: self)
  344. item?.representedObject = selections
  345. menu.addItem(.separator())
  346. let hideNotes = self.hideNotes()
  347. let allowsNotes = self.allowsNotes()
  348. if hideNotes == false && allowsNotes {
  349. item = menu.addItem(withTitle: KMLocalizedString("Add New Circle", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.circle.rawValue)
  350. item?.representedObject = selections
  351. item = menu.addItem(withTitle: KMLocalizedString("Add New Rectangle", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.square.rawValue)
  352. item?.representedObject = selections
  353. item = menu.addItem(withTitle: KMLocalizedString("Add New Highlight", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.highlight.rawValue)
  354. item?.representedObject = selections
  355. item = menu.addItem(withTitle: KMLocalizedString("Add New Underline", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.underline.rawValue)
  356. item?.representedObject = selections
  357. item = menu.addItem(withTitle: KMLocalizedString("Add New Strikethrough", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.strikeOut.rawValue)
  358. item?.representedObject = selections
  359. }
  360. }
  361. } else if menu.isEqual(to: self.snapshotTableView.menu) {
  362. let row = self.snapshotTableView.clickedRow
  363. if (row != -1) {
  364. let model = self.snapshots[row]
  365. let controller = model.windowC
  366. if let data = controller?.window?.isVisible, data {
  367. item = menu.addItem(title: KMLocalizedString("Hide", "Menu item title"), action: #selector(hideSnapshot), target: self)
  368. item?.representedObject = controller
  369. } else {
  370. item = menu.addItem(title: KMLocalizedString("Show", "Menu item title"), action: #selector(showSnapshot), target: self)
  371. item?.representedObject = controller
  372. }
  373. menu.addItem(.separator())
  374. item = menu.addItem(title: KMLocalizedString("Export", "Menu item title"), action: nil, target: self)
  375. let subMenu = NSMenu()
  376. var t = subMenu.addItem(title: KMLocalizedString("PNG", "Menu item title"), action: #selector(menuItemClick_ExportPNG), target: self)
  377. t?.representedObject = controller
  378. t = subMenu.addItem(title: KMLocalizedString("JPG", "Menu item title"), action: #selector(menuItemClick_ExportJPG), target: self)
  379. t?.representedObject = controller
  380. t = subMenu.addItem(title: KMLocalizedString("PDF", "Menu item title"), action: #selector(menuItemClick_ExportPDF), target: self)
  381. t?.representedObject = controller
  382. item?.submenu = subMenu
  383. item = menu.addItem(title: KMLocalizedString("Print", "Menu item title"), action: #selector(menuItemClick_Print), target: self)
  384. item?.representedObject = controller
  385. menu.addItem(.separator())
  386. item = menu.addItem(title: KMLocalizedString("Copy", "Menu item title"), action: #selector(menuItemClick_Copy), target: self)
  387. item?.representedObject = controller
  388. menu.addItem(.separator())
  389. item = menu.addItem(title: KMLocalizedString("Delete", "Menu item title"), action: #selector(deleteSnapshot), target: self)
  390. item?.representedObject = controller
  391. item = menu.addItem(title: KMLocalizedString("Delete All Snapshots", "Menu item title"), action: #selector(deleteAllSnapshot), target: self)
  392. item?.representedObject = controller
  393. }
  394. } else if menu.isEqual(to: self.noteOutlineView.menu) {
  395. var items: NSArray?
  396. var rowIndexes = self.noteOutlineView.selectedRowIndexes
  397. let row = self.noteOutlineView.clickedRow
  398. if (row != -1) {
  399. if rowIndexes.contains(row) == false {
  400. rowIndexes = IndexSet(integer: row)
  401. }
  402. items = self.noteOutlineView.itemsAtRowIndexes(rowIndexes) as NSArray
  403. // PDFAnnotation *foldNote = (PDFAnnotation *)notes[row];
  404. // let foldNote = self.allAnnotations[row]
  405. guard let foldNote = self.fetchNote(for: row) else {
  406. return
  407. }
  408. var isFold = true
  409. if self.allFoldNotes.contains(foldNote) {
  410. isFold = false
  411. }
  412. item = menu.addItem(title: KMLocalizedString("Expand", nil), action: #selector(unfoldNoteAction), target: self)
  413. if (isFold) {
  414. item?.state = .off
  415. } else {
  416. item?.state = .on
  417. }
  418. item?.representedObject = items
  419. item = menu.addItem(title: KMLocalizedString("Collapse", nil), action: #selector(foldNoteAction), target: self)
  420. if (isFold) {
  421. item?.state = .on
  422. } else {
  423. item?.state = .off
  424. }
  425. item?.representedObject = items
  426. menu.addItem(.separator())
  427. let hideNotes = self.hideNotes()
  428. if hideNotes == false && (items?.count ?? 0) == 1 {
  429. let annotation = self.noteItems(items!).lastObject as? CPDFAnnotation
  430. if let data = annotation?.isEditable(), data {
  431. if annotation?.type == nil {
  432. let isNote = annotation?.isNote() ?? false
  433. if isNote {
  434. // [NSLocalizedString(@"Edit", @"Menu item title") stringByAppendingEllipsis]
  435. item = menu.addItem(title: KMLocalizedString("Edit", "Menu item title"), action: #selector(editNoteTextFromTable), target: self)
  436. item?.representedObject = annotation
  437. }
  438. } else if let data = self.noteOutlineView.tableColumn(withIdentifier: NSUserInterfaceItemIdentifier("note"))?.isHidden, data {
  439. // [NSLocalizedString(@"Edit", @"Menu item title") stringByAppendingEllipsis]
  440. item = menu.addItem(title: KMLocalizedString("Edit", "Menu item title"), action: #selector(editThisAnnotation), target: self)
  441. item?.representedObject = annotation
  442. } else {
  443. item = menu.addItem(title: KMLocalizedString("Edit", "Menu item title"), action: #selector(editNoteFromTable), target: self)
  444. item?.representedObject = annotation
  445. item = menu.addItem(title: KMLocalizedString("Edit", "Menu item title"), action: #selector(editThisAnnotation), target: self)
  446. item?.representedObject = annotation
  447. item?.keyEquivalentModifierMask = [.option]
  448. item?.isAlternate = true
  449. }
  450. }
  451. }
  452. if menu.numberOfItems > 0 {
  453. item = menu.addItem(title: NSLocalizedString("Export Annotations…", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  454. let subMenu = NSMenu()
  455. item?.submenu = subMenu
  456. item = subMenu.addItem(title: NSLocalizedString("PDF", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  457. item?.tag = 0
  458. item = subMenu.addItem(title: NSLocalizedString("PDF Bundle", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  459. item?.tag = 1
  460. item = subMenu.addItem(title: NSLocalizedString("PDF Reader Pro Edition Notes", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  461. item?.tag = 2
  462. item = subMenu.addItem(title: NSLocalizedString("Notes as Texts", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  463. item?.tag = 3
  464. item = subMenu.addItem(title: NSLocalizedString("Notes as RTF", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  465. item?.tag = 4
  466. item = subMenu.addItem(title: NSLocalizedString("Notes as RTFD", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  467. item?.tag = 5
  468. item = subMenu.addItem(title: NSLocalizedString("Notes as FDF", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  469. item?.tag = 6
  470. menu.addItem(.separator())
  471. if self.outlineView(self.noteOutlineView, canDeleteItems: items as? [Any] ?? []) {
  472. item = menu.addItem(title: KMLocalizedString("Delete", "Menu item title"), action: #selector(deleteNotes), target: self)
  473. item?.representedObject = items
  474. }
  475. item = menu.addItem(title: NSLocalizedString("Remove All Annotations", tableName: "MainMenu", comment: "Menu item title"), action: #selector(removeAllAnnotations), target: self)
  476. }
  477. } else {
  478. let subMenu = NSMenu()
  479. item?.submenu = subMenu
  480. item = subMenu.addItem(title: NSLocalizedString("PDF", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  481. item?.tag = 0
  482. item = subMenu.addItem(title: NSLocalizedString("PDF Bundle", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  483. item?.tag = 1
  484. item = subMenu.addItem(title: NSLocalizedString("PDF Reader Pro Edition Notes", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  485. item?.tag = 2
  486. item = subMenu.addItem(title: NSLocalizedString("Notes as Texts", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  487. item?.tag = 3
  488. item = subMenu.addItem(title: NSLocalizedString("Notes as RTF", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  489. item?.tag = 4
  490. item = subMenu.addItem(title: NSLocalizedString("Notes as RTFD", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  491. item?.tag = 5
  492. item = subMenu.addItem(title: NSLocalizedString("Notes as FDF", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  493. item?.tag = 6
  494. item = menu.addItem(title: NSLocalizedString("Remove All Annotations", tableName: "MainMenu", comment: "Menu item title"), action: #selector(removeAllAnnotations), target: self)
  495. }
  496. }
  497. }
  498. }
  499. // MARK: - NSMenuItemValidation
  500. extension KMLeftSideViewController: NSMenuItemValidation {
  501. func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  502. let isLocked = self.isLocked()
  503. if isLocked {
  504. return false
  505. }
  506. let action = menuItem.action
  507. if (action == #selector(toggleCaseInsensitiveSearch)) {
  508. let state = KMDataManager.ud_integer(forKey: SKCaseInsensitiveSearchKey)
  509. menuItem.state = state == 1 ? .on : .off
  510. return true
  511. } else if (action == #selector(toggleWholeWordSearch)) {
  512. menuItem.state = self.mwcFlags.wholeWordSearch == 1 ? .on : .off
  513. return true
  514. } else if action == #selector(toggleCaseInsensitiveNoteSearch) {
  515. menuItem.state = self.caseInsensitiveNoteSearch ? .on : .off
  516. return true
  517. }
  518. if (action == #selector(outlineContextMenuItemClicked_AddEntry) ||
  519. action == #selector(outlineContextMenuItemClicked_AddChildEntry) ||
  520. action == #selector(outlineContextMenuItemClicked_AddAuntEntry) ||
  521. action == #selector(outlineContextMenuItemClicked_RemoveEntry) ||
  522. action == #selector(outlineContextMenuItemClicked_Edit) ||
  523. action == #selector(outlineContextMenuItemClicked_SetDestination) ||
  524. action == #selector(outlineContextMenuItemClicked_Rename) ||
  525. action == #selector(outlineContextMenuItemClicked_Promote) ||
  526. action == #selector(outlineContextMenuItemClicked_Demote)) {
  527. return self.outlineListValidateMenuItem(menuItem)
  528. } else if action == #selector(toggleOutlineCaseInsensitiveSearch) {
  529. menuItem.state = self.outlineIgnoreCaseFlag ? .on : .off
  530. return true
  531. }
  532. // } else if (action == @selector(note_expandAllComments:) ||
  533. // action == @selector(note_foldAllComments:) ||
  534. // action == @selector(exportAnnotationNotes:) ||
  535. // action == @selector(leftSideEmptyAnnotationClick_DeleteAnnotation:) ||
  536. // action == @selector(removeAllAnnotations:)) {
  537. // if (@available(macOS 10.13, *)) {
  538. // if (notes.count == 0) {
  539. // return NO;
  540. // } else {
  541. // return YES;
  542. // }
  543. // } else {
  544. // if (action == @selector(note_expandAllComments:) ||
  545. // action == @selector(note_foldAllComments:)) {
  546. // return NO;
  547. // }
  548. // }
  549. // } else
  550. if (action == #selector(unfoldNoteAction) ||
  551. action == #selector(foldNoteAction)) {
  552. let row = self.noteOutlineView.clickedRow
  553. // NSArray *noteArr = [rightSideController.noteArrayController arrangedObjects];
  554. // PDFAnnotation *foldNote = (PDFAnnotation *)noteArr[row];
  555. // let foldNote = self.allAnnotations[row]
  556. let foldNote = self.fetchNote(for: row)
  557. // SKNPDFAnnotationNote
  558. if foldNote is CPDFMarkupAnnotation || foldNote is CPDFTextAnnotation {
  559. return true
  560. } else {
  561. return false
  562. }
  563. } else if (action == #selector(menuItemClick_ExportPNG) ||
  564. action == #selector(menuItemClick_ExportJPG) ||
  565. action == #selector(menuItemClick_ExportPDF)) {
  566. if (self.snapshotTableView.selectedRow == -1 ) {
  567. return false
  568. } else {
  569. return true
  570. }
  571. }
  572. // else if (action == @selector(editNoteFromTable:)) {
  573. // NSInteger row = [rightSideController.noteOutlineView clickedRow];
  574. // NSArray * noteArr = [rightSideController.noteArrayController arrangedObjects];
  575. // PDFAnnotation *foldNote = (PDFAnnotation *)noteArr[row];
  576. // if (@available(macOS 10.13, *)) {
  577. // if ([foldNote.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeSignature]) {
  578. // return NO;
  579. // }
  580. // }
  581. // if ([foldNote isKindOfClass:[PDFAnnotationStamp class]] ||
  582. // [foldNote isKindOfClass:[KMAnnotationStamp class]]) {
  583. // return NO;
  584. // } else {
  585. // return YES;
  586. // }
  587. // }
  588. else if (action == #selector(menuItemClick_SelectAll)) {
  589. menuItem.state = self.snapshotListIsAllSelected() ? .on : .off
  590. return true
  591. }
  592. // else if (action == @selector(leftSideEmptyAnnotationClick_DeleteOutline:)) {
  593. // PDFOutline * item = [[pdfView document] outlineRoot];
  594. // if (self.isSearchOutlineMode) {
  595. // NSInteger num = 0;
  596. // for (NSUInteger i = 0; i < [item numberOfChildren]; i++) {
  597. // PDFOutline *outline = [item childAtIndex:i];
  598. // if ([self hasContainString:self.leftSideController.outlineSearchField.stringValue rootOutline:outline]) {
  599. // num ++;
  600. // }
  601. // }
  602. // if (num > 0)
  603. // return YES;
  604. // else
  605. // return NO;
  606. // } else {
  607. // if ([item numberOfChildren] > 0)
  608. // return YES;
  609. // else
  610. // return NO;
  611. // }
  612. // }
  613. return true
  614. }
  615. }
  616. // MARK: - NSPopoverDelegate
  617. extension KMLeftSideViewController: NSPopoverDelegate {
  618. func popoverWillClose(_ notification: Notification) {
  619. guard let popover = notification.object as? NSPopover else {
  620. return
  621. }
  622. if let vc = popover.contentViewController as? KMOutlineEditViewController {
  623. self.editOutlineUI(vc)
  624. self.tocOutlineView.reloadData()
  625. }
  626. }
  627. }
  628. // MARK: - NSSearchFieldDelegate
  629. extension KMLeftSideViewController: NSSearchFieldDelegate {
  630. func controlTextDidChange(_ obj: Notification) {
  631. if self.outlineSearchField.isEqual(to: obj.object) {
  632. if (self.outlineSearchField.stringValue.isEmpty == false) {
  633. self.isSearchOutlineMode = true
  634. self.tocOutlineView.reloadData()
  635. self.tocOutlineView.expandItem(nil, expandChildren: true)
  636. self.tocType = .unfold
  637. } else {
  638. self.isSearchOutlineMode = false
  639. self.showSearchOutlineBlankState(false)
  640. self.tocOutlineView.reloadData()
  641. }
  642. self.outlineAddButton.isEnabled = !self.isSearchOutlineMode
  643. } else if self.snapshotSearchField.isEqual(to: obj.object) {
  644. let searchString = self.snapshotSearchField.stringValue
  645. // NSPredicate *filterPredicate = nil;
  646. // if ([searchString length] > 0) {
  647. // NSExpression *lhs = [NSExpression expressionForConstantValue:searchString];
  648. // NSExpression *rhs = [NSExpression expressionForKeyPath:@"string"];
  649. // NSUInteger options = NSDiacriticInsensitivePredicateOption;
  650. // if (mwcFlags.caseInsensitiveNoteSearch)
  651. // options |= NSCaseInsensitivePredicateOption;
  652. // filterPredicate = [NSComparisonPredicate predicateWithLeftExpression:lhs rightExpression:rhs modifier:NSDirectPredicateModifier type:NSInPredicateOperatorType options:options];
  653. // }
  654. // [rightSideController.snapshotArrayController setFilterPredicate:filterPredicate];
  655. // NSArray * snapshots = [rightSideController.snapshotArrayController arrangedObjects];
  656. self.searchSnapshots.removeAll()
  657. if searchString.isEmpty {
  658. self.isSearchSnapshotMode = false
  659. self.searchSnapshots = self.snapshots.filter({ model in
  660. let data = model.windowC?.string.contains(searchString) ?? false
  661. return data
  662. })
  663. } else {
  664. self.isSearchSnapshotMode = true
  665. }
  666. var snapshots = self.searchSnapshots
  667. if self.isSearchSnapshotMode == false {
  668. snapshots = self.snapshots
  669. }
  670. Task { @MainActor in
  671. self.updataLeftSideSnapView()
  672. self.snapshotTableView.reloadData()
  673. }
  674. if (snapshots.count > 0) {
  675. self.leftSideEmptyVC.outlineSearchView.removeFromSuperview()
  676. } else {
  677. let view = self.snapshotTableView.enclosingScrollView
  678. let viewFrmae = view?.frame ?? .zero
  679. let emptyVcSize = self.leftSideEmptyVC.outlineSearchView.frame.size
  680. self.leftSideEmptyVC.outlineSearchView.frame = NSMakeRect((viewFrmae.size.width-emptyVcSize.width)/2.0,(viewFrmae.size.height-emptyVcSize.height)/2.0, emptyVcSize.width, emptyVcSize.height)
  681. self.leftSideEmptyVC.outlineSearchView.autoresizingMask = [.minXMargin, .maxXMargin, .minYMargin, .maxYMargin]
  682. self.snapshotTableView.enclosingScrollView?.documentView?.addSubview(self.leftSideEmptyVC.outlineSearchView)
  683. }
  684. } else if self.noteSearchField.isEqual(to: obj.object) {
  685. if let isEmpty = self.noteSearchField?.stringValue.isEmpty {
  686. self.noteSearchMode = !isEmpty
  687. } else {
  688. self.noteSearchMode = false
  689. }
  690. }
  691. }
  692. }
  693. // MARK: - NSTextFieldDelegate
  694. extension KMLeftSideViewController: NSTextFieldDelegate {
  695. func controlTextDidBeginEditing(_ obj: Notification) {
  696. if self.noteOutlineView.isEqual(to: obj.object) {
  697. // if (mwcFlags.isEditingTable == NO && mwcFlags.isEditingPDF == NO)
  698. // [[self document] objectDidBeginEditing:self];
  699. // mwcFlags.isEditingTable = YES;
  700. }
  701. }
  702. func controlTextDidEndEditing(_ obj: Notification) {
  703. if self.noteOutlineView.isEqual(to: obj.object) {
  704. // if (mwcFlags.isEditingTable && mwcFlags.isEditingPDF == NO)
  705. // [[self document] objectDidEndEditing:self];
  706. // mwcFlags.isEditingTable = NO;
  707. } else if let data = self.renamePDFOutlineTextField?.isEqual(to: obj.object), data {
  708. let tf = obj.object as! NSTextField
  709. if let ol = self.renamePDFOutline {
  710. if self.isRenameNoteOutline == false {
  711. if tf.stringValue == ol.label {
  712. return
  713. }
  714. }
  715. self.renamePDFOutline(ol, label: tf.stringValue)
  716. self.updateSelectRowHeight()
  717. self.tocOutlineView.reloadData()
  718. }
  719. }
  720. }
  721. }
  722. // MARK: - Menu Item Actions
  723. extension KMLeftSideViewController {
  724. @objc func toggleCaseInsensitiveNoteSearch(_ sender: AnyObject?) {
  725. self.caseInsensitiveNoteSearch = !self.caseInsensitiveNoteSearch
  726. if self.searchField.stringValue.isEmpty == false {
  727. self.searchNotes(self.searchField)
  728. }
  729. KMDataManager.ud_set(self.caseInsensitiveNoteSearch, forKey: SKCaseInsensitiveNoteSearchKey)
  730. }
  731. @objc func searchNotes(_ sender: AnyObject?) {
  732. self.noteSearchMode = false
  733. if self.findState == .note {
  734. if self.noteSearchField.isEqual(to: sender) {
  735. self.noteSearchMode = true
  736. }
  737. self.updateNoteFilterPredicate()
  738. } else {
  739. self.updateSnapshotFilterPredicate()
  740. }
  741. let textfield = sender as? NSSearchField
  742. if let data = textfield?.stringValue.isEmpty, data == false {
  743. let findPboard = NSPasteboard(name: .find)
  744. findPboard.clearContents()
  745. // findPboard.writeObjects([textfield!.stringValue])
  746. }
  747. }
  748. func updateSnapshotFilterPredicate() {
  749. let searchString = self.snapshotSearchField.stringValue
  750. self.searchSnapshots.removeAll()
  751. if self.findState == .snapshot && searchString.isEmpty == false {
  752. self.searchSnapshots = self.snapshots.filter({ model in
  753. let data = model.windowC?.string.contains(searchString) ?? false
  754. return data
  755. })
  756. }
  757. Task { @MainActor in
  758. self.updataLeftSideSnapView()
  759. self.snapshotTableView.reloadData()
  760. }
  761. }
  762. func snapshotListIsAllSelected() -> Bool {
  763. if self.snapshots.isEmpty {
  764. return false
  765. }
  766. for model in self.snapshots {
  767. if model.isSelected == false {
  768. return false
  769. }
  770. }
  771. return true
  772. }
  773. }