SidebarDemoVC.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //
  2. // SidebarDemoVC.swift
  3. // KMComponentLibraryDemo
  4. //
  5. // Created by wanjun on 2024/8/2.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class SidebarDemoVC: NSViewController {
  10. @IBOutlet weak var searchItem: ComponentSideBarItem!
  11. @IBOutlet weak var thumbnailItem: ComponentSideBarItem!
  12. @IBOutlet weak var outlineItem: ComponentSideBarItem!
  13. @IBOutlet weak var bookmarkItem: ComponentSideBarItem!
  14. @IBOutlet weak var annotationItem: ComponentSideBarItem!
  15. @IBOutlet weak var pageTurnerView: ComponentSidebarPageTurner!
  16. @IBOutlet weak var firstPageBtn: NSButton!
  17. @IBOutlet weak var lastPageBtn: NSButton!
  18. @IBOutlet weak var previousBtn: NSButton!
  19. @IBOutlet weak var nextBtn: NSButton!
  20. let searchProperty: ComponentSideBarItemProperty = ComponentSideBarItemProperty(state: .normal, icon: ComponentLibrary.shared.image(forResource: "Sidebar_search"))
  21. let thumbnailProperty: ComponentSideBarItemProperty = ComponentSideBarItemProperty(state: .normal, icon: ComponentLibrary.shared.image(forResource: "Sidebar_thumbnails"))
  22. let outlineProperty: ComponentSideBarItemProperty = ComponentSideBarItemProperty(state: .normal, icon: ComponentLibrary.shared.image(forResource: "Sidebar_outline"))
  23. let bookmarkProperty: ComponentSideBarItemProperty = ComponentSideBarItemProperty(state: .normal, icon: ComponentLibrary.shared.image(forResource: "Sidebar_bookmark"))
  24. let annotationProperty: ComponentSideBarItemProperty = ComponentSideBarItemProperty(state: .normal, icon: ComponentLibrary.shared.image(forResource: "Sidebar_annotation"))
  25. var sideBarType: KMPDFSidebarType = .search
  26. override func viewDidLoad() {
  27. super.viewDidLoad()
  28. // Do view setup here.
  29. self.searchItem.properties = searchProperty
  30. self.thumbnailItem.properties = thumbnailProperty
  31. self.outlineItem.properties = outlineProperty
  32. self.bookmarkItem.properties = bookmarkProperty
  33. self.annotationItem.properties = annotationProperty
  34. self.searchItem.setTarget(self, action: #selector(sidebarItemClicked(_ :)))
  35. self.thumbnailItem.setTarget(self, action: #selector(sidebarItemClicked(_ :)))
  36. self.outlineItem.setTarget(self, action: #selector(sidebarItemClicked(_ :)))
  37. self.bookmarkItem.setTarget(self, action: #selector(sidebarItemClicked(_ :)))
  38. self.annotationItem.setTarget(self, action: #selector(sidebarItemClicked(_ :)))
  39. self.reloadData()
  40. self.reloadPageTurnerData()
  41. }
  42. func reloadData() {
  43. searchProperty.state = .normal
  44. thumbnailProperty.state = .normal
  45. outlineProperty.state = .normal
  46. bookmarkProperty.state = .normal
  47. annotationProperty.state = .normal
  48. if self.sideBarType == .search {
  49. searchProperty.state = .pressed
  50. } else if self.sideBarType == .thumbnail {
  51. thumbnailProperty.state = .pressed
  52. } else if self.sideBarType == .outline {
  53. outlineProperty.state = .pressed
  54. } else if self.sideBarType == .bookmark {
  55. bookmarkProperty.state = .pressed
  56. } else if self.sideBarType == .annotation {
  57. annotationProperty.state = .pressed
  58. }
  59. searchItem.reloadData()
  60. thumbnailItem.reloadData()
  61. outlineItem.reloadData()
  62. bookmarkItem.reloadData()
  63. annotationItem.reloadData()
  64. }
  65. func reloadPageTurnerData() {
  66. let turnerProperty: ComponentSidebarPageTurnerProperty = ComponentSidebarPageTurnerProperty(currentIndex: 1, totalCount: 200)
  67. turnerProperty.firstPage_show = self.firstPageBtn.state == .on
  68. turnerProperty.lastPage_show = self.lastPageBtn.state == .on
  69. turnerProperty.previous_show = self.previousBtn.state == .on
  70. turnerProperty.next_show = self.nextBtn.state == .on
  71. self.pageTurnerView.properties = turnerProperty
  72. }
  73. @objc func sidebarItemClicked(_ sender: ComponentSideBarItem) {
  74. if sender == self.searchItem {
  75. self.sideBarType = .search
  76. } else if sender == self.thumbnailItem {
  77. self.sideBarType = .thumbnail
  78. } else if sender == self.outlineItem {
  79. self.sideBarType = .outline
  80. } else if sender == self.bookmarkItem {
  81. self.sideBarType = .bookmark
  82. } else if sender == self.annotationItem {
  83. self.sideBarType = .annotation
  84. }
  85. self.reloadData()
  86. }
  87. @IBAction func clickAction(_ sender: Any) {
  88. self.reloadData()
  89. self.reloadPageTurnerData()
  90. }
  91. override func mouseDown(with event: NSEvent) {
  92. self.view.window?.makeFirstResponder(nil)
  93. }
  94. }