KMQucikToolsView.swift 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. // DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) {
  21. // self.collectionView.reloadData()
  22. // }
  23. }
  24. }
  25. override func draw(_ dirtyRect: NSRect) {
  26. super.draw(dirtyRect)
  27. // Drawing code here.
  28. }
  29. override func setup() {
  30. //设置代理
  31. let layout = NSCollectionViewFlowLayout()
  32. layout.scrollDirection = .horizontal
  33. layout.minimumLineSpacing = 10
  34. layout.minimumInteritemSpacing = 10
  35. // 设置布局到 NSCollectionView
  36. self.collectionView.collectionViewLayout = layout
  37. self.collectionView.delegate = self
  38. self.collectionView.dataSource = self
  39. //是否可选中
  40. self.collectionView.isSelectable = true
  41. //注册cell
  42. self.collectionView.register(KMQucikToolCollectionViewItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMQucikToolCollectionViewItem"))
  43. NotificationCenter.default.addObserver(self, selector: #selector(scrollViewDidScroll(notification:)), name: NSScrollView.didLiveScrollNotification, object: collectionView.enclosingScrollView)
  44. // self.backgroundColor(NSColor.red)
  45. // if KMAdvertisementConfig.isDarkModel() {
  46. // self.collectionView.appearance = NSAppearance(named: .darkAqua)
  47. // } else {
  48. // self.collectionView.appearance = NSAppearance(named: .aqua)
  49. // }
  50. // self.collectionView.appearance = NSAppearance(named: .darkAqua)
  51. // self.backgroundColor(KMAppearance.Layout.l0Color())
  52. }
  53. override func reloadData() {
  54. self.data.removeAll()
  55. for type in KMQucikToolsModel.showType() {
  56. let model = KMQucikToolsModel.init(type: type)
  57. if model.type == .FileCompare {
  58. model.isNew = true
  59. }
  60. self.data.append(model)
  61. }
  62. self.collectionView.reloadData()
  63. }
  64. }
  65. //Notification
  66. extension KMQucikToolsView {
  67. @objc func scrollViewDidScroll(notification: Notification) {
  68. // // 处理滚动事件
  69. // if let scrollView = notification.object as? NSScrollView {
  70. // print("NSScrollView did scroll.")
  71. // // 获取滚动位置等信息
  72. // let contentOffset = scrollView.contentView.bounds.origin
  73. // print("Content Offset: \(contentOffset)")
  74. //
  75. // }
  76. //
  77. guard let callBack = pageChange else { return }
  78. callBack(self)
  79. }
  80. }
  81. extension KMQucikToolsView: NSCollectionViewDelegate {
  82. //当item被选中
  83. public func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
  84. // let view = collectionView.item(at: indexPaths.first!) as! KMQucikToolCollectionViewItem
  85. //
  86. // let content = view.model
  87. //
  88. // guard let callBack = didSelect else { return }
  89. // callBack(self, content!)
  90. }
  91. // //当item取消选中
  92. // public func collectionView(_ collectionView: NSCollectionView, didDeselectItemsAt indexPaths: Set<IndexPath>) {
  93. // _ = collectionView.item(at: indexPaths.first!) as! KMQucikToolCollectionViewItem
  94. // }
  95. }
  96. extension KMQucikToolsView: NSCollectionViewDataSource {
  97. public func numberOfSections(in collectionView: NSCollectionView) -> Int {
  98. return 1
  99. }
  100. public func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
  101. return self.data.count
  102. }
  103. //返回对应的item自定义个体
  104. public func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
  105. let view = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMQucikToolCollectionViewItem"), for: indexPath) as! KMQucikToolCollectionViewItem
  106. if self.data.count > indexPath.item {
  107. view.model = self.data[indexPath.item]
  108. }
  109. view.type = type
  110. view.addAction = { [unowned self] view, item in
  111. KMBatchQuickActionManager.defaultManager.actionType = .add
  112. self.addAction?(view, item)
  113. }
  114. view.removeAction = { [unowned self] view, item in
  115. KMBatchQuickActionManager.defaultManager.actionType = .add
  116. self.removeAction?(view, item)
  117. }
  118. view.downAction = { [unowned self] view, item in
  119. if view.model?.type == .Watermark ||
  120. view.model?.type == .Background ||
  121. view.model?.type == .BatesCode ||
  122. view.model?.type == .HeaderAndFooter ||
  123. view.model?.type == .Security {
  124. } else {
  125. self.didSelect?(self, item)
  126. }
  127. }
  128. return view
  129. }
  130. }
  131. extension KMQucikToolsView: NSCollectionViewDelegateFlowLayout {
  132. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
  133. if type == .collapse {
  134. return NSSize(width: 172, height: 48)
  135. } else if type == .expand {
  136. return NSSize(width: 260, height: 96)
  137. } else {
  138. return NSSize(width: 260, height: 96)
  139. }
  140. }
  141. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, insetForSectionAt section: Int) -> NSEdgeInsets {
  142. return NSEdgeInsets(top: 10, left: 0, bottom: 10, right: 10)
  143. }
  144. }
  145. //Collection Page
  146. extension KMQucikToolsView {
  147. func pageCount() -> Int {
  148. print(self.collectionView.frame.size)
  149. print(self.collectionView.visibleRect.size)
  150. var width = 260.0
  151. var count = ceilf(Float(self.data.count) * 0.5)
  152. if type == .collapse {
  153. width = 170
  154. } else if type == .expand {
  155. width = 260
  156. } else {
  157. width = 260
  158. }
  159. width = Double(count) * (width + 10)
  160. return Int(ceilf(Float(width / self.collectionView.visibleRect.size.width)))
  161. // return Int(ceilf(Float(self.collectionView.frame.size.width / self.collectionView.visibleRect.size.width)))
  162. }
  163. func currentPage() -> Int {
  164. return Int(ceilf(Float(self.collectionView.visibleRect.origin.x / self.collectionView.visibleRect.size.width))) + 1
  165. }
  166. func nextPage() {
  167. let currentPage = self.currentPage()
  168. let pageCount = self.pageCount()
  169. if currentPage < pageCount {
  170. self.collectionView.scroll(CGPoint(x: Int(self.collectionView.visibleRect.size.width) * currentPage, y: 0))
  171. guard let callBack = pageChange else { return }
  172. callBack(self)
  173. }
  174. }
  175. func previousPage() {
  176. let currentPage = self.currentPage()
  177. let pageCount = self.pageCount()
  178. if currentPage > 1 {
  179. self.collectionView.scroll(CGPoint(x: Int(self.collectionView.visibleRect.size.width) * (currentPage - 2), y: 0))
  180. guard let callBack = pageChange else { return }
  181. callBack(self)
  182. }
  183. }
  184. }