123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- //
- // 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<IndexPath>) {
-
- }
-
-
- }
- 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)
- }
- }
|