// // HomeLynxGuide.swift // PDF Reader Pro // // Created by Niehaoyu on 2024/7/22. // import Cocoa class HomeLynxGuide: NSView, NibLoadable { @IBOutlet var scrollView: NSScrollView! @IBOutlet var collectionView: NSCollectionView! @IBOutlet var previousBtn: NSButton! @IBOutlet var pageCountLabel: NSTextField! @IBOutlet var nextBtn: NSButton! var data: [HomeLynxGuideModel] = [] private var timer: Timer? override func draw(_ dirtyRect: NSRect) { super.draw(dirtyRect) // Drawing code here. self.reloadPageInfo() } deinit { timer?.invalidate() } override func awakeFromNib() { super.awakeFromNib() self.wantsLayer = true self.layer?.backgroundColor = NSColor.clear.cgColor self.scrollView.backgroundColor = NSColor.clear self.scrollView.drawsBackground = false self.scrollView.scrollerStyle = .overlay //设置代理 let layout = NSCollectionViewFlowLayout() layout.scrollDirection = .horizontal layout.minimumLineSpacing = 8 layout.minimumInteritemSpacing = 8 // 设置布局到 NSCollectionView self.collectionView.collectionViewLayout = layout self.collectionView.delegate = self self.collectionView.dataSource = self self.collectionView.isSelectable = true self.collectionView.register(HomeLynxGuideItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "HomeLynxGuideItemID")) NotificationCenter.default.addObserver(self, selector: #selector(scrollViewDidScroll(notification:)), name: NSScrollView.didLiveScrollNotification, object: collectionView.enclosingScrollView) self.reloadData() self.reloadPageInfo() self.setUpTimer() } func reloadData() { self.data.removeAll() let array: [LynxGuideType] = [.AdminConsole, .Onpremise, .SSO, .MultipleInstall, .CustomSolutions, .BusinessFeature] for type in array { let model = HomeLynxGuideModel.init(type: type) self.data.append(model) } self.collectionView.reloadData() } func reloadPageInfo() { self.pageCountLabel.stringValue = "\(self.currentPage()) / \(max(self.pageCount(), 1))" if KMAppearance.isDarkMode() { self.pageCountLabel.textColor = NSColor.white } else { self.pageCountLabel.textColor = NSColor(red: 14/255, green: 17/255, blue: 20/255, alpha: 1) } } //MARK: IBAction @IBAction func previousBtnClicked(_ sender: Any) { self.previousPage() self.reloadPageInfo() self.setUpTimer() } @IBAction func nextBtnClicked(_ sender: Any) { self.nextPage() self.reloadPageInfo() self.setUpTimer() } //MARK: Timer func setUpTimer() { self.timerInvalidate() timer = Timer.scheduledTimer(timeInterval: 8, target: self, selector: #selector(animate(_:)), userInfo: nil, repeats: true) } @objc private func animate(_ timer: Timer) { var showLynxGuide = true if VerificationManager.default().isVerificationCode { showLynxGuide = false } if showLynxGuide { self.nextBtnClicked(self.nextBtn as Any) } else { self.timerInvalidate() } } func timerInvalidate() { timer?.invalidate() } } extension HomeLynxGuide { func pageCount() -> Int { var width = 254.0 let count = ceilf(Float(self.data.count) * 0.5) width = Double(count) * (width + 8) if self.collectionView.visibleRect.size.width == 0 { self.timerInvalidate() return 1 } let indexFloat = Float(width / self.collectionView.visibleRect.size.width) if indexFloat.isInfinite || indexFloat.isNaN { self.timerInvalidate() return 1 } let result = Int(ceilf(indexFloat)) return result + 1 } func currentPage() -> Int { if self.collectionView.visibleRect.size.width == 0 { self.timerInvalidate() return 1 } let indexFloat = Float(self.collectionView.visibleRect.origin.x / self.collectionView.visibleRect.size.width) if indexFloat.isInfinite || indexFloat.isNaN { self.timerInvalidate() return 1 } let index = Int(ceilf(indexFloat)) + 1 return min(self.pageCount(), index) } func nextPage() { let currentPage = self.currentPage() let pageCount = self.pageCount() var point = CGPointZero 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) } func previousPage() { let currentPage = self.currentPage() let pageCount = self.pageCount() var point = CGPointZero 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) } @objc func scrollViewDidScroll(notification: Notification) { self.reloadPageInfo() } } extension HomeLynxGuide: NSCollectionViewDelegate { //当item被选中 public func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set) { } } extension HomeLynxGuide: 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: "HomeLynxGuideItemID"), for: indexPath) as! HomeLynxGuideItem if self.data.count > indexPath.item { view.model = self.data[indexPath.item] } // view.addAction = { [unowned self] view, item in // KMBatchQuickActionManager.defaultManager.actionType = .add // self.addAction?(view, item) // } // // view.removeAction = { [unowned 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) // } // } view.view.wantsLayer = true view.view.layer?.backgroundColor = NSColor.yellow.cgColor return view } } extension HomeLynxGuide: NSCollectionViewDelegateFlowLayout { public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize { return NSSize(width: 254, height: 194) } public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, insetForSectionAt section: Int) -> NSEdgeInsets { return NSEdgeInsets(top: 0.1, left: 0.1, bottom: 0.1, right: 0.1) } }