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