KMLeftSideViewController+Action.swift 55 KB

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