// // 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() // DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.1) { // 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 //注册cell self.collectionView.register(KMQucikToolCollectionViewItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMQucikToolCollectionViewItem")) NotificationCenter.default.addObserver(self, selector: #selector(scrollViewDidScroll(notification:)), name: NSScrollView.didLiveScrollNotification, object: collectionView.enclosingScrollView) // self.backgroundColor(NSColor.red) // if KMAdvertisementConfig.isDarkModel() { // self.collectionView.appearance = NSAppearance(named: .darkAqua) // } else { // self.collectionView.appearance = NSAppearance(named: .aqua) // } // self.collectionView.appearance = NSAppearance(named: .darkAqua) // self.backgroundColor(KMAppearance.Layout.l0Color()) } override func reloadData() { self.data.removeAll() for type in KMQucikToolsModel.showType() { let model = KMQucikToolsModel.init(type: type) // if model.type == .FileCompare { // var isNew = false // if let isNewValue = UserDefaults.standard.object(forKey: "QucikToolsModelFileCompareKey") as? Bool { // isNew = isNewValue // } // model.isNew = !isNew // } else if model.type == .AITools { var isNew = false if let isNewValue = UserDefaults.standard.object(forKey: "QucikToolsModelAIToolsKey") as? Bool { isNew = isNewValue } model.isNew = !isNew } self.data.append(model) } self.collectionView.reloadData() } } //Notification extension KMQucikToolsView { @objc func scrollViewDidScroll(notification: Notification) { // // 处理滚动事件 // if let scrollView = notification.object as? NSScrollView { // print("NSScrollView did scroll.") // // 获取滚动位置等信息 // let contentOffset = scrollView.contentView.bounds.origin // print("Content Offset: \(contentOffset)") // // } // guard let callBack = pageChange else { return } callBack(self) } } extension KMQucikToolsView: NSCollectionViewDelegate { //当item被选中 public func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set) { // let view = collectionView.item(at: indexPaths.first!) as! KMQucikToolCollectionViewItem // // let content = view.model // // guard let callBack = didSelect else { return } // callBack(self, content!) } // //当item取消选中 // public func collectionView(_ collectionView: NSCollectionView, didDeselectItemsAt indexPaths: Set) { // _ = collectionView.item(at: indexPaths.first!) as! KMQucikToolCollectionViewItem // } } 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))) // return Int(ceilf(Float(self.collectionView.frame.size.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) } }