123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- //
- // KMHomeQuickToolsView.swift
- // PDF Reader Pro
- //
- // Created by Niehaoyu on 2024/10/10.
- //
- import Cocoa
- import KMComponentLibrary
- @objc public protocol KMHomeQuickToolsViewDelegate: AnyObject {
-
- @objc optional func homeQuickToolsViewDidCollapseStateChanged(_ view: KMHomeQuickToolsView)
-
- @objc optional func homeQuickToolsViewDidManageTools(_ view: KMHomeQuickToolsView)
-
- @objc optional func homeQuickToolsViewDidItemClicked(_ view: KMHomeQuickToolsView, _ toolType: HomeQuickToolType)
-
- }
- public class KMHomeQuickToolsView: BaseXibView {
- @IBOutlet var contendBox: NSBox!
- @IBOutlet var titleLabel: NSTextField!
- @IBOutlet var moreButton: ComponentButton!
-
- @IBOutlet var itemsContendView: NSView!
- @IBOutlet var cardPDFTool1: ComponentCardPDFTool!
- @IBOutlet var cardPDFTool2: ComponentCardPDFTool!
- @IBOutlet var cardPDFTool3: ComponentCardPDFTool!
- @IBOutlet var cardPDFTool4: ComponentCardPDFTool!
- @IBOutlet var cardPDFTool5: ComponentCardPDFTool!
- @IBOutlet var cardPDFTool6: ComponentCardPDFTool!
- @IBOutlet var cardPDFTool7: ComponentCardPDFTool!
- @IBOutlet var cardPDFTool8: ComponentCardPDFTool!
-
- @IBOutlet var newTag1: ComponentTag!
- @IBOutlet var newTag2: ComponentTag!
- @IBOutlet var newTag3: ComponentTag!
- @IBOutlet var newTag4: ComponentTag!
- @IBOutlet var newTag5: ComponentTag!
- @IBOutlet var newTag6: ComponentTag!
- @IBOutlet var newTag7: ComponentTag!
- @IBOutlet var newTag8: ComponentTag!
-
- @IBOutlet var itemWidthConst: NSLayoutConstraint!
- @IBOutlet var itemHeightConst: NSLayoutConstraint!
-
- private var groupView: ComponentGroup!
- var itemIdentifys = ["CollapseToolsID", "ManageToolsID"]
- weak open var delegate: KMHomeQuickToolsViewDelegate?
- var isCollapse = false
-
- //MARK: - func
- public override func draw(_ dirtyRect: NSRect) {
- super.draw(dirtyRect)
-
- // Drawing code here.
- }
-
- public required init?(coder decoder: NSCoder) {
- super.init(coder: decoder)
-
- }
-
- override init(frame frameRect: NSRect) {
- super.init(frame: frameRect)
- }
-
- public override func awakeFromNib() {
- super.awakeFromNib()
-
- self.setupUI()
- }
-
- func setupUI() {
-
- titleLabel.stringValue = KMLocalizedString("Advanced Editing Tools")
- titleLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/1")
- titleLabel.font = ComponentLibrary.shared.font(forToken: "mac/body-m-bold")
-
- moreButton.properties = ComponentButtonProperty(type: .text_gray,
- size: .xxs,
- onlyIcon: true)
- moreButton.properties.propertyInfo.leftIcon_nor = NSImage(named: "quickTools_more_nor")
- moreButton.properties.propertyInfo.leftIcon_nor = NSImage(named: "quickTools_more_press")
- moreButton.reloadData()
- moreButton.setTarget(self, action: #selector(moreButtonClicked(_:)))
-
- let items = [cardPDFTool1, cardPDFTool2, cardPDFTool3, cardPDFTool4, cardPDFTool5, cardPDFTool6, cardPDFTool7, cardPDFTool8]
- for item in items {
- item!.delegate = self
- }
-
- }
-
- func refreshUI() {
- var showCount: CGFloat = 4 //每行显示的个数
- if CGRectGetWidth(itemsContendView.frame) < 168*2 + 1*16 {
- showCount = 1
- } else if CGRectGetWidth(itemsContendView.frame) < 168*3 + 2*16 {
- showCount = 2
- } else if CGRectGetWidth(itemsContendView.frame) < 168*4 + 3*16 {
- showCount = 3
- }
- print(showCount)
-
- var itemWidth = (CGRectGetWidth(self.itemsContendView.frame) - (showCount-1)*16 - 8)/showCount
- if itemWidth < 168 {
- itemWidth = 168
- }
-
- itemWidthConst.constant = itemWidth
-
- if let subviews = self.itemsContendView?.subviews {
- for view in subviews {
- if view is ComponentCardPDFTool {
- let index = subviews.firstIndex(of: view) ?? 0
- if index > KMNHomeQuickToolManager.defaultManager.quickToolsItemMutableArray.count - 1 {
- view.isHidden = true
- } else {
- if CGRectGetMinX(view.frame) > CGRectGetWidth(itemsContendView.frame) {
- view.isHidden = true
- } else {
- view.isHidden = false
- }
- }
- }
- }
- }
- }
-
- public func reloadData() {
- if KMNHomeQuickToolManager.defaultManager.collapseTools {
- itemHeightConst.constant = 60
- } else {
- itemHeightConst.constant = 88
- }
- self.refreshUI()
-
- let items = [cardPDFTool1, cardPDFTool2, cardPDFTool3, cardPDFTool4, cardPDFTool5, cardPDFTool6, cardPDFTool7, cardPDFTool8]
- let tagItems = [newTag1, newTag2, newTag3, newTag4, newTag5, newTag6, newTag7, newTag8]
- for tagItem in tagItems {
- tagItem?.isHidden = true
- tagItem?.properties = ComponentTagProperty(tagType: .mark_New, size: .m, text: KMLocalizedString("new"))
- }
-
- for item in items {
- var index = items.firstIndex(of: item) ?? 0
- if index > KMNHomeQuickToolManager.defaultManager.quickToolsItemMutableArray.count - 1 {
- index = 0
-
- item?.isHidden = true
- continue
- }
-
- item?.isHidden = false
-
- let toolType = HomeQuickToolType(rawValue: KMNHomeQuickToolManager.defaultManager.quickToolsItemMutableArray[index])!
- let toolMode = KMNHomeQuickToolMode.toolModeData(type: toolType)
-
- if let cardItem = item {
- cardItem.properties = ComponentCardPDFToolProperty(collapse: KMNHomeQuickToolManager.defaultManager.collapseTools,
- state: .normal,
- icon: NSImage(named: toolMode.toolImageName),
- text: toolMode.toolTitle,
- subText: toolMode.toolSubtitle)
- cardItem.properties.customData = toolMode
- }
- if toolMode.isNewState {
- let tagItem = tagItems[index]
- tagItem?.isHidden = false
- }
- }
- }
-
- //MARK: - Action
- @objc func moreButtonClicked(_ sender: NSView) {
- self.showThumbRightMenu(point: CGPointZero)
- }
-
- func showThumbRightMenu(point: CGPoint) {
- var viewHeight: CGFloat = 8
- var menuItemArr: [ComponentMenuitemProperty] = []
-
- var items = ["Collapse All", "Manage Quick Tools"]
- if KMNHomeQuickToolManager.defaultManager.collapseTools {
- items = ["Expand All", "Manage Quick Tools"]
- }
-
- for index in 0...items.count-1 {
- let properties_Menuitem: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
- itemSelected: false,
- isDisabled: false,
- keyEquivalent: nil,
- text: KMLocalizedString(items[index]),
- identifier: itemIdentifys[index])
- menuItemArr.append(properties_Menuitem)
- viewHeight += 36
- }
-
- if groupView == nil {
- groupView = ComponentGroup.createFromNib(in: ComponentLibrary.shared.componentBundle())
- }
- groupView.groupDelegate = self
- groupView?.frame = CGRectMake(0, 0, 160, viewHeight)
- groupView.updateGroupInfo(menuItemArr)
- if let superview = moreButton.superview {
- var point = superview.convert(moreButton.frame.origin, to: nil)
- point.y -= viewHeight
- point.y -= 10
-
- groupView.showWithPoint(point, relativeTo: moreButton)
- }
-
- }
-
- }
-
- //MARK: -
- extension KMHomeQuickToolsView: ComponentCardPDFToolDelegate {
- public func componentCardPDFToolDidClicked(_ view: ComponentCardPDFTool) {
- if view.properties.customData is KMNHomeQuickToolMode {
- let toolMode: KMNHomeQuickToolMode = view.properties.customData as! KMNHomeQuickToolMode
-
- delegate?.homeQuickToolsViewDidItemClicked?(self, toolMode.quickToolType)
- }
-
- }
- }
- //MARK: - ComponentGroupDelegate
- extension KMHomeQuickToolsView: ComponentGroupDelegate {
- public func componentGroupDidDismiss(group: ComponentGroup?) {
- moreButton.properties.state = .normal
- moreButton.reloadData()
-
- }
-
- public func componentGroupDidSelect(group: ComponentGroup?, menuItemProperty: ComponentMenuitemProperty?) {
- if menuItemProperty?.identifier == "CollapseToolsID" {
- KMNHomeQuickToolManager.defaultManager.collapseTools = !KMNHomeQuickToolManager.defaultManager.collapseTools
-
- self.delegate?.homeQuickToolsViewDidCollapseStateChanged?(self)
- } else if menuItemProperty?.identifier == "ManageToolsID" {
- self.delegate?.homeQuickToolsViewDidManageTools?(self)
- }
- }
-
- }
|