// // KMHomeFastToolViewController.swift // PDF Master // // Home界面-便捷工具 模块 // Created by wanjun on 2022/10/19. // import Cocoa @objc protocol KMHomeFastToolViewControllerDelegate { @objc optional func fastToolViewController(_ viewController: KMHomeFastToolViewController, foldChangeState: Bool) @objc optional func fastToolDidSelectAllTools(_ viewController: KMHomeFastToolViewController) @objc optional func fastToolViewController(_ viewController: KMHomeFastToolViewController, didSelectItemsAt indexPaths: [Int]) } class KMHomeFastToolViewController: NSViewController { @IBOutlet weak var fastToolLabel: NSTextField! @IBOutlet weak var fastToolMoreBox: KMBox! @IBOutlet weak var fastToolMoreBoxHeight: NSLayoutConstraint! @IBOutlet weak var fastToolAllToolsBox: KMBox! @IBOutlet weak var fastToolAllToolsBoxHeight: NSLayoutConstraint! @IBOutlet weak var fastToolBox: NSBox! @IBOutlet weak var fastToolCollectionView: KMFastToolCollectionView! @IBOutlet weak var fastToolBoxHeightConstraint: NSLayoutConstraint! let ScrollerViewWidget: CGFloat = 15.0 var collectionItemWidth: Int = 230 var fastToolMoreButtonVC: KMDesignButton! var fastToolAllToolsButtonVC: KMDesignButton! open weak var delete: KMHomeFastToolViewControllerDelegate? var isFold: Bool! //快捷工具数据 var quickToolsItemMutableArray: [Int] = [] // MARK: init override func viewDidLoad() { super.viewDidLoad() // Do view setup here. fastToolMoreButtonVC = KMDesignButton.init(withType: .Image) fastToolAllToolsButtonVC = KMDesignButton.init(withType: .Text) fastToolCollectionView.register(KMFastToolCollectionViewItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMFastToolCollectionViewItem")) reloadUserDefault() } override func viewDidAppear() { super.viewDidAppear() reloadUserDefault() self.view.postsBoundsChangedNotifications = true NotificationCenter.default.addObserver(self, selector: #selector(boundsDidChangeNotification), name: NSView.frameDidChangeNotification, object: self.view) reloadData() } func initializeUI() { fastToolLabel.font = NSFont(name: "SFProText-Semibold", size: 20) fastToolLabel.textColor = NSColor.init(hex: "#252629") fastToolAllToolsBox.fillColor = .clear fastToolAllToolsBox.contentView = fastToolAllToolsButtonVC.view fastToolAllToolsButtonVC.target = self fastToolAllToolsButtonVC.action = #selector(fastToolAllToolsAction(_:)) fastToolAllToolsButtonVC.button(type: .Sec, size: .m, height: fastToolAllToolsBoxHeight) fastToolMoreBox.fillColor = .clear fastToolMoreBox.contentView = fastToolMoreButtonVC.view fastToolMoreButtonVC.target = self fastToolMoreButtonVC.action = #selector(fastToolMoreAction(_:)) fastToolMoreButtonVC.image = NSImage(named: "icon_btn_more_false_false")! fastToolMoreButtonVC.button(type: .Sec, size: .m, height: fastToolMoreBoxHeight) } func initLocalization() { let fastToolParagraphStyle = NSMutableParagraphStyle() fastToolParagraphStyle.lineSpacing = 28.0 fastToolLabel.attributedStringValue = NSAttributedString(string: NSLocalizedString("Quick Tools", comment: ""), attributes: [NSAttributedString.Key.paragraphStyle: fastToolParagraphStyle]) fastToolAllToolsButtonVC.stringValue = NSLocalizedString("Tools", comment: "") } // MARK: private func reloadData() -> Void { initializeUI() initLocalization() collectionItemWidth = Int((fastToolCollectionView.bounds.width - (20 * 3))/4) fastToolCollectionView.reloadData() fastToolAllToolsButtonVC.updateUI() fastToolMoreButtonVC.updateUI() } func refreshUI(_ isfold: Bool) { if isfold { fastToolBoxHeightConstraint.constant = 152 + ScrollerViewWidget UserDefaults.standard.setValue(true, forKey: "kHomeQucikToolsTypeKey") } else { fastToolBoxHeightConstraint.constant = 248 + ScrollerViewWidget UserDefaults.standard.setValue(false, forKey: "kHomeQucikToolsTypeKey") } UserDefaults.standard.synchronize() } func reloadUserDefault() -> Void { isFold = UserDefaults.standard.bool(forKey: "kHomeQucikToolsTypeKey") self.refreshUI(isFold) let quickToolsArray = UserDefaults.standard.array(forKey: "HomeQucikToolsItemArrayKey") as? [Int] ?? [] if (quickToolsArray.count > 0) { quickToolsItemMutableArray = quickToolsArray } else { quickToolsItemMutableArray = quickToolsFullArray() UserDefaults.standard.set(quickToolsItemMutableArray, forKey: "HomeQucikToolsItemArrayKey") UserDefaults.standard.synchronize() } } func createPopoverAction(_ data: [String], view: NSView) { let vc: KMHomePopViewController = KMHomePopViewController().initWithPopViewDataArr(data) let createFilePopover: NSPopover = NSPopover.init() createFilePopover.contentViewController = vc createFilePopover.animates = true createFilePopover.behavior = .semitransient createFilePopover.setValue(true, forKey: "shouldHideAnchor") createFilePopover.show(relativeTo: CGRect(x: view.bounds.origin.x, y: 10, width: view.bounds.size.width, height: view.bounds.size.height), of: view, preferredEdge: .minY) vc.downCallback = {(downEntered: Bool, count: String) -> Void in if downEntered { self.moreToolsAction(vc) } } } func quickToolsFullArray() -> [Int] { return [DataNavigationViewButtonActionType.PDFToWord.rawValue, DataNavigationViewButtonActionType.ImageToPDF.rawValue, DataNavigationViewButtonActionType.PDFToExcel.rawValue, DataNavigationViewButtonActionType.MergePDF.rawValue, DataNavigationViewButtonActionType.PDFToPPT.rawValue, DataNavigationViewButtonActionType.Compression.rawValue, DataNavigationViewButtonActionType.PDFToImage.rawValue, DataNavigationViewButtonActionType.Security.rawValue] } // MARK: Action func moreToolsAction(_ viewController: KMHomePopViewController) { isFold = !isFold self.refreshUI(isFold) if isFold { viewController.changePopViewCellData(0, content: NSLocalizedString("Expand", comment: "")) } else { viewController.changePopViewCellData(0, content: NSLocalizedString("Collapse", comment: "")) } fastToolCollectionView.reloadData() self.delete?.fastToolViewController!(self, foldChangeState: isFold) } @IBAction func fastToolAllToolsAction(_ sender: NSButton) -> Void { self.delete?.fastToolDidSelectAllTools!(self) } @IBAction func fastToolMoreAction(_ sender: NSButton) -> Void { if isFold { self.createPopoverAction([NSLocalizedString("Expand", comment: "")], view: fastToolMoreBox) } else { self.createPopoverAction([NSLocalizedString("Collapse", comment: "")], view: fastToolMoreBox) } } // MARK: NSNotification Action @IBAction func boundsDidChangeNotification(_ notification: NSNotification) -> Void { reloadData() } } // MARK: - NSCollectionViewDataSource extension KMHomeFastToolViewController: NSCollectionViewDataSource { func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int { return quickToolsItemMutableArray.count } func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem { if collectionView.isEqual(fastToolCollectionView) { let item: KMFastToolCollectionViewItem = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMFastToolCollectionViewItem"), for: indexPath) as! KMFastToolCollectionViewItem item.card(bg: "card.has-desc.bg.norm", titleText: "card.mac-text.title", descText: "card.mac-text.desc", spacing: "card.item-spacing") item.card(bg: "card.has-desc.bg.hov", titleText: "card.mac-text.title", descText: "card.mac-text.desc", spacing: "card.item-spacing", state: .Hov) item.card(bg: "card.has-desc.bg.sel", titleText: "card.mac-text.title", descText: "card.mac-text.desc", spacing: "card.item-spacing", state: .Sel) item.card(bg: "card.has-desc.bg.custom", titleText: "card.mac-text.title", descText: "card.mac-text.desc", spacing: "card.item-spacing", state: .Custom) item.card(bg: "card.has-desc.bg.none", state: .None) item.card(bg: "card.has-desc.bg.change", titleText: "card.win-text.title", descText: "card.win-text.desc", spacing: "card.item-spacing", state: .Change) let i: Int = indexPath.item let index: Int = quickToolsItemMutableArray[i] item.quickToolsCollectionCellItem(type: DataNavigationViewButtonActionType(rawValue: index) ?? .Batch) item.fastToolItemBox.downCallback = { (downEntered: Bool, mouseBox: KMBox, event) -> Void in if downEntered { item.state = .Sel } else { item.state = .Norm } item.updateUI() } item.fastToolItemSubtitleLabel.isHidden = isFold return item } return NSCollectionViewItem.init() } } // MARK: - NSCollectionViewDelegate extension KMHomeFastToolViewController: NSCollectionViewDelegate { func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set) { var indexs: [Int] = [] for index in indexPaths { let i: Int = index.item let index: Int = quickToolsItemMutableArray[i] indexs.append(index) } self.delete?.fastToolViewController!(self, didSelectItemsAt: indexs) collectionView.deselectAll([]) } } // MARK: - NSCollectionViewDelegateFlowLayout extension KMHomeFastToolViewController: NSCollectionViewDelegateFlowLayout { func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize { if collectionView.isEqual(to: fastToolCollectionView) { if isFold { return CGSize(width: collectionItemWidth, height: 68) } else { return CGSize(width: collectionItemWidth, height: 116) } } return CGSize(width: 230, height: 116) } func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { return 16 } func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat { return 8 } // func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, insetForSectionAt section: Int) -> NSEdgeInsets { // return NSEdgeInsets(top: 0, left: 0, bottom: 15, right: 0) // } }