KMLeftSideViewController+Action.swift 28 KB

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