HomeLynxGuide.swift 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. //
  2. // HomeLynxGuide.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/7/22.
  6. //
  7. import Cocoa
  8. class HomeLynxGuide: NSView, NibLoadable {
  9. @IBOutlet var scrollView: NSScrollView!
  10. @IBOutlet var collectionView: NSCollectionView!
  11. @IBOutlet var previousBtn: NSButton!
  12. @IBOutlet var pageCountLabel: NSTextField!
  13. @IBOutlet var nextBtn: NSButton!
  14. var data: [HomeLynxGuideModel] = []
  15. private var timer: Timer?
  16. override func draw(_ dirtyRect: NSRect) {
  17. super.draw(dirtyRect)
  18. // Drawing code here.
  19. self.reloadPageInfo()
  20. }
  21. deinit {
  22. timer?.invalidate()
  23. }
  24. override func awakeFromNib() {
  25. super.awakeFromNib()
  26. self.wantsLayer = true
  27. self.layer?.backgroundColor = NSColor.clear.cgColor
  28. self.scrollView.backgroundColor = NSColor.clear
  29. self.scrollView.drawsBackground = false
  30. self.scrollView.scrollerStyle = .overlay
  31. //设置代理
  32. let layout = NSCollectionViewFlowLayout()
  33. layout.scrollDirection = .horizontal
  34. layout.minimumLineSpacing = 8
  35. layout.minimumInteritemSpacing = 8
  36. // 设置布局到 NSCollectionView
  37. self.collectionView.collectionViewLayout = layout
  38. self.collectionView.delegate = self
  39. self.collectionView.dataSource = self
  40. self.collectionView.isSelectable = true
  41. self.collectionView.register(HomeLynxGuideItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "HomeLynxGuideItemID"))
  42. NotificationCenter.default.addObserver(self, selector: #selector(scrollViewDidScroll(notification:)), name: NSScrollView.didLiveScrollNotification, object: collectionView.enclosingScrollView)
  43. self.reloadData()
  44. self.reloadPageInfo()
  45. self.setUpTimer()
  46. }
  47. func reloadData() {
  48. self.data.removeAll()
  49. let array: [LynxGuideType] = [.AdminConsole, .Onpremise, .SSO, .MultipleInstall, .CustomSolutions, .BusinessFeature]
  50. for type in array {
  51. let model = HomeLynxGuideModel.init(type: type)
  52. self.data.append(model)
  53. }
  54. self.collectionView.reloadData()
  55. }
  56. func reloadPageInfo() {
  57. self.pageCountLabel.stringValue = "\(self.currentPage()) / \(max(self.pageCount(), 1))"
  58. if KMAppearance.isDarkMode() {
  59. self.pageCountLabel.textColor = NSColor.white
  60. } else {
  61. self.pageCountLabel.textColor = NSColor(red: 14/255, green: 17/255, blue: 20/255, alpha: 1)
  62. }
  63. }
  64. //MARK: IBAction
  65. @IBAction func previousBtnClicked(_ sender: Any) {
  66. self.previousPage()
  67. self.reloadPageInfo()
  68. self.setUpTimer()
  69. }
  70. @IBAction func nextBtnClicked(_ sender: Any) {
  71. self.nextPage()
  72. self.reloadPageInfo()
  73. self.setUpTimer()
  74. }
  75. //MARK: Timer
  76. func setUpTimer() {
  77. self.timerInvalidate()
  78. timer = Timer.scheduledTimer(timeInterval: 8, target: self, selector: #selector(animate(_:)), userInfo: nil, repeats: true)
  79. }
  80. @objc private func animate(_ timer: Timer) {
  81. var showLynxGuide = true
  82. if VerificationManager.default().isVerificationCode {
  83. showLynxGuide = false
  84. }
  85. if showLynxGuide {
  86. self.nextBtnClicked(self.nextBtn as Any)
  87. } else {
  88. self.timerInvalidate()
  89. }
  90. }
  91. func timerInvalidate() {
  92. timer?.invalidate()
  93. }
  94. }
  95. extension HomeLynxGuide {
  96. func pageCount() -> Int {
  97. var width = 254.0
  98. let count = ceilf(Float(self.data.count) * 0.5)
  99. width = Double(count) * (width + 8)
  100. if self.collectionView.visibleRect.size.width == 0 {
  101. self.timerInvalidate()
  102. return 1
  103. }
  104. let indexFloat = Float(width / self.collectionView.visibleRect.size.width)
  105. if indexFloat.isInfinite || indexFloat.isNaN {
  106. self.timerInvalidate()
  107. return 1
  108. }
  109. let result = Int(ceilf(indexFloat))
  110. return result + 1
  111. }
  112. func currentPage() -> Int {
  113. if self.collectionView.visibleRect.size.width == 0 {
  114. self.timerInvalidate()
  115. return 1
  116. }
  117. let indexFloat = Float(self.collectionView.visibleRect.origin.x / self.collectionView.visibleRect.size.width)
  118. if indexFloat.isInfinite || indexFloat.isNaN {
  119. self.timerInvalidate()
  120. return 1
  121. }
  122. let index = Int(ceilf(indexFloat)) + 1
  123. return min(self.pageCount(), index)
  124. }
  125. func nextPage() {
  126. let currentPage = self.currentPage()
  127. let pageCount = self.pageCount()
  128. var point = CGPointZero
  129. if currentPage < pageCount {
  130. point = CGPoint(x: Int(self.collectionView.visibleRect.size.width) * currentPage, y: 0)
  131. } else {
  132. point = CGPoint(x: 0, y: 0)
  133. }
  134. self.collectionView.scroll(point)
  135. }
  136. func previousPage() {
  137. let currentPage = self.currentPage()
  138. let pageCount = self.pageCount()
  139. var point = CGPointZero
  140. if currentPage > 1 {
  141. point = CGPoint(x: Int(self.collectionView.visibleRect.size.width) * (currentPage - 2), y: 0)
  142. } else {
  143. point = CGPoint(x: Int(self.collectionView.visibleRect.size.width) * (pageCount), y: 0)
  144. }
  145. self.collectionView.scroll(point)
  146. }
  147. @objc func scrollViewDidScroll(notification: Notification) {
  148. self.reloadPageInfo()
  149. }
  150. }
  151. extension HomeLynxGuide: NSCollectionViewDelegate {
  152. //当item被选中
  153. public func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
  154. }
  155. }
  156. extension HomeLynxGuide: NSCollectionViewDataSource {
  157. public func numberOfSections(in collectionView: NSCollectionView) -> Int {
  158. return 1
  159. }
  160. public func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
  161. return self.data.count
  162. }
  163. //返回对应的item自定义个体
  164. public func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
  165. let view = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "HomeLynxGuideItemID"), for: indexPath) as! HomeLynxGuideItem
  166. if self.data.count > indexPath.item {
  167. view.model = self.data[indexPath.item]
  168. }
  169. // view.addAction = { [unowned self] view, item in
  170. // KMBatchQuickActionManager.defaultManager.actionType = .add
  171. // self.addAction?(view, item)
  172. // }
  173. //
  174. // view.removeAction = { [unowned self] view, item in
  175. // KMBatchQuickActionManager.defaultManager.actionType = .add
  176. // self.removeAction?(view, item)
  177. // }
  178. //
  179. // view.downAction = { [unowned self] view, item in
  180. // if view.model?.type == .Watermark ||
  181. // view.model?.type == .Background ||
  182. // view.model?.type == .BatesCode ||
  183. // view.model?.type == .HeaderAndFooter ||
  184. // view.model?.type == .Security {
  185. //
  186. // } else {
  187. // self.didSelect?(self, item)
  188. // }
  189. // }
  190. view.view.wantsLayer = true
  191. view.view.layer?.backgroundColor = NSColor.yellow.cgColor
  192. return view
  193. }
  194. }
  195. extension HomeLynxGuide: NSCollectionViewDelegateFlowLayout {
  196. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
  197. return NSSize(width: 254, height: 194)
  198. }
  199. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, insetForSectionAt section: Int) -> NSEdgeInsets {
  200. return NSEdgeInsets(top: 0.1, left: 0.1, bottom: 0.1, right: 0.1)
  201. }
  202. }