KMLeftSideViewController+Action.swift 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  1. //
  2. // KMLeftSideViewController+Action.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/12/22.
  6. //
  7. import Foundation
  8. // MARK: - Action
  9. extension KMLeftSideViewController {
  10. @IBAction func leftSideViewMoreButtonAction(_ sender: AnyObject?) {
  11. guard let button = sender as? NSButton else {
  12. NSSound.beep()
  13. return
  14. }
  15. let tag = button.tag
  16. if (tag == 300) {
  17. var selectedRow = 0
  18. if (self.snapshotTableView.selectedRow >= 0) {
  19. selectedRow = self.snapshotTableView.selectedRow;
  20. } else {
  21. return
  22. }
  23. let model = self.snapshots[selectedRow]
  24. let controller = model.windowC
  25. let menu = NSMenu()
  26. let itemExport = menu.addItem(title: KMLocalizedString("Export", "Menu item title"), action: nil, target: self)
  27. let subMenu = NSMenu()
  28. var t = subMenu.addItem(title: KMLocalizedString("PNG", "Menu item title"), action: #selector(menuItemClick_ExportPNG), target: self)
  29. t?.representedObject = controller
  30. t = subMenu.addItem(title: KMLocalizedString("JPG", "Menu item title"), action: #selector(menuItemClick_ExportJPG), target: self)
  31. t?.representedObject = controller
  32. t = subMenu.addItem(title: KMLocalizedString("PDF", "Menu item title"), action: #selector(menuItemClick_ExportPDF), target: self)
  33. t?.representedObject = controller
  34. itemExport?.submenu = subMenu
  35. let itemPrint = menu.addItem(title: KMLocalizedString("Print", "Menu item title"), action: #selector(menuItemClick_Print), target: self)
  36. itemPrint?.representedObject = controller
  37. menu.addItem(.separator())
  38. let itemSelectAll = menu.addItem(title: KMLocalizedString("Select All", "Menu item title"), action: #selector(menuItemClick_SelectAll), target: self)
  39. itemSelectAll?.representedObject = controller
  40. menu.addItem(.separator())
  41. let itemDeleteAllSnapshot = menu.addItem(title: KMLocalizedString("Delete All Snapshots", "Menu item title"), action: #selector(deleteAllSnapshot), target: self)
  42. itemDeleteAllSnapshot?.representedObject = controller
  43. if let data = NSApp.currentEvent {
  44. NSMenu.popUpContextMenu(menu, with: data, for: button)
  45. }
  46. } else if (tag == 302) {
  47. let menu = NSMenu()
  48. let expandAllCommentsItem = menu.addItem(title: KMLocalizedString("Expand All", nil), action: #selector(toc_expandAllComments), target: self)
  49. expandAllCommentsItem?.representedObject = self.tocOutlineView
  50. let foldAllCommentsItem = menu.addItem(title: KMLocalizedString("Collapse All", nil), action: #selector(toc_foldAllComments), target: self)
  51. expandAllCommentsItem?.representedObject = self.tocOutlineView
  52. let item = self.listView.document.outlineRoot()
  53. var num = 0
  54. for i in 0 ..< Int(item?.numberOfChildren ?? 0) {
  55. let outline = item?.child(at: UInt(i))
  56. if self.tocOutlineView.isItemExpanded(outline) {
  57. num += 1
  58. }
  59. }
  60. if let cnt = item?.numberOfChildren, cnt > 0 && num == 0 {
  61. self.tocType = .fold
  62. } else if let cnt = item?.numberOfChildren, cnt > 0 && num == cnt {
  63. self.tocType = .unfold
  64. } else {
  65. self.tocType = .none
  66. }
  67. if (self.tocType == .unfold) {
  68. expandAllCommentsItem?.state = .on
  69. foldAllCommentsItem?.state = .off
  70. } else if (self.tocType == .fold) {
  71. expandAllCommentsItem?.state = .off
  72. foldAllCommentsItem?.state = .on
  73. } else {
  74. expandAllCommentsItem?.state = .off
  75. foldAllCommentsItem?.state = .off
  76. }
  77. let removeEntryItem = menu.addItem(title: KMLocalizedString("Remove All Outlines", nil), action: #selector(leftSideEmptyAnnotationClick_DeleteOutline), target: self)
  78. removeEntryItem?.representedObject = self.tocOutlineView
  79. if let data = NSApp.currentEvent {
  80. NSMenu.popUpContextMenu(menu, with: data, for: button)
  81. }
  82. } else if (tag == 304) {
  83. let menu = NSMenu()
  84. let object = KMPopupMenuObject()
  85. object.menuTag = 1001
  86. menu.delegate = object
  87. object.enterControllerCallback = { [weak self] isEnter in
  88. if (isEnter) {
  89. self?.moreButtonLayer?.isHidden = false
  90. } else {
  91. self?.moreButtonLayer?.isHidden = true
  92. }
  93. }
  94. let expandAllCommentsItem = menu.addItem(title: KMLocalizedString("Expand All", nil), action: #selector(note_expandAllComments), target: self)
  95. expandAllCommentsItem?.representedObject = self.noteOutlineView
  96. let foldAllCommentsItem = menu.addItem(title: KMLocalizedString("Collapse All", nil), action: #selector(note_foldAllComments), target: self)
  97. foldAllCommentsItem?.representedObject = self.noteOutlineView
  98. if (self.foldType == .unfold) {
  99. expandAllCommentsItem?.state = .on
  100. foldAllCommentsItem?.state = .off
  101. } else if (self.foldType == .fold) {
  102. expandAllCommentsItem?.state = .off
  103. foldAllCommentsItem?.state = .on
  104. } else {
  105. expandAllCommentsItem?.state = .off
  106. foldAllCommentsItem?.state = .off
  107. }
  108. let showAnnotationItem = menu.addItem(title: KMLocalizedString("Show Note", nil), action: nil, target: self)
  109. let subMenu = NSMenu()
  110. var t = subMenu.addItem(title: KMLocalizedString("Page", nil), action: #selector(noteShowNoteAction), target: self)
  111. let pageKey = self.noteTypeDict[Self.Key.noteFilterPage] as? Bool ?? false
  112. if pageKey {
  113. t?.state = .off
  114. } else {
  115. t?.state = .on
  116. }
  117. t?.representedObject = self.noteOutlineView
  118. t?.tag = 101
  119. t = subMenu.addItem(title: KMLocalizedString("Time", nil) , action: #selector(noteShowNoteAction), target: self)
  120. let timeKey = self.noteTypeDict[Self.Key.noteFilterTime] as? Bool ?? false
  121. if timeKey {
  122. t?.state = .off
  123. } else {
  124. t?.state = .on
  125. }
  126. t?.representedObject = self.noteOutlineView
  127. t?.tag = 102
  128. t = subMenu.addItem(title: KMLocalizedString("Author", nil) , action: #selector(noteShowNoteAction), target: self)
  129. let authorKey = self.noteTypeDict[Self.Key.noteFilterAuther] as? Bool ?? false
  130. if authorKey {
  131. t?.state = .off
  132. } else {
  133. t?.state = .on
  134. }
  135. t?.representedObject = self.noteOutlineView
  136. t?.tag = 103
  137. showAnnotationItem?.submenu = subMenu
  138. menu.addItem(.separator())
  139. let exportAnnotationsItem = menu.addItem(title: NSLocalizedString("Export Annotations…", comment: ""), action: nil, target: self)
  140. let subMenu2 = NSMenu()
  141. var t2 = subMenu2.addItem(title: NSLocalizedString("PDF", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  142. t2?.representedObject = self.noteOutlineView
  143. t2?.tag = 0
  144. t2 = subMenu2.addItem(title: NSLocalizedString("PDF Bundle", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  145. t2?.representedObject = self.noteOutlineView
  146. t2?.tag = 1
  147. t2 = subMenu2.addItem(title: NSLocalizedString("PDF Reader Pro Edition Notes", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  148. t2?.representedObject = self.noteOutlineView
  149. t2?.tag = 2
  150. t2 = subMenu2.addItem(title: NSLocalizedString("Notes as Text", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  151. t2?.representedObject = self.noteOutlineView
  152. t2?.tag = 3
  153. t2 = subMenu2.addItem(title: NSLocalizedString("Notes as RTF", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  154. t2?.representedObject = self.noteOutlineView
  155. t2?.tag = 4
  156. t2 = subMenu2.addItem(title: NSLocalizedString("Notes as RTFD", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  157. t2?.representedObject = self.noteOutlineView
  158. t2?.tag = 5
  159. t2 = subMenu2.addItem(title: NSLocalizedString("Notes as FDF", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  160. t2?.representedObject = self.noteOutlineView
  161. t2?.tag = 6
  162. exportAnnotationsItem?.submenu = subMenu2
  163. menu.addItem(.separator())
  164. let removeAllAnnotationsItem = menu.addItem(title: NSLocalizedString("Remove All Annotations", comment: ""), action: #selector(leftSideEmptyAnnotationClick_DeleteAnnotation), target: self)
  165. removeAllAnnotationsItem?.representedObject = self.noteOutlineView
  166. if let data = NSApp.currentEvent {
  167. NSMenu.popUpContextMenu(menu, with: data, for: button, with: nil)
  168. }
  169. }
  170. }
  171. func searchFieldChangeAction(_ editContent: String) {
  172. if self.findState == .note {
  173. // if (mwcFlags.findState == SKFindStateNote)
  174. self.updateNoteFilterPredicate()
  175. } else {
  176. self.updateSnapshotFilterPredicate()
  177. }
  178. if editContent.count > 0 {
  179. let findPboard = NSPasteboard(name: .find)
  180. findPboard.clearContents()
  181. findPboard.writeObjects([editContent as NSPasteboardWriting])
  182. }
  183. }
  184. @IBAction func search(_ sender: NSSearchField) {
  185. if sender.stringValue.isEmpty {
  186. self.applySearchTableHeader("")
  187. }
  188. self.delegate?.searchAction?(searchString: sender.stringValue, isCase: self.mwcFlags.caseInsensitiveSearch == 1)
  189. }
  190. }
  191. // MARK: - Double Action
  192. extension KMLeftSideViewController {
  193. @objc func toggleSelectedSnapshots(_ sender: AnyObject?) {
  194. let indexs = self.snapshotTableView.selectedRowIndexes
  195. if indexs.isEmpty {
  196. return
  197. }
  198. let model = self.snapshots[indexs.last!]
  199. let windowC = model.windowC
  200. if let data = windowC?.window?.isVisible, data {
  201. windowC?.miniaturize()
  202. } else {
  203. windowC?.deminiaturize()
  204. }
  205. var rowIndexSet = IndexSet()
  206. let row = self.snapshotTableView.selectedRow
  207. if row >= 0 && row < self.snapshots.count {
  208. rowIndexSet.insert(row)
  209. }
  210. var columnIndexSet = IndexSet()
  211. columnIndexSet.insert(0)
  212. self.snapshotTableView.reloadData(forRowIndexes: rowIndexSet, columnIndexes: columnIndexSet)
  213. }
  214. // MARK: - KMInterfaceThemeChangedProtocol
  215. override func interfaceThemeDidChanged(_ appearance: NSAppearance.Name) {
  216. super.interfaceThemeDidChanged(appearance)
  217. self.updateViewColor()
  218. self.leftView.interfaceThemeDidChanged(appearance)
  219. }
  220. }
  221. // MARK: - NSMenuDelegate
  222. extension KMLeftSideViewController: NSMenuDelegate {
  223. func menuNeedsUpdate(_ menu: NSMenu) {
  224. if menu.isEqual(to: self.tocOutlineView.menu) {
  225. menu.removeAllItems()
  226. _ = menu.addItem(withTitle: NSLocalizedString("Add Item", comment: ""), action: #selector(outlineContextMenuItemClicked_AddEntry), target: self, tag: KMOutlineViewMenuItemTag.addEntry.rawValue)
  227. _ = menu.addItem(withTitle: NSLocalizedString("Add Sub-Item", comment: ""), action: #selector(outlineContextMenuItemClicked_AddChildEntry), target: self, tag: KMOutlineViewMenuItemTag.addChild.rawValue)
  228. _ = menu.addItem(withTitle: NSLocalizedString("Add To A Higher Level", comment: ""), action: #selector(outlineContextMenuItemClicked_AddAuntEntry), target: self, tag: KMOutlineViewMenuItemTag.addAunt.rawValue)
  229. menu.addItem(.separator())
  230. _ = menu.addItem(withTitle: NSLocalizedString("Delete", comment: ""), action: #selector(outlineContextMenuItemClicked_RemoveEntry), target: self, tag: KMOutlineViewMenuItemTag.remove.rawValue)
  231. menu.addItem(.separator())
  232. _ = menu.addItem(withTitle: NSLocalizedString("Edit", comment: ""), action: #selector(outlineContextMenuItemClicked_Edit), target: self, tag: KMOutlineViewMenuItemTag.edit.rawValue)
  233. _ = menu.addItem(withTitle: NSLocalizedString("Change Destination", comment: ""), action: #selector(outlineContextMenuItemClicked_SetDestination), target: self, tag: KMOutlineViewMenuItemTag.setDestination.rawValue)
  234. _ = menu.addItem(withTitle: NSLocalizedString("Rename", comment: ""), action: #selector(outlineContextMenuItemClicked_Rename), target: self, tag: KMOutlineViewMenuItemTag.rename.rawValue)
  235. menu.addItem(.separator())
  236. _ = menu.addItem(withTitle: NSLocalizedString("Promote", comment: ""), action: #selector(outlineContextMenuItemClicked_Promote), target: self, tag: KMOutlineViewMenuItemTag.promote.rawValue)
  237. _ = menu.addItem(withTitle: NSLocalizedString("Demote", comment: ""), action: #selector(outlineContextMenuItemClicked_Demote), target: self, tag: KMOutlineViewMenuItemTag.demote.rawValue)
  238. return
  239. }
  240. var item: NSMenuItem?
  241. menu.removeAllItems()
  242. if menu.isEqual(to: self.thumbnailTableView.menu) {
  243. let row = self.thumbnailTableView.clickedRow
  244. if self.listView.document.documentURL == nil || self.thumbnailTableView.selectedRowIndexes.contains(row) == false{
  245. return
  246. }
  247. if (row != -1 && self.listView.document.isLocked == false) {
  248. if(self.thumbnailTableView.selectedRowIndexes.count == self.listView.document.pageCount) {
  249. item = menu.addItem(title: KMLocalizedString("Cut", "Menu item title"), action: nil, target: self)
  250. } else {
  251. item = menu.addItem(title: KMLocalizedString("Cut", "Menu item title"), action: #selector(cutPage), target: self)
  252. }
  253. item?.representedObject = IndexSet(integer: row)
  254. item = menu.addItem(title: KMLocalizedString("Copy", "Menu item title"), action: #selector(copyPage), target: self)
  255. item?.representedObject = IndexSet(integer: row)
  256. if (self.copyPages.count > 0) {
  257. item = menu.addItem(title: KMLocalizedString("Paste", "Menu item title"), action: #selector(pastePage), target: self)
  258. }else{
  259. item = menu.addItem(title: KMLocalizedString("Paste", "Menu item title"), action: nil, target: self)
  260. }
  261. item?.representedObject = IndexSet(integer: row)
  262. menu.addItem(.separator())
  263. if(self.thumbnailTableView.selectedRowIndexes.count == self.listView.document.pageCount) {
  264. item = menu.addItem(title: KMLocalizedString("Delete", "Menu item title"), action: nil, target: self)
  265. } else {
  266. item = menu.addItem(title: KMLocalizedString("Delete", "Menu item title"), action: #selector(deletePage), target: self)
  267. }
  268. item?.representedObject = IndexSet(integer: row)
  269. menu.addItem(.separator())
  270. item = menu.addItem(title: KMLocalizedString("Rotate", "Menu item title"), action: #selector(rotatePageMenuAction), target: self)
  271. item?.representedObject = IndexSet(integer: row)
  272. item = menu.addItem(title: KMLocalizedString("Insert", "Menu item title"), action: nil, target: self)
  273. let subMenu = NSMenu()
  274. _ = subMenu.addItem(title: KMLocalizedString("Blank Page", "Menu item title"), action: #selector(quickInsert), target: self)
  275. _ = subMenu.addItem(title: KMLocalizedString("Blank Page - Custom...", "Menu item title"), action: #selector(insert), target: self)
  276. _ = subMenu.addItem(title: KMLocalizedString("From PDF", "Menu item title"), action: #selector(insertPDF), target: self)
  277. item?.submenu = subMenu
  278. item?.representedObject = IndexSet(integer: row)
  279. item = menu.addItem(title: KMLocalizedString("Extract", "Menu item title"), action: #selector(extractPage), target: self)
  280. item?.representedObject = IndexSet(integer: row)
  281. item = menu.addItem(title: KMLocalizedString("Page Edit", "Menu item title"), action: #selector(pageEdit), target: self)
  282. menu.addItem(.separator())
  283. var displayStr = ""
  284. if (self.isDisplayPageSize) {
  285. displayStr = KMLocalizedString("Hide Page Size", "Menu item title")
  286. } else {
  287. displayStr = KMLocalizedString("Display Page Size", "Menu item title")
  288. }
  289. item = menu.addItem(title: displayStr, action: #selector(displayPageSize), target: self)
  290. item?.representedObject = IndexSet(integer: row)
  291. if let doct = self.listView?.document {
  292. item = menu.addItem(title: KMLocalizedString("Share", "Menu item title"), action: nil, target: self)
  293. var tName = self.fileNameWithSelectedPages(self.thumbnailTableView.selectedRowIndexes)
  294. if (tName.count > 50) {
  295. tName = tName.substring(to: 50)
  296. }
  297. item?.submenu = NSSharingServicePicker.menu(forSharingItems: [doct.documentURL as Any], subjectContext: tName, withTarget: self, selector: #selector(sharePage), serviceDelegate: nil)
  298. }
  299. }
  300. } else if menu.isEqual(to: self.findTableView.menu) {
  301. var rowIndexes = self.findTableView.selectedRowIndexes
  302. let row = self.findTableView.clickedRow
  303. if (row != -1) {
  304. if rowIndexes.contains(row) == false {
  305. rowIndexes = IndexSet(integer: row)
  306. }
  307. var selections: [CPDFSelection] = []
  308. for (i, data) in self.searchResults.enumerated() {
  309. if rowIndexes.contains(i) {
  310. selections.append(data.selection)
  311. }
  312. }
  313. if self.listView.hideNotes == false && self.listView.allowsNotes() {
  314. item = menu.addItem(withTitle: KMLocalizedString("Add New Circle", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.circle.rawValue)
  315. item?.representedObject = selections
  316. item = menu.addItem(withTitle: KMLocalizedString("Add New Rectangle", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.square.rawValue)
  317. item?.representedObject = selections
  318. item = menu.addItem(withTitle: KMLocalizedString("Add New Highlight", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.highlight.rawValue)
  319. item?.representedObject = selections
  320. item = menu.addItem(withTitle: KMLocalizedString("Add New Underline", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.underline.rawValue)
  321. item?.representedObject = selections
  322. item = menu.addItem(withTitle: KMLocalizedString("Add New Strikethrough", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.strikeOut.rawValue)
  323. item?.representedObject = selections
  324. }
  325. }
  326. } else if menu.isEqual(to: self.groupedFindTableView.menu) {
  327. var rowIndexes = self.groupedFindTableView.selectedRowIndexes
  328. let row = self.groupedFindTableView.clickedRow
  329. if (row != -1) {
  330. if rowIndexes.contains(row) == false {
  331. rowIndexes = IndexSet(integer: row)
  332. }
  333. // NSArray *selections = [[[leftSideController.groupedFindArrayController arrangedObjects] objectsAtIndexes:rowIndexes] valueForKeyPath:@"@unionOfArrays.matches"];
  334. var selections: [CPDFSelection] = []
  335. for (i, data) in self.groupSearchResults.enumerated() {
  336. if rowIndexes.contains(i) {
  337. for searchM in data.datas {
  338. selections.append(searchM.selection)
  339. }
  340. }
  341. }
  342. item = menu.addItem(title: KMLocalizedString("Select", "Menu item title"), action: #selector(selectSelections), target: self)
  343. item?.representedObject = selections
  344. menu.addItem(.separator())
  345. if self.listView.hideNotes == false && self.listView.allowsNotes() {
  346. item = menu.addItem(withTitle: KMLocalizedString("Add New Circle", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.circle.rawValue)
  347. item?.representedObject = selections
  348. item = menu.addItem(withTitle: KMLocalizedString("Add New Rectangle", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.square.rawValue)
  349. item?.representedObject = selections
  350. item = menu.addItem(withTitle: KMLocalizedString("Add New Highlight", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.highlight.rawValue)
  351. item?.representedObject = selections
  352. item = menu.addItem(withTitle: KMLocalizedString("Add New Underline", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.underline.rawValue)
  353. item?.representedObject = selections
  354. item = menu.addItem(withTitle: KMLocalizedString("Add New Strikethrough", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.strikeOut.rawValue)
  355. item?.representedObject = selections
  356. }
  357. }
  358. } else if menu.isEqual(to: self.snapshotTableView.menu) {
  359. let row = self.snapshotTableView.clickedRow
  360. if (row != -1) {
  361. let model = self.snapshots[row]
  362. let controller = model.windowC
  363. if let data = controller?.window?.isVisible, data {
  364. item = menu.addItem(title: KMLocalizedString("Hide", "Menu item title"), action: #selector(hideSnapshot), target: self)
  365. item?.representedObject = controller
  366. } else {
  367. item = menu.addItem(title: KMLocalizedString("Show", "Menu item title"), action: #selector(showSnapshot), target: self)
  368. item?.representedObject = controller
  369. }
  370. menu.addItem(.separator())
  371. item = menu.addItem(title: KMLocalizedString("Export", "Menu item title"), action: nil, target: self)
  372. let subMenu = NSMenu()
  373. var t = subMenu.addItem(title: KMLocalizedString("PNG", "Menu item title"), action: #selector(menuItemClick_ExportPNG), target: self)
  374. t?.representedObject = controller
  375. t = subMenu.addItem(title: KMLocalizedString("JPG", "Menu item title"), action: #selector(menuItemClick_ExportJPG), target: self)
  376. t?.representedObject = controller
  377. t = subMenu.addItem(title: KMLocalizedString("PDF", "Menu item title"), action: #selector(menuItemClick_ExportPDF), target: self)
  378. t?.representedObject = controller
  379. item?.submenu = subMenu
  380. item = menu.addItem(title: KMLocalizedString("Print", "Menu item title"), action: #selector(menuItemClick_Print), target: self)
  381. item?.representedObject = controller
  382. menu.addItem(.separator())
  383. item = menu.addItem(title: KMLocalizedString("Copy", "Menu item title"), action: #selector(menuItemClick_Copy), target: self)
  384. item?.representedObject = controller
  385. menu.addItem(.separator())
  386. item = menu.addItem(title: KMLocalizedString("Delete", "Menu item title"), action: #selector(deleteSnapshot), target: self)
  387. item?.representedObject = controller
  388. item = menu.addItem(title: KMLocalizedString("Delete All Snapshots", "Menu item title"), action: #selector(deleteAllSnapshot), target: self)
  389. item?.representedObject = controller
  390. }
  391. } else if menu.isEqual(to: self.noteOutlineView.menu) {
  392. var items: NSArray?
  393. var rowIndexes = self.noteOutlineView.selectedRowIndexes
  394. let row = self.noteOutlineView.clickedRow
  395. if (row != -1) {
  396. if rowIndexes.contains(row) == false {
  397. rowIndexes = IndexSet(integer: row)
  398. }
  399. items = self.noteOutlineView.itemsAtRowIndexes(rowIndexes) as NSArray
  400. // PDFAnnotation *foldNote = (PDFAnnotation *)notes[row];
  401. // let foldNote = self.allAnnotations[row]
  402. guard let foldNote = self.fetchNote(for: row) else {
  403. return
  404. }
  405. var isFold = true
  406. if self.allFoldNotes.contains(foldNote) {
  407. isFold = false
  408. }
  409. item = menu.addItem(title: KMLocalizedString("Expand", nil), action: #selector(unfoldNoteAction), target: self)
  410. if (isFold) {
  411. item?.state = .off
  412. } else {
  413. item?.state = .on
  414. }
  415. item?.representedObject = items
  416. item = menu.addItem(title: KMLocalizedString("Collapse", nil), action: #selector(foldNoteAction), target: self)
  417. if (isFold) {
  418. item?.state = .on
  419. } else {
  420. item?.state = .off
  421. }
  422. item?.representedObject = items
  423. menu.addItem(.separator())
  424. if self.listView.hideNotes == false && (items?.count ?? 0) == 1 {
  425. let annotation = self.noteItems(items!).lastObject as? CPDFAnnotation
  426. if let data = annotation?.isEditable(), data {
  427. if annotation?.type == nil {
  428. let isNote = annotation?.isNote() ?? false
  429. if isNote {
  430. // [NSLocalizedString(@"Edit", @"Menu item title") stringByAppendingEllipsis]
  431. item = menu.addItem(title: KMLocalizedString("Edit", "Menu item title"), action: #selector(editNoteTextFromTable), target: self)
  432. item?.representedObject = annotation
  433. }
  434. } else if let data = self.noteOutlineView.tableColumn(withIdentifier: NSUserInterfaceItemIdentifier("note"))?.isHidden, data {
  435. // [NSLocalizedString(@"Edit", @"Menu item title") stringByAppendingEllipsis]
  436. item = menu.addItem(title: KMLocalizedString("Edit", "Menu item title"), action: #selector(editThisAnnotation), target: self)
  437. item?.representedObject = annotation
  438. } else {
  439. item = menu.addItem(title: KMLocalizedString("Edit", "Menu item title"), action: #selector(editNoteFromTable), target: self)
  440. item?.representedObject = annotation
  441. item = menu.addItem(title: KMLocalizedString("Edit", "Menu item title"), action: #selector(editThisAnnotation), target: self)
  442. item?.representedObject = annotation
  443. item?.keyEquivalentModifierMask = [.option]
  444. item?.isAlternate = true
  445. }
  446. }
  447. }
  448. if menu.numberOfItems > 0 {
  449. item = menu.addItem(title: NSLocalizedString("Export Annotations…", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  450. let subMenu = NSMenu()
  451. item?.submenu = subMenu
  452. item = subMenu.addItem(title: NSLocalizedString("PDF", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  453. item?.tag = 0
  454. item = subMenu.addItem(title: NSLocalizedString("PDF Bundle", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  455. item?.tag = 1
  456. item = subMenu.addItem(title: NSLocalizedString("PDF Reader Pro Edition Notes", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  457. item?.tag = 2
  458. item = subMenu.addItem(title: NSLocalizedString("Notes as Texts", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  459. item?.tag = 3
  460. item = subMenu.addItem(title: NSLocalizedString("Notes as RTF", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  461. item?.tag = 4
  462. item = subMenu.addItem(title: NSLocalizedString("Notes as RTFD", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  463. item?.tag = 5
  464. item = subMenu.addItem(title: NSLocalizedString("Notes as FDF", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  465. item?.tag = 6
  466. menu.addItem(.separator())
  467. if self.outlineView(self.noteOutlineView, canDeleteItems: items as? [Any] ?? []) {
  468. item = menu.addItem(title: KMLocalizedString("Delete", "Menu item title"), action: #selector(deleteNotes), target: self)
  469. item?.representedObject = items
  470. }
  471. item = menu.addItem(title: NSLocalizedString("Remove All Annotations", tableName: "MainMenu", comment: "Menu item title"), action: #selector(removeAllAnnotations), target: self)
  472. }
  473. } else {
  474. let subMenu = NSMenu()
  475. item?.submenu = subMenu
  476. item = subMenu.addItem(title: NSLocalizedString("PDF", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  477. item?.tag = 0
  478. item = subMenu.addItem(title: NSLocalizedString("PDF Bundle", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  479. item?.tag = 1
  480. item = subMenu.addItem(title: NSLocalizedString("PDF Reader Pro Edition Notes", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  481. item?.tag = 2
  482. item = subMenu.addItem(title: NSLocalizedString("Notes as Texts", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  483. item?.tag = 3
  484. item = subMenu.addItem(title: NSLocalizedString("Notes as RTF", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  485. item?.tag = 4
  486. item = subMenu.addItem(title: NSLocalizedString("Notes as RTFD", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  487. item?.tag = 5
  488. item = subMenu.addItem(title: NSLocalizedString("Notes as FDF", tableName: "MainMenu", comment: "Menu item title"), action: #selector(exportAnnotationNotes), target: self)
  489. item?.tag = 6
  490. item = menu.addItem(title: NSLocalizedString("Remove All Annotations", tableName: "MainMenu", comment: "Menu item title"), action: #selector(removeAllAnnotations), target: self)
  491. }
  492. }
  493. }
  494. }
  495. // MARK: - NSMenuItemValidation
  496. extension KMLeftSideViewController: NSMenuItemValidation {
  497. func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  498. if (self.listView.document == nil || self.listView.document!.isLocked) {
  499. return false
  500. }
  501. let action = menuItem.action
  502. // if (self.isCompareMode) {
  503. // return NO;
  504. // }
  505. // if (action == @selector(createNewNote:)) {
  506. // BOOL isMarkup = [menuItem tag] == SKHighlightNote || [menuItem tag] == SKUnderlineNote || [menuItem tag] == SKStrikeOutNote;
  507. // isMarkup = NO;
  508. // return [self interactionMode] != SKPresentationMode && [[self pdfDocument] allowsNotes] && ([pdfView toolMode] == SKTextToolMode || [pdfView toolMode] == SKNoteToolMode) && [pdfView hideNotes] == NO && (isMarkup == NO || [[pdfView currentSelection] hasCharacters]);
  509. // } else if (action == @selector(toggleHideNotes:)) {
  510. // if ([pdfView hideNotes])
  511. // [menuItem setTitle:NSLocalizedString(@"Show Notes", @"Menu item title")];
  512. // else
  513. // [menuItem setTitle:NSLocalizedString(@"Hide Notes", @"Menu item title")];
  514. // return YES;
  515. // }
  516. //
  517. if (action == #selector(toggleCaseInsensitiveSearch)) {
  518. let state = KMDataManager.ud_integer(forKey: SKCaseInsensitiveSearchKey)
  519. menuItem.state = state == 1 ? .on : .off
  520. return true
  521. } else if (action == #selector(toggleWholeWordSearch)) {
  522. menuItem.state = self.mwcFlags.wholeWordSearch == 1 ? .on : .off
  523. return true
  524. } else if action == #selector(toggleCaseInsensitiveNoteSearch) {
  525. menuItem.state = self.caseInsensitiveNoteSearch ? .on : .off
  526. return true
  527. }
  528. // else if (action == @selector(toggleAutoResizeNoteRows:)) {
  529. // [menuItem setState:mwcFlags.autoResizeNoteRows ? NSOnState : NSOffState];
  530. // return YES;
  531. // } else if (action == @selector(performFindPanelAction:)) {
  532. // if ([self interactionMode] == SKPresentationMode)
  533. // return NO;
  534. // switch ([menuItem tag]) {
  535. // case NSFindPanelActionShowFindPanel:
  536. // return YES;
  537. // case NSFindPanelActionNext:
  538. // case NSFindPanelActionPrevious:
  539. // return YES;
  540. // case NSFindPanelActionSetFindString:
  541. // return [[[self pdfView] currentSelection] hasCharacters];
  542. // default:
  543. // return NO;
  544. // }
  545. // } else
  546. // if (action == @selector(highlightFormFiled:)) {
  547. // BOOL highlightFormFiled = [[NSUserDefaults standardUserDefaults] boolForKey:@"kPDFViewHighlightFormFiledKey"];
  548. // [menuItem setState:highlightFormFiled ? NSOnState : NSOffState];
  549. // return YES;
  550. // } else if (action == @selector(highlightLinks:)) {
  551. // BOOL highlightLinks = [[NSUserDefaults standardUserDefaults] boolForKey:@"kPDFViewHighlightLinksKey"];
  552. // [menuItem setState:highlightLinks ? NSOnState : NSOffState];
  553. // return YES;
  554. // } else if (action == @selector(link:)) {
  555. // return ([self.pdfView toolMode] == SKTextToolMode || [self.pdfView toolMode] == SKNoteToolMode) && [self.pdfView hideNotes] == NO && [self.pdfView.document isLocked] == NO;
  556. // return ([self.pdfView toolMode] == SKTextToolMode || [self.pdfView toolMode] == SKNoteToolMode) && [self.pdfView hideNotes] == NO && [self.pdfView.document isLocked] == NO && [[self.pdfView currentSelection] hasCharacters];
  557. // } else if (action == @selector(deletePage:)) {
  558. // return self.pdfView.document.pageCount > 1 ? YES : NO;
  559. // } else if (action == @selector(themesColor:)){
  560. // if (KMPDFViewModeNormal== self.pdfView.viewMode) {
  561. // [menuItem setState:(menuItem.tag == 0)?NSOnState:NSOffState];
  562. // } else if (KMPDFViewModeSoft == self.pdfView.viewMode){
  563. // [menuItem setState:(menuItem.tag == 1)?NSOnState:NSOffState];
  564. // } else if (KMPDFViewModeNight == self.pdfView.viewMode){
  565. // [menuItem setState:(menuItem.tag == 2)?NSOnState:NSOffState];
  566. // } else if (KMPDFViewModeGreen == self.pdfView.viewMode){
  567. // [menuItem setState:(menuItem.tag == 3)?NSOnState:NSOffState];
  568. // } else if (KMPDFViewModeThemes1 == self.pdfView.viewMode){
  569. // [menuItem setState:(menuItem.tag == 4)?NSOnState:NSOffState];
  570. // } else if (KMPDFViewModeThemes2 == self.pdfView.viewMode){
  571. // [menuItem setState:(menuItem.tag == 5)?NSOnState:NSOffState];
  572. // } else if (KMPDFViewModeThemes3 == self.pdfView.viewMode){
  573. // [menuItem setState:(menuItem.tag == 6)?NSOnState:NSOffState];
  574. // } else if (KMPDFViewModeThemes4 == self.pdfView.viewMode){
  575. // [menuItem setState:(menuItem.tag == 7)?NSOnState:NSOffState];
  576. // }
  577. // // else {
  578. // // [menuItem setState:(menuItem.tag == 4)?NSOnState:NSOffState];
  579. // // }
  580. // NSData * data = [[NSUserDefaults standardUserDefaults] objectForKey:@"kKMPDFViewModeThemesArray"] ? : nil;
  581. // NSInteger themesCount;
  582. // if (data) {
  583. // NSArray * appArray = [NSKeyedUnarchiver unarchiveObjectWithData:data];
  584. // NSMutableArray * mutableArray = [NSMutableArray arrayWithArray:appArray];
  585. // themesCount = [menuItem tag] - mutableArray.count;
  586. // } else {
  587. // themesCount = [menuItem tag] - 4;
  588. // }
  589. // if (themesCount >= 0) {
  590. // menuItem.hidden = YES;
  591. // return NO;
  592. // } else {
  593. // menuItem.hidden = NO;
  594. // return YES;
  595. // }
  596. // } else
  597. if (action == #selector(outlineContextMenuItemClicked_AddEntry) ||
  598. action == #selector(outlineContextMenuItemClicked_AddChildEntry) ||
  599. action == #selector(outlineContextMenuItemClicked_AddAuntEntry) ||
  600. action == #selector(outlineContextMenuItemClicked_RemoveEntry) ||
  601. action == #selector(outlineContextMenuItemClicked_Edit) ||
  602. action == #selector(outlineContextMenuItemClicked_SetDestination) ||
  603. action == #selector(outlineContextMenuItemClicked_Rename) ||
  604. action == #selector(outlineContextMenuItemClicked_Promote) ||
  605. action == #selector(outlineContextMenuItemClicked_Demote)) {
  606. if let data = self.listView.document?.isLocked, data {
  607. return false
  608. }
  609. if (self.isSearchOutlineMode) {
  610. if (action == #selector(outlineContextMenuItemClicked_AddEntry) ||
  611. action == #selector(outlineContextMenuItemClicked_AddChildEntry) ||
  612. action == #selector(outlineContextMenuItemClicked_AddAuntEntry) ||
  613. action == #selector(outlineContextMenuItemClicked_Edit) ||
  614. action == #selector(outlineContextMenuItemClicked_Rename)
  615. ) {
  616. return false
  617. }
  618. }
  619. if (self.tocOutlineView.selectedRowIndexes.count > 1) {
  620. if (menuItem.tag == KMOutlineViewMenuItemTag.remove.rawValue) {
  621. return true
  622. }
  623. return false
  624. } else if (self.tocOutlineView.selectedRowIndexes.count > 0) {
  625. if (action == #selector(outlineContextMenuItemClicked_AddChildEntry) ||
  626. action == #selector(outlineContextMenuItemClicked_SetDestination)) {
  627. return true
  628. }
  629. }
  630. if (self.tocOutlineView.clickedRow == -1) {
  631. if (action == #selector(outlineContextMenuItemClicked_AddEntry)) {
  632. return true
  633. } else {
  634. return false
  635. }
  636. } else {
  637. let clickedOutline = self.tocOutlineView.item(atRow: self.tocOutlineView.clickedRow) as? CPDFOutline
  638. if let data = clickedOutline?.index, data > 0 {
  639. if (action == #selector(outlineContextMenuItemClicked_Demote)) {
  640. return true
  641. }
  642. } else {
  643. if (action == #selector(outlineContextMenuItemClicked_Demote)) {
  644. return false
  645. }
  646. }
  647. let parentOutLine = clickedOutline?.parent
  648. let grandparentOutLine = parentOutLine?.parent
  649. //if clicked PDFOutline 's parent exist
  650. if (grandparentOutLine != nil) {
  651. //and title is equla "add_AuntOutlineKey"
  652. if (action == #selector(outlineContextMenuItemClicked_AddAuntEntry)) {
  653. return true
  654. } else if (action == #selector(outlineContextMenuItemClicked_Promote)){
  655. return true
  656. }
  657. } else {
  658. if (action == #selector(outlineContextMenuItemClicked_AddAuntEntry)) {
  659. return false
  660. }else if (action == #selector(outlineContextMenuItemClicked_Promote)){
  661. return false
  662. }
  663. }
  664. }
  665. return true
  666. }
  667. // else
  668. // if ([menuItem action] == @selector(toggleEncryptFilePanel:)) {
  669. // [menuItem setTitle:NSLocalizedString(@"Set Passwords", @"Menu item title")];
  670. // NSURL *fileURL = [self.pdfView.document documentURL];
  671. // if (!fileURL) {
  672. // return YES;
  673. // }
  674. // PDFDocument *pdfDoc = [[[PDFDocument alloc] initWithURL:fileURL] autorelease];
  675. //
  676. // if ([pdfDoc isLocked]) {
  677. // [menuItem setTitle:NSLocalizedString(@"Remove Security", @"Menu item title")];
  678. // }
  679. // } else if (action == @selector(toggleToolbarShow:)) {
  680. // NSToolbar *toolbar = [self.window toolbar];
  681. // if ([toolbar isVisible])
  682. // [menuItem setTitle:NSLocalizedString(@"Hide Toolbar", @"Menu item title")];
  683. // else
  684. // [menuItem setTitle:NSLocalizedString(@"Show Toolbar", @"Menu item title")];
  685. // return YES;
  686. // } else if (action == @selector(readMode:)) {
  687. // menuItem.state = self.isReadMode ? NSControlStateValueOn : NSControlStateValueOff;
  688. // return YES;
  689. // } else if (action == @selector(addForm:)) {
  690. // return YES;
  691. else if action == #selector(toggleOutlineCaseInsensitiveSearch) {
  692. menuItem.state = self.outlineIgnoreCaseFlag ? .on : .off
  693. return true
  694. }
  695. // } else if (action == @selector(note_expandAllComments:) ||
  696. // action == @selector(note_foldAllComments:) ||
  697. // action == @selector(exportAnnotationNotes:) ||
  698. // action == @selector(leftSideEmptyAnnotationClick_DeleteAnnotation:) ||
  699. // action == @selector(removeAllAnnotations:)) {
  700. // if (@available(macOS 10.13, *)) {
  701. // if (notes.count == 0) {
  702. // return NO;
  703. // } else {
  704. // return YES;
  705. // }
  706. // } else {
  707. // if (action == @selector(note_expandAllComments:) ||
  708. // action == @selector(note_foldAllComments:)) {
  709. // return NO;
  710. // }
  711. // }
  712. // } else
  713. if (action == #selector(unfoldNoteAction) ||
  714. action == #selector(foldNoteAction)) {
  715. let row = self.noteOutlineView.clickedRow
  716. // NSArray *noteArr = [rightSideController.noteArrayController arrangedObjects];
  717. // PDFAnnotation *foldNote = (PDFAnnotation *)noteArr[row];
  718. // let foldNote = self.allAnnotations[row]
  719. let foldNote = self.fetchNote(for: row)
  720. // SKNPDFAnnotationNote
  721. if foldNote is CPDFMarkupAnnotation || foldNote is CPDFTextAnnotation {
  722. return true
  723. } else {
  724. return false
  725. }
  726. } else if (action == #selector(menuItemClick_ExportPNG) ||
  727. action == #selector(menuItemClick_ExportJPG) ||
  728. action == #selector(menuItemClick_ExportPDF)) {
  729. if (self.snapshotTableView.selectedRow == -1 ) {
  730. return false
  731. } else {
  732. return true
  733. }
  734. }
  735. // else if (action == @selector(editNoteFromTable:)) {
  736. // NSInteger row = [rightSideController.noteOutlineView clickedRow];
  737. // NSArray * noteArr = [rightSideController.noteArrayController arrangedObjects];
  738. // PDFAnnotation *foldNote = (PDFAnnotation *)noteArr[row];
  739. // if (@available(macOS 10.13, *)) {
  740. // if ([foldNote.widgetFieldType isEqualToString:PDFAnnotationWidgetSubtypeSignature]) {
  741. // return NO;
  742. // }
  743. // }
  744. // if ([foldNote isKindOfClass:[PDFAnnotationStamp class]] ||
  745. // [foldNote isKindOfClass:[KMAnnotationStamp class]]) {
  746. // return NO;
  747. // } else {
  748. // return YES;
  749. // }
  750. // }
  751. else if (action == #selector(menuItemClick_SelectAll)) {
  752. menuItem.state = self.snapshotListIsAllSelected() ? .on : .off
  753. return true
  754. }
  755. // else if (action == @selector(leftSideEmptyAnnotationClick_DeleteOutline:)) {
  756. // PDFOutline * item = [[pdfView document] outlineRoot];
  757. // if (self.isSearchOutlineMode) {
  758. // NSInteger num = 0;
  759. // for (NSUInteger i = 0; i < [item numberOfChildren]; i++) {
  760. // PDFOutline *outline = [item childAtIndex:i];
  761. // if ([self hasContainString:self.leftSideController.outlineSearchField.stringValue rootOutline:outline]) {
  762. // num ++;
  763. // }
  764. // }
  765. // if (num > 0)
  766. // return YES;
  767. // else
  768. // return NO;
  769. // } else {
  770. // if ([item numberOfChildren] > 0)
  771. // return YES;
  772. // else
  773. // return NO;
  774. // }
  775. // }
  776. return true
  777. }
  778. }
  779. // MARK: - NSPopoverDelegate
  780. extension KMLeftSideViewController: NSPopoverDelegate {
  781. func popoverWillClose(_ notification: Notification) {
  782. guard let popover = notification.object as? NSPopover else {
  783. return
  784. }
  785. if let vc = popover.contentViewController as? KMOutlineEditViewController {
  786. self.editOutlineUI(vc)
  787. self.tocOutlineView.reloadData()
  788. }
  789. }
  790. }
  791. // MARK: - NSSearchFieldDelegate
  792. extension KMLeftSideViewController: NSSearchFieldDelegate {
  793. func controlTextDidChange(_ obj: Notification) {
  794. if self.outlineSearchField.isEqual(to: obj.object) {
  795. if (self.outlineSearchField.stringValue.isEmpty == false) {
  796. self.isSearchOutlineMode = true
  797. self.tocOutlineView.reloadData()
  798. self.tocOutlineView.expandItem(nil, expandChildren: true)
  799. self.tocType = .unfold
  800. } else {
  801. self.isSearchOutlineMode = false
  802. self.showSearchOutlineBlankState(false)
  803. self.tocOutlineView.reloadData()
  804. }
  805. // self.leftSideEmptyVC.addOutlineBtn.enabled = !self.isSearchOutlineMode;
  806. self.outlineAddButton.isEnabled = !self.isSearchOutlineMode
  807. } else if self.snapshotSearchField.isEqual(to: obj.object) {
  808. let searchString = self.snapshotSearchField.stringValue
  809. // NSPredicate *filterPredicate = nil;
  810. // if ([searchString length] > 0) {
  811. // NSExpression *lhs = [NSExpression expressionForConstantValue:searchString];
  812. // NSExpression *rhs = [NSExpression expressionForKeyPath:@"string"];
  813. // NSUInteger options = NSDiacriticInsensitivePredicateOption;
  814. // if (mwcFlags.caseInsensitiveNoteSearch)
  815. // options |= NSCaseInsensitivePredicateOption;
  816. // filterPredicate = [NSComparisonPredicate predicateWithLeftExpression:lhs rightExpression:rhs modifier:NSDirectPredicateModifier type:NSInPredicateOperatorType options:options];
  817. // }
  818. // [rightSideController.snapshotArrayController setFilterPredicate:filterPredicate];
  819. // NSArray * snapshots = [rightSideController.snapshotArrayController arrangedObjects];
  820. self.searchSnapshots.removeAll()
  821. if searchString.isEmpty {
  822. self.isSearchSnapshotMode = false
  823. self.searchSnapshots = self.snapshots.filter({ model in
  824. let data = model.windowC?.string.contains(searchString) ?? false
  825. return data
  826. })
  827. } else {
  828. self.isSearchSnapshotMode = true
  829. }
  830. var snapshots = self.searchSnapshots
  831. if self.isSearchSnapshotMode == false {
  832. snapshots = self.snapshots
  833. }
  834. Task { @MainActor in
  835. self.updataLeftSideSnapView()
  836. self.snapshotTableView.reloadData()
  837. }
  838. if (snapshots.count > 0) {
  839. self.leftSideEmptyVC.outlineSearchView.removeFromSuperview()
  840. } else {
  841. let view = self.snapshotTableView.enclosingScrollView
  842. let viewFrmae = view?.frame ?? .zero
  843. let emptyVcSize = self.leftSideEmptyVC.outlineSearchView.frame.size
  844. self.leftSideEmptyVC.outlineSearchView.frame = NSMakeRect((viewFrmae.size.width-emptyVcSize.width)/2.0,(viewFrmae.size.height-emptyVcSize.height)/2.0, emptyVcSize.width, emptyVcSize.height)
  845. self.leftSideEmptyVC.outlineSearchView.autoresizingMask = [.minXMargin, .maxXMargin, .minYMargin, .maxYMargin]
  846. self.snapshotTableView.enclosingScrollView?.documentView?.addSubview(self.leftSideEmptyVC.outlineSearchView)
  847. }
  848. }
  849. }
  850. }
  851. // MARK: - NSTextFieldDelegate
  852. extension KMLeftSideViewController: NSTextFieldDelegate {
  853. func controlTextDidBeginEditing(_ obj: Notification) {
  854. if self.noteOutlineView.isEqual(to: obj.object) {
  855. // if (mwcFlags.isEditingTable == NO && mwcFlags.isEditingPDF == NO)
  856. // [[self document] objectDidBeginEditing:self];
  857. // mwcFlags.isEditingTable = YES;
  858. }
  859. }
  860. func controlTextDidEndEditing(_ obj: Notification) {
  861. if self.noteOutlineView.isEqual(to: obj.object) {
  862. // if (mwcFlags.isEditingTable && mwcFlags.isEditingPDF == NO)
  863. // [[self document] objectDidEndEditing:self];
  864. // mwcFlags.isEditingTable = NO;
  865. } else if let data = self.renamePDFOutlineTextField?.isEqual(to: obj.object), data {
  866. let textField = obj.object as! NSTextField
  867. if let editPDFOutline = self.renamePDFOutline {
  868. if self.isRenameNoteOutline == false {
  869. if textField.stringValue == editPDFOutline.label {
  870. return
  871. }
  872. }
  873. self.renamePDFOutline(editPDFOutline, label: textField.stringValue)
  874. self.updateSelectRowHeight()
  875. self.tocOutlineView.reloadData()
  876. }
  877. }
  878. }
  879. }
  880. // MARK: - Menu Item Actions
  881. extension KMLeftSideViewController {
  882. @objc func toggleCaseInsensitiveNoteSearch(_ sender: AnyObject?) {
  883. self.caseInsensitiveNoteSearch = !self.caseInsensitiveNoteSearch
  884. if self.searchField.stringValue.isEmpty == false {
  885. self.searchNotes(self.searchField)
  886. }
  887. KMDataManager.ud_set(self.caseInsensitiveNoteSearch, forKey: SKCaseInsensitiveNoteSearchKey)
  888. }
  889. @objc func searchNotes(_ sender: AnyObject?) {
  890. self.noteSearchMode = false
  891. if self.findState == .note {
  892. if self.noteSearchField.isEqual(to: sender) {
  893. self.noteSearchMode = true
  894. }
  895. self.updateNoteFilterPredicate()
  896. } else {
  897. self.updateSnapshotFilterPredicate()
  898. }
  899. let textfield = sender as? NSSearchField
  900. if let data = textfield?.stringValue.isEmpty, data == false {
  901. let findPboard = NSPasteboard(name: .find)
  902. findPboard.clearContents()
  903. // findPboard.writeObjects([textfield!.stringValue])
  904. }
  905. }
  906. func updateSnapshotFilterPredicate() {
  907. let searchString = self.snapshotSearchField.stringValue
  908. self.searchSnapshots.removeAll()
  909. if self.findState == .snapshot && searchString.isEmpty == false {
  910. self.searchSnapshots = self.snapshots.filter({ model in
  911. let data = model.windowC?.string.contains(searchString) ?? false
  912. return data
  913. })
  914. }
  915. Task { @MainActor in
  916. self.updataLeftSideSnapView()
  917. self.snapshotTableView.reloadData()
  918. }
  919. }
  920. func snapshotListIsAllSelected() -> Bool {
  921. if self.snapshots.isEmpty {
  922. return false
  923. }
  924. for model in self.snapshots {
  925. if model.isSelected == false {
  926. return false
  927. }
  928. }
  929. return true
  930. }
  931. }