// // KMQucikToolsView.swift // PDF Reader Pro // // Created by lizhe on 2023/10/27. // import Cocoa typealias KMQucikToolsViewDidSelect = (_ view: KMQucikToolsView, _ item: KMQucikToolsModel) -> Void typealias KMQucikToolsViewPageChange = (_ view: KMQucikToolsView) -> Void class KMQucikToolsView: KMBaseXibView { @IBOutlet weak var collectionView: NSCollectionView! var addAction: KMQucikToolCollectionViewItemAddAction? var removeAction: KMQucikToolCollectionViewItemRemoveAction? var didSelect: KMQucikToolsViewDidSelect? var pageChange: KMQucikToolsViewPageChange? var data: [KMQucikToolsModel] = [] var type: KMHomeQucikToolsShowType = .expand { didSet { self.collectionView.reloadData() } } override func draw(_ dirtyRect: NSRect) { super.draw(dirtyRect) // Drawing code here. } override func setup() { //设置代理 let layout = NSCollectionViewFlowLayout() layout.scrollDirection = .horizontal layout.minimumLineSpacing = 10 layout.minimumInteritemSpacing = 10 // 设置布局到 NSCollectionView self.collectionView.collectionViewLayout = layout self.collectionView.delegate = self self.collectionView.dataSource = self self.collectionView.isSelectable = true self.collectionView.register(KMQucikToolCollectionViewItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMQucikToolCollectionViewItem")) NotificationCenter.default.addObserver(self, selector: #selector(scrollViewDidScroll(notification:)), name: NSScrollView.didLiveScrollNotification, object: collectionView.enclosingScrollView) } override func reloadData() { self.data.removeAll() for type in KMQucikToolsModel.showType() { let model = KMQucikToolsModel.init(type: type) self.data.append(model) } self.collectionView.reloadData() } } //Notification extension KMQucikToolsView { @objc func scrollViewDidScroll(notification: Notification) { guard let callBack = pageChange else { return } callBack(self) } } extension KMQucikToolsView: NSCollectionViewDelegate { //当item被选中 public func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set) { } } extension KMQucikToolsView: NSCollectionViewDataSource { public func numberOfSections(in collectionView: NSCollectionView) -> Int { return 1 } public func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int { return self.data.count } //返回对应的item自定义个体 public func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem { let view = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMQucikToolCollectionViewItem"), for: indexPath) as! KMQucikToolCollectionViewItem if self.data.count > indexPath.item { view.model = self.data[indexPath.item] } view.type = type view.addAction = { [weak self] view, item in KMBatchQuickActionManager.defaultManager.actionType = .add self?.addAction?(view, item) } view.removeAction = { [weak self] view, item in KMBatchQuickActionManager.defaultManager.actionType = .add self?.removeAction?(view, item) } view.downAction = { [unowned self] view, item in if view.model?.type == .Watermark || view.model?.type == .Background || view.model?.type == .BatesCode || view.model?.type == .HeaderAndFooter || view.model?.type == .Security { } else { self.didSelect?(self, item) } } return view } } extension KMQucikToolsView: NSCollectionViewDelegateFlowLayout { public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize { if type == .collapse { return NSSize(width: 172, height: 48) } else if type == .expand { return NSSize(width: 260, height: 96) } else { return NSSize(width: 260, height: 96) } } public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, insetForSectionAt section: Int) -> NSEdgeInsets { return NSEdgeInsets(top: 10, left: 0, bottom: 10, right: 10) } } //Collection Page extension KMQucikToolsView { func pageCount() -> Int { var width = 260.0 var count = ceilf(Float(self.data.count) * 0.5) if type == .collapse { width = 170 } else if type == .expand { width = 260 } else { width = 260 } width = Double(count) * (width + 10) return Int(ceilf(Float(width / self.collectionView.visibleRect.size.width))) } func currentPage() -> Int { return Int(ceilf(Float(self.collectionView.visibleRect.origin.x / self.collectionView.visibleRect.size.width))) + 1 } func nextPage() { let currentPage = self.currentPage() let pageCount = self.pageCount() var point: NSPoint = .zero if currentPage < pageCount { point = CGPoint(x: Int(self.collectionView.visibleRect.size.width) * currentPage, y: 0) } else { point = CGPoint(x: 0, y: 0) } self.collectionView.scroll(point) guard let callBack = pageChange else { return } callBack(self) } func previousPage() { let currentPage = self.currentPage() let pageCount = self.pageCount() var point: NSPoint = .zero if currentPage > 1 { point = CGPoint(x: Int(self.collectionView.visibleRect.size.width) * (currentPage - 2), y: 0) } else { point = CGPoint(x: Int(self.collectionView.visibleRect.size.width) * (pageCount), y: 0) } self.collectionView.scroll(point) guard let callBack = pageChange else { return } callBack(self) } }