KMLeftSideViewController+Action.swift 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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 == 4 {
  91. self.bookmarkViewC?.reloadData()
  92. } else {
  93. self.leftView.segmentedControl.selectedSegment = 4
  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. } else if action == #selector(exportNotes) {
  365. let cnt = self.annoListModel?.datas.count ?? 0
  366. return cnt > 0
  367. } else if action == #selector(removeAllReplyAnnotations) {
  368. for sectionM in self.annoListModel?.datas ?? [] {
  369. for model in sectionM.items {
  370. guard let itemM = model as? KMBotaAnnotationModel else {
  371. continue
  372. }
  373. if itemM.replyAnnos.count > 0 {
  374. return true
  375. }
  376. }
  377. }
  378. // let cnt = self.listView?.notes?.count ?? 0
  379. // return cnt > 0
  380. return false
  381. }
  382. return true
  383. }
  384. }
  385. // MARK: - NSPopoverDelegate
  386. extension KMLeftSideViewController: NSPopoverDelegate {
  387. func popoverWillClose(_ notification: Notification) {
  388. guard let popover = notification.object as? NSPopover else {
  389. return
  390. }
  391. if let vc = popover.contentViewController as? KMOutlineEditViewController {
  392. self.editOutlineUI(vc)
  393. self.tocOutlineView.reloadData()
  394. }
  395. }
  396. }
  397. // MARK: - NSSearchFieldDelegate
  398. extension KMLeftSideViewController: NSSearchFieldDelegate {
  399. func controlTextDidChange(_ obj: Notification) {
  400. if self.outlineSearchField.isEqual(to: obj.object) {
  401. if (self.outlineSearchField.stringValue.isEmpty == false) {
  402. self.isSearchOutlineMode = true
  403. self.tocOutlineView.reloadData()
  404. self.tocOutlineView.expandItem(nil, expandChildren: true)
  405. self.tocType = .unfold
  406. } else {
  407. self.isSearchOutlineMode = false
  408. self.showSearchOutlineBlankState(false)
  409. self.tocOutlineView.reloadData()
  410. }
  411. self.outlineAddButton.isEnabled = !self.isSearchOutlineMode
  412. } else if self.snapshotSearchField.isEqual(to: obj.object) {
  413. let searchString = self.snapshotSearchField.stringValue
  414. // NSPredicate *filterPredicate = nil;
  415. // if ([searchString length] > 0) {
  416. // NSExpression *lhs = [NSExpression expressionForConstantValue:searchString];
  417. // NSExpression *rhs = [NSExpression expressionForKeyPath:@"string"];
  418. // NSUInteger options = NSDiacriticInsensitivePredicateOption;
  419. // if (mwcFlags.caseInsensitiveNoteSearch)
  420. // options |= NSCaseInsensitivePredicateOption;
  421. // filterPredicate = [NSComparisonPredicate predicateWithLeftExpression:lhs rightExpression:rhs modifier:NSDirectPredicateModifier type:NSInPredicateOperatorType options:options];
  422. // }
  423. // [rightSideController.snapshotArrayController setFilterPredicate:filterPredicate];
  424. // NSArray * snapshots = [rightSideController.snapshotArrayController arrangedObjects];
  425. self.searchSnapshots.removeAll()
  426. if searchString.isEmpty {
  427. self.isSearchSnapshotMode = false
  428. self.searchSnapshots = self.snapshots.filter({ model in
  429. let data = model.windowC?.string.contains(searchString) ?? false
  430. return data
  431. })
  432. } else {
  433. self.isSearchSnapshotMode = true
  434. }
  435. var snapshots = self.searchSnapshots
  436. if self.isSearchSnapshotMode == false {
  437. snapshots = self.snapshots
  438. }
  439. Task { @MainActor in
  440. self.updataLeftSideSnapView()
  441. self.snapshotTableView.reloadData()
  442. }
  443. if (snapshots.count > 0) {
  444. self.leftSideEmptyVC.outlineSearchView.removeFromSuperview()
  445. } else {
  446. let view = self.snapshotTableView.enclosingScrollView
  447. let viewFrmae = view?.frame ?? .zero
  448. let emptyVcSize = self.leftSideEmptyVC.outlineSearchView.frame.size
  449. self.leftSideEmptyVC.outlineSearchView.frame = NSMakeRect((viewFrmae.size.width-emptyVcSize.width)/2.0,(viewFrmae.size.height-emptyVcSize.height)/2.0, emptyVcSize.width, emptyVcSize.height)
  450. self.leftSideEmptyVC.outlineSearchView.autoresizingMask = [.minXMargin, .maxXMargin, .minYMargin, .maxYMargin]
  451. self.snapshotTableView.enclosingScrollView?.documentView?.addSubview(self.leftSideEmptyVC.outlineSearchView)
  452. }
  453. } else if self.noteSearchField.isEqual(to: obj.object) {
  454. if let isEmpty = self.noteSearchField?.stringValue.isEmpty {
  455. self.noteSearchMode = !isEmpty
  456. } else {
  457. self.noteSearchMode = false
  458. }
  459. }
  460. }
  461. }
  462. // MARK: - NSTextFieldDelegate
  463. extension KMLeftSideViewController: NSTextFieldDelegate {
  464. func controlTextDidBeginEditing(_ obj: Notification) {
  465. if self.noteOutlineView.isEqual(to: obj.object) {
  466. // if (mwcFlags.isEditingTable == NO && mwcFlags.isEditingPDF == NO)
  467. // [[self document] objectDidBeginEditing:self];
  468. // mwcFlags.isEditingTable = YES;
  469. }
  470. }
  471. func controlTextDidEndEditing(_ obj: Notification) {
  472. if let data = self.editNoteTextField, data.isEqual(to: obj.object) {
  473. // if self.noteOutlineView.isEqual(to: obj.object) {
  474. // if (mwcFlags.isEditingTable && mwcFlags.isEditingPDF == NO)
  475. // [[self document] objectDidEndEditing:self];
  476. // mwcFlags.isEditingTable = NO;
  477. // (self.view.window?.windowController?.document as? NSDocument)?.objectDidEndEditing(self.view.window?.windowController as! NSEditor)
  478. if let data = self.editNote as? CPDFMarkupAnnotation {
  479. data.setMarkupText(self.editNoteTextField?.stringValue ?? "")
  480. } else if let data = self.editNote as? CPDFTextAnnotation {
  481. data.contents = self.editNoteTextField?.stringValue ?? ""
  482. }
  483. } else if let data = self.renamePDFOutlineTextField?.isEqual(to: obj.object), data {
  484. let tf = obj.object as! NSTextField
  485. if let ol = self.renamePDFOutline {
  486. if self.isRenameNoteOutline == false {
  487. if tf.stringValue == ol.label {
  488. return
  489. }
  490. }
  491. self.renamePDFOutline(ol, label: tf.stringValue)
  492. self.updateSelectRowHeight()
  493. self.tocOutlineView.reloadData()
  494. }
  495. }
  496. }
  497. }
  498. // MARK: - Menu Item Actions
  499. extension KMLeftSideViewController {
  500. @objc func toggleCaseInsensitiveNoteSearch(_ sender: AnyObject?) {
  501. self.caseInsensitiveNoteSearch = !self.caseInsensitiveNoteSearch
  502. if self.searchField.stringValue.isEmpty == false {
  503. self.searchNotes(self.searchField)
  504. }
  505. KMDataManager.ud_set(self.caseInsensitiveNoteSearch, forKey: SKCaseInsensitiveNoteSearchKey)
  506. }
  507. @objc func searchNotes(_ sender: AnyObject?) {
  508. self.noteSearchMode = false
  509. if self.findState == .note {
  510. if self.noteSearchField.isEqual(to: sender) {
  511. self.noteSearchMode = !self.noteSearchField.stringValue.isEmpty
  512. }
  513. self.updateNoteFilterPredicate()
  514. } else {
  515. self.updateSnapshotFilterPredicate()
  516. }
  517. let textfield = sender as? NSSearchField
  518. if let data = textfield?.stringValue.isEmpty, data == false {
  519. let findPboard = NSPasteboard(name: .find)
  520. findPboard.clearContents()
  521. // findPboard.writeObjects([textfield!.stringValue])
  522. }
  523. }
  524. func updateSnapshotFilterPredicate() {
  525. let searchString = self.snapshotSearchField.stringValue
  526. self.searchSnapshots.removeAll()
  527. if self.findState == .snapshot && searchString.isEmpty == false {
  528. self.searchSnapshots = self.snapshots.filter({ model in
  529. let data = model.windowC?.string.contains(searchString) ?? false
  530. return data
  531. })
  532. }
  533. Task { @MainActor in
  534. self.updataLeftSideSnapView()
  535. self.snapshotTableView.reloadData()
  536. }
  537. }
  538. func snapshotListIsAllSelected() -> Bool {
  539. if self.snapshots.isEmpty {
  540. return false
  541. }
  542. for model in self.snapshots {
  543. if model.isSelected == false {
  544. return false
  545. }
  546. }
  547. return true
  548. }
  549. }