KMLeftSideViewController+Action.swift 27 KB

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