KMLeftSideViewController+Note.swift 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207
  1. //
  2. // KMLeftSideViewController+Note.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/12/23.
  6. //
  7. import Foundation
  8. extension KMLeftSideViewController.Key {
  9. static let noteAscendSortKey = "KMLeftSideViewAscendSortBoolKey"
  10. static let noteSortTypeKey = "KMLeftSideViewNoteSortTypeKey"
  11. static let noteTableColumn = "KMNoteOutlineViewTableColumnKey"
  12. static let noteFilterPage = "kKMNoteFilterAnnotationPageKey"
  13. static let noteFilterTime = "kKMNoteFilterAnnotationTimeKey"
  14. static let noteFilterAuther = "kKMNoteFilterAnnotationAutherKey"
  15. }
  16. // MARK: - Action
  17. extension KMLeftSideViewController {
  18. func note_initSubViews() {
  19. noteOutlineView.delegate = self
  20. noteOutlineView.dataSource = self
  21. noteOutlineView.botaDelegate = self
  22. noteOutlineView.botaDataSource = self
  23. noteOutlineView.noteDelegate = self
  24. noteOutlineView.menu = NSMenu()
  25. noteOutlineView.menu?.delegate = self
  26. noteOutlineView.typeSelectHelper = SKTypeSelectHelper(matchOption: .SKSubstringMatch)
  27. noteOutlineView.enclosingScrollView?.scrollerStyle = .legacy
  28. noteOutlineView.enclosingScrollView?.autohidesScrollers = true
  29. noteOutlineView.registerForDraggedTypes(NSColor.readableTypes(for: NSPasteboard(name: .drag)))
  30. noteOutlineView.target = self
  31. noteOutlineView.doubleAction = #selector(selectSelectedNote)
  32. }
  33. func note_initDefalutValue() {
  34. let sud = UserDefaults.standard
  35. if let dict = sud.dictionary(forKey: Self.Key.noteTableColumn) {
  36. self.noteTypeDict = dict
  37. } else {
  38. self.noteTypeDict = [Self.Key.noteFilterPage : false,
  39. Self.Key.noteFilterTime : false,
  40. Self.Key.noteFilterAuther : false]
  41. sud.sync_setValue(self.noteTypeDict, forKey: Self.Key.noteTableColumn)
  42. }
  43. self.caseInsensitiveNoteSearch = sud.bool(forKey: SKCaseInsensitiveNoteSearchKey)
  44. self.isAscendSort = KMDataManager.ud_bool(forKey: Self.Key.noteAscendSortKey)
  45. var sortType = 0
  46. if let data = self.listView?.document?.documentURL.path {
  47. sortType = KMDataManager.udExtension_object(forKey: Self.Key.noteSortTypeKey + data) as? Int ?? 0
  48. }
  49. if (sortType == 1) {
  50. noteSortType = KMNoteSortType(rawValue: sortType) ?? .none
  51. } else {
  52. noteSortType = .page
  53. }
  54. noteOutlineView.autoresizesOutlineColumn = false
  55. noteOutlineView.indentationPerLevel = 0
  56. }
  57. func annoListIsShowPage() -> Bool {
  58. return !(self.noteTypeDict[Self.Key.noteFilterPage] as? Bool ?? false)
  59. }
  60. func annoListIsShowTime() -> Bool {
  61. return !(self.noteTypeDict[Self.Key.noteFilterTime] as? Bool ?? false)
  62. }
  63. func annoListIsShowAnther() -> Bool {
  64. return !(self.noteTypeDict[Self.Key.noteFilterAuther] as? Bool ?? false)
  65. }
  66. }
  67. // MARK: - Menu
  68. extension KMLeftSideViewController {
  69. func annoListMenu(_ menu: NSMenu) {
  70. var item: NSMenuItem?
  71. var items: NSArray?
  72. var rowIndexes = self.noteOutlineView.selectedRowIndexes
  73. let row = self.noteOutlineView.clickedRow
  74. if row == -1 {
  75. _ = self._addDeleteAllAnnoItem(menu)
  76. return
  77. }
  78. if rowIndexes.contains(row) == false {
  79. rowIndexes = IndexSet(integer: row)
  80. }
  81. items = self.noteOutlineView.itemsAtRowIndexes(rowIndexes) as NSArray
  82. guard let model = self.fetchAnnoModel(for: row) else {
  83. return
  84. }
  85. let isFold = model.isFold()
  86. item = menu.addItem(title: KMLocalizedString("Expand"), action: #selector(unfoldNoteAction), target: self)
  87. item?.state = isFold ? .off : .on
  88. item?.representedObject = items
  89. item = menu.addItem(title: KMLocalizedString("Collapse"), action: #selector(foldNoteAction), target: self)
  90. item?.state = isFold ? .on : .off
  91. item?.representedObject = items
  92. menu.addItem(.separator())
  93. let hideNotes = self.hideNotes()
  94. if hideNotes == false && (items?.count ?? 0) == 1 {
  95. }
  96. if menu.numberOfItems > 0 {
  97. if self.outlineView(self.noteOutlineView, canDeleteItems: items as? [Any] ?? []) {
  98. item = menu.addItem(title: KMLocalizedString("Delete", comment: "Menu item title"), action: #selector(deleteNotes), target: self)
  99. item?.representedObject = items
  100. }
  101. _ = self._addDeleteAllAnnoItem(menu)
  102. }
  103. }
  104. private func _addExportPDFMenu(_ menu: NSMenu) -> NSMenu {
  105. var item = menu.addItem(title: KMLocalizedString("Export Annotations…"), action: nil, target: self)
  106. let subMenu = NSMenu()
  107. item?.submenu = subMenu
  108. item = subMenu.addItem(title: KMLocalizedString("PDF", comment: ""), action: #selector(exportAnnotationNotes), target: self)
  109. item?.tag = 0
  110. item = subMenu.addItem(title: KMLocalizedString("PDF Bundle"), action: #selector(exportAnnotationNotes), target: self)
  111. item?.tag = 1
  112. item = subMenu.addItem(title: KMLocalizedString("PDF Reader Pro Edition Notes"), action: #selector(exportAnnotationNotes), target: self)
  113. item?.tag = 2
  114. item = subMenu.addItem(title: KMLocalizedString("Notes as Text"), action: #selector(exportAnnotationNotes), target: self)
  115. item?.tag = 3
  116. item = subMenu.addItem(title: KMLocalizedString("Notes as RTF"), action: #selector(exportAnnotationNotes), target: self)
  117. item?.tag = 4
  118. item = subMenu.addItem(title: KMLocalizedString("Notes as RTFD"), action: #selector(exportAnnotationNotes), target: self)
  119. item?.tag = 5
  120. item = subMenu.addItem(title: KMLocalizedString("Notes as FDF"), action: #selector(exportAnnotationNotes), target: self)
  121. item?.tag = 6
  122. return menu
  123. }
  124. private func _addDeleteAllAnnoItem(_ menu: NSMenu) -> NSMenuItem? {
  125. return menu.addItem(title: KMLocalizedString("Remove All Annotations"), action: #selector(removeAllAnnotations), target: self)
  126. }
  127. private func _addDeleteAllReplyAnnoItem(_ menu: NSMenu) -> NSMenuItem? {
  128. return menu.addItem(title: KMLocalizedString("Delete All Reply"), action: #selector(removeAllReplyAnnotations), target: self)
  129. }
  130. @IBAction func note_expandAllComments(_ sender: AnyObject?) {
  131. guard let model = self.annoListModel else {
  132. return
  133. }
  134. for secM in self.annoListModel?.datas ?? [] {
  135. secM.isExpand = true
  136. }
  137. noteOutlineView.reloadData()
  138. }
  139. @IBAction func note_foldAllComments(_ sender: AnyObject?) {
  140. guard let model = self.annoListModel else {
  141. return
  142. }
  143. for secM in self.annoListModel?.datas ?? [] {
  144. secM.isExpand = false
  145. }
  146. self.noteOutlineView.reloadData()
  147. }
  148. @IBAction func noteShowNoteAction(_ sender: AnyObject?) {
  149. let item = sender as? NSMenuItem
  150. let tag = item?.tag ?? 0
  151. if (tag == 100) {
  152. } else if (tag == 101) {
  153. let isPage = !self.annoListIsShowPage()
  154. self.noteTypeDict[Self.Key.noteFilterPage] = !isPage
  155. } else if (tag == 102) {
  156. let isTime = !self.annoListIsShowTime()
  157. self.noteTypeDict[Self.Key.noteFilterTime] = !isTime
  158. } else if (tag == 103) {
  159. let isAuther = !self.annoListIsShowAnther()
  160. self.noteTypeDict[Self.Key.noteFilterAuther] = !isAuther
  161. }
  162. UserDefaults.standard.sync_setValue(self.noteTypeDict, forKey: Self.Key.noteTableColumn)
  163. // 更新数据
  164. var models: [KMBotaAnnotationModel] = []
  165. if self.noteSearchMode {
  166. for model in self.noteSearchArray {
  167. guard let data = model as? KMBotaAnnotationModel else {
  168. continue
  169. }
  170. models.append(data)
  171. }
  172. } else {
  173. let selModels = self.annoListModel?.datas ?? []
  174. for selModel in selModels {
  175. for item in selModel.items {
  176. if let data = item as? KMBotaAnnotationModel {
  177. models.append(data)
  178. }
  179. }
  180. }
  181. }
  182. for model in models {
  183. model.showPage = self.annoListIsShowPage()
  184. model.showTime = self.annoListIsShowTime()
  185. model.showAuthor = self.annoListIsShowAnther()
  186. }
  187. let selectRow = self.noteOutlineView.selectedRow
  188. self.noteOutlineView.reloadData()
  189. self.noteOutlineView.selectRowIndexes(IndexSet(integer: selectRow), byExtendingSelection: false)
  190. }
  191. @objc func importNotes(_ sender: NSMenuItem?) {
  192. let panel = NSOpenPanel()
  193. panel.allowedFileTypes = ["xfdf"]
  194. panel.allowsMultipleSelection = false
  195. panel.beginSheetModal(for: self.view.window!) { resp in
  196. if resp != .OK {
  197. return
  198. }
  199. if let result = self.pdfDocument()?.importAnnotation(fromXFDFPath: panel.url?.path), result {
  200. self.reloadAnnotation()
  201. self.listView?.setNeedsDisplayForVisiblePages()
  202. }
  203. }
  204. }
  205. @objc func exportNotes(_ sender: NSMenuItem?) {
  206. guard let cnt = self.listView?.notes?.count, cnt > 0 else {
  207. NSSound.beep()
  208. return
  209. }
  210. let fileName = "\(self.pdfDocument()?.documentURL.deletingPathExtension().lastPathComponent ?? "")" + "_xfdf"
  211. let panel = NSSavePanel()
  212. panel.directoryURL = self.pdfDocument()?.documentURL.deletingLastPathComponent()
  213. panel.allowedFileTypes = ["xfdf"]
  214. panel.nameFieldStringValue = fileName
  215. panel.beginSheetModal(for: self.view.window!) { resp in
  216. if resp != .OK {
  217. return
  218. }
  219. let filePath = panel.url?.path
  220. if let success = self.pdfDocument()?.exportAnnotation(toXFDFPath: filePath), success {
  221. NSWorkspace.shared.selectFile(filePath, inFileViewerRootedAtPath: "")
  222. } else {
  223. Task {
  224. _ = await KMAlertTool.runModel(message: KMLocalizedString("Export Failure!"), buttons: [KMLocalizedString("OK")])
  225. }
  226. }
  227. }
  228. }
  229. @objc func exportAnnotationNotes(_ sender: AnyObject?) {
  230. let doc = self.view.window?.windowController?.document as? NSDocument
  231. doc?.saveTo(sender)
  232. }
  233. // 展开
  234. @objc func unfoldNoteAction(_ sender: NSMenuItem) {
  235. if sender.state == .on {
  236. return
  237. }
  238. let row = self.noteOutlineView.clickedRow
  239. guard let model = self.fetchAnnoModel(for: row) else {
  240. return
  241. }
  242. model.foldType = .unfold
  243. model.isExpand = true
  244. let viewS = self.noteOutlineView.view(atColumn: 0, row: row, makeIfNecessary: true)
  245. (viewS as? KMNoteTableViewCell)?.isFold = false
  246. model.footerModel?.isExpand = true
  247. // self.noteOutlineView.reloadItem(model.footerModel)
  248. self.noteOutlineView.reloadData()
  249. }
  250. @objc func foldNoteAction(_ sender: NSMenuItem) {
  251. if sender.state == .on {
  252. return
  253. }
  254. let row = self.noteOutlineView.clickedRow
  255. guard let model = self.fetchAnnoModel(for: row) else {
  256. return
  257. }
  258. model.foldType = .fold
  259. model.isExpand = false
  260. let viewS = self.noteOutlineView.view(atColumn: 0, row: row, makeIfNecessary: true)
  261. (viewS as? KMNoteTableViewCell)?.isFold = true
  262. model.footerModel?.isExpand = false
  263. // self.noteOutlineView.reloadItem(model.footerModel)
  264. self.noteOutlineView.reloadData()
  265. }
  266. @objc func deleteNotes(_ sender: NSMenuItem) {
  267. self.outlineView(self.noteOutlineView, deleteItems: sender.representedObject as? [Any] ?? [])
  268. }
  269. @objc func removeAllAnnotations(_ sender: AnyObject?) {
  270. guard let doc = self.pdfDocument() else {
  271. return
  272. }
  273. Task {
  274. let response = await KMAlertTool.runModel(message: KMLocalizedString("This will permanently remove all annotations. Are you sure to continue?"), buttons: [KMLocalizedString("Yes"), KMLocalizedString("No")])
  275. if response == .alertFirstButtonReturn {
  276. self.dataUpdating = true
  277. for model in self.annoListModel?.datas ?? [] {
  278. for item in model.items {
  279. if let anno = item.anno {
  280. if let data = anno as? CPDFFreeTextAnnotation {
  281. self.listView?.commitEditAnnotationFreeText(data)
  282. }
  283. self.listView?.remove(anno)
  284. }
  285. }
  286. }
  287. self.annoListModel?.datas.removeAll()
  288. self.dataUpdating = false
  289. self.note_refrshUIIfNeed()
  290. }
  291. }
  292. }
  293. @objc func removeAllReplyAnnotations(_ sender: NSMenuItem?) {
  294. Task {
  295. let response = await KMAlertTool.runModel(message: KMLocalizedString("Are you sure to delete all comment replies?"), buttons: [KMLocalizedString("Yes"), KMLocalizedString("No")])
  296. if response == .alertFirstButtonReturn {
  297. self.dataUpdating = true
  298. var hasAnno = false
  299. for model in self.annoListModel?.datas ?? [] {
  300. for item in model.items {
  301. guard let annoM = item as? KMBotaAnnotationModel else {
  302. continue
  303. }
  304. for replyM in annoM.replyAnnos {
  305. replyM.replyAnno?.page.removeAnnotation(replyM.replyAnno)
  306. hasAnno = true
  307. }
  308. annoM.replyAnnos.removeAll()
  309. }
  310. }
  311. self.dataUpdating = false
  312. self.note_refrshUIIfNeed()
  313. if let data = self.view.window, hasAnno {
  314. KMTools.setDocumentEditedState(window: data)
  315. }
  316. }
  317. }
  318. }
  319. @objc func editNoteTextFromTable(_ sender: NSMenuItem) {
  320. guard let annotation = sender.representedObject as? CPDFAnnotation else {
  321. return
  322. }
  323. self.listView?.scrollAnnotationToVisible(annotation)
  324. }
  325. @objc func editThisAnnotation(_ sender: AnyObject?) {
  326. guard let annotation = (sender as? NSMenuItem)?.representedObject as? CPDFAnnotation else {
  327. NSSound.beep()
  328. return
  329. }
  330. self.listView?.edit(annotation)
  331. }
  332. @objc func editNoteFromTable(_ sender: AnyObject?) {
  333. guard let annotation = (sender as? NSMenuItem)?.representedObject as? CPDFAnnotation else {
  334. NSSound.beep()
  335. return
  336. }
  337. let model = fetchAnnoModel(for: annotation)
  338. let row = self.noteOutlineView.row(forItem: model)
  339. self.noteOutlineView.km_safe_selectRowIndexes(.init(integer: row), byExtendingSelection: false)
  340. let noteIndex = self.noteOutlineView.column(withIdentifier: .init("note"))
  341. if (noteIndex >= 0) {
  342. self.noteOutlineView.scrollColumnToVisible(noteIndex)
  343. //
  344. self.isRenameNoteOutline = true
  345. let viewS = self.noteOutlineView.view(atColumn: 0, row: row, makeIfNecessary: true) as? KMNoteTableViewCell
  346. viewS!.isFold = false
  347. let targrtTextField = viewS?.noteContentLabel
  348. self.editNoteTextField = targrtTextField
  349. self.editNote = annotation
  350. targrtTextField?.delegate = self
  351. targrtTextField?.isEditable = true
  352. targrtTextField?.becomeFirstResponder()
  353. }
  354. }
  355. @IBAction func noteSortAction(_ sender: AnyObject?) {
  356. // if (self.isAscendSort) {
  357. // self.isAscendSort = false
  358. // self.noteSortButton.image = NSImage(named: KMImageNameBtnSidebarRankPositive)
  359. // self.noteSortButton.toolTip = KMLocalizedString("descending sort", nil)
  360. // } else {
  361. // self.isAscendSort = true
  362. // self.noteSortButton.image = NSImage(named: KMImageNameBtnSidebarRankReverse)
  363. // self.noteSortButton.toolTip = KMLocalizedString("ascending sort", nil)
  364. // }
  365. KMDataManager.ud_set(self.isAscendSort, forKey: Self.Key.noteAscendSortKey)
  366. if self.noteSearchMode {
  367. reloadNoteForSearchMode()
  368. } else {
  369. reloadNoteForSortMode()
  370. }
  371. }
  372. @IBAction func noteSearchAction(_ sender: NSButton) {
  373. // self.noteTitleLabel.isHidden = true
  374. // self.noteSearchButton.isHidden = true
  375. // self.noteDoneButton.isHidden = false
  376. // self.noteFilterButton.isHidden = true
  377. // self.noteMoreButton.isHidden = true
  378. self.noteFilterButtonHoverView.isHidden = true
  379. }
  380. @IBAction func noteFilterAction(_ sender: AnyObject?) {
  381. let button = sender as? NSButton
  382. if self.noteFilterSelected == false {
  383. }
  384. let menu = NSMenu()
  385. self.noteFilterMenu = menu
  386. let filterViewController = KMNoteOutlineFilterViewController()
  387. filterViewController.listView = self.listView
  388. filterViewController.view.layer?.backgroundColor = .clear
  389. var states: [CPDFAnnotationState] = []
  390. for anno in self.allAnnotations {
  391. if let reviewS = self.noteReplyHanddler.fetchReviewState(anno) {
  392. if states.contains(reviewS) == false {
  393. states.append(reviewS)
  394. }
  395. } else {
  396. if states.contains(.none) == false {
  397. states.append(.none)
  398. }
  399. }
  400. if let markS = self.noteReplyHanddler.fetchAnnoState(anno) {
  401. if states.contains(markS) == false {
  402. states.append(markS)
  403. }
  404. } else {
  405. if states.contains(.unMarked) == false {
  406. states.append(.unMarked)
  407. }
  408. }
  409. }
  410. filterViewController.updateStates(states: states)
  411. filterViewController.setNotesArray(self.allAnnotations as NSArray)
  412. filterViewController.applyFilterCallback = { [weak self] typeArr, colorArr, authorArr, isEmpty in
  413. menu.cancelTracking()
  414. self?.headerView.sortButton.properties.state = .normal
  415. self?.headerView.sortButton.reloadData()
  416. self?.reloadAnnotation()
  417. }
  418. filterViewController.cancelCallback = {[weak self] isCancel in
  419. if (isCancel) {
  420. self?.headerView.sortButton.properties.state = .normal
  421. self?.headerView.sortButton.reloadData()
  422. menu.cancelTracking()
  423. }
  424. }
  425. let item = menu.addItem(withTitle: "", action: nil, keyEquivalent: "")
  426. item.target = self
  427. item.representedObject = filterViewController
  428. item.view = filterViewController.view
  429. menu.delegate = self
  430. menu.popUp(positioning: nil, at: NSMakePoint(-130, 30), in: self.headerView.sortButton)
  431. }
  432. func fetchNote(for index: Int) -> CPDFAnnotation? {
  433. return self.fetchAnnoModel(for: index)?.anno
  434. }
  435. func fetchAnnoModel(for index: Int) -> KMBotaAnnotationModel? {
  436. if self.noteSearchMode { // 搜索模式
  437. return self.noteSearchArray.safe_element(for: index) as? KMBotaAnnotationModel
  438. } else { // 常规模式(非搜索)
  439. // let model = self.annoListModel?.datas.safe_element(for: index)
  440. let model = self.noteOutlineView.item(atRow: index)
  441. if let data = model as? KMBotaAnnotationSectionModel {
  442. return nil
  443. }
  444. if let data = model as? KMBotaAnnotationModel {
  445. return data
  446. }
  447. if let data = model as? KMBotaAnnotationFooterModel {
  448. }
  449. if let data = model as? KMBotaAnnotationReplyModel {
  450. }
  451. return nil
  452. }
  453. }
  454. func fetchAnnoModel(for anno: CPDFAnnotation) -> KMBotaAnnotationModel? {
  455. if self.noteSearchMode { // 搜索模式
  456. for model in self.noteSearchArray {
  457. guard let data = model as? KMBotaAnnotationModel else {
  458. continue
  459. }
  460. if anno.isEqual(to: data.anno) {
  461. return data
  462. }
  463. }
  464. } else { // 常规模式(非搜索)
  465. for model in self.annoListModel?.datas ?? [] {
  466. for item in model.items {
  467. if let data = item as? KMBotaAnnotationModel {
  468. if anno.isEqual(to: data.anno) {
  469. return data
  470. }
  471. }
  472. }
  473. }
  474. }
  475. return nil
  476. }
  477. func showNoteEmptyView() {
  478. self.emptyView.isHidden = false
  479. }
  480. func hideNoteEmptyView() {
  481. self.emptyView.isHidden = true
  482. }
  483. }
  484. // MARK: - Note
  485. extension KMLeftSideViewController {
  486. public func refreshUIForAddAnnotation(annos: [CPDFAnnotation]?, page: CPDFPage?) {
  487. let need = self._annoList_needRefreshUI(annos: annos)
  488. if need == false {
  489. return
  490. }
  491. self.updateThumbnail(at: Int(page?.pageIndex() ?? 0))
  492. self.note_reloadDataIfNeed()
  493. }
  494. public func refreshUIForAnnoAttributeDidChange(_ anno: CPDFAnnotation?, attributes: [String : Any]?) {
  495. self.updateThumbnail(at: Int(anno?.page?.pageIndex() ?? 0))
  496. let need = self._annoList_needRefreshUI(annos: anno != nil ? [anno!] : [])
  497. if need == false {
  498. return
  499. }
  500. if let data = anno {
  501. self.note_reloadDataForAnnoIfNeed(anno: data)
  502. }
  503. }
  504. public func annoList_refreshUIForDeleteAnnotations(annos: [CPDFAnnotation]?, page: CPDFPage?) {
  505. self.updateThumbnail(at: Int(page?.pageIndex() ?? 0))
  506. let need = self._annoList_needRefreshUI(annos: annos)
  507. if need == false {
  508. return
  509. }
  510. for anno in annos ?? [] {
  511. if let model = self.fetchAnnoModel(for: anno) {
  512. self.noteSearchArray.removeObject(model)
  513. model.sectionModel?.items.removeObject(model)
  514. if let footer = model.footerModel {
  515. model.sectionModel?.items.removeObject(footer)
  516. }
  517. if let cnt = model.sectionModel?.items.count, cnt == 0 {
  518. self.annoListModel?.datas.removeObject(model.sectionModel!)
  519. }
  520. if self.allAnnotations.contains(anno) {
  521. self.allAnnotations.removeObject(anno)
  522. }
  523. }
  524. }
  525. if self.dataUpdating == false {
  526. self.note_refrshUIIfNeed()
  527. }
  528. }
  529. func note_refrshUIIfNeed() {
  530. Task { @MainActor in
  531. self.noteOutlineView?.reloadData()
  532. }
  533. }
  534. func note_reloadDataIfNeed() {
  535. self.reloadAnnotation()
  536. }
  537. func note_reloadDataForAnnoIfNeed(anno: CPDFAnnotation) {
  538. if anno is CPDFLineAnnotation ||
  539. anno is CPDFSquareAnnotation ||
  540. anno is CPDFCircleAnnotation ||
  541. anno is CPDFInkAnnotation {
  542. // 形状注释 + Ink 需要显示框住的内容【刷新】
  543. for item in self.annoListModel?.datas ?? [] {
  544. for itemM in item.items {
  545. if anno.isEqual(to: itemM.anno) {
  546. self.noteOutlineView.reloadItem(itemM)
  547. break
  548. }
  549. }
  550. }
  551. } else {
  552. for item in self.annoListModel?.datas ?? [] {
  553. for itemM in item.items {
  554. if anno.isEqual(to: itemM.anno) {
  555. self.noteOutlineView.reloadItem(itemM)
  556. break
  557. }
  558. }
  559. }
  560. }
  561. }
  562. func reloadAnnotation() {
  563. if self.listView != nil {
  564. let filterKey = self.pdfDocument()?.documentURL.path ?? ""
  565. let typeArr: [String] = KMBotaTools.noteFilterAnnoTypes(key: filterKey)
  566. let colorArr: [Any] = KMBotaTools.noteFilterColors(key: filterKey)
  567. let authorArr: [Any] = KMBotaTools.noteFilterAuthors(key: filterKey)
  568. var stateArr: [NSNumber] = KMBotaTools.noteFilterStates(key: filterKey)
  569. var hasMark = false
  570. var hasReview = false
  571. for data in stateArr {
  572. let state = data.intValue
  573. if state == CPDFAnnotationState.marked.rawValue || state == CPDFAnnotationState.unMarked.rawValue {
  574. hasMark = true
  575. }
  576. if state == CPDFAnnotationState.none.rawValue || state == CPDFAnnotationState.accepted.rawValue || state == CPDFAnnotationState.rejected.rawValue || state == CPDFAnnotationState.canceled.rawValue || state == CPDFAnnotationState.completed.rawValue {
  577. hasReview = true
  578. }
  579. }
  580. if hasMark == false {
  581. stateArr.append(NSNumber(value: CPDFAnnotationState.marked.rawValue))
  582. stateArr.append(NSNumber(value: CPDFAnnotationState.unMarked.rawValue))
  583. }
  584. if hasReview == false {
  585. stateArr.append(NSNumber(value: CPDFAnnotationState.none.rawValue))
  586. stateArr.append(NSNumber(value: CPDFAnnotationState.accepted.rawValue))
  587. stateArr.append(NSNumber(value: CPDFAnnotationState.rejected.rawValue))
  588. stateArr.append(NSNumber(value: CPDFAnnotationState.canceled.rawValue))
  589. stateArr.append(NSNumber(value: CPDFAnnotationState.completed.rawValue))
  590. }
  591. var annotationArray: [CPDFAnnotation] = []
  592. var allAnnotation: [CPDFAnnotation] = []
  593. for i in 0 ..< self.pageCount() {
  594. let page = self.pdfDocument()?.page(at: UInt(i))
  595. var annos: [CPDFAnnotation] = []
  596. // 处理过滤
  597. let types = ["Highlight","Underline","Strikeout","Squiggly","Freehand","FreeText","Note","Square","Circle","Line","Stamp","Arrow","Image","Sign"/*, "table"*/,"Polyline","Polygon"]
  598. if typeArr.count == 0 && colorArr.count == 0 && authorArr.count == 0 && stateArr.count == 0 {
  599. annos = KMOCToolClass.filterAnnotation(annotations: page?.annotations ?? [],types: types) as? [CPDFAnnotation] ?? []
  600. annotationArray += annos
  601. } else {
  602. var filterAnnos: [CPDFAnnotation] = page?.annotations ?? []
  603. let allAnnos = KMOCToolClass.filterAnnotation(annotations: filterAnnos,types: types) as? [CPDFAnnotation] ?? []
  604. annotationArray += allAnnos
  605. if typeArr.count > 0 {
  606. var theTypes = typeArr
  607. if typeArr.contains("Measure") { //包含测量的话
  608. theTypes.append("Polygon")
  609. theTypes.append("Polyline")
  610. theTypes.append(CPDFAnnotation.kType.measureArrow)
  611. }
  612. if theTypes.contains(CPDFAnnotation.kType.measureArrow) && theTypes.contains(CPDFAnnotation.kType.arrow) == false {
  613. theTypes.append(CPDFAnnotation.kType.arrow)
  614. }
  615. filterAnnos = (KMOCToolClass.filterAnnotation(annotations: filterAnnos, types: theTypes) as? [CPDFAnnotation]) ?? []
  616. }
  617. if (colorArr.count > 0) {
  618. filterAnnos = (KMOCToolClass.filterAnnotation(annotations: filterAnnos,colors: colorArr) as? [CPDFAnnotation]) ?? []
  619. }
  620. if (authorArr.count > 0) {
  621. filterAnnos = (KMOCToolClass.filterAnnotation(annotations: filterAnnos,authors: authorArr) as? [CPDFAnnotation]) ?? []
  622. }
  623. if typeArr.isEmpty == false {
  624. if typeArr.contains("Measure") {
  625. if typeArr.contains(CPDFAnnotation.kType.arrow) == false {
  626. for anno in filterAnnos {
  627. if let data = anno as? CPDFLineAnnotation, data.type == CPDFAnnotation.kType.arrow && data.isMeasure == false {
  628. filterAnnos.removeObject(anno)
  629. }
  630. }
  631. }
  632. } else {
  633. for anno in filterAnnos {
  634. if let data = anno as? CPDFLineAnnotation, data.isMeasure {
  635. filterAnnos.removeObject(anno)
  636. }
  637. }
  638. }
  639. }
  640. if stateArr.count > 0 {
  641. for anno in filterAnnos {
  642. let markState = self.noteReplyHanddler.fetchAnnoState(anno) ?? .unMarked
  643. let reviewState = self.noteReplyHanddler.fetchReviewState(anno) ?? .none
  644. if stateArr.contains(NSNumber(value: markState.rawValue)) == false || stateArr.contains(NSNumber(value: reviewState.rawValue)) == false {
  645. filterAnnos.removeObject(anno)
  646. }
  647. }
  648. }
  649. annos = filterAnnos
  650. }
  651. //添加签名注释
  652. for annotation in page?.annotations ?? [] {
  653. if annotation.isKind(of: CPDFSignatureAnnotation.self) {
  654. annos.append(annotation)
  655. annotationArray.append(annotation)
  656. }
  657. }
  658. for annotation in annos {
  659. if annotation.isKind(of: KMTableAnnotation.self) {
  660. annos.removeObject(annotation)
  661. if annotationArray.contains(annotation) {
  662. annotationArray.removeObject(annotation)
  663. }
  664. } else if annotation.annotationShouldDisplay() == false {
  665. annos.removeObject(annotation)
  666. if annotationArray.contains(annotation) {
  667. annotationArray.removeObject(annotation)
  668. }
  669. } else if annotation.isKind(of: CPDFLinkAnnotation.self) {
  670. annos.removeObject(annotation)
  671. if annotationArray.contains(annotation) {
  672. annotationArray.removeObject(annotation)
  673. }
  674. } else if annotation.isKind(of: CPDFRedactAnnotation.self) {
  675. annos.removeObject(annotation)
  676. if annotationArray.contains(annotation) {
  677. annotationArray.removeObject(annotation)
  678. }
  679. } else if annotation.isForm() {
  680. annos.removeObject(annotation)
  681. if annotationArray.contains(annotation) {
  682. annotationArray.removeObject(annotation)
  683. }
  684. }
  685. }
  686. // 添加刷选后的注释
  687. allAnnotation += annos
  688. }
  689. // 处理排序
  690. if self.noteSortType == .page {
  691. /// 排序(升序)
  692. if self.isAscendSort {
  693. allAnnotation.sort {
  694. let idx0 = $0.page?.pageIndex() ?? 0
  695. let idx1 = $1.page?.pageIndex() ?? 0
  696. return idx0 <= idx1
  697. }
  698. } else {
  699. allAnnotation.sort {
  700. let idx0 = $0.page?.pageIndex() ?? 0
  701. let idx1 = $1.page?.pageIndex() ?? 0
  702. return idx0 > idx1
  703. }
  704. }
  705. } else if self.noteSortType == .time {
  706. /// 排序(升序)
  707. if self.isAscendSort {
  708. allAnnotation.sort {
  709. if $0.modificationDate() == nil {
  710. return false
  711. }
  712. if $1.modificationDate() == nil {
  713. return false
  714. }
  715. return $0.modificationDate() <= $1.modificationDate()
  716. }
  717. } else {
  718. allAnnotation.sort {
  719. if $0.modificationDate() == nil {
  720. return false
  721. }
  722. if $1.modificationDate() == nil {
  723. return false
  724. }
  725. return $0.modificationDate() > $1.modificationDate()
  726. }
  727. }
  728. }
  729. // 数据模型化
  730. let model = KMAnnotationListModel()
  731. var datas: [KMBotaAnnotationModel] = []
  732. var prePageIdx: Int = NSNotFound
  733. var preDate: Date?
  734. var secM: KMBotaAnnotationSectionModel?
  735. for anno in allAnnotation {
  736. let item = KMBotaAnnotationModel()
  737. item.anno = anno
  738. item.showPage = self.annoListIsShowPage()
  739. item.showTime = self.annoListIsShowTime()
  740. item.showAuthor = self.annoListIsShowAnther()
  741. if self.noteSortType == .page {
  742. let pageIdx = Int(anno.pageIndex())
  743. if pageIdx != prePageIdx { // 不是同一个页面
  744. secM = KMBotaAnnotationSectionModel()
  745. model.datas.append(secM!)
  746. }
  747. secM?.items.append(item)
  748. prePageIdx = Int(anno.pageIndex())
  749. } else { // time
  750. let date = anno.modificationDate()
  751. if let same = date?.isSameDay(other: preDate), same == false { // 不是同一天
  752. secM = KMBotaAnnotationSectionModel()
  753. model.datas.append(secM!)
  754. }
  755. secM?.items.append(item)
  756. preDate = date
  757. }
  758. item.sectionModel = secM
  759. let replyAnnos = self.noteReplyHanddler.fetchReplyAnnotations(anno) ?? []
  760. for replyAnno in replyAnnos {
  761. let replyM = KMBotaAnnotationReplyModel()
  762. replyM.anno = anno
  763. replyM.replyAnno = replyAnno
  764. // secM?.items.append(replyM)
  765. replyM.annoModel = item
  766. item.replyAnnos.append(replyM)
  767. }
  768. let footerI = KMBotaAnnotationFooterModel()
  769. footerI.anno = anno
  770. secM?.items.append(footerI)
  771. item.footerModel = footerI
  772. footerI.annoModel = item
  773. }
  774. // model.datas = datas
  775. self.annoListModel = model
  776. // 转换对象,用于数据显示
  777. self.allAnnotations = annotationArray
  778. // self.noteFilterButton.isEnabled = self.allAnnotations.count >= 1
  779. }
  780. self.note_refrshUIIfNeed()
  781. }
  782. func reloadNoteForSearchMode() {
  783. if self.noteSearchMode == false {
  784. return
  785. }
  786. // 处理排序
  787. if self.noteSortType == .page {
  788. if self.isAscendSort { /// 排序(升序)
  789. self.noteSearchArray.sort {
  790. let idx0 = $0.anno?.page?.pageIndex() ?? 0
  791. let idx1 = $1.anno?.page?.pageIndex() ?? 0
  792. return idx0 <= idx1
  793. }
  794. } else {
  795. self.noteSearchArray.sort {
  796. let idx0 = $0.anno?.page?.pageIndex() ?? 0
  797. let idx1 = $1.anno?.page?.pageIndex() ?? 0
  798. return idx0 > idx1
  799. }
  800. }
  801. } else if self.noteSortType == .time {
  802. if self.isAscendSort { /// 排序(升序)
  803. self.noteSearchArray.sort {
  804. if $0.anno?.modificationDate() == nil {
  805. return false
  806. }
  807. if $1.anno?.modificationDate() == nil {
  808. return false
  809. }
  810. return $0.anno!.modificationDate() <= $1.anno!.modificationDate()
  811. }
  812. } else {
  813. self.noteSearchArray.sort {
  814. if $0.anno?.modificationDate() == nil {
  815. return false
  816. }
  817. if $1.anno?.modificationDate() == nil {
  818. return false
  819. }
  820. return $0.anno!.modificationDate() > $1.anno!.modificationDate()
  821. }
  822. }
  823. }
  824. self.note_refrshUIIfNeed()
  825. }
  826. func reloadNoteForSortMode() {
  827. // 处理排序
  828. var models: [KMBotaAnnotationModel] = []
  829. for selM in self.annoListModel?.datas ?? [] {
  830. for model in selM.items {
  831. guard let annoModel = model as? KMBotaAnnotationModel else {
  832. continue
  833. }
  834. models.append(annoModel)
  835. }
  836. }
  837. if self.noteSortType == .page {
  838. if self.isAscendSort { /// 排序(升序)
  839. models.sort {
  840. let idx0 = $0.anno?.page?.pageIndex() ?? 0
  841. let idx1 = $1.anno?.page?.pageIndex() ?? 0
  842. return idx0 <= idx1
  843. }
  844. } else {
  845. models.sort {
  846. let idx0 = $0.anno?.page?.pageIndex() ?? 0
  847. let idx1 = $1.anno?.page?.pageIndex() ?? 0
  848. return idx0 > idx1
  849. }
  850. }
  851. } else if self.noteSortType == .time {
  852. if self.isAscendSort { /// 排序(升序)
  853. models.sort {
  854. if $0.anno?.modificationDate() == nil {
  855. return false
  856. }
  857. if $1.anno?.modificationDate() == nil {
  858. return false
  859. }
  860. return $0.anno!.modificationDate() <= $1.anno!.modificationDate()
  861. }
  862. } else {
  863. models.sort {
  864. if $0.anno?.modificationDate() == nil {
  865. return false
  866. }
  867. if $1.anno?.modificationDate() == nil {
  868. return false
  869. }
  870. return $0.anno!.modificationDate() > $1.anno!.modificationDate()
  871. }
  872. }
  873. }
  874. // 数据模型\化
  875. let listModel = KMAnnotationListModel()
  876. var prePageIdx: Int = NSNotFound
  877. var preDate: Date?
  878. var secM: KMBotaAnnotationSectionModel?
  879. for itemM in models {
  880. guard let anno = itemM.anno else {
  881. continue
  882. }
  883. if self.noteSortType == .page {
  884. let pageIdx = Int(anno.pageIndex())
  885. if pageIdx != prePageIdx { // 不是同一个页面
  886. secM = KMBotaAnnotationSectionModel()
  887. listModel.datas.append(secM!)
  888. secM?.isExpand = itemM.sectionModel?.isExpand ?? true
  889. }
  890. secM?.items.append(itemM)
  891. prePageIdx = Int(anno.pageIndex())
  892. } else { // time
  893. let date = anno.modificationDate()
  894. if let same = date?.isSameDay(other: preDate), same == false { // 不是同一天
  895. secM = KMBotaAnnotationSectionModel()
  896. listModel.datas.append(secM!)
  897. secM?.isExpand = itemM.sectionModel?.isExpand ?? true
  898. }
  899. secM?.items.append(itemM)
  900. preDate = date
  901. }
  902. var tmpSelM = itemM.sectionModel
  903. itemM.sectionModel = secM
  904. let footerI = KMBotaAnnotationFooterModel()
  905. footerI.anno = anno
  906. secM?.items.append(footerI)
  907. footerI.isExpand = itemM.footerModel?.isExpand ?? false
  908. itemM.footerModel = footerI
  909. footerI.annoModel = itemM
  910. }
  911. self.annoListModel = listModel
  912. self.note_refrshUIIfNeed()
  913. }
  914. // 搜索 Action
  915. func updateNoteFilterPredicate(searchString: String) {
  916. var stringValue = searchString
  917. // 清空数据
  918. self.noteSearchArray.removeAll()
  919. if stringValue.isEmpty {
  920. for model in self.annoListModel?.datas ?? [] {
  921. for item in model.items {
  922. guard let _ = item.anno else {
  923. continue
  924. }
  925. guard let data = item as? KMBotaAnnotationModel else {
  926. continue
  927. }
  928. self.noteSearchArray.append(item)
  929. }
  930. }
  931. } else {
  932. // 忽略大小写
  933. let caseInsensite = self.caseInsensitiveNoteSearch
  934. if caseInsensite {
  935. stringValue = stringValue.lowercased()
  936. }
  937. for model in self.annoListModel?.datas ?? [] {
  938. for item in model.items {
  939. guard let note = item.anno else {
  940. continue
  941. }
  942. var noteString = ""
  943. if let anno = note as? CPDFMarkupAnnotation {
  944. noteString = anno.markupContent()
  945. } else {
  946. noteString = KMBOTAAnnotationTool.fetchContentLabelString(annotation: note)
  947. }
  948. if caseInsensite {
  949. noteString = noteString.lowercased()
  950. }
  951. guard let data = item as? KMBotaAnnotationModel else {
  952. continue
  953. }
  954. if noteString.contains(stringValue) {
  955. self.noteSearchArray.append(item)
  956. }
  957. }
  958. }
  959. }
  960. self.note_refrshUIIfNeed()
  961. }
  962. @objc func selectSelectedNote(_ sender: AnyObject?) {
  963. if self.hideNotes() == false {
  964. let selectedNotes = self.selectedNotes()
  965. if selectedNotes.count == 1 {
  966. let annotation = selectedNotes.last!
  967. self.listView?.go(to: annotation.bounds, on: annotation.page, animated: true)
  968. self.listView?.updateActiveAnnotations([annotation])
  969. self.listView?.setNeedsDisplayAnnotationViewForVisiblePages()
  970. if annotation is CPDFPolygonAnnotation || annotation is CPDFPolylineAnnotation {
  971. self.listView?.pdfListViewDelegate.pdfListViewAnnotationMeasureInfoChange?(self.listView, with: annotation)
  972. } else if let anno = annotation as? CPDFLineAnnotation {
  973. if anno.isMeasure {
  974. self.listView?.pdfListViewDelegate.pdfListViewAnnotationMeasureInfoChange?(self.listView, with: annotation)
  975. }
  976. }
  977. }
  978. }
  979. }
  980. func selectedNotes() -> [CPDFAnnotation] {
  981. var selectedNotes: [CPDFAnnotation] = []
  982. let rowIndexes = self.noteOutlineView.selectedRowIndexes
  983. for row in rowIndexes {
  984. let item = self.noteOutlineView.item(atRow: row)
  985. if item is KMBotaAnnotationModel {
  986. if let anno = (item as! KMBotaAnnotationModel).anno {
  987. if selectedNotes.contains(anno) == false {
  988. selectedNotes.append(anno)
  989. }
  990. }
  991. }
  992. }
  993. return selectedNotes
  994. }
  995. func selectedObjcNotes() -> [Any] {
  996. var selectedNotes: [Any] = []
  997. let rowIndexes = self.noteOutlineView.selectedRowIndexes
  998. for row in rowIndexes {
  999. let item = self.noteOutlineView.item(atRow: row)
  1000. selectedNotes.append(item)
  1001. }
  1002. return selectedNotes
  1003. }
  1004. func clearAnnotationFilterData() {
  1005. if let _key = self.pdfDocument()?.documentURL?.path {
  1006. let userDefaults = UserDefaults.standard
  1007. let typeData = try?NSKeyedArchiver.archivedData(withRootObject: [Any](), requiringSecureCoding: false)
  1008. userDefaults.set(typeData, forKey: NoteFilterVC.filterSelectTypeKey + _key)
  1009. let colorData = try?NSKeyedArchiver.archivedData(withRootObject: [Any](), requiringSecureCoding: false)
  1010. userDefaults.set(colorData, forKey: NoteFilterVC.filterSelectColorKey + _key)
  1011. let authorData = try?NSKeyedArchiver.archivedData(withRootObject: [Any](), requiringSecureCoding: false)
  1012. userDefaults.set(authorData, forKey: NoteFilterVC.filterSelectAuthorKey + _key)
  1013. userDefaults.synchronize()
  1014. }
  1015. }
  1016. private func _annoList_needRefreshUI(annos: [CPDFAnnotation]?) -> Bool {
  1017. guard let data = annos else {
  1018. return false
  1019. }
  1020. var need = false
  1021. for anno in data {
  1022. if anno.isForm() {
  1023. continue
  1024. }
  1025. if anno is CPDFLinkAnnotation {
  1026. continue
  1027. }
  1028. need = true
  1029. }
  1030. return need
  1031. }
  1032. }
  1033. extension KMLeftSideViewController: NSTextFieldDelegate {
  1034. func controlTextDidBeginEditing(_ obj: Notification) {
  1035. if self.noteOutlineView.isEqual(to: obj.object) {
  1036. }
  1037. }
  1038. func controlTextDidEndEditing(_ obj: Notification) {
  1039. if let data = self.editNoteTextField, data.isEqual(to: obj.object) {
  1040. if let data = self.editNote as? CPDFMarkupAnnotation {
  1041. data.setMarkupText(self.editNoteTextField?.stringValue ?? "")
  1042. } else if let data = self.editNote as? CPDFTextAnnotation {
  1043. data.contents = self.editNoteTextField?.stringValue ?? ""
  1044. }
  1045. }
  1046. }
  1047. }
  1048. extension KMLeftSideViewController: KMNoteOutlineViewDelegate {
  1049. func outlineView(_ anOutlineView: NSOutlineView, canResizeRowByItem item: AnyObject?) -> Bool? {
  1050. if anOutlineView.isEqual(to: self.noteOutlineView) {
  1051. return true
  1052. }
  1053. return false
  1054. }
  1055. func outlineView(_ anOutlineView: NSOutlineView, setHeight newHeight: CGFloat, ofRowByItem item: AnyObject?) {
  1056. }
  1057. func outlineView(_ anOutlineView: NSOutlineView, didChangeHiddenOfTableColumn aTableColumn: NSTableColumn) {
  1058. }
  1059. func outlineViewCommandKeyPressedDuringNavigation(_ anOutlineView: NSOutlineView) {
  1060. }
  1061. }
  1062. // MARK: - NSMenuDelegate
  1063. extension KMLeftSideViewController: NSMenuDelegate {
  1064. func menuNeedsUpdate(_ menu: NSMenu) {
  1065. menu.removeAllItems()
  1066. }
  1067. func menuDidClose(_ menu: NSMenu) {
  1068. if(menu == self.noteFilterMenu) {
  1069. headerView.sortButton.properties.state = .normal
  1070. headerView.sortButton.reloadData()
  1071. }
  1072. }
  1073. }