KMLeftSideViewController+Action.swift 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  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.listView.document.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. }
  190. // MARK: - Double Action
  191. extension KMLeftSideViewController {
  192. @objc func toggleSelectedSnapshots(_ sender: AnyObject?) {
  193. let indexs = self.snapshotTableView.selectedRowIndexes
  194. if indexs.isEmpty {
  195. return
  196. }
  197. let model = self.snapshots[indexs.last!]
  198. let windowC = model.windowC
  199. if let data = windowC?.window?.isVisible, data {
  200. windowC?.miniaturize()
  201. } else {
  202. windowC?.deminiaturize()
  203. }
  204. var rowIndexSet = IndexSet()
  205. let row = self.snapshotTableView.selectedRow
  206. if row >= 0 && row < self.snapshots.count {
  207. rowIndexSet.insert(row)
  208. }
  209. var columnIndexSet = IndexSet()
  210. columnIndexSet.insert(0)
  211. self.snapshotTableView.reloadData(forRowIndexes: rowIndexSet, columnIndexes: columnIndexSet)
  212. }
  213. // MARK: - KMInterfaceThemeChangedProtocol
  214. override func interfaceThemeDidChanged(_ appearance: NSAppearance.Name) {
  215. super.interfaceThemeDidChanged(appearance)
  216. self.updateViewColor()
  217. self.leftView.interfaceThemeDidChanged(appearance)
  218. }
  219. }
  220. // MARK: - NSMenuDelegate
  221. extension KMLeftSideViewController: NSMenuDelegate {
  222. func menuNeedsUpdate(_ menu: NSMenu) {
  223. if menu.isEqual(to: self.tocOutlineView.menu) {
  224. menu.removeAllItems()
  225. _ = menu.addItem(withTitle: NSLocalizedString("Add Item", comment: ""), action: #selector(outlineContextMenuItemClicked_AddEntry), target: self, tag: KMOutlineViewMenuItemTag.addEntry.rawValue)
  226. _ = menu.addItem(withTitle: NSLocalizedString("Add Sub-Item", comment: ""), action: #selector(outlineContextMenuItemClicked_AddChildEntry), target: self, tag: KMOutlineViewMenuItemTag.addChild.rawValue)
  227. _ = menu.addItem(withTitle: NSLocalizedString("Add To A Higher Level", comment: ""), action: #selector(outlineContextMenuItemClicked_AddAuntEntry), target: self, tag: KMOutlineViewMenuItemTag.addAunt.rawValue)
  228. menu.addItem(.separator())
  229. _ = menu.addItem(withTitle: NSLocalizedString("Delete", comment: ""), action: #selector(outlineContextMenuItemClicked_RemoveEntry), target: self, tag: KMOutlineViewMenuItemTag.remove.rawValue)
  230. menu.addItem(.separator())
  231. _ = menu.addItem(withTitle: NSLocalizedString("Edit", comment: ""), action: #selector(outlineContextMenuItemClicked_Edit), target: self, tag: KMOutlineViewMenuItemTag.edit.rawValue)
  232. _ = menu.addItem(withTitle: NSLocalizedString("Change Destination", comment: ""), action: #selector(outlineContextMenuItemClicked_SetDestination), target: self, tag: KMOutlineViewMenuItemTag.setDestination.rawValue)
  233. _ = menu.addItem(withTitle: NSLocalizedString("Rename", comment: ""), action: #selector(outlineContextMenuItemClicked_Rename), target: self, tag: KMOutlineViewMenuItemTag.rename.rawValue)
  234. menu.addItem(.separator())
  235. _ = menu.addItem(withTitle: NSLocalizedString("Promote", comment: ""), action: #selector(outlineContextMenuItemClicked_Promote), target: self, tag: KMOutlineViewMenuItemTag.promote.rawValue)
  236. _ = menu.addItem(withTitle: NSLocalizedString("Demote", comment: ""), action: #selector(outlineContextMenuItemClicked_Demote), target: self, tag: KMOutlineViewMenuItemTag.demote.rawValue)
  237. return
  238. }
  239. var item: NSMenuItem?
  240. menu.removeAllItems()
  241. if menu.isEqual(to: self.thumbnailTableView.menu) {
  242. let row = self.thumbnailTableView.clickedRow
  243. if self.listView.document.documentURL == nil || self.thumbnailTableView.selectedRowIndexes.contains(row) == false{
  244. return
  245. }
  246. if (row != -1 && self.listView.document.isLocked == false) {
  247. if(self.thumbnailTableView.selectedRowIndexes.count == self.listView.document.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 == self.listView.document.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.listView?.document {
  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. if self.listView.hideNotes == false && self.listView.allowsNotes() {
  313. item = menu.addItem(withTitle: KMLocalizedString("Add New Circle", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.circle.rawValue)
  314. item?.representedObject = selections
  315. item = menu.addItem(withTitle: KMLocalizedString("Add New Rectangle", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.square.rawValue)
  316. item?.representedObject = selections
  317. item = menu.addItem(withTitle: KMLocalizedString("Add New Highlight", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.highlight.rawValue)
  318. item?.representedObject = selections
  319. item = menu.addItem(withTitle: KMLocalizedString("Add New Underline", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.underline.rawValue)
  320. item?.representedObject = selections
  321. item = menu.addItem(withTitle: KMLocalizedString("Add New Strikethrough", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.strikeOut.rawValue)
  322. item?.representedObject = selections
  323. }
  324. }
  325. } else if menu.isEqual(to: self.groupedFindTableView.menu) {
  326. var rowIndexes = self.groupedFindTableView.selectedRowIndexes
  327. let row = self.groupedFindTableView.clickedRow
  328. if (row != -1) {
  329. if rowIndexes.contains(row) == false {
  330. rowIndexes = IndexSet(integer: row)
  331. }
  332. // NSArray *selections = [[[leftSideController.groupedFindArrayController arrangedObjects] objectsAtIndexes:rowIndexes] valueForKeyPath:@"@unionOfArrays.matches"];
  333. var selections: [CPDFSelection] = []
  334. for (i, data) in self.groupSearchResults.enumerated() {
  335. if rowIndexes.contains(i) {
  336. for searchM in data.datas {
  337. selections.append(searchM.selection)
  338. }
  339. }
  340. }
  341. item = menu.addItem(title: KMLocalizedString("Select", "Menu item title"), action: #selector(selectSelections), target: self)
  342. item?.representedObject = selections
  343. menu.addItem(.separator())
  344. if self.listView.hideNotes == false && self.listView.allowsNotes() {
  345. item = menu.addItem(withTitle: KMLocalizedString("Add New Circle", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.circle.rawValue)
  346. item?.representedObject = selections
  347. item = menu.addItem(withTitle: KMLocalizedString("Add New Rectangle", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.square.rawValue)
  348. item?.representedObject = selections
  349. item = menu.addItem(withTitle: KMLocalizedString("Add New Highlight", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.highlight.rawValue)
  350. item?.representedObject = selections
  351. item = menu.addItem(withTitle: KMLocalizedString("Add New Underline", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.underline.rawValue)
  352. item?.representedObject = selections
  353. item = menu.addItem(withTitle: KMLocalizedString("Add New Strikethrough", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.strikeOut.rawValue)
  354. item?.representedObject = selections
  355. }
  356. }
  357. } else if menu.isEqual(to: self.snapshotTableView.menu) {
  358. let row = self.snapshotTableView.clickedRow
  359. if (row != -1) {
  360. let model = self.snapshots[row]
  361. let controller = model.windowC
  362. if let data = controller?.window?.isVisible, data {
  363. item = menu.addItem(title: KMLocalizedString("Hide", "Menu item title"), action: #selector(hideSnapshot), target: self)
  364. item?.representedObject = controller
  365. } else {
  366. item = menu.addItem(title: KMLocalizedString("Show", "Menu item title"), action: #selector(showSnapshot), target: self)
  367. item?.representedObject = controller
  368. }
  369. menu.addItem(.separator())
  370. item = menu.addItem(title: KMLocalizedString("Export", "Menu item title"), action: nil, target: self)
  371. let subMenu = NSMenu()
  372. var t = subMenu.addItem(title: KMLocalizedString("PNG", "Menu item title"), action: #selector(menuItemClick_ExportPNG), target: self)
  373. t?.representedObject = controller
  374. t = subMenu.addItem(title: KMLocalizedString("JPG", "Menu item title"), action: #selector(menuItemClick_ExportJPG), target: self)
  375. t?.representedObject = controller
  376. t = subMenu.addItem(title: KMLocalizedString("PDF", "Menu item title"), action: #selector(menuItemClick_ExportPDF), target: self)
  377. t?.representedObject = controller
  378. item?.submenu = subMenu
  379. item = menu.addItem(title: KMLocalizedString("Print", "Menu item title"), action: #selector(menuItemClick_Print), target: self)
  380. item?.representedObject = controller
  381. menu.addItem(.separator())
  382. item = menu.addItem(title: KMLocalizedString("Copy", "Menu item title"), action: #selector(menuItemClick_Copy), target: self)
  383. item?.representedObject = controller
  384. menu.addItem(.separator())
  385. item = menu.addItem(title: KMLocalizedString("Delete", "Menu item title"), action: #selector(deleteSnapshot), target: self)
  386. item?.representedObject = controller
  387. item = menu.addItem(title: KMLocalizedString("Delete All Snapshots", "Menu item title"), action: #selector(deleteAllSnapshot), target: self)
  388. item?.representedObject = controller
  389. }
  390. } else if menu.isEqual(to: self.noteOutlineView.menu) {
  391. var items: NSArray?
  392. var rowIndexes = self.noteOutlineView.selectedRowIndexes
  393. let row = self.noteOutlineView.clickedRow
  394. if (row != -1) {
  395. if rowIndexes.contains(row) == false {
  396. rowIndexes = IndexSet(integer: row)
  397. }
  398. items = self.noteOutlineView.itemsAtRowIndexes(rowIndexes) as NSArray
  399. // PDFAnnotation *foldNote = (PDFAnnotation *)notes[row];
  400. // let foldNote = self.allAnnotations[row]
  401. guard let foldNote = self.fetchNote(for: row) else {
  402. return
  403. }
  404. var isFold = true
  405. if self.allFoldNotes.contains(foldNote) {
  406. isFold = false
  407. }
  408. item = menu.addItem(title: KMLocalizedString("Expand", nil), action: #selector(unfoldNoteAction), target: self)
  409. if (isFold) {
  410. item?.state = .off
  411. } else {
  412. item?.state = .on
  413. }
  414. item?.representedObject = items
  415. item = menu.addItem(title: KMLocalizedString("Collapse", nil), action: #selector(foldNoteAction), target: self)
  416. if (isFold) {
  417. item?.state = .on
  418. } else {
  419. item?.state = .off
  420. }
  421. item?.representedObject = items
  422. menu.addItem(.separator())
  423. if self.listView.hideNotes == false && (items?.count ?? 0) == 1 {
  424. let annotation = self.noteItems(items!).lastObject as? CPDFAnnotation
  425. if let data = annotation?.isEditable(), data {
  426. if annotation?.type == nil {
  427. let isNote = annotation?.isNote() ?? false
  428. if isNote {
  429. // [NSLocalizedString(@"Edit", @"Menu item title") stringByAppendingEllipsis]
  430. item = menu.addItem(title: KMLocalizedString("Edit", "Menu item title"), action: #selector(editNoteTextFromTable), target: self)
  431. item?.representedObject = annotation
  432. }
  433. } else if let data = self.noteOutlineView.tableColumn(withIdentifier: NSUserInterfaceItemIdentifier("note"))?.isHidden, data {
  434. // [NSLocalizedString(@"Edit", @"Menu item title") stringByAppendingEllipsis]
  435. item = menu.addItem(title: KMLocalizedString("Edit", "Menu item title"), action: #selector(editThisAnnotation), target: self)
  436. item?.representedObject = annotation
  437. } else {
  438. item = menu.addItem(title: KMLocalizedString("Edit", "Menu item title"), action: #selector(editNoteFromTable), target: self)
  439. item?.representedObject = annotation
  440. item = menu.addItem(title: KMLocalizedString("Edit", "Menu item title"), action: #selector(editThisAnnotation), target: self)
  441. item?.representedObject = annotation
  442. item?.keyEquivalentModifierMask = [.option]
  443. item?.isAlternate = true
  444. }
  445. }
  446. }
  447. if menu.numberOfItems > 0 {
  448. item = menu.addItem(title: NSLocalizedString("Export Annotations…", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  449. let subMenu = NSMenu()
  450. item?.submenu = subMenu
  451. item = subMenu.addItem(title: NSLocalizedString("PDF", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  452. item?.tag = 0
  453. item = subMenu.addItem(title: NSLocalizedString("PDF Bundle", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  454. item?.tag = 1
  455. item = subMenu.addItem(title: NSLocalizedString("PDF Reader Pro Edition Notes", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  456. item?.tag = 2
  457. item = subMenu.addItem(title: NSLocalizedString("Notes as Texts", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  458. item?.tag = 3
  459. item = subMenu.addItem(title: NSLocalizedString("Notes as RTF", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  460. item?.tag = 4
  461. item = subMenu.addItem(title: NSLocalizedString("Notes as RTFD", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  462. item?.tag = 5
  463. item = subMenu.addItem(title: NSLocalizedString("Notes as FDF", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  464. item?.tag = 6
  465. menu.addItem(.separator())
  466. if self.outlineView(self.noteOutlineView, canDeleteItems: items as? [Any] ?? []) {
  467. item = menu.addItem(title: KMLocalizedString("Delete", "Menu item title"), action: #selector(deleteNotes), target: self)
  468. item?.representedObject = items
  469. }
  470. item = menu.addItem(title: NSLocalizedString("Remove All Annotations", tableName: "MainMenu", comment: "Menu item title"), action: #selector(removeAllAnnotations), target: self)
  471. }
  472. } else {
  473. let subMenu = NSMenu()
  474. item?.submenu = subMenu
  475. item = subMenu.addItem(title: NSLocalizedString("PDF", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  476. item?.tag = 0
  477. item = subMenu.addItem(title: NSLocalizedString("PDF Bundle", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  478. item?.tag = 1
  479. item = subMenu.addItem(title: NSLocalizedString("PDF Reader Pro Edition Notes", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  480. item?.tag = 2
  481. item = subMenu.addItem(title: NSLocalizedString("Notes as Texts", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  482. item?.tag = 3
  483. item = subMenu.addItem(title: NSLocalizedString("Notes as RTF", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  484. item?.tag = 4
  485. item = subMenu.addItem(title: NSLocalizedString("Notes as RTFD", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  486. item?.tag = 5
  487. item = subMenu.addItem(title: NSLocalizedString("Notes as FDF", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  488. item?.tag = 6
  489. item = menu.addItem(title: NSLocalizedString("Remove All Annotations", tableName: "MainMenu", comment: "Menu item title"), action: #selector(removeAllAnnotations), target: self)
  490. }
  491. }
  492. }
  493. }
  494. // MARK: - NSMenuItemValidation
  495. extension KMLeftSideViewController: NSMenuItemValidation {
  496. func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  497. if (self.listView.document == nil || self.listView.document!.isLocked) {
  498. return false
  499. }
  500. let action = menuItem.action
  501. // if (self.isCompareMode) {
  502. // return NO;
  503. // }
  504. // if (action == @selector(createNewNote:)) {
  505. // BOOL isMarkup = [menuItem tag] == SKHighlightNote || [menuItem tag] == SKUnderlineNote || [menuItem tag] == SKStrikeOutNote;
  506. // isMarkup = NO;
  507. // return [self interactionMode] != SKPresentationMode && [[self pdfDocument] allowsNotes] && ([pdfView toolMode] == SKTextToolMode || [pdfView toolMode] == SKNoteToolMode) && [pdfView hideNotes] == NO && (isMarkup == NO || [[pdfView currentSelection] hasCharacters]);
  508. // } else if (action == @selector(toggleHideNotes:)) {
  509. // if ([pdfView hideNotes])
  510. // [menuItem setTitle:NSLocalizedString(@"Show Notes", @"Menu item title")];
  511. // else
  512. // [menuItem setTitle:NSLocalizedString(@"Hide Notes", @"Menu item title")];
  513. // return YES;
  514. // }
  515. //
  516. if (action == #selector(toggleCaseInsensitiveSearch)) {
  517. let state = KMDataManager.ud_integer(forKey: SKCaseInsensitiveSearchKey)
  518. menuItem.state = state == 1 ? .on : .off
  519. return true
  520. } else if (action == #selector(toggleWholeWordSearch)) {
  521. menuItem.state = self.mwcFlags.wholeWordSearch == 1 ? .on : .off
  522. return true
  523. } else if action == #selector(toggleCaseInsensitiveNoteSearch) {
  524. menuItem.state = self.caseInsensitiveNoteSearch ? .on : .off
  525. return true
  526. }
  527. // else if (action == @selector(toggleAutoResizeNoteRows:)) {
  528. // [menuItem setState:mwcFlags.autoResizeNoteRows ? NSOnState : NSOffState];
  529. // return YES;
  530. // } else if (action == @selector(performFindPanelAction:)) {
  531. // if ([self interactionMode] == SKPresentationMode)
  532. // return NO;
  533. // switch ([menuItem tag]) {
  534. // case NSFindPanelActionShowFindPanel:
  535. // return YES;
  536. // case NSFindPanelActionNext:
  537. // case NSFindPanelActionPrevious:
  538. // return YES;
  539. // case NSFindPanelActionSetFindString:
  540. // return [[[self pdfView] currentSelection] hasCharacters];
  541. // default:
  542. // return NO;
  543. // }
  544. // } else
  545. // if (action == @selector(highlightFormFiled:)) {
  546. // BOOL highlightFormFiled = [[NSUserDefaults standardUserDefaults] boolForKey:@"kPDFViewHighlightFormFiledKey"];
  547. // [menuItem setState:highlightFormFiled ? NSOnState : NSOffState];
  548. // return YES;
  549. // } else if (action == @selector(highlightLinks:)) {
  550. // BOOL highlightLinks = [[NSUserDefaults standardUserDefaults] boolForKey:@"kPDFViewHighlightLinksKey"];
  551. // [menuItem setState:highlightLinks ? NSOnState : NSOffState];
  552. // return YES;
  553. // } else if (action == @selector(link:)) {
  554. // return ([self.pdfView toolMode] == SKTextToolMode || [self.pdfView toolMode] == SKNoteToolMode) && [self.pdfView hideNotes] == NO && [self.pdfView.document isLocked] == NO;
  555. // return ([self.pdfView toolMode] == SKTextToolMode || [self.pdfView toolMode] == SKNoteToolMode) && [self.pdfView hideNotes] == NO && [self.pdfView.document isLocked] == NO && [[self.pdfView currentSelection] hasCharacters];
  556. // } else if (action == @selector(deletePage:)) {
  557. // return self.pdfView.document.pageCount > 1 ? YES : NO;
  558. // } else if (action == @selector(themesColor:)){
  559. // if (KMPDFViewModeNormal== self.pdfView.viewMode) {
  560. // [menuItem setState:(menuItem.tag == 0)?NSOnState:NSOffState];
  561. // } else if (KMPDFViewModeSoft == self.pdfView.viewMode){
  562. // [menuItem setState:(menuItem.tag == 1)?NSOnState:NSOffState];
  563. // } else if (KMPDFViewModeNight == self.pdfView.viewMode){
  564. // [menuItem setState:(menuItem.tag == 2)?NSOnState:NSOffState];
  565. // } else if (KMPDFViewModeGreen == self.pdfView.viewMode){
  566. // [menuItem setState:(menuItem.tag == 3)?NSOnState:NSOffState];
  567. // } else if (KMPDFViewModeThemes1 == self.pdfView.viewMode){
  568. // [menuItem setState:(menuItem.tag == 4)?NSOnState:NSOffState];
  569. // } else if (KMPDFViewModeThemes2 == self.pdfView.viewMode){
  570. // [menuItem setState:(menuItem.tag == 5)?NSOnState:NSOffState];
  571. // } else if (KMPDFViewModeThemes3 == self.pdfView.viewMode){
  572. // [menuItem setState:(menuItem.tag == 6)?NSOnState:NSOffState];
  573. // } else if (KMPDFViewModeThemes4 == self.pdfView.viewMode){
  574. // [menuItem setState:(menuItem.tag == 7)?NSOnState:NSOffState];
  575. // }
  576. // // else {
  577. // // [menuItem setState:(menuItem.tag == 4)?NSOnState:NSOffState];
  578. // // }
  579. // NSData * data = [[NSUserDefaults standardUserDefaults] objectForKey:@"kKMPDFViewModeThemesArray"] ? : nil;
  580. // NSInteger themesCount;
  581. // if (data) {
  582. // NSArray * appArray = [NSKeyedUnarchiver unarchiveObjectWithData:data];
  583. // NSMutableArray * mutableArray = [NSMutableArray arrayWithArray:appArray];
  584. // themesCount = [menuItem tag] - mutableArray.count;
  585. // } else {
  586. // themesCount = [menuItem tag] - 4;
  587. // }
  588. // if (themesCount >= 0) {
  589. // menuItem.hidden = YES;
  590. // return NO;
  591. // } else {
  592. // menuItem.hidden = NO;
  593. // return YES;
  594. // }
  595. // } else
  596. if (action == #selector(outlineContextMenuItemClicked_AddEntry) ||
  597. action == #selector(outlineContextMenuItemClicked_AddChildEntry) ||
  598. action == #selector(outlineContextMenuItemClicked_AddAuntEntry) ||
  599. action == #selector(outlineContextMenuItemClicked_RemoveEntry) ||
  600. action == #selector(outlineContextMenuItemClicked_Edit) ||
  601. action == #selector(outlineContextMenuItemClicked_SetDestination) ||
  602. action == #selector(outlineContextMenuItemClicked_Rename) ||
  603. action == #selector(outlineContextMenuItemClicked_Promote) ||
  604. action == #selector(outlineContextMenuItemClicked_Demote)) {
  605. if let data = self.listView.document?.isLocked, data {
  606. return false
  607. }
  608. if (self.isSearchOutlineMode) {
  609. if (action == #selector(outlineContextMenuItemClicked_AddEntry) ||
  610. action == #selector(outlineContextMenuItemClicked_AddChildEntry) ||
  611. action == #selector(outlineContextMenuItemClicked_AddAuntEntry) ||
  612. action == #selector(outlineContextMenuItemClicked_Edit) ||
  613. action == #selector(outlineContextMenuItemClicked_Rename)
  614. ) {
  615. return false
  616. }
  617. }
  618. if (self.tocOutlineView.selectedRowIndexes.count > 1) {
  619. if (menuItem.tag == KMOutlineViewMenuItemTag.remove.rawValue) {
  620. return true
  621. }
  622. return false
  623. } else if (self.tocOutlineView.selectedRowIndexes.count > 0) {
  624. if (action == #selector(outlineContextMenuItemClicked_AddChildEntry) ||
  625. action == #selector(outlineContextMenuItemClicked_SetDestination)) {
  626. return true
  627. }
  628. }
  629. if (self.tocOutlineView.clickedRow == -1) {
  630. if (action == #selector(outlineContextMenuItemClicked_AddEntry)) {
  631. return true
  632. } else {
  633. return false
  634. }
  635. } else {
  636. let clickedOutline = self.tocOutlineView.item(atRow: self.tocOutlineView.clickedRow) as? CPDFOutline
  637. if let data = clickedOutline?.index, data > 0 {
  638. if (action == #selector(outlineContextMenuItemClicked_Demote)) {
  639. return true
  640. }
  641. } else {
  642. if (action == #selector(outlineContextMenuItemClicked_Demote)) {
  643. return false
  644. }
  645. }
  646. let parentOutLine = clickedOutline?.parent
  647. let grandparentOutLine = parentOutLine?.parent
  648. //if clicked PDFOutline 's parent exist
  649. if (grandparentOutLine != nil) {
  650. //and title is equla "add_AuntOutlineKey"
  651. if (action == #selector(outlineContextMenuItemClicked_AddAuntEntry)) {
  652. return true
  653. } else if (action == #selector(outlineContextMenuItemClicked_Promote)){
  654. return true
  655. }
  656. } else {
  657. if (action == #selector(outlineContextMenuItemClicked_AddAuntEntry)) {
  658. return false
  659. }else if (action == #selector(outlineContextMenuItemClicked_Promote)){
  660. return false
  661. }
  662. }
  663. }
  664. return true
  665. }
  666. // else
  667. // if ([menuItem action] == @selector(toggleEncryptFilePanel:)) {
  668. // [menuItem setTitle:NSLocalizedString(@"Set Passwords", @"Menu item title")];
  669. // NSURL *fileURL = [self.pdfView.document documentURL];
  670. // if (!fileURL) {
  671. // return YES;
  672. // }
  673. // PDFDocument *pdfDoc = [[[PDFDocument alloc] initWithURL:fileURL] autorelease];
  674. //
  675. // if ([pdfDoc isLocked]) {
  676. // [menuItem setTitle:NSLocalizedString(@"Remove Security", @"Menu item title")];
  677. // }
  678. // } else if (action == @selector(toggleToolbarShow:)) {
  679. // NSToolbar *toolbar = [self.window toolbar];
  680. // if ([toolbar isVisible])
  681. // [menuItem setTitle:NSLocalizedString(@"Hide Toolbar", @"Menu item title")];
  682. // else
  683. // [menuItem setTitle:NSLocalizedString(@"Show Toolbar", @"Menu item title")];
  684. // return YES;
  685. // } else if (action == @selector(readMode:)) {
  686. // menuItem.state = self.isReadMode ? NSControlStateValueOn : NSControlStateValueOff;
  687. // return YES;
  688. // } else if (action == @selector(addForm:)) {
  689. // return YES;
  690. else if action == #selector(toggleOutlineCaseInsensitiveSearch) {
  691. menuItem.state = self.outlineIgnoreCaseFlag ? .on : .off
  692. return true
  693. }
  694. // } else if (action == @selector(note_expandAllComments:) ||
  695. // action == @selector(note_foldAllComments:) ||
  696. // action == @selector(exportAnnotationNotes:) ||
  697. // action == @selector(leftSideEmptyAnnotationClick_DeleteAnnotation:) ||
  698. // action == @selector(removeAllAnnotations:)) {
  699. // if (@available(macOS 10.13, *)) {
  700. // if (notes.count == 0) {
  701. // return NO;
  702. // } else {
  703. // return YES;
  704. // }
  705. // } else {
  706. // if (action == @selector(note_expandAllComments:) ||
  707. // action == @selector(note_foldAllComments:)) {
  708. // return NO;
  709. // }
  710. // }
  711. // } else
  712. if (action == #selector(unfoldNoteAction) ||
  713. action == #selector(foldNoteAction)) {
  714. let row = self.noteOutlineView.clickedRow
  715. // NSArray *noteArr = [rightSideController.noteArrayController arrangedObjects];
  716. // PDFAnnotation *foldNote = (PDFAnnotation *)noteArr[row];
  717. // let foldNote = self.allAnnotations[row]
  718. let foldNote = self.fetchNote(for: row)
  719. // SKNPDFAnnotationNote
  720. if foldNote is CPDFMarkupAnnotation || foldNote is CPDFTextAnnotation {
  721. return true
  722. } else {
  723. return false
  724. }
  725. } else if (action == #selector(menuItemClick_ExportPNG) ||
  726. action == #selector(menuItemClick_ExportJPG) ||
  727. action == #selector(menuItemClick_ExportPDF)) {
  728. if (self.snapshotTableView.selectedRow == -1 ) {
  729. return false
  730. } else {
  731. return true
  732. }
  733. }
  734. // else if (action == @selector(editNoteFromTable:)) {
  735. // NSInteger row = [rightSideController.noteOutlineView clickedRow];
  736. // NSArray * noteArr = [rightSideController.noteArrayController arrangedObjects];
  737. // PDFAnnotation *foldNote = (PDFAnnotation *)noteArr[row];
  738. // if (@available(macOS 10.13, *)) {
  739. // if ([foldNote.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeSignature]) {
  740. // return NO;
  741. // }
  742. // }
  743. // if ([foldNote isKindOfClass:[PDFAnnotationStamp class]] ||
  744. // [foldNote isKindOfClass:[KMAnnotationStamp class]]) {
  745. // return NO;
  746. // } else {
  747. // return YES;
  748. // }
  749. // }
  750. else if (action == #selector(menuItemClick_SelectAll)) {
  751. menuItem.state = self.snapshotListIsAllSelected() ? .on : .off
  752. return true
  753. }
  754. // else if (action == @selector(leftSideEmptyAnnotationClick_DeleteOutline:)) {
  755. // PDFOutline * item = [[pdfView document] outlineRoot];
  756. // if (self.isSearchOutlineMode) {
  757. // NSInteger num = 0;
  758. // for (NSUInteger i = 0; i < [item numberOfChildren]; i++) {
  759. // PDFOutline *outline = [item childAtIndex:i];
  760. // if ([self hasContainString:self.leftSideController.outlineSearchField.stringValue rootOutline:outline]) {
  761. // num ++;
  762. // }
  763. // }
  764. // if (num > 0)
  765. // return YES;
  766. // else
  767. // return NO;
  768. // } else {
  769. // if ([item numberOfChildren] > 0)
  770. // return YES;
  771. // else
  772. // return NO;
  773. // }
  774. // }
  775. return true
  776. }
  777. }
  778. // MARK: - NSPopoverDelegate
  779. extension KMLeftSideViewController: NSPopoverDelegate {
  780. func popoverWillClose(_ notification: Notification) {
  781. guard let popover = notification.object as? NSPopover else {
  782. return
  783. }
  784. if let vc = popover.contentViewController as? KMOutlineEditViewController {
  785. self.editOutlineUI(vc)
  786. self.tocOutlineView.reloadData()
  787. }
  788. }
  789. }
  790. // MARK: - NSSearchFieldDelegate
  791. extension KMLeftSideViewController: NSSearchFieldDelegate {
  792. func controlTextDidChange(_ obj: Notification) {
  793. if self.outlineSearchField.isEqual(to: obj.object) {
  794. if (self.outlineSearchField.stringValue.isEmpty == false) {
  795. self.isSearchOutlineMode = true
  796. self.tocOutlineView.reloadData()
  797. self.tocOutlineView.expandItem(nil, expandChildren: true)
  798. self.tocType = .unfold
  799. } else {
  800. self.isSearchOutlineMode = false
  801. self.showSearchOutlineBlankState(false)
  802. self.tocOutlineView.reloadData()
  803. }
  804. // self.leftSideEmptyVC.addOutlineBtn.enabled = !self.isSearchOutlineMode;
  805. self.outlineAddButton.isEnabled = !self.isSearchOutlineMode
  806. } else if self.snapshotSearchField.isEqual(to: obj.object) {
  807. let searchString = self.snapshotSearchField.stringValue
  808. // NSPredicate *filterPredicate = nil;
  809. // if ([searchString length] > 0) {
  810. // NSExpression *lhs = [NSExpression expressionForConstantValue:searchString];
  811. // NSExpression *rhs = [NSExpression expressionForKeyPath:@"string"];
  812. // NSUInteger options = NSDiacriticInsensitivePredicateOption;
  813. // if (mwcFlags.caseInsensitiveNoteSearch)
  814. // options |= NSCaseInsensitivePredicateOption;
  815. // filterPredicate = [NSComparisonPredicate predicateWithLeftExpression:lhs rightExpression:rhs modifier:NSDirectPredicateModifier type:NSInPredicateOperatorType options:options];
  816. // }
  817. // [rightSideController.snapshotArrayController setFilterPredicate:filterPredicate];
  818. // NSArray * snapshots = [rightSideController.snapshotArrayController arrangedObjects];
  819. self.searchSnapshots.removeAll()
  820. if searchString.isEmpty {
  821. self.isSearchSnapshotMode = false
  822. self.searchSnapshots = self.snapshots.filter({ model in
  823. let data = model.windowC?.string.contains(searchString) ?? false
  824. return data
  825. })
  826. } else {
  827. self.isSearchSnapshotMode = true
  828. }
  829. var snapshots = self.searchSnapshots
  830. if self.isSearchSnapshotMode == false {
  831. snapshots = self.snapshots
  832. }
  833. Task { @MainActor in
  834. self.updataLeftSideSnapView()
  835. self.snapshotTableView.reloadData()
  836. }
  837. if (snapshots.count > 0) {
  838. self.leftSideEmptyVC.outlineSearchView.removeFromSuperview()
  839. } else {
  840. let view = self.snapshotTableView.enclosingScrollView
  841. let viewFrmae = view?.frame ?? .zero
  842. let emptyVcSize = self.leftSideEmptyVC.outlineSearchView.frame.size
  843. self.leftSideEmptyVC.outlineSearchView.frame = NSMakeRect((viewFrmae.size.width-emptyVcSize.width)/2.0,(viewFrmae.size.height-emptyVcSize.height)/2.0, emptyVcSize.width, emptyVcSize.height)
  844. self.leftSideEmptyVC.outlineSearchView.autoresizingMask = [.minXMargin, .maxXMargin, .minYMargin, .maxYMargin]
  845. self.snapshotTableView.enclosingScrollView?.documentView?.addSubview(self.leftSideEmptyVC.outlineSearchView)
  846. }
  847. }
  848. }
  849. }
  850. // MARK: - NSTextFieldDelegate
  851. extension KMLeftSideViewController: NSTextFieldDelegate {
  852. func controlTextDidBeginEditing(_ obj: Notification) {
  853. if self.noteOutlineView.isEqual(to: obj.object) {
  854. // if (mwcFlags.isEditingTable == NO && mwcFlags.isEditingPDF == NO)
  855. // [[self document] objectDidBeginEditing:self];
  856. // mwcFlags.isEditingTable = YES;
  857. }
  858. }
  859. func controlTextDidEndEditing(_ obj: Notification) {
  860. if self.noteOutlineView.isEqual(to: obj.object) {
  861. // if (mwcFlags.isEditingTable && mwcFlags.isEditingPDF == NO)
  862. // [[self document] objectDidEndEditing:self];
  863. // mwcFlags.isEditingTable = NO;
  864. } else if let data = self.renamePDFOutlineTextField?.isEqual(to: obj.object), data {
  865. let textField = obj.object as! NSTextField
  866. if let editPDFOutline = self.renamePDFOutline {
  867. if self.isRenameNoteOutline == false {
  868. if textField.stringValue == editPDFOutline.label {
  869. return
  870. }
  871. }
  872. self.renamePDFOutline(editPDFOutline, label: textField.stringValue)
  873. self.updateSelectRowHeight()
  874. self.tocOutlineView.reloadData()
  875. }
  876. }
  877. }
  878. }
  879. // MARK: - Menu Item Actions
  880. extension KMLeftSideViewController {
  881. @objc func toggleCaseInsensitiveNoteSearch(_ sender: AnyObject?) {
  882. self.caseInsensitiveNoteSearch = !self.caseInsensitiveNoteSearch
  883. if self.searchField.stringValue.isEmpty == false {
  884. self.searchNotes(self.searchField)
  885. }
  886. KMDataManager.ud_set(self.caseInsensitiveNoteSearch, forKey: SKCaseInsensitiveNoteSearchKey)
  887. }
  888. @objc func searchNotes(_ sender: AnyObject?) {
  889. self.noteSearchMode = false
  890. if self.findState == .note {
  891. if self.noteSearchField.isEqual(to: sender) {
  892. self.noteSearchMode = true
  893. }
  894. self.updateNoteFilterPredicate()
  895. } else {
  896. self.updateSnapshotFilterPredicate()
  897. }
  898. let textfield = sender as? NSSearchField
  899. if let data = textfield?.stringValue.isEmpty, data == false {
  900. let findPboard = NSPasteboard(name: .find)
  901. findPboard.clearContents()
  902. // findPboard.writeObjects([textfield!.stringValue])
  903. }
  904. }
  905. func updateSnapshotFilterPredicate() {
  906. let searchString = self.snapshotSearchField.stringValue
  907. self.searchSnapshots.removeAll()
  908. if self.findState == .snapshot && searchString.isEmpty == false {
  909. self.searchSnapshots = self.snapshots.filter({ model in
  910. let data = model.windowC?.string.contains(searchString) ?? false
  911. return data
  912. })
  913. }
  914. Task { @MainActor in
  915. self.updataLeftSideSnapView()
  916. self.snapshotTableView.reloadData()
  917. }
  918. }
  919. func snapshotListIsAllSelected() -> Bool {
  920. if self.snapshots.isEmpty {
  921. return false
  922. }
  923. for model in self.snapshots {
  924. if model.isSelected == false {
  925. return false
  926. }
  927. }
  928. return true
  929. }
  930. }