KMQucikToolsView.swift 8.5 KB

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