KMLeftSideViewController+Action.swift 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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(leftSideEmptyAnnotationClick_DeleteOutline)) { // 大纲列表
  324. return self.outlineListValidateMenuItem(menuItem)
  325. }
  326. if (action == #selector(note_expandAllComments) ||
  327. action == #selector(note_foldAllComments) ||
  328. action == #selector(exportAnnotationNotes) ||
  329. action == #selector(removeAllAnnotations) ||
  330. action == #selector(unfoldNoteAction) ||
  331. action == #selector(foldNoteAction) ||
  332. action == #selector(editNoteFromTable)) { // 注释列表
  333. return self.annoListValidateMenuItem(menuItem)
  334. }
  335. if (action == #selector(menuItemClick_ExportPNG) ||
  336. action == #selector(menuItemClick_ExportJPG) ||
  337. action == #selector(menuItemClick_ExportPDF)) {
  338. if (self.snapshotTableView.selectedRow == -1 ) {
  339. return false
  340. } else {
  341. return true
  342. }
  343. } else if (action == #selector(menuItemClick_SelectAll)) {
  344. menuItem.state = self.snapshotListIsAllSelected() ? .on : .off
  345. return true
  346. }
  347. return true
  348. }
  349. }
  350. // MARK: - NSPopoverDelegate
  351. extension KMLeftSideViewController: NSPopoverDelegate {
  352. func popoverWillClose(_ notification: Notification) {
  353. guard let popover = notification.object as? NSPopover else {
  354. return
  355. }
  356. if let vc = popover.contentViewController as? KMOutlineEditViewController {
  357. self.editOutlineUI(vc)
  358. self.tocOutlineView.reloadData()
  359. }
  360. }
  361. }
  362. // MARK: - NSSearchFieldDelegate
  363. extension KMLeftSideViewController: NSSearchFieldDelegate {
  364. func controlTextDidChange(_ obj: Notification) {
  365. if self.outlineSearchField.isEqual(to: obj.object) {
  366. if (self.outlineSearchField.stringValue.isEmpty == false) {
  367. self.isSearchOutlineMode = true
  368. self.tocOutlineView.reloadData()
  369. self.tocOutlineView.expandItem(nil, expandChildren: true)
  370. self.tocType = .unfold
  371. } else {
  372. self.isSearchOutlineMode = false
  373. self.showSearchOutlineBlankState(false)
  374. self.tocOutlineView.reloadData()
  375. }
  376. self.outlineAddButton.isEnabled = !self.isSearchOutlineMode
  377. } else if self.snapshotSearchField.isEqual(to: obj.object) {
  378. let searchString = self.snapshotSearchField.stringValue
  379. // NSPredicate *filterPredicate = nil;
  380. // if ([searchString length] > 0) {
  381. // NSExpression *lhs = [NSExpression expressionForConstantValue:searchString];
  382. // NSExpression *rhs = [NSExpression expressionForKeyPath:@"string"];
  383. // NSUInteger options = NSDiacriticInsensitivePredicateOption;
  384. // if (mwcFlags.caseInsensitiveNoteSearch)
  385. // options |= NSCaseInsensitivePredicateOption;
  386. // filterPredicate = [NSComparisonPredicate predicateWithLeftExpression:lhs rightExpression:rhs modifier:NSDirectPredicateModifier type:NSInPredicateOperatorType options:options];
  387. // }
  388. // [rightSideController.snapshotArrayController setFilterPredicate:filterPredicate];
  389. // NSArray * snapshots = [rightSideController.snapshotArrayController arrangedObjects];
  390. self.searchSnapshots.removeAll()
  391. if searchString.isEmpty {
  392. self.isSearchSnapshotMode = false
  393. self.searchSnapshots = self.snapshots.filter({ model in
  394. let data = model.windowC?.string.contains(searchString) ?? false
  395. return data
  396. })
  397. } else {
  398. self.isSearchSnapshotMode = true
  399. }
  400. var snapshots = self.searchSnapshots
  401. if self.isSearchSnapshotMode == false {
  402. snapshots = self.snapshots
  403. }
  404. Task { @MainActor in
  405. self.updataLeftSideSnapView()
  406. self.snapshotTableView.reloadData()
  407. }
  408. if (snapshots.count > 0) {
  409. self.leftSideEmptyVC.outlineSearchView.removeFromSuperview()
  410. } else {
  411. let view = self.snapshotTableView.enclosingScrollView
  412. let viewFrmae = view?.frame ?? .zero
  413. let emptyVcSize = self.leftSideEmptyVC.outlineSearchView.frame.size
  414. self.leftSideEmptyVC.outlineSearchView.frame = NSMakeRect((viewFrmae.size.width-emptyVcSize.width)/2.0,(viewFrmae.size.height-emptyVcSize.height)/2.0, emptyVcSize.width, emptyVcSize.height)
  415. self.leftSideEmptyVC.outlineSearchView.autoresizingMask = [.minXMargin, .maxXMargin, .minYMargin, .maxYMargin]
  416. self.snapshotTableView.enclosingScrollView?.documentView?.addSubview(self.leftSideEmptyVC.outlineSearchView)
  417. }
  418. } else if self.noteSearchField.isEqual(to: obj.object) {
  419. if let isEmpty = self.noteSearchField?.stringValue.isEmpty {
  420. self.noteSearchMode = !isEmpty
  421. } else {
  422. self.noteSearchMode = false
  423. }
  424. }
  425. }
  426. }
  427. // MARK: - NSTextFieldDelegate
  428. extension KMLeftSideViewController: NSTextFieldDelegate {
  429. func controlTextDidBeginEditing(_ obj: Notification) {
  430. if self.noteOutlineView.isEqual(to: obj.object) {
  431. // if (mwcFlags.isEditingTable == NO && mwcFlags.isEditingPDF == NO)
  432. // [[self document] objectDidBeginEditing:self];
  433. // mwcFlags.isEditingTable = YES;
  434. }
  435. }
  436. func controlTextDidEndEditing(_ obj: Notification) {
  437. if let data = self.editNoteTextField, data.isEqual(to: obj.object) {
  438. // if self.noteOutlineView.isEqual(to: obj.object) {
  439. // if (mwcFlags.isEditingTable && mwcFlags.isEditingPDF == NO)
  440. // [[self document] objectDidEndEditing:self];
  441. // mwcFlags.isEditingTable = NO;
  442. // (self.view.window?.windowController?.document as? NSDocument)?.objectDidEndEditing(self.view.window?.windowController as! NSEditor)
  443. if let data = self.editNote as? CPDFMarkupAnnotation {
  444. data.setMarkupText(self.editNoteTextField?.stringValue ?? "")
  445. } else if let data = self.editNote as? CPDFTextAnnotation {
  446. data.contents = self.editNoteTextField?.stringValue ?? ""
  447. }
  448. } else if let data = self.renamePDFOutlineTextField?.isEqual(to: obj.object), data {
  449. let tf = obj.object as! NSTextField
  450. if let ol = self.renamePDFOutline {
  451. if self.isRenameNoteOutline == false {
  452. if tf.stringValue == ol.label {
  453. return
  454. }
  455. }
  456. self.renamePDFOutline(ol, label: tf.stringValue)
  457. self.updateSelectRowHeight()
  458. self.tocOutlineView.reloadData()
  459. }
  460. }
  461. }
  462. }
  463. // MARK: - Menu Item Actions
  464. extension KMLeftSideViewController {
  465. @objc func toggleCaseInsensitiveNoteSearch(_ sender: AnyObject?) {
  466. self.caseInsensitiveNoteSearch = !self.caseInsensitiveNoteSearch
  467. if self.searchField.stringValue.isEmpty == false {
  468. self.searchNotes(self.searchField)
  469. }
  470. KMDataManager.ud_set(self.caseInsensitiveNoteSearch, forKey: SKCaseInsensitiveNoteSearchKey)
  471. }
  472. @objc func searchNotes(_ sender: AnyObject?) {
  473. self.noteSearchMode = false
  474. if self.findState == .note {
  475. if self.noteSearchField.isEqual(to: sender) {
  476. self.noteSearchMode = !self.noteSearchField.stringValue.isEmpty
  477. }
  478. self.updateNoteFilterPredicate()
  479. } else {
  480. self.updateSnapshotFilterPredicate()
  481. }
  482. let textfield = sender as? NSSearchField
  483. if let data = textfield?.stringValue.isEmpty, data == false {
  484. let findPboard = NSPasteboard(name: .find)
  485. findPboard.clearContents()
  486. // findPboard.writeObjects([textfield!.stringValue])
  487. }
  488. }
  489. func updateSnapshotFilterPredicate() {
  490. let searchString = self.snapshotSearchField.stringValue
  491. self.searchSnapshots.removeAll()
  492. if self.findState == .snapshot && searchString.isEmpty == false {
  493. self.searchSnapshots = self.snapshots.filter({ model in
  494. let data = model.windowC?.string.contains(searchString) ?? false
  495. return data
  496. })
  497. }
  498. Task { @MainActor in
  499. self.updataLeftSideSnapView()
  500. self.snapshotTableView.reloadData()
  501. }
  502. }
  503. func snapshotListIsAllSelected() -> Bool {
  504. if self.snapshots.isEmpty {
  505. return false
  506. }
  507. for model in self.snapshots {
  508. if model.isSelected == false {
  509. return false
  510. }
  511. }
  512. return true
  513. }
  514. }