KMLeftSideViewController+Action.swift 55 KB

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