KMLeftSideViewController+Action.swift 28 KB

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