KMLeftSideViewController+Action.swift 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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. self.outlineMoreMenu(button)
  48. } else if (tag == 304) {
  49. self.annoListMoreMenu(button)
  50. }
  51. }
  52. func searchFieldChangeAction(_ editContent: String) {
  53. if self.findState == .note {
  54. self.updateNoteFilterPredicate()
  55. } else {
  56. self.updateSnapshotFilterPredicate()
  57. }
  58. if editContent.count > 0 {
  59. let findPboard = NSPasteboard(name: .find)
  60. findPboard.clearContents()
  61. findPboard.writeObjects([editContent as NSPasteboardWriting])
  62. }
  63. }
  64. @IBAction func search(_ sender: NSSearchField) {
  65. if sender.stringValue.isEmpty {
  66. self.applySearchTableHeader("")
  67. }
  68. self.delegate?.searchAction?(searchString: sender.stringValue, isCase: self.mwcFlags.caseInsensitiveSearch == 1)
  69. }
  70. // RefreshUI
  71. public func refreshUIForDocumentChanged() {
  72. if self.type.methodType == .Thumbnail {
  73. self.resetThumbnails(ks: false)
  74. } else if self.type.methodType == .Outline {
  75. Task { @MainActor in
  76. self.tocOutlineView.reloadData()
  77. }
  78. } else if self.type.methodType == .Annotation {
  79. self.note_reloadDataIfNeed()
  80. } else if self.type.methodType == .snapshot {
  81. self.reloadSnapshotDataIfNeed()
  82. } else if self.type.methodType == .Search {
  83. if self.searchField.stringValue.isEmpty == false {
  84. self.search(self.searchField)
  85. }
  86. }
  87. }
  88. }
  89. // MARK: - Double Action
  90. extension KMLeftSideViewController {
  91. @objc func toggleSelectedSnapshots(_ sender: AnyObject?) {
  92. let indexs = self.snapshotTableView.selectedRowIndexes
  93. if indexs.isEmpty {
  94. return
  95. }
  96. let model = self.snapshots[indexs.last!]
  97. let windowC = model.windowC
  98. if let data = windowC?.window?.isVisible, data {
  99. windowC?.miniaturize()
  100. } else {
  101. windowC?.deminiaturize()
  102. }
  103. var rowIndexSet = IndexSet()
  104. let row = self.snapshotTableView.selectedRow
  105. if row >= 0 && row < self.snapshots.count {
  106. rowIndexSet.insert(row)
  107. }
  108. var columnIndexSet = IndexSet()
  109. columnIndexSet.insert(0)
  110. self.snapshotTableView.reloadData(forRowIndexes: rowIndexSet, columnIndexes: columnIndexSet)
  111. }
  112. // MARK: - KMInterfaceThemeChangedProtocol
  113. override func interfaceThemeDidChanged(_ appearance: NSAppearance.Name) {
  114. super.interfaceThemeDidChanged(appearance)
  115. Task { @MainActor in
  116. self.updateViewColor()
  117. self.leftView.interfaceThemeDidChanged(appearance)
  118. if self.type.methodType == .Search {
  119. if self.findPaneState == .singular {
  120. self.findTableView.reloadData()
  121. }
  122. }
  123. }
  124. }
  125. }
  126. // MARK: - NSMenuDelegate
  127. extension KMLeftSideViewController: NSMenuDelegate {
  128. func menuNeedsUpdate(_ menu: NSMenu) {
  129. menu.removeAllItems()
  130. if menu.isEqual(to: self.tocOutlineView.menu) {
  131. self.outlineListMenu(menu)
  132. return
  133. }
  134. var item: NSMenuItem?
  135. if menu.isEqual(to: self.thumbnailTableView.menu) {
  136. let row = self.thumbnailTableView.clickedRow
  137. if self.pdfDocument()?.documentURL == nil || self.thumbnailTableView.selectedRowIndexes.contains(row) == false{
  138. return
  139. }
  140. let isLocked = self.isLocked()
  141. let pageCount = self.pageCount()
  142. if (row != -1 && isLocked == false) {
  143. if(self.thumbnailTableView.selectedRowIndexes.count == pageCount) {
  144. item = menu.addItem(title: KMLocalizedString("Cut", "Menu item title"), action: nil, target: self)
  145. } else {
  146. item = menu.addItem(title: KMLocalizedString("Cut", "Menu item title"), action: #selector(cutPage), target: self)
  147. }
  148. item?.representedObject = IndexSet(integer: row)
  149. item = menu.addItem(title: KMLocalizedString("Copy", "Menu item title"), action: #selector(copyPage), target: self)
  150. item?.representedObject = IndexSet(integer: row)
  151. if (self.copyPages.count > 0) {
  152. item = menu.addItem(title: KMLocalizedString("Paste", "Menu item title"), action: #selector(pastePage), target: self)
  153. }else{
  154. item = menu.addItem(title: KMLocalizedString("Paste", "Menu item title"), action: nil, target: self)
  155. }
  156. item?.representedObject = IndexSet(integer: row)
  157. menu.addItem(.separator())
  158. if(self.thumbnailTableView.selectedRowIndexes.count == pageCount) {
  159. item = menu.addItem(title: KMLocalizedString("Delete", "Menu item title"), action: nil, target: self)
  160. } else {
  161. item = menu.addItem(title: KMLocalizedString("Delete", "Menu item title"), action: #selector(deletePage), target: self)
  162. }
  163. item?.representedObject = IndexSet(integer: row)
  164. menu.addItem(.separator())
  165. item = menu.addItem(title: KMLocalizedString("Rotate", "Menu item title"), action: #selector(rotatePageMenuAction), target: self)
  166. item?.representedObject = IndexSet(integer: row)
  167. item = menu.addItem(title: KMLocalizedString("Insert", "Menu item title"), action: nil, target: self)
  168. let subMenu = NSMenu()
  169. _ = subMenu.addItem(title: KMLocalizedString("Blank Page", "Menu item title"), action: #selector(quickInsert), target: self)
  170. _ = subMenu.addItem(title: KMLocalizedString("Blank Page - Custom...", "Menu item title"), action: #selector(insert), target: self)
  171. _ = subMenu.addItem(title: KMLocalizedString("From PDF", "Menu item title"), action: #selector(insertPDF), target: self)
  172. item?.submenu = subMenu
  173. item?.representedObject = IndexSet(integer: row)
  174. item = menu.addItem(title: KMLocalizedString("Extract", "Menu item title"), action: #selector(extractPage), target: self)
  175. item?.representedObject = IndexSet(integer: row)
  176. item = menu.addItem(title: KMLocalizedString("Page Edit", "Menu item title"), action: #selector(pageEdit), target: self)
  177. menu.addItem(.separator())
  178. var displayStr = ""
  179. if (self.isDisplayPageSize) {
  180. displayStr = KMLocalizedString("Hide Page Size", "Menu item title")
  181. } else {
  182. displayStr = KMLocalizedString("Display Page Size", "Menu item title")
  183. }
  184. item = menu.addItem(title: displayStr, action: #selector(displayPageSize), target: self)
  185. item?.representedObject = IndexSet(integer: row)
  186. if let doct = self.pdfDocument() {
  187. item = menu.addItem(title: KMLocalizedString("Share", "Menu item title"), action: nil, target: self)
  188. var tName = self.fileNameWithSelectedPages(self.thumbnailTableView.selectedRowIndexes)
  189. if (tName.count > 50) {
  190. tName = tName.substring(to: 50)
  191. }
  192. item?.submenu = NSSharingServicePicker.menu(forSharingItems: [doct.documentURL as Any], subjectContext: tName, withTarget: self, selector: #selector(sharePage), serviceDelegate: nil)
  193. }
  194. }
  195. } else if menu.isEqual(to: self.findTableView.menu) {
  196. var rowIndexes = self.findTableView.selectedRowIndexes
  197. let row = self.findTableView.clickedRow
  198. if (row != -1) {
  199. if rowIndexes.contains(row) == false {
  200. rowIndexes = IndexSet(integer: row)
  201. }
  202. var selections: [CPDFSelection] = []
  203. for (i, data) in self.searchResults.enumerated() {
  204. if rowIndexes.contains(i) {
  205. selections.append(data.selection)
  206. }
  207. }
  208. let hideNotes = self.hideNotes()
  209. let allowsNotes = self.allowsNotes()
  210. if hideNotes == false && allowsNotes {
  211. item = menu.addItem(withTitle: KMLocalizedString("Add New Circle", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.circle.rawValue)
  212. item?.representedObject = selections
  213. item = menu.addItem(withTitle: KMLocalizedString("Add New Rectangle", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.square.rawValue)
  214. item?.representedObject = selections
  215. item = menu.addItem(withTitle: KMLocalizedString("Add New Highlight", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.highlight.rawValue)
  216. item?.representedObject = selections
  217. item = menu.addItem(withTitle: KMLocalizedString("Add New Underline", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.underline.rawValue)
  218. item?.representedObject = selections
  219. item = menu.addItem(withTitle: KMLocalizedString("Add New Strikethrough", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.strikeOut.rawValue)
  220. item?.representedObject = selections
  221. }
  222. }
  223. } else if menu.isEqual(to: self.groupedFindTableView.menu) {
  224. var rowIndexes = self.groupedFindTableView.selectedRowIndexes
  225. let row = self.groupedFindTableView.clickedRow
  226. if (row != -1) {
  227. if rowIndexes.contains(row) == false {
  228. rowIndexes = IndexSet(integer: row)
  229. }
  230. // NSArray *selections = [[[leftSideController.groupedFindArrayController arrangedObjects] objectsAtIndexes:rowIndexes] valueForKeyPath:@"@unionOfArrays.matches"];
  231. var selections: [CPDFSelection] = []
  232. for (i, data) in self.groupSearchResults.enumerated() {
  233. if rowIndexes.contains(i) {
  234. for searchM in data.datas {
  235. selections.append(searchM.selection)
  236. }
  237. }
  238. }
  239. item = menu.addItem(title: KMLocalizedString("Select", "Menu item title"), action: #selector(selectSelections), target: self)
  240. item?.representedObject = selections
  241. menu.addItem(.separator())
  242. let hideNotes = self.hideNotes()
  243. let allowsNotes = self.allowsNotes()
  244. if hideNotes == false && allowsNotes {
  245. item = menu.addItem(withTitle: KMLocalizedString("Add New Circle", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.circle.rawValue)
  246. item?.representedObject = selections
  247. item = menu.addItem(withTitle: KMLocalizedString("Add New Rectangle", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.square.rawValue)
  248. item?.representedObject = selections
  249. item = menu.addItem(withTitle: KMLocalizedString("Add New Highlight", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.highlight.rawValue)
  250. item?.representedObject = selections
  251. item = menu.addItem(withTitle: KMLocalizedString("Add New Underline", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.underline.rawValue)
  252. item?.representedObject = selections
  253. item = menu.addItem(withTitle: KMLocalizedString("Add New Strikethrough", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.strikeOut.rawValue)
  254. item?.representedObject = selections
  255. }
  256. }
  257. } else if menu.isEqual(to: self.snapshotTableView.menu) {
  258. let row = self.snapshotTableView.clickedRow
  259. if (row != -1) {
  260. let model = self.snapshots[row]
  261. let controller = model.windowC
  262. if let data = controller?.window?.isVisible, data {
  263. item = menu.addItem(title: KMLocalizedString("Hide", "Menu item title"), action: #selector(hideSnapshot), target: self)
  264. item?.representedObject = controller
  265. } else {
  266. item = menu.addItem(title: KMLocalizedString("Show", "Menu item title"), action: #selector(showSnapshot), target: self)
  267. item?.representedObject = controller
  268. }
  269. menu.addItem(.separator())
  270. item = menu.addItem(title: KMLocalizedString("Export", "Menu item title"), action: nil, target: self)
  271. let subMenu = NSMenu()
  272. var t = subMenu.addItem(title: KMLocalizedString("PNG", "Menu item title"), action: #selector(menuItemClick_ExportPNG), target: self)
  273. t?.representedObject = controller
  274. t = subMenu.addItem(title: KMLocalizedString("JPG", "Menu item title"), action: #selector(menuItemClick_ExportJPG), target: self)
  275. t?.representedObject = controller
  276. t = subMenu.addItem(title: KMLocalizedString("PDF", "Menu item title"), action: #selector(menuItemClick_ExportPDF), target: self)
  277. t?.representedObject = controller
  278. item?.submenu = subMenu
  279. item = menu.addItem(title: KMLocalizedString("Print", "Menu item title"), action: #selector(menuItemClick_Print), target: self)
  280. item?.representedObject = controller
  281. menu.addItem(.separator())
  282. item = menu.addItem(title: KMLocalizedString("Copy", "Menu item title"), action: #selector(menuItemClick_Copy), target: self)
  283. item?.representedObject = controller
  284. menu.addItem(.separator())
  285. item = menu.addItem(title: KMLocalizedString("Delete", "Menu item title"), action: #selector(deleteSnapshot), target: self)
  286. item?.representedObject = controller
  287. item = menu.addItem(title: KMLocalizedString("Delete All Snapshots", "Menu item title"), action: #selector(deleteAllSnapshot), target: self)
  288. item?.representedObject = controller
  289. }
  290. } else if menu.isEqual(to: self.noteOutlineView.menu) {
  291. self.annoListMenu(menu)
  292. }
  293. }
  294. }
  295. // MARK: - NSMenuItemValidation
  296. extension KMLeftSideViewController: NSMenuItemValidation {
  297. func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  298. let isLocked = self.isLocked()
  299. if isLocked {
  300. return false
  301. }
  302. let action = menuItem.action
  303. if (action == #selector(toggleCaseInsensitiveSearch)) {
  304. let state = KMDataManager.ud_integer(forKey: SKCaseInsensitiveSearchKey)
  305. menuItem.state = state == 1 ? .on : .off
  306. return true
  307. } else if (action == #selector(toggleWholeWordSearch)) {
  308. menuItem.state = self.mwcFlags.wholeWordSearch == 1 ? .on : .off
  309. return true
  310. } else if action == #selector(toggleCaseInsensitiveNoteSearch) {
  311. menuItem.state = self.caseInsensitiveNoteSearch ? .on : .off
  312. return true
  313. }
  314. if (action == #selector(outlineContextMenuItemClicked_AddEntry) ||
  315. action == #selector(outlineContextMenuItemClicked_AddChildEntry) ||
  316. action == #selector(outlineContextMenuItemClicked_AddAuntEntry) ||
  317. action == #selector(outlineContextMenuItemClicked_RemoveEntry) ||
  318. action == #selector(outlineContextMenuItemClicked_Edit) ||
  319. action == #selector(outlineContextMenuItemClicked_SetDestination) ||
  320. action == #selector(outlineContextMenuItemClicked_Rename) ||
  321. action == #selector(outlineContextMenuItemClicked_Promote) ||
  322. action == #selector(outlineContextMenuItemClicked_Demote) ||
  323. action == #selector(toggleOutlineCaseInsensitiveSearch) ||
  324. action == #selector(leftSideEmptyAnnotationClick_DeleteOutline)) { // 大纲列表
  325. return self.outlineListValidateMenuItem(menuItem)
  326. }
  327. if (action == #selector(note_expandAllComments) ||
  328. action == #selector(note_foldAllComments) ||
  329. action == #selector(exportAnnotationNotes) ||
  330. action == #selector(removeAllAnnotations) ||
  331. action == #selector(unfoldNoteAction) ||
  332. action == #selector(foldNoteAction) ||
  333. action == #selector(editNoteFromTable)) { // 注释列表
  334. return self.annoListValidateMenuItem(menuItem)
  335. }
  336. if (action == #selector(menuItemClick_ExportPNG) ||
  337. action == #selector(menuItemClick_ExportJPG) ||
  338. action == #selector(menuItemClick_ExportPDF)) {
  339. if (self.snapshotTableView.selectedRow == -1 ) {
  340. return false
  341. } else {
  342. return true
  343. }
  344. } else if (action == #selector(menuItemClick_SelectAll)) {
  345. menuItem.state = self.snapshotListIsAllSelected() ? .on : .off
  346. return true
  347. }
  348. return true
  349. }
  350. }
  351. // MARK: - NSPopoverDelegate
  352. extension KMLeftSideViewController: NSPopoverDelegate {
  353. func popoverWillClose(_ notification: Notification) {
  354. guard let popover = notification.object as? NSPopover else {
  355. return
  356. }
  357. if let vc = popover.contentViewController as? KMOutlineEditViewController {
  358. self.editOutlineUI(vc)
  359. self.tocOutlineView.reloadData()
  360. }
  361. }
  362. }
  363. // MARK: - NSSearchFieldDelegate
  364. extension KMLeftSideViewController: NSSearchFieldDelegate {
  365. func controlTextDidChange(_ obj: Notification) {
  366. if self.outlineSearchField.isEqual(to: obj.object) {
  367. if (self.outlineSearchField.stringValue.isEmpty == false) {
  368. self.isSearchOutlineMode = true
  369. self.tocOutlineView.reloadData()
  370. self.tocOutlineView.expandItem(nil, expandChildren: true)
  371. self.tocType = .unfold
  372. } else {
  373. self.isSearchOutlineMode = false
  374. self.showSearchOutlineBlankState(false)
  375. self.tocOutlineView.reloadData()
  376. }
  377. self.outlineAddButton.isEnabled = !self.isSearchOutlineMode
  378. } else if self.snapshotSearchField.isEqual(to: obj.object) {
  379. let searchString = self.snapshotSearchField.stringValue
  380. // NSPredicate *filterPredicate = nil;
  381. // if ([searchString length] > 0) {
  382. // NSExpression *lhs = [NSExpression expressionForConstantValue:searchString];
  383. // NSExpression *rhs = [NSExpression expressionForKeyPath:@"string"];
  384. // NSUInteger options = NSDiacriticInsensitivePredicateOption;
  385. // if (mwcFlags.caseInsensitiveNoteSearch)
  386. // options |= NSCaseInsensitivePredicateOption;
  387. // filterPredicate = [NSComparisonPredicate predicateWithLeftExpression:lhs rightExpression:rhs modifier:NSDirectPredicateModifier type:NSInPredicateOperatorType options:options];
  388. // }
  389. // [rightSideController.snapshotArrayController setFilterPredicate:filterPredicate];
  390. // NSArray * snapshots = [rightSideController.snapshotArrayController arrangedObjects];
  391. self.searchSnapshots.removeAll()
  392. if searchString.isEmpty {
  393. self.isSearchSnapshotMode = false
  394. self.searchSnapshots = self.snapshots.filter({ model in
  395. let data = model.windowC?.string.contains(searchString) ?? false
  396. return data
  397. })
  398. } else {
  399. self.isSearchSnapshotMode = true
  400. }
  401. var snapshots = self.searchSnapshots
  402. if self.isSearchSnapshotMode == false {
  403. snapshots = self.snapshots
  404. }
  405. Task { @MainActor in
  406. self.updataLeftSideSnapView()
  407. self.snapshotTableView.reloadData()
  408. }
  409. if (snapshots.count > 0) {
  410. self.leftSideEmptyVC.outlineSearchView.removeFromSuperview()
  411. } else {
  412. let view = self.snapshotTableView.enclosingScrollView
  413. let viewFrmae = view?.frame ?? .zero
  414. let emptyVcSize = self.leftSideEmptyVC.outlineSearchView.frame.size
  415. self.leftSideEmptyVC.outlineSearchView.frame = NSMakeRect((viewFrmae.size.width-emptyVcSize.width)/2.0,(viewFrmae.size.height-emptyVcSize.height)/2.0, emptyVcSize.width, emptyVcSize.height)
  416. self.leftSideEmptyVC.outlineSearchView.autoresizingMask = [.minXMargin, .maxXMargin, .minYMargin, .maxYMargin]
  417. self.snapshotTableView.enclosingScrollView?.documentView?.addSubview(self.leftSideEmptyVC.outlineSearchView)
  418. }
  419. } else if self.noteSearchField.isEqual(to: obj.object) {
  420. if let isEmpty = self.noteSearchField?.stringValue.isEmpty {
  421. self.noteSearchMode = !isEmpty
  422. } else {
  423. self.noteSearchMode = false
  424. }
  425. }
  426. }
  427. }
  428. // MARK: - NSTextFieldDelegate
  429. extension KMLeftSideViewController: NSTextFieldDelegate {
  430. func controlTextDidBeginEditing(_ obj: Notification) {
  431. if self.noteOutlineView.isEqual(to: obj.object) {
  432. // if (mwcFlags.isEditingTable == NO && mwcFlags.isEditingPDF == NO)
  433. // [[self document] objectDidBeginEditing:self];
  434. // mwcFlags.isEditingTable = YES;
  435. }
  436. }
  437. func controlTextDidEndEditing(_ obj: Notification) {
  438. if let data = self.editNoteTextField, data.isEqual(to: obj.object) {
  439. // if self.noteOutlineView.isEqual(to: obj.object) {
  440. // if (mwcFlags.isEditingTable && mwcFlags.isEditingPDF == NO)
  441. // [[self document] objectDidEndEditing:self];
  442. // mwcFlags.isEditingTable = NO;
  443. // (self.view.window?.windowController?.document as? NSDocument)?.objectDidEndEditing(self.view.window?.windowController as! NSEditor)
  444. if let data = self.editNote as? CPDFMarkupAnnotation {
  445. data.setMarkupText(self.editNoteTextField?.stringValue ?? "")
  446. } else if let data = self.editNote as? CPDFTextAnnotation {
  447. data.contents = self.editNoteTextField?.stringValue ?? ""
  448. }
  449. } else if let data = self.renamePDFOutlineTextField?.isEqual(to: obj.object), data {
  450. let tf = obj.object as! NSTextField
  451. if let ol = self.renamePDFOutline {
  452. if self.isRenameNoteOutline == false {
  453. if tf.stringValue == ol.label {
  454. return
  455. }
  456. }
  457. self.renamePDFOutline(ol, label: tf.stringValue)
  458. self.updateSelectRowHeight()
  459. self.tocOutlineView.reloadData()
  460. }
  461. }
  462. }
  463. }
  464. // MARK: - Menu Item Actions
  465. extension KMLeftSideViewController {
  466. @objc func toggleCaseInsensitiveNoteSearch(_ sender: AnyObject?) {
  467. self.caseInsensitiveNoteSearch = !self.caseInsensitiveNoteSearch
  468. if self.searchField.stringValue.isEmpty == false {
  469. self.searchNotes(self.searchField)
  470. }
  471. KMDataManager.ud_set(self.caseInsensitiveNoteSearch, forKey: SKCaseInsensitiveNoteSearchKey)
  472. }
  473. @objc func searchNotes(_ sender: AnyObject?) {
  474. self.noteSearchMode = false
  475. if self.findState == .note {
  476. if self.noteSearchField.isEqual(to: sender) {
  477. self.noteSearchMode = !self.noteSearchField.stringValue.isEmpty
  478. }
  479. self.updateNoteFilterPredicate()
  480. } else {
  481. self.updateSnapshotFilterPredicate()
  482. }
  483. let textfield = sender as? NSSearchField
  484. if let data = textfield?.stringValue.isEmpty, data == false {
  485. let findPboard = NSPasteboard(name: .find)
  486. findPboard.clearContents()
  487. // findPboard.writeObjects([textfield!.stringValue])
  488. }
  489. }
  490. func updateSnapshotFilterPredicate() {
  491. let searchString = self.snapshotSearchField.stringValue
  492. self.searchSnapshots.removeAll()
  493. if self.findState == .snapshot && searchString.isEmpty == false {
  494. self.searchSnapshots = self.snapshots.filter({ model in
  495. let data = model.windowC?.string.contains(searchString) ?? false
  496. return data
  497. })
  498. }
  499. Task { @MainActor in
  500. self.updataLeftSideSnapView()
  501. self.snapshotTableView.reloadData()
  502. }
  503. }
  504. func snapshotListIsAllSelected() -> Bool {
  505. if self.snapshots.isEmpty {
  506. return false
  507. }
  508. for model in self.snapshots {
  509. if model.isSelected == false {
  510. return false
  511. }
  512. }
  513. return true
  514. }
  515. }