KMLeftSideViewController+Action.swift 28 KB

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