KMQucikToolsView.swift 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. //
  2. // KMQucikToolsView.swift
  3. // EaseUS PDF Editor
  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. // var isNew = false
  59. // if let isNewValue = UserDefaults.standard.object(forKey: "QucikToolsModelFileCompareKey") as? Bool {
  60. // isNew = isNewValue
  61. // }
  62. // model.isNew = !isNew
  63. // } else
  64. self.data.append(model)
  65. }
  66. self.collectionView.reloadData()
  67. }
  68. }
  69. //Notification
  70. extension KMQucikToolsView {
  71. @objc func scrollViewDidScroll(notification: Notification) {
  72. // // 处理滚动事件
  73. // if let scrollView = notification.object as? NSScrollView {
  74. // print("NSScrollView did scroll.")
  75. // // 获取滚动位置等信息
  76. // let contentOffset = scrollView.contentView.bounds.origin
  77. // print("Content Offset: \(contentOffset)")
  78. //
  79. // }
  80. //
  81. guard let callBack = pageChange else { return }
  82. callBack(self)
  83. }
  84. }
  85. extension KMQucikToolsView: NSCollectionViewDelegate {
  86. //当item被选中
  87. public func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
  88. // let view = collectionView.item(at: indexPaths.first!) as! KMQucikToolCollectionViewItem
  89. //
  90. // let content = view.model
  91. //
  92. // guard let callBack = didSelect else { return }
  93. // callBack(self, content!)
  94. }
  95. // //当item取消选中
  96. // public func collectionView(_ collectionView: NSCollectionView, didDeselectItemsAt indexPaths: Set<IndexPath>) {
  97. // _ = collectionView.item(at: indexPaths.first!) as! KMQucikToolCollectionViewItem
  98. // }
  99. }
  100. extension KMQucikToolsView: NSCollectionViewDataSource {
  101. public func numberOfSections(in collectionView: NSCollectionView) -> Int {
  102. return 1
  103. }
  104. public func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
  105. return self.data.count
  106. }
  107. //返回对应的item自定义个体
  108. public func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
  109. let view = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMQucikToolCollectionViewItem"), for: indexPath) as! KMQucikToolCollectionViewItem
  110. if self.data.count > indexPath.item {
  111. view.model = self.data[indexPath.item]
  112. }
  113. view.type = type
  114. view.addAction = { [weak self] view, item in
  115. KMBatchQuickActionManager.defaultManager.actionType = .add
  116. self?.addAction?(view, item)
  117. }
  118. view.removeAction = { [weak self] view, item in
  119. KMBatchQuickActionManager.defaultManager.actionType = .add
  120. self?.removeAction?(view, item)
  121. }
  122. view.downAction = { [unowned self] view, item in
  123. if view.model?.type == .Watermark ||
  124. view.model?.type == .Background ||
  125. view.model?.type == .BatesCode ||
  126. view.model?.type == .HeaderAndFooter ||
  127. view.model?.type == .Security {
  128. } else {
  129. self.didSelect?(self, item)
  130. }
  131. }
  132. return view
  133. }
  134. }
  135. extension KMQucikToolsView: NSCollectionViewDelegateFlowLayout {
  136. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
  137. if type == .collapse {
  138. return NSSize(width: 172, height: 48)
  139. } else if type == .expand {
  140. return NSSize(width: 260, height: 96)
  141. } else {
  142. return NSSize(width: 260, height: 96)
  143. }
  144. }
  145. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, insetForSectionAt section: Int) -> NSEdgeInsets {
  146. return NSEdgeInsets(top: 10, left: 0, bottom: 10, right: 10)
  147. }
  148. }
  149. //Collection Page
  150. extension KMQucikToolsView {
  151. func pageCount() -> Int {
  152. var width = 260.0
  153. var count = ceilf(Float(self.data.count) * 0.5)
  154. if type == .collapse {
  155. width = 170
  156. } else if type == .expand {
  157. width = 260
  158. } else {
  159. width = 260
  160. }
  161. width = Double(count) * (width + 10)
  162. return Int(ceilf(Float(width / self.collectionView.visibleRect.size.width)))
  163. // return Int(ceilf(Float(self.collectionView.frame.size.width / self.collectionView.visibleRect.size.width)))
  164. }
  165. func currentPage() -> Int {
  166. return Int(ceilf(Float(self.collectionView.visibleRect.origin.x / self.collectionView.visibleRect.size.width))) + 1
  167. }
  168. func nextPage() {
  169. let currentPage = self.currentPage()
  170. let pageCount = self.pageCount()
  171. var point: NSPoint = .zero
  172. if currentPage < pageCount {
  173. point = CGPoint(x: Int(self.collectionView.visibleRect.size.width) * currentPage, y: 0)
  174. } else {
  175. point = CGPoint(x: 0, y: 0)
  176. }
  177. self.collectionView.scroll(point)
  178. guard let callBack = pageChange else { return }
  179. callBack(self)
  180. }
  181. func previousPage() {
  182. let currentPage = self.currentPage()
  183. let pageCount = self.pageCount()
  184. var point: NSPoint = .zero
  185. if currentPage > 1 {
  186. point = CGPoint(x: Int(self.collectionView.visibleRect.size.width) * (currentPage - 2), y: 0)
  187. } else {
  188. point = CGPoint(x: Int(self.collectionView.visibleRect.size.width) * (pageCount), y: 0)
  189. }
  190. self.collectionView.scroll(point)
  191. guard let callBack = pageChange else { return }
  192. callBack(self)
  193. }
  194. }