KMQucikToolsView.swift 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. //
  2. // KMQucikToolsView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2023/10/27.
  6. //
  7. import Cocoa
  8. typealias KMQucikToolsViewDidSelect = (_ view: KMQucikToolsView, _ item: KMQucikToolsModel) -> Void
  9. typealias KMQucikToolsViewPageChange = (_ view: KMQucikToolsView) -> Void
  10. class KMQucikToolsView: KMBaseXibView {
  11. @IBOutlet weak var collectionView: NSCollectionView!
  12. var addAction: KMQucikToolCollectionViewItemAddAction?
  13. var removeAction: KMQucikToolCollectionViewItemRemoveAction?
  14. var didSelect: KMQucikToolsViewDidSelect?
  15. var pageChange: KMQucikToolsViewPageChange?
  16. var data: [KMQucikToolsModel] = []
  17. var type: KMHomeQucikToolsShowType = .expand {
  18. didSet {
  19. self.collectionView.reloadData()
  20. }
  21. }
  22. override func draw(_ dirtyRect: NSRect) {
  23. super.draw(dirtyRect)
  24. // Drawing code here.
  25. }
  26. override func setup() {
  27. //设置代理
  28. let layout = NSCollectionViewFlowLayout()
  29. layout.scrollDirection = .horizontal
  30. layout.minimumLineSpacing = 10
  31. layout.minimumInteritemSpacing = 10
  32. // 设置布局到 NSCollectionView
  33. self.collectionView.collectionViewLayout = layout
  34. self.collectionView.delegate = self
  35. self.collectionView.dataSource = self
  36. self.collectionView.isSelectable = true
  37. self.collectionView.register(KMQucikToolCollectionViewItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMQucikToolCollectionViewItem"))
  38. NotificationCenter.default.addObserver(self, selector: #selector(scrollViewDidScroll(notification:)), name: NSScrollView.didLiveScrollNotification, object: collectionView.enclosingScrollView)
  39. }
  40. override func reloadData() {
  41. self.data.removeAll()
  42. for type in KMQucikToolsModel.showType() {
  43. let model = KMQucikToolsModel.init(type: type)
  44. self.data.append(model)
  45. }
  46. self.collectionView.reloadData()
  47. }
  48. }
  49. //Notification
  50. extension KMQucikToolsView {
  51. @objc func scrollViewDidScroll(notification: Notification) {
  52. guard let callBack = pageChange else { return }
  53. callBack(self)
  54. }
  55. }
  56. extension KMQucikToolsView: NSCollectionViewDelegate {
  57. //当item被选中
  58. public func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
  59. }
  60. }
  61. extension KMQucikToolsView: NSCollectionViewDataSource {
  62. public func numberOfSections(in collectionView: NSCollectionView) -> Int {
  63. return 1
  64. }
  65. public func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
  66. return self.data.count
  67. }
  68. //返回对应的item自定义个体
  69. public func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
  70. let view = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMQucikToolCollectionViewItem"), for: indexPath) as! KMQucikToolCollectionViewItem
  71. if self.data.count > indexPath.item {
  72. view.model = self.data[indexPath.item]
  73. }
  74. view.type = type
  75. view.addAction = { [weak self] view, item in
  76. KMBatchQuickActionManager.defaultManager.actionType = .add
  77. self?.addAction?(view, item)
  78. }
  79. view.removeAction = { [weak self] view, item in
  80. KMBatchQuickActionManager.defaultManager.actionType = .add
  81. self?.removeAction?(view, item)
  82. }
  83. view.downAction = { [unowned self] view, item in
  84. if view.model?.type == .Watermark ||
  85. view.model?.type == .Background ||
  86. view.model?.type == .BatesCode ||
  87. view.model?.type == .HeaderAndFooter ||
  88. view.model?.type == .Security {
  89. } else {
  90. self.didSelect?(self, item)
  91. }
  92. }
  93. return view
  94. }
  95. }
  96. extension KMQucikToolsView: NSCollectionViewDelegateFlowLayout {
  97. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
  98. if type == .collapse {
  99. return NSSize(width: 172, height: 48)
  100. } else if type == .expand {
  101. return NSSize(width: 260, height: 96)
  102. } else {
  103. return NSSize(width: 260, height: 96)
  104. }
  105. }
  106. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, insetForSectionAt section: Int) -> NSEdgeInsets {
  107. return NSEdgeInsets(top: 10, left: 0, bottom: 10, right: 10)
  108. }
  109. }
  110. //Collection Page
  111. extension KMQucikToolsView {
  112. func pageCount() -> Int {
  113. var width = 260.0
  114. var count = ceilf(Float(self.data.count) * 0.5)
  115. if type == .collapse {
  116. width = 170
  117. } else if type == .expand {
  118. width = 260
  119. } else {
  120. width = 260
  121. }
  122. width = Double(count) * (width + 10)
  123. return Int(ceilf(Float(width / self.collectionView.visibleRect.size.width)))
  124. }
  125. func currentPage() -> Int {
  126. return Int(ceilf(Float(self.collectionView.visibleRect.origin.x / self.collectionView.visibleRect.size.width))) + 1
  127. }
  128. func nextPage() {
  129. let currentPage = self.currentPage()
  130. let pageCount = self.pageCount()
  131. var point: NSPoint = .zero
  132. if currentPage < pageCount {
  133. point = CGPoint(x: Int(self.collectionView.visibleRect.size.width) * currentPage, y: 0)
  134. } else {
  135. point = CGPoint(x: 0, y: 0)
  136. }
  137. self.collectionView.scroll(point)
  138. guard let callBack = pageChange else { return }
  139. callBack(self)
  140. }
  141. func previousPage() {
  142. let currentPage = self.currentPage()
  143. let pageCount = self.pageCount()
  144. var point: NSPoint = .zero
  145. if currentPage > 1 {
  146. point = CGPoint(x: Int(self.collectionView.visibleRect.size.width) * (currentPage - 2), y: 0)
  147. } else {
  148. point = CGPoint(x: Int(self.collectionView.visibleRect.size.width) * (pageCount), y: 0)
  149. }
  150. self.collectionView.scroll(point)
  151. guard let callBack = pageChange else { return }
  152. callBack(self)
  153. }
  154. }