KMLeftSideViewController+Action.swift 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  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. self.outlineMoreMenu(button)
  48. } else if (tag == 304) {
  49. let menu = NSMenu()
  50. let object = KMPopupMenuObject()
  51. object.menuTag = 1001
  52. menu.delegate = object
  53. object.enterControllerCallback = { [weak self] isEnter in
  54. if (isEnter) {
  55. self?.moreButtonLayer?.isHidden = false
  56. } else {
  57. self?.moreButtonLayer?.isHidden = true
  58. }
  59. }
  60. let expandAllCommentsItem = menu.addItem(title: KMLocalizedString("Expand All", nil), action: #selector(note_expandAllComments), target: self)
  61. expandAllCommentsItem?.representedObject = self.noteOutlineView
  62. let foldAllCommentsItem = menu.addItem(title: KMLocalizedString("Collapse All", nil), action: #selector(note_foldAllComments), target: self)
  63. foldAllCommentsItem?.representedObject = self.noteOutlineView
  64. if (self.foldType == .unfold) {
  65. expandAllCommentsItem?.state = .on
  66. foldAllCommentsItem?.state = .off
  67. } else if (self.foldType == .fold) {
  68. expandAllCommentsItem?.state = .off
  69. foldAllCommentsItem?.state = .on
  70. } else {
  71. expandAllCommentsItem?.state = .off
  72. foldAllCommentsItem?.state = .off
  73. }
  74. let showAnnotationItem = menu.addItem(title: KMLocalizedString("Show Note", nil), action: nil, target: self)
  75. let subMenu = NSMenu()
  76. var t = subMenu.addItem(title: KMLocalizedString("Page", nil), action: #selector(noteShowNoteAction), target: self)
  77. let pageKey = self.noteTypeDict[Self.Key.noteFilterPage] as? Bool ?? false
  78. if pageKey {
  79. t?.state = .off
  80. } else {
  81. t?.state = .on
  82. }
  83. t?.representedObject = self.noteOutlineView
  84. t?.tag = 101
  85. t = subMenu.addItem(title: KMLocalizedString("Time", nil) , action: #selector(noteShowNoteAction), target: self)
  86. let timeKey = self.noteTypeDict[Self.Key.noteFilterTime] as? Bool ?? false
  87. if timeKey {
  88. t?.state = .off
  89. } else {
  90. t?.state = .on
  91. }
  92. t?.representedObject = self.noteOutlineView
  93. t?.tag = 102
  94. t = subMenu.addItem(title: KMLocalizedString("Author", nil) , action: #selector(noteShowNoteAction), target: self)
  95. let authorKey = self.noteTypeDict[Self.Key.noteFilterAuther] as? Bool ?? false
  96. if authorKey {
  97. t?.state = .off
  98. } else {
  99. t?.state = .on
  100. }
  101. t?.representedObject = self.noteOutlineView
  102. t?.tag = 103
  103. showAnnotationItem?.submenu = subMenu
  104. menu.addItem(.separator())
  105. let exportAnnotationsItem = menu.addItem(title: NSLocalizedString("Export Annotations…", comment: ""), action: nil, target: self)
  106. let subMenu2 = NSMenu()
  107. var t2 = subMenu2.addItem(title: NSLocalizedString("PDF", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  108. t2?.representedObject = self.noteOutlineView
  109. t2?.tag = 0
  110. t2 = subMenu2.addItem(title: NSLocalizedString("PDF Bundle", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  111. t2?.representedObject = self.noteOutlineView
  112. t2?.tag = 1
  113. t2 = subMenu2.addItem(title: NSLocalizedString("PDF Reader Pro Edition Notes", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  114. t2?.representedObject = self.noteOutlineView
  115. t2?.tag = 2
  116. t2 = subMenu2.addItem(title: NSLocalizedString("Notes as Text", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  117. t2?.representedObject = self.noteOutlineView
  118. t2?.tag = 3
  119. t2 = subMenu2.addItem(title: NSLocalizedString("Notes as RTF", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  120. t2?.representedObject = self.noteOutlineView
  121. t2?.tag = 4
  122. t2 = subMenu2.addItem(title: NSLocalizedString("Notes as RTFD", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  123. t2?.representedObject = self.noteOutlineView
  124. t2?.tag = 5
  125. t2 = subMenu2.addItem(title: NSLocalizedString("Notes as FDF", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  126. t2?.representedObject = self.noteOutlineView
  127. t2?.tag = 6
  128. exportAnnotationsItem?.submenu = subMenu2
  129. menu.addItem(.separator())
  130. let removeAllAnnotationsItem = menu.addItem(title: NSLocalizedString("Remove All Annotations", comment: ""), action: #selector(leftSideEmptyAnnotationClick_DeleteAnnotation), target: self)
  131. removeAllAnnotationsItem?.representedObject = self.noteOutlineView
  132. if let data = NSApp.currentEvent {
  133. NSMenu.popUpContextMenu(menu, with: data, for: button, with: nil)
  134. }
  135. }
  136. }
  137. func searchFieldChangeAction(_ editContent: String) {
  138. if self.findState == .note {
  139. self.updateNoteFilterPredicate()
  140. } else {
  141. self.updateSnapshotFilterPredicate()
  142. }
  143. if editContent.count > 0 {
  144. let findPboard = NSPasteboard(name: .find)
  145. findPboard.clearContents()
  146. findPboard.writeObjects([editContent as NSPasteboardWriting])
  147. }
  148. }
  149. @IBAction func search(_ sender: NSSearchField) {
  150. if sender.stringValue.isEmpty {
  151. self.applySearchTableHeader("")
  152. }
  153. self.delegate?.searchAction?(searchString: sender.stringValue, isCase: self.mwcFlags.caseInsensitiveSearch == 1)
  154. }
  155. // RefreshUI
  156. public func refreshUIForAddAnnotation(annos: [CPDFAnnotation]?, page: CPDFPage?) {
  157. self.updateThumbnail(at: Int(page?.pageIndex() ?? 0))
  158. self.note_reloadDataIfNeed()
  159. }
  160. public func refreshUIForAnnoAttributeDidChange(_ anno: CPDFAnnotation?, attributes: [String : Any]?) {
  161. if let data = anno {
  162. self.note_reloadDataForAnnoIfNeed(anno: data)
  163. }
  164. }
  165. public func refreshUIForDocumentChanged() {
  166. if self.type.methodType == .Thumbnail {
  167. self.resetThumbnails(ks: false)
  168. } else if self.type.methodType == .Outline {
  169. Task { @MainActor in
  170. self.tocOutlineView.reloadData()
  171. }
  172. } else if self.type.methodType == .Annotation {
  173. self.note_reloadDataIfNeed()
  174. } else if self.type.methodType == .snapshot {
  175. self.reloadSnapshotDataIfNeed()
  176. } else if self.type.methodType == .Search {
  177. if self.searchField.stringValue.isEmpty == false {
  178. self.search(self.searchField)
  179. }
  180. }
  181. }
  182. }
  183. // MARK: - Double Action
  184. extension KMLeftSideViewController {
  185. @objc func toggleSelectedSnapshots(_ sender: AnyObject?) {
  186. let indexs = self.snapshotTableView.selectedRowIndexes
  187. if indexs.isEmpty {
  188. return
  189. }
  190. let model = self.snapshots[indexs.last!]
  191. let windowC = model.windowC
  192. if let data = windowC?.window?.isVisible, data {
  193. windowC?.miniaturize()
  194. } else {
  195. windowC?.deminiaturize()
  196. }
  197. var rowIndexSet = IndexSet()
  198. let row = self.snapshotTableView.selectedRow
  199. if row >= 0 && row < self.snapshots.count {
  200. rowIndexSet.insert(row)
  201. }
  202. var columnIndexSet = IndexSet()
  203. columnIndexSet.insert(0)
  204. self.snapshotTableView.reloadData(forRowIndexes: rowIndexSet, columnIndexes: columnIndexSet)
  205. }
  206. // MARK: - KMInterfaceThemeChangedProtocol
  207. override func interfaceThemeDidChanged(_ appearance: NSAppearance.Name) {
  208. super.interfaceThemeDidChanged(appearance)
  209. self.updateViewColor()
  210. self.leftView.interfaceThemeDidChanged(appearance)
  211. }
  212. }
  213. // MARK: - NSMenuDelegate
  214. extension KMLeftSideViewController: NSMenuDelegate {
  215. func menuNeedsUpdate(_ menu: NSMenu) {
  216. menu.removeAllItems()
  217. if menu.isEqual(to: self.tocOutlineView.menu) {
  218. self.outlineListMenu(menu)
  219. return
  220. }
  221. var item: NSMenuItem?
  222. if menu.isEqual(to: self.thumbnailTableView.menu) {
  223. let row = self.thumbnailTableView.clickedRow
  224. if self.pdfDocument()?.documentURL == nil || self.thumbnailTableView.selectedRowIndexes.contains(row) == false{
  225. return
  226. }
  227. let isLocked = self.isLocked()
  228. let pageCount = self.pageCount()
  229. if (row != -1 && isLocked == false) {
  230. if(self.thumbnailTableView.selectedRowIndexes.count == pageCount) {
  231. item = menu.addItem(title: KMLocalizedString("Cut", "Menu item title"), action: nil, target: self)
  232. } else {
  233. item = menu.addItem(title: KMLocalizedString("Cut", "Menu item title"), action: #selector(cutPage), target: self)
  234. }
  235. item?.representedObject = IndexSet(integer: row)
  236. item = menu.addItem(title: KMLocalizedString("Copy", "Menu item title"), action: #selector(copyPage), target: self)
  237. item?.representedObject = IndexSet(integer: row)
  238. if (self.copyPages.count > 0) {
  239. item = menu.addItem(title: KMLocalizedString("Paste", "Menu item title"), action: #selector(pastePage), target: self)
  240. }else{
  241. item = menu.addItem(title: KMLocalizedString("Paste", "Menu item title"), action: nil, target: self)
  242. }
  243. item?.representedObject = IndexSet(integer: row)
  244. menu.addItem(.separator())
  245. if(self.thumbnailTableView.selectedRowIndexes.count == pageCount) {
  246. item = menu.addItem(title: KMLocalizedString("Delete", "Menu item title"), action: nil, target: self)
  247. } else {
  248. item = menu.addItem(title: KMLocalizedString("Delete", "Menu item title"), action: #selector(deletePage), target: self)
  249. }
  250. item?.representedObject = IndexSet(integer: row)
  251. menu.addItem(.separator())
  252. item = menu.addItem(title: KMLocalizedString("Rotate", "Menu item title"), action: #selector(rotatePageMenuAction), target: self)
  253. item?.representedObject = IndexSet(integer: row)
  254. item = menu.addItem(title: KMLocalizedString("Insert", "Menu item title"), action: nil, target: self)
  255. let subMenu = NSMenu()
  256. _ = subMenu.addItem(title: KMLocalizedString("Blank Page", "Menu item title"), action: #selector(quickInsert), target: self)
  257. _ = subMenu.addItem(title: KMLocalizedString("Blank Page - Custom...", "Menu item title"), action: #selector(insert), target: self)
  258. _ = subMenu.addItem(title: KMLocalizedString("From PDF", "Menu item title"), action: #selector(insertPDF), target: self)
  259. item?.submenu = subMenu
  260. item?.representedObject = IndexSet(integer: row)
  261. item = menu.addItem(title: KMLocalizedString("Extract", "Menu item title"), action: #selector(extractPage), target: self)
  262. item?.representedObject = IndexSet(integer: row)
  263. item = menu.addItem(title: KMLocalizedString("Page Edit", "Menu item title"), action: #selector(pageEdit), target: self)
  264. menu.addItem(.separator())
  265. var displayStr = ""
  266. if (self.isDisplayPageSize) {
  267. displayStr = KMLocalizedString("Hide Page Size", "Menu item title")
  268. } else {
  269. displayStr = KMLocalizedString("Display Page Size", "Menu item title")
  270. }
  271. item = menu.addItem(title: displayStr, action: #selector(displayPageSize), target: self)
  272. item?.representedObject = IndexSet(integer: row)
  273. if let doct = self.pdfDocument() {
  274. item = menu.addItem(title: KMLocalizedString("Share", "Menu item title"), action: nil, target: self)
  275. var tName = self.fileNameWithSelectedPages(self.thumbnailTableView.selectedRowIndexes)
  276. if (tName.count > 50) {
  277. tName = tName.substring(to: 50)
  278. }
  279. item?.submenu = NSSharingServicePicker.menu(forSharingItems: [doct.documentURL as Any], subjectContext: tName, withTarget: self, selector: #selector(sharePage), serviceDelegate: nil)
  280. }
  281. }
  282. } else if menu.isEqual(to: self.findTableView.menu) {
  283. var rowIndexes = self.findTableView.selectedRowIndexes
  284. let row = self.findTableView.clickedRow
  285. if (row != -1) {
  286. if rowIndexes.contains(row) == false {
  287. rowIndexes = IndexSet(integer: row)
  288. }
  289. var selections: [CPDFSelection] = []
  290. for (i, data) in self.searchResults.enumerated() {
  291. if rowIndexes.contains(i) {
  292. selections.append(data.selection)
  293. }
  294. }
  295. let hideNotes = self.hideNotes()
  296. let allowsNotes = self.allowsNotes()
  297. if hideNotes == false && allowsNotes {
  298. item = menu.addItem(withTitle: KMLocalizedString("Add New Circle", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.circle.rawValue)
  299. item?.representedObject = selections
  300. item = menu.addItem(withTitle: KMLocalizedString("Add New Rectangle", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.square.rawValue)
  301. item?.representedObject = selections
  302. item = menu.addItem(withTitle: KMLocalizedString("Add New Highlight", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.highlight.rawValue)
  303. item?.representedObject = selections
  304. item = menu.addItem(withTitle: KMLocalizedString("Add New Underline", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.underline.rawValue)
  305. item?.representedObject = selections
  306. item = menu.addItem(withTitle: KMLocalizedString("Add New Strikethrough", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.strikeOut.rawValue)
  307. item?.representedObject = selections
  308. }
  309. }
  310. } else if menu.isEqual(to: self.groupedFindTableView.menu) {
  311. var rowIndexes = self.groupedFindTableView.selectedRowIndexes
  312. let row = self.groupedFindTableView.clickedRow
  313. if (row != -1) {
  314. if rowIndexes.contains(row) == false {
  315. rowIndexes = IndexSet(integer: row)
  316. }
  317. // NSArray *selections = [[[leftSideController.groupedFindArrayController arrangedObjects] objectsAtIndexes:rowIndexes] valueForKeyPath:@"@unionOfArrays.matches"];
  318. var selections: [CPDFSelection] = []
  319. for (i, data) in self.groupSearchResults.enumerated() {
  320. if rowIndexes.contains(i) {
  321. for searchM in data.datas {
  322. selections.append(searchM.selection)
  323. }
  324. }
  325. }
  326. item = menu.addItem(title: KMLocalizedString("Select", "Menu item title"), action: #selector(selectSelections), target: self)
  327. item?.representedObject = selections
  328. menu.addItem(.separator())
  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.snapshotTableView.menu) {
  345. let row = self.snapshotTableView.clickedRow
  346. if (row != -1) {
  347. let model = self.snapshots[row]
  348. let controller = model.windowC
  349. if let data = controller?.window?.isVisible, data {
  350. item = menu.addItem(title: KMLocalizedString("Hide", "Menu item title"), action: #selector(hideSnapshot), target: self)
  351. item?.representedObject = controller
  352. } else {
  353. item = menu.addItem(title: KMLocalizedString("Show", "Menu item title"), action: #selector(showSnapshot), target: self)
  354. item?.representedObject = controller
  355. }
  356. menu.addItem(.separator())
  357. item = menu.addItem(title: KMLocalizedString("Export", "Menu item title"), action: nil, target: self)
  358. let subMenu = NSMenu()
  359. var t = subMenu.addItem(title: KMLocalizedString("PNG", "Menu item title"), action: #selector(menuItemClick_ExportPNG), target: self)
  360. t?.representedObject = controller
  361. t = subMenu.addItem(title: KMLocalizedString("JPG", "Menu item title"), action: #selector(menuItemClick_ExportJPG), target: self)
  362. t?.representedObject = controller
  363. t = subMenu.addItem(title: KMLocalizedString("PDF", "Menu item title"), action: #selector(menuItemClick_ExportPDF), target: self)
  364. t?.representedObject = controller
  365. item?.submenu = subMenu
  366. item = menu.addItem(title: KMLocalizedString("Print", "Menu item title"), action: #selector(menuItemClick_Print), target: self)
  367. item?.representedObject = controller
  368. menu.addItem(.separator())
  369. item = menu.addItem(title: KMLocalizedString("Copy", "Menu item title"), action: #selector(menuItemClick_Copy), target: self)
  370. item?.representedObject = controller
  371. menu.addItem(.separator())
  372. item = menu.addItem(title: KMLocalizedString("Delete", "Menu item title"), action: #selector(deleteSnapshot), target: self)
  373. item?.representedObject = controller
  374. item = menu.addItem(title: KMLocalizedString("Delete All Snapshots", "Menu item title"), action: #selector(deleteAllSnapshot), target: self)
  375. item?.representedObject = controller
  376. }
  377. } else if menu.isEqual(to: self.noteOutlineView.menu) {
  378. self.annoListMenu(menu)
  379. }
  380. }
  381. }
  382. // MARK: - NSMenuItemValidation
  383. extension KMLeftSideViewController: NSMenuItemValidation {
  384. func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  385. let isLocked = self.isLocked()
  386. if isLocked {
  387. return false
  388. }
  389. let action = menuItem.action
  390. if (action == #selector(toggleCaseInsensitiveSearch)) {
  391. let state = KMDataManager.ud_integer(forKey: SKCaseInsensitiveSearchKey)
  392. menuItem.state = state == 1 ? .on : .off
  393. return true
  394. } else if (action == #selector(toggleWholeWordSearch)) {
  395. menuItem.state = self.mwcFlags.wholeWordSearch == 1 ? .on : .off
  396. return true
  397. } else if action == #selector(toggleCaseInsensitiveNoteSearch) {
  398. menuItem.state = self.caseInsensitiveNoteSearch ? .on : .off
  399. return true
  400. }
  401. if (action == #selector(outlineContextMenuItemClicked_AddEntry) ||
  402. action == #selector(outlineContextMenuItemClicked_AddChildEntry) ||
  403. action == #selector(outlineContextMenuItemClicked_AddAuntEntry) ||
  404. action == #selector(outlineContextMenuItemClicked_RemoveEntry) ||
  405. action == #selector(outlineContextMenuItemClicked_Edit) ||
  406. action == #selector(outlineContextMenuItemClicked_SetDestination) ||
  407. action == #selector(outlineContextMenuItemClicked_Rename) ||
  408. action == #selector(outlineContextMenuItemClicked_Promote) ||
  409. action == #selector(outlineContextMenuItemClicked_Demote) ||
  410. action == #selector(leftSideEmptyAnnotationClick_DeleteOutline)) {
  411. return self.outlineListValidateMenuItem(menuItem)
  412. } else if action == #selector(toggleOutlineCaseInsensitiveSearch) {
  413. menuItem.state = self.outlineIgnoreCaseFlag ? .on : .off
  414. return true
  415. } else if (action == #selector(note_expandAllComments) ||
  416. action == #selector(note_foldAllComments) ||
  417. action == #selector(exportAnnotationNotes) ||
  418. action == #selector(leftSideEmptyAnnotationClick_DeleteAnnotation) ||
  419. action == #selector(removeAllAnnotations)) {
  420. return self.annotations.count > 0
  421. } else if (action == #selector(unfoldNoteAction) ||
  422. action == #selector(foldNoteAction)) {
  423. let row = self.noteOutlineView.clickedRow
  424. // NSArray *noteArr = [rightSideController.noteArrayController arrangedObjects];
  425. // PDFAnnotation *foldNote = (PDFAnnotation *)noteArr[row];
  426. // let foldNote = self.allAnnotations[row]
  427. let foldNote = self.fetchNote(for: row)
  428. // SKNPDFAnnotationNote
  429. if foldNote is CPDFMarkupAnnotation || foldNote is CPDFTextAnnotation {
  430. return true
  431. } else {
  432. return false
  433. }
  434. } else if (action == #selector(menuItemClick_ExportPNG) ||
  435. action == #selector(menuItemClick_ExportJPG) ||
  436. action == #selector(menuItemClick_ExportPDF)) {
  437. if (self.snapshotTableView.selectedRow == -1 ) {
  438. return false
  439. } else {
  440. return true
  441. }
  442. } else if (action == #selector(editNoteFromTable)) {
  443. let row = self.noteOutlineView.clickedRow
  444. let foldNote = self.fetchNote(for: row)
  445. // if (@available(macOS 10.13, *)) {
  446. // if ([foldNote.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeSignature]) {
  447. // return NO;
  448. // }
  449. // }
  450. if foldNote is CPDFStampAnnotation || foldNote is KMAnnotationStamp || foldNote is CPDFListStampAnnotation {
  451. return false
  452. } else {
  453. return true
  454. }
  455. } else if (action == #selector(menuItemClick_SelectAll)) {
  456. menuItem.state = self.snapshotListIsAllSelected() ? .on : .off
  457. return true
  458. }
  459. return true
  460. }
  461. }
  462. // MARK: - NSPopoverDelegate
  463. extension KMLeftSideViewController: NSPopoverDelegate {
  464. func popoverWillClose(_ notification: Notification) {
  465. guard let popover = notification.object as? NSPopover else {
  466. return
  467. }
  468. if let vc = popover.contentViewController as? KMOutlineEditViewController {
  469. self.editOutlineUI(vc)
  470. self.tocOutlineView.reloadData()
  471. }
  472. }
  473. }
  474. // MARK: - NSSearchFieldDelegate
  475. extension KMLeftSideViewController: NSSearchFieldDelegate {
  476. func controlTextDidChange(_ obj: Notification) {
  477. if self.outlineSearchField.isEqual(to: obj.object) {
  478. if (self.outlineSearchField.stringValue.isEmpty == false) {
  479. self.isSearchOutlineMode = true
  480. self.tocOutlineView.reloadData()
  481. self.tocOutlineView.expandItem(nil, expandChildren: true)
  482. self.tocType = .unfold
  483. } else {
  484. self.isSearchOutlineMode = false
  485. self.showSearchOutlineBlankState(false)
  486. self.tocOutlineView.reloadData()
  487. }
  488. self.outlineAddButton.isEnabled = !self.isSearchOutlineMode
  489. } else if self.snapshotSearchField.isEqual(to: obj.object) {
  490. let searchString = self.snapshotSearchField.stringValue
  491. // NSPredicate *filterPredicate = nil;
  492. // if ([searchString length] > 0) {
  493. // NSExpression *lhs = [NSExpression expressionForConstantValue:searchString];
  494. // NSExpression *rhs = [NSExpression expressionForKeyPath:@"string"];
  495. // NSUInteger options = NSDiacriticInsensitivePredicateOption;
  496. // if (mwcFlags.caseInsensitiveNoteSearch)
  497. // options |= NSCaseInsensitivePredicateOption;
  498. // filterPredicate = [NSComparisonPredicate predicateWithLeftExpression:lhs rightExpression:rhs modifier:NSDirectPredicateModifier type:NSInPredicateOperatorType options:options];
  499. // }
  500. // [rightSideController.snapshotArrayController setFilterPredicate:filterPredicate];
  501. // NSArray * snapshots = [rightSideController.snapshotArrayController arrangedObjects];
  502. self.searchSnapshots.removeAll()
  503. if searchString.isEmpty {
  504. self.isSearchSnapshotMode = false
  505. self.searchSnapshots = self.snapshots.filter({ model in
  506. let data = model.windowC?.string.contains(searchString) ?? false
  507. return data
  508. })
  509. } else {
  510. self.isSearchSnapshotMode = true
  511. }
  512. var snapshots = self.searchSnapshots
  513. if self.isSearchSnapshotMode == false {
  514. snapshots = self.snapshots
  515. }
  516. Task { @MainActor in
  517. self.updataLeftSideSnapView()
  518. self.snapshotTableView.reloadData()
  519. }
  520. if (snapshots.count > 0) {
  521. self.leftSideEmptyVC.outlineSearchView.removeFromSuperview()
  522. } else {
  523. let view = self.snapshotTableView.enclosingScrollView
  524. let viewFrmae = view?.frame ?? .zero
  525. let emptyVcSize = self.leftSideEmptyVC.outlineSearchView.frame.size
  526. self.leftSideEmptyVC.outlineSearchView.frame = NSMakeRect((viewFrmae.size.width-emptyVcSize.width)/2.0,(viewFrmae.size.height-emptyVcSize.height)/2.0, emptyVcSize.width, emptyVcSize.height)
  527. self.leftSideEmptyVC.outlineSearchView.autoresizingMask = [.minXMargin, .maxXMargin, .minYMargin, .maxYMargin]
  528. self.snapshotTableView.enclosingScrollView?.documentView?.addSubview(self.leftSideEmptyVC.outlineSearchView)
  529. }
  530. } else if self.noteSearchField.isEqual(to: obj.object) {
  531. if let isEmpty = self.noteSearchField?.stringValue.isEmpty {
  532. self.noteSearchMode = !isEmpty
  533. } else {
  534. self.noteSearchMode = false
  535. }
  536. }
  537. }
  538. }
  539. // MARK: - NSTextFieldDelegate
  540. extension KMLeftSideViewController: NSTextFieldDelegate {
  541. func controlTextDidBeginEditing(_ obj: Notification) {
  542. if self.noteOutlineView.isEqual(to: obj.object) {
  543. // if (mwcFlags.isEditingTable == NO && mwcFlags.isEditingPDF == NO)
  544. // [[self document] objectDidBeginEditing:self];
  545. // mwcFlags.isEditingTable = YES;
  546. }
  547. }
  548. func controlTextDidEndEditing(_ obj: Notification) {
  549. if self.noteOutlineView.isEqual(to: obj.object) {
  550. // if (mwcFlags.isEditingTable && mwcFlags.isEditingPDF == NO)
  551. // [[self document] objectDidEndEditing:self];
  552. // mwcFlags.isEditingTable = NO;
  553. } else if let data = self.renamePDFOutlineTextField?.isEqual(to: obj.object), data {
  554. let tf = obj.object as! NSTextField
  555. if let ol = self.renamePDFOutline {
  556. if self.isRenameNoteOutline == false {
  557. if tf.stringValue == ol.label {
  558. return
  559. }
  560. }
  561. self.renamePDFOutline(ol, label: tf.stringValue)
  562. self.updateSelectRowHeight()
  563. self.tocOutlineView.reloadData()
  564. }
  565. }
  566. }
  567. }
  568. // MARK: - Menu Item Actions
  569. extension KMLeftSideViewController {
  570. @objc func toggleCaseInsensitiveNoteSearch(_ sender: AnyObject?) {
  571. self.caseInsensitiveNoteSearch = !self.caseInsensitiveNoteSearch
  572. if self.searchField.stringValue.isEmpty == false {
  573. self.searchNotes(self.searchField)
  574. }
  575. KMDataManager.ud_set(self.caseInsensitiveNoteSearch, forKey: SKCaseInsensitiveNoteSearchKey)
  576. }
  577. @objc func searchNotes(_ sender: AnyObject?) {
  578. self.noteSearchMode = false
  579. if self.findState == .note {
  580. if self.noteSearchField.isEqual(to: sender) {
  581. self.noteSearchMode = true
  582. }
  583. self.updateNoteFilterPredicate()
  584. } else {
  585. self.updateSnapshotFilterPredicate()
  586. }
  587. let textfield = sender as? NSSearchField
  588. if let data = textfield?.stringValue.isEmpty, data == false {
  589. let findPboard = NSPasteboard(name: .find)
  590. findPboard.clearContents()
  591. // findPboard.writeObjects([textfield!.stringValue])
  592. }
  593. }
  594. func updateSnapshotFilterPredicate() {
  595. let searchString = self.snapshotSearchField.stringValue
  596. self.searchSnapshots.removeAll()
  597. if self.findState == .snapshot && searchString.isEmpty == false {
  598. self.searchSnapshots = self.snapshots.filter({ model in
  599. let data = model.windowC?.string.contains(searchString) ?? false
  600. return data
  601. })
  602. }
  603. Task { @MainActor in
  604. self.updataLeftSideSnapView()
  605. self.snapshotTableView.reloadData()
  606. }
  607. }
  608. func snapshotListIsAllSelected() -> Bool {
  609. if self.snapshots.isEmpty {
  610. return false
  611. }
  612. for model in self.snapshots {
  613. if model.isSelected == false {
  614. return false
  615. }
  616. }
  617. return true
  618. }
  619. }