KMLeftSideViewController+Action.swift 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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. self.bookmarkViewC?.interfaceThemeDidChanged(appearance)
  119. if self.type.methodType == .Search {
  120. if self.findPaneState == .singular {
  121. self.findTableView.reloadData()
  122. }
  123. }
  124. }
  125. }
  126. }
  127. // MARK: - NSMenuDelegate
  128. extension KMLeftSideViewController: NSMenuDelegate {
  129. func menuNeedsUpdate(_ menu: NSMenu) {
  130. menu.removeAllItems()
  131. if menu.isEqual(to: self.tocOutlineView.menu) {
  132. self.outlineListMenu(menu)
  133. return
  134. }
  135. var item: NSMenuItem?
  136. if menu.isEqual(to: self.thumbnailTableView.menu) {
  137. let row = self.thumbnailTableView.clickedRow
  138. if self.pdfDocument()?.documentURL == nil || self.thumbnailTableView.selectedRowIndexes.contains(row) == false{
  139. return
  140. }
  141. let isLocked = self.isLocked()
  142. let pageCount = self.pageCount()
  143. if (row != -1 && isLocked == false) {
  144. if(self.thumbnailTableView.selectedRowIndexes.count == pageCount) {
  145. item = menu.addItem(title: KMLocalizedString("Cut", "Menu item title"), action: nil, target: self)
  146. } else {
  147. item = menu.addItem(title: KMLocalizedString("Cut", "Menu item title"), action: #selector(cutPage), target: self)
  148. }
  149. item?.representedObject = IndexSet(integer: row)
  150. item = menu.addItem(title: KMLocalizedString("Copy", "Menu item title"), action: #selector(copyPage), target: self)
  151. item?.representedObject = IndexSet(integer: row)
  152. if (self.copyPages.count > 0) {
  153. item = menu.addItem(title: KMLocalizedString("Paste", "Menu item title"), action: #selector(pastePage), target: self)
  154. }else{
  155. item = menu.addItem(title: KMLocalizedString("Paste", "Menu item title"), action: nil, target: self)
  156. }
  157. item?.representedObject = IndexSet(integer: row)
  158. menu.addItem(.separator())
  159. if(self.thumbnailTableView.selectedRowIndexes.count == pageCount) {
  160. item = menu.addItem(title: KMLocalizedString("Delete", "Menu item title"), action: nil, target: self)
  161. } else {
  162. item = menu.addItem(title: KMLocalizedString("Delete", "Menu item title"), action: #selector(deletePage), target: self)
  163. }
  164. item?.representedObject = IndexSet(integer: row)
  165. menu.addItem(.separator())
  166. item = menu.addItem(title: KMLocalizedString("Rotate", "Menu item title"), action: #selector(rotatePageMenuAction), target: self)
  167. item?.representedObject = IndexSet(integer: row)
  168. item = menu.addItem(title: KMLocalizedString("Insert", "Menu item title"), action: nil, target: self)
  169. let subMenu = NSMenu()
  170. _ = subMenu.addItem(title: KMLocalizedString("Blank Page", "Menu item title"), action: #selector(quickInsert), target: self)
  171. _ = subMenu.addItem(title: KMLocalizedString("Blank Page - Custom...", "Menu item title"), action: #selector(insert), target: self)
  172. _ = subMenu.addItem(title: KMLocalizedString("From PDF", "Menu item title"), action: #selector(insertPDF), target: self)
  173. item?.submenu = subMenu
  174. item?.representedObject = IndexSet(integer: row)
  175. item = menu.addItem(title: KMLocalizedString("Extract", "Menu item title"), action: #selector(extractPage), target: self)
  176. item?.representedObject = IndexSet(integer: row)
  177. item = menu.addItem(title: KMLocalizedString("Page Edit", "Menu item title"), action: #selector(pageEdit), target: self)
  178. menu.addItem(.separator())
  179. var displayStr = ""
  180. if (self.isDisplayPageSize) {
  181. displayStr = KMLocalizedString("Hide Page Size", "Menu item title")
  182. } else {
  183. displayStr = KMLocalizedString("Display Page Size", "Menu item title")
  184. }
  185. item = menu.addItem(title: displayStr, action: #selector(displayPageSize), target: self)
  186. item?.representedObject = IndexSet(integer: row)
  187. if let doct = self.pdfDocument() {
  188. item = menu.addItem(title: KMLocalizedString("Share", "Menu item title"), action: nil, target: self)
  189. var tName = self.fileNameWithSelectedPages(self.thumbnailTableView.selectedRowIndexes)
  190. if (tName.count > 50) {
  191. tName = tName.substring(to: 50)
  192. }
  193. item?.submenu = NSSharingServicePicker.menu(forSharingItems: [doct.documentURL as Any], subjectContext: tName, withTarget: self, selector: #selector(sharePage), serviceDelegate: nil)
  194. }
  195. }
  196. } else if menu.isEqual(to: self.findTableView.menu) {
  197. var rowIndexes = self.findTableView.selectedRowIndexes
  198. let row = self.findTableView.clickedRow
  199. if (row != -1) {
  200. if rowIndexes.contains(row) == false {
  201. rowIndexes = IndexSet(integer: row)
  202. }
  203. var selections: [CPDFSelection] = []
  204. for (i, data) in self.searchResults.enumerated() {
  205. if rowIndexes.contains(i) {
  206. selections.append(data.selection)
  207. }
  208. }
  209. let hideNotes = self.hideNotes()
  210. let allowsNotes = self.allowsNotes()
  211. if hideNotes == false && allowsNotes {
  212. item = menu.addItem(withTitle: KMLocalizedString("Add New Circle", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.circle.rawValue)
  213. item?.representedObject = selections
  214. item = menu.addItem(withTitle: KMLocalizedString("Add New Rectangle", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.square.rawValue)
  215. item?.representedObject = selections
  216. item = menu.addItem(withTitle: KMLocalizedString("Add New Highlight", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.highlight.rawValue)
  217. item?.representedObject = selections
  218. item = menu.addItem(withTitle: KMLocalizedString("Add New Underline", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.underline.rawValue)
  219. item?.representedObject = selections
  220. item = menu.addItem(withTitle: KMLocalizedString("Add New Strikethrough", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.strikeOut.rawValue)
  221. item?.representedObject = selections
  222. }
  223. }
  224. } else if menu.isEqual(to: self.groupedFindTableView.menu) {
  225. var rowIndexes = self.groupedFindTableView.selectedRowIndexes
  226. let row = self.groupedFindTableView.clickedRow
  227. if (row != -1) {
  228. if rowIndexes.contains(row) == false {
  229. rowIndexes = IndexSet(integer: row)
  230. }
  231. // NSArray *selections = [[[leftSideController.groupedFindArrayController arrangedObjects] objectsAtIndexes:rowIndexes] valueForKeyPath:@"@unionOfArrays.matches"];
  232. var selections: [CPDFSelection] = []
  233. for (i, data) in self.groupSearchResults.enumerated() {
  234. if rowIndexes.contains(i) {
  235. for searchM in data.datas {
  236. selections.append(searchM.selection)
  237. }
  238. }
  239. }
  240. item = menu.addItem(title: KMLocalizedString("Select", "Menu item title"), action: #selector(selectSelections), target: self)
  241. item?.representedObject = selections
  242. menu.addItem(.separator())
  243. let hideNotes = self.hideNotes()
  244. let allowsNotes = self.allowsNotes()
  245. if hideNotes == false && allowsNotes {
  246. item = menu.addItem(withTitle: KMLocalizedString("Add New Circle", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.circle.rawValue)
  247. item?.representedObject = selections
  248. item = menu.addItem(withTitle: KMLocalizedString("Add New Rectangle", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.square.rawValue)
  249. item?.representedObject = selections
  250. item = menu.addItem(withTitle: KMLocalizedString("Add New Highlight", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.highlight.rawValue)
  251. item?.representedObject = selections
  252. item = menu.addItem(withTitle: KMLocalizedString("Add New Underline", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.underline.rawValue)
  253. item?.representedObject = selections
  254. item = menu.addItem(withTitle: KMLocalizedString("Add New Strikethrough", "Menu item title"), action: #selector(addAnnotationsForSelections), target: self, tag: CAnnotationType.strikeOut.rawValue)
  255. item?.representedObject = selections
  256. }
  257. }
  258. } else if menu.isEqual(to: self.snapshotTableView.menu) {
  259. let row = self.snapshotTableView.clickedRow
  260. if (row != -1) {
  261. let model = self.snapshots[row]
  262. let controller = model.windowC
  263. if let data = controller?.window?.isVisible, data {
  264. item = menu.addItem(title: KMLocalizedString("Hide", "Menu item title"), action: #selector(hideSnapshot), target: self)
  265. item?.representedObject = controller
  266. } else {
  267. item = menu.addItem(title: KMLocalizedString("Show", "Menu item title"), action: #selector(showSnapshot), target: self)
  268. item?.representedObject = controller
  269. }
  270. menu.addItem(.separator())
  271. item = menu.addItem(title: KMLocalizedString("Export", "Menu item title"), action: nil, target: self)
  272. let subMenu = NSMenu()
  273. var t = subMenu.addItem(title: KMLocalizedString("PNG", "Menu item title"), action: #selector(menuItemClick_ExportPNG), target: self)
  274. t?.representedObject = controller
  275. t = subMenu.addItem(title: KMLocalizedString("JPG", "Menu item title"), action: #selector(menuItemClick_ExportJPG), target: self)
  276. t?.representedObject = controller
  277. t = subMenu.addItem(title: KMLocalizedString("PDF", "Menu item title"), action: #selector(menuItemClick_ExportPDF), target: self)
  278. t?.representedObject = controller
  279. item?.submenu = subMenu
  280. item = menu.addItem(title: KMLocalizedString("Print", "Menu item title"), action: #selector(menuItemClick_Print), target: self)
  281. item?.representedObject = controller
  282. menu.addItem(.separator())
  283. item = menu.addItem(title: KMLocalizedString("Copy", "Menu item title"), action: #selector(menuItemClick_Copy), target: self)
  284. item?.representedObject = controller
  285. menu.addItem(.separator())
  286. item = menu.addItem(title: KMLocalizedString("Delete", "Menu item title"), action: #selector(deleteSnapshot), target: self)
  287. item?.representedObject = controller
  288. item = menu.addItem(title: KMLocalizedString("Delete All Snapshots", "Menu item title"), action: #selector(deleteAllSnapshot), target: self)
  289. item?.representedObject = controller
  290. }
  291. } else if menu.isEqual(to: self.noteOutlineView.menu) {
  292. self.annoListMenu(menu)
  293. }
  294. }
  295. }
  296. // MARK: - NSMenuItemValidation
  297. extension KMLeftSideViewController: NSMenuItemValidation {
  298. func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  299. let isLocked = self.isLocked()
  300. if isLocked {
  301. return false
  302. }
  303. let action = menuItem.action
  304. if (action == #selector(toggleCaseInsensitiveSearch)) {
  305. let state = KMDataManager.ud_integer(forKey: SKCaseInsensitiveSearchKey)
  306. menuItem.state = state == 1 ? .on : .off
  307. return true
  308. } else if (action == #selector(toggleWholeWordSearch)) {
  309. menuItem.state = self.mwcFlags.wholeWordSearch == 1 ? .on : .off
  310. return true
  311. } else if action == #selector(toggleCaseInsensitiveNoteSearch) {
  312. menuItem.state = self.caseInsensitiveNoteSearch ? .on : .off
  313. return true
  314. }
  315. if (action == #selector(outlineContextMenuItemClicked_AddEntry) ||
  316. action == #selector(outlineContextMenuItemClicked_AddChildEntry) ||
  317. action == #selector(outlineContextMenuItemClicked_AddAuntEntry) ||
  318. action == #selector(outlineContextMenuItemClicked_RemoveEntry) ||
  319. action == #selector(outlineContextMenuItemClicked_Edit) ||
  320. action == #selector(outlineContextMenuItemClicked_SetDestination) ||
  321. action == #selector(outlineContextMenuItemClicked_Rename) ||
  322. action == #selector(outlineContextMenuItemClicked_Promote) ||
  323. action == #selector(outlineContextMenuItemClicked_Demote) ||
  324. action == #selector(toggleOutlineCaseInsensitiveSearch) ||
  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 let data = self.editNoteTextField, data.isEqual(to: obj.object) {
  440. // if self.noteOutlineView.isEqual(to: obj.object) {
  441. // if (mwcFlags.isEditingTable && mwcFlags.isEditingPDF == NO)
  442. // [[self document] objectDidEndEditing:self];
  443. // mwcFlags.isEditingTable = NO;
  444. // (self.view.window?.windowController?.document as? NSDocument)?.objectDidEndEditing(self.view.window?.windowController as! NSEditor)
  445. if let data = self.editNote as? CPDFMarkupAnnotation {
  446. data.setMarkupText(self.editNoteTextField?.stringValue ?? "")
  447. } else if let data = self.editNote as? CPDFTextAnnotation {
  448. data.contents = self.editNoteTextField?.stringValue ?? ""
  449. }
  450. } else if let data = self.renamePDFOutlineTextField?.isEqual(to: obj.object), data {
  451. let tf = obj.object as! NSTextField
  452. if let ol = self.renamePDFOutline {
  453. if self.isRenameNoteOutline == false {
  454. if tf.stringValue == ol.label {
  455. return
  456. }
  457. }
  458. self.renamePDFOutline(ol, label: tf.stringValue)
  459. self.updateSelectRowHeight()
  460. self.tocOutlineView.reloadData()
  461. }
  462. }
  463. }
  464. }
  465. // MARK: - Menu Item Actions
  466. extension KMLeftSideViewController {
  467. @objc func toggleCaseInsensitiveNoteSearch(_ sender: AnyObject?) {
  468. self.caseInsensitiveNoteSearch = !self.caseInsensitiveNoteSearch
  469. if self.searchField.stringValue.isEmpty == false {
  470. self.searchNotes(self.searchField)
  471. }
  472. KMDataManager.ud_set(self.caseInsensitiveNoteSearch, forKey: SKCaseInsensitiveNoteSearchKey)
  473. }
  474. @objc func searchNotes(_ sender: AnyObject?) {
  475. self.noteSearchMode = false
  476. if self.findState == .note {
  477. if self.noteSearchField.isEqual(to: sender) {
  478. self.noteSearchMode = !self.noteSearchField.stringValue.isEmpty
  479. }
  480. self.updateNoteFilterPredicate()
  481. } else {
  482. self.updateSnapshotFilterPredicate()
  483. }
  484. let textfield = sender as? NSSearchField
  485. if let data = textfield?.stringValue.isEmpty, data == false {
  486. let findPboard = NSPasteboard(name: .find)
  487. findPboard.clearContents()
  488. // findPboard.writeObjects([textfield!.stringValue])
  489. }
  490. }
  491. func updateSnapshotFilterPredicate() {
  492. let searchString = self.snapshotSearchField.stringValue
  493. self.searchSnapshots.removeAll()
  494. if self.findState == .snapshot && searchString.isEmpty == false {
  495. self.searchSnapshots = self.snapshots.filter({ model in
  496. let data = model.windowC?.string.contains(searchString) ?? false
  497. return data
  498. })
  499. }
  500. Task { @MainActor in
  501. self.updataLeftSideSnapView()
  502. self.snapshotTableView.reloadData()
  503. }
  504. }
  505. func snapshotListIsAllSelected() -> Bool {
  506. if self.snapshots.isEmpty {
  507. return false
  508. }
  509. for model in self.snapshots {
  510. if model.isSelected == false {
  511. return false
  512. }
  513. }
  514. return true
  515. }
  516. }