123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- //
- // KMHomeFastToolViewController.swift
- // PDF Reader Pro
- //
- // 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] = []
- var createFilePopover: NSPopover = NSPopover.init()
-
- // MARK: init
-
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do view setup here.
- self.fastToolMoreBox.fillColor = .clear
-
- 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.km_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)
- self.createFilePopover.contentViewController = vc
- self.createFilePopover.animates = true
- self.createFilePopover.behavior = .semitransient
- self.createFilePopover.setValue(true, forKey: "shouldHideAnchor")
- self.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)
-
- self.createFilePopover.close()
- }
- }
- }
-
- 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 self.createFilePopover.isShown {
- self.createFilePopover.close()
- } else {
- 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<IndexPath>) {
- 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)
- // }
- }
|