KMLeftSideViewController+Action.swift 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  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 (action == #selector(outlineContextMenuItemClicked_Demote)) {
  656. if let data = clickedOutline {
  657. return self.pdfDocument()?.canDemote(outline: data) ?? false
  658. }
  659. return false
  660. }
  661. if (action == #selector(outlineContextMenuItemClicked_Promote)) {
  662. if let data = clickedOutline {
  663. return self.pdfDocument()?.canPromote(outline: data) ?? false
  664. } else {
  665. return false
  666. }
  667. }
  668. if (action == #selector(outlineContextMenuItemClicked_AddAuntEntry)) {
  669. let parentOutLine = clickedOutline?.parent
  670. let grandparentOutLine = parentOutLine?.parent
  671. if (grandparentOutLine != nil) {
  672. return true
  673. } else {
  674. return false
  675. }
  676. }
  677. }
  678. return true
  679. } else if action == #selector(toggleOutlineCaseInsensitiveSearch) {
  680. menuItem.state = self.outlineIgnoreCaseFlag ? .on : .off
  681. return true
  682. }
  683. // } else if (action == @selector(note_expandAllComments:) ||
  684. // action == @selector(note_foldAllComments:) ||
  685. // action == @selector(exportAnnotationNotes:) ||
  686. // action == @selector(leftSideEmptyAnnotationClick_DeleteAnnotation:) ||
  687. // action == @selector(removeAllAnnotations:)) {
  688. // if (@available(macOS 10.13, *)) {
  689. // if (notes.count == 0) {
  690. // return NO;
  691. // } else {
  692. // return YES;
  693. // }
  694. // } else {
  695. // if (action == @selector(note_expandAllComments:) ||
  696. // action == @selector(note_foldAllComments:)) {
  697. // return NO;
  698. // }
  699. // }
  700. // } else
  701. if (action == #selector(unfoldNoteAction) ||
  702. action == #selector(foldNoteAction)) {
  703. let row = self.noteOutlineView.clickedRow
  704. // NSArray *noteArr = [rightSideController.noteArrayController arrangedObjects];
  705. // PDFAnnotation *foldNote = (PDFAnnotation *)noteArr[row];
  706. // let foldNote = self.allAnnotations[row]
  707. let foldNote = self.fetchNote(for: row)
  708. // SKNPDFAnnotationNote
  709. if foldNote is CPDFMarkupAnnotation || foldNote is CPDFTextAnnotation {
  710. return true
  711. } else {
  712. return false
  713. }
  714. } else if (action == #selector(menuItemClick_ExportPNG) ||
  715. action == #selector(menuItemClick_ExportJPG) ||
  716. action == #selector(menuItemClick_ExportPDF)) {
  717. if (self.snapshotTableView.selectedRow == -1 ) {
  718. return false
  719. } else {
  720. return true
  721. }
  722. }
  723. // else if (action == @selector(editNoteFromTable:)) {
  724. // NSInteger row = [rightSideController.noteOutlineView clickedRow];
  725. // NSArray * noteArr = [rightSideController.noteArrayController arrangedObjects];
  726. // PDFAnnotation *foldNote = (PDFAnnotation *)noteArr[row];
  727. // if (@available(macOS 10.13, *)) {
  728. // if ([foldNote.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeSignature]) {
  729. // return NO;
  730. // }
  731. // }
  732. // if ([foldNote isKindOfClass:[PDFAnnotationStamp class]] ||
  733. // [foldNote isKindOfClass:[KMAnnotationStamp class]]) {
  734. // return NO;
  735. // } else {
  736. // return YES;
  737. // }
  738. // }
  739. else if (action == #selector(menuItemClick_SelectAll)) {
  740. menuItem.state = self.snapshotListIsAllSelected() ? .on : .off
  741. return true
  742. }
  743. // else if (action == @selector(leftSideEmptyAnnotationClick_DeleteOutline:)) {
  744. // PDFOutline * item = [[pdfView document] outlineRoot];
  745. // if (self.isSearchOutlineMode) {
  746. // NSInteger num = 0;
  747. // for (NSUInteger i = 0; i < [item numberOfChildren]; i++) {
  748. // PDFOutline *outline = [item childAtIndex:i];
  749. // if ([self hasContainString:self.leftSideController.outlineSearchField.stringValue rootOutline:outline]) {
  750. // num ++;
  751. // }
  752. // }
  753. // if (num > 0)
  754. // return YES;
  755. // else
  756. // return NO;
  757. // } else {
  758. // if ([item numberOfChildren] > 0)
  759. // return YES;
  760. // else
  761. // return NO;
  762. // }
  763. // }
  764. return true
  765. }
  766. }
  767. // MARK: - NSPopoverDelegate
  768. extension KMLeftSideViewController: NSPopoverDelegate {
  769. func popoverWillClose(_ notification: Notification) {
  770. guard let popover = notification.object as? NSPopover else {
  771. return
  772. }
  773. if let vc = popover.contentViewController as? KMOutlineEditViewController {
  774. self.editOutlineUI(vc)
  775. self.tocOutlineView.reloadData()
  776. }
  777. }
  778. }
  779. // MARK: - NSSearchFieldDelegate
  780. extension KMLeftSideViewController: NSSearchFieldDelegate {
  781. func controlTextDidChange(_ obj: Notification) {
  782. if self.outlineSearchField.isEqual(to: obj.object) {
  783. if (self.outlineSearchField.stringValue.isEmpty == false) {
  784. self.isSearchOutlineMode = true
  785. self.tocOutlineView.reloadData()
  786. self.tocOutlineView.expandItem(nil, expandChildren: true)
  787. self.tocType = .unfold
  788. } else {
  789. self.isSearchOutlineMode = false
  790. self.showSearchOutlineBlankState(false)
  791. self.tocOutlineView.reloadData()
  792. }
  793. // self.leftSideEmptyVC.addOutlineBtn.enabled = !self.isSearchOutlineMode;
  794. self.outlineAddButton.isEnabled = !self.isSearchOutlineMode
  795. } else if self.snapshotSearchField.isEqual(to: obj.object) {
  796. let searchString = self.snapshotSearchField.stringValue
  797. // NSPredicate *filterPredicate = nil;
  798. // if ([searchString length] > 0) {
  799. // NSExpression *lhs = [NSExpression expressionForConstantValue:searchString];
  800. // NSExpression *rhs = [NSExpression expressionForKeyPath:@"string"];
  801. // NSUInteger options = NSDiacriticInsensitivePredicateOption;
  802. // if (mwcFlags.caseInsensitiveNoteSearch)
  803. // options |= NSCaseInsensitivePredicateOption;
  804. // filterPredicate = [NSComparisonPredicate predicateWithLeftExpression:lhs rightExpression:rhs modifier:NSDirectPredicateModifier type:NSInPredicateOperatorType options:options];
  805. // }
  806. // [rightSideController.snapshotArrayController setFilterPredicate:filterPredicate];
  807. // NSArray * snapshots = [rightSideController.snapshotArrayController arrangedObjects];
  808. self.searchSnapshots.removeAll()
  809. if searchString.isEmpty {
  810. self.isSearchSnapshotMode = false
  811. self.searchSnapshots = self.snapshots.filter({ model in
  812. let data = model.windowC?.string.contains(searchString) ?? false
  813. return data
  814. })
  815. } else {
  816. self.isSearchSnapshotMode = true
  817. }
  818. var snapshots = self.searchSnapshots
  819. if self.isSearchSnapshotMode == false {
  820. snapshots = self.snapshots
  821. }
  822. Task { @MainActor in
  823. self.updataLeftSideSnapView()
  824. self.snapshotTableView.reloadData()
  825. }
  826. if (snapshots.count > 0) {
  827. self.leftSideEmptyVC.outlineSearchView.removeFromSuperview()
  828. } else {
  829. let view = self.snapshotTableView.enclosingScrollView
  830. let viewFrmae = view?.frame ?? .zero
  831. let emptyVcSize = self.leftSideEmptyVC.outlineSearchView.frame.size
  832. self.leftSideEmptyVC.outlineSearchView.frame = NSMakeRect((viewFrmae.size.width-emptyVcSize.width)/2.0,(viewFrmae.size.height-emptyVcSize.height)/2.0, emptyVcSize.width, emptyVcSize.height)
  833. self.leftSideEmptyVC.outlineSearchView.autoresizingMask = [.minXMargin, .maxXMargin, .minYMargin, .maxYMargin]
  834. self.snapshotTableView.enclosingScrollView?.documentView?.addSubview(self.leftSideEmptyVC.outlineSearchView)
  835. }
  836. } else if self.noteSearchField.isEqual(to: obj.object) {
  837. if let isEmpty = self.noteSearchField?.stringValue.isEmpty {
  838. self.noteSearchMode = !isEmpty
  839. } else {
  840. self.noteSearchMode = false
  841. }
  842. }
  843. }
  844. }
  845. // MARK: - NSTextFieldDelegate
  846. extension KMLeftSideViewController: NSTextFieldDelegate {
  847. func controlTextDidBeginEditing(_ obj: Notification) {
  848. if self.noteOutlineView.isEqual(to: obj.object) {
  849. // if (mwcFlags.isEditingTable == NO && mwcFlags.isEditingPDF == NO)
  850. // [[self document] objectDidBeginEditing:self];
  851. // mwcFlags.isEditingTable = YES;
  852. }
  853. }
  854. func controlTextDidEndEditing(_ obj: Notification) {
  855. if self.noteOutlineView.isEqual(to: obj.object) {
  856. // if (mwcFlags.isEditingTable && mwcFlags.isEditingPDF == NO)
  857. // [[self document] objectDidEndEditing:self];
  858. // mwcFlags.isEditingTable = NO;
  859. } else if let data = self.renamePDFOutlineTextField?.isEqual(to: obj.object), data {
  860. let textField = obj.object as! NSTextField
  861. if let editPDFOutline = self.renamePDFOutline {
  862. if self.isRenameNoteOutline == false {
  863. if textField.stringValue == editPDFOutline.label {
  864. return
  865. }
  866. }
  867. self.renamePDFOutline(editPDFOutline, label: textField.stringValue)
  868. self.updateSelectRowHeight()
  869. self.tocOutlineView.reloadData()
  870. }
  871. }
  872. }
  873. }
  874. // MARK: - Menu Item Actions
  875. extension KMLeftSideViewController {
  876. @objc func toggleCaseInsensitiveNoteSearch(_ sender: AnyObject?) {
  877. self.caseInsensitiveNoteSearch = !self.caseInsensitiveNoteSearch
  878. if self.searchField.stringValue.isEmpty == false {
  879. self.searchNotes(self.searchField)
  880. }
  881. KMDataManager.ud_set(self.caseInsensitiveNoteSearch, forKey: SKCaseInsensitiveNoteSearchKey)
  882. }
  883. @objc func searchNotes(_ sender: AnyObject?) {
  884. self.noteSearchMode = false
  885. if self.findState == .note {
  886. if self.noteSearchField.isEqual(to: sender) {
  887. self.noteSearchMode = true
  888. }
  889. self.updateNoteFilterPredicate()
  890. } else {
  891. self.updateSnapshotFilterPredicate()
  892. }
  893. let textfield = sender as? NSSearchField
  894. if let data = textfield?.stringValue.isEmpty, data == false {
  895. let findPboard = NSPasteboard(name: .find)
  896. findPboard.clearContents()
  897. // findPboard.writeObjects([textfield!.stringValue])
  898. }
  899. }
  900. func updateSnapshotFilterPredicate() {
  901. let searchString = self.snapshotSearchField.stringValue
  902. self.searchSnapshots.removeAll()
  903. if self.findState == .snapshot && searchString.isEmpty == false {
  904. self.searchSnapshots = self.snapshots.filter({ model in
  905. let data = model.windowC?.string.contains(searchString) ?? false
  906. return data
  907. })
  908. }
  909. Task { @MainActor in
  910. self.updataLeftSideSnapView()
  911. self.snapshotTableView.reloadData()
  912. }
  913. }
  914. func snapshotListIsAllSelected() -> Bool {
  915. if self.snapshots.isEmpty {
  916. return false
  917. }
  918. for model in self.snapshots {
  919. if model.isSelected == false {
  920. return false
  921. }
  922. }
  923. return true
  924. }
  925. }