KMLeftSideViewController+Action.swift 67 KB

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