KMQucikToolsView.swift 8.5 KB

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