KMHomeQuickToolsView.swift 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. //
  2. // KMHomeQuickToolsView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/10/10.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. @objc public protocol KMHomeQuickToolsViewDelegate: AnyObject {
  10. @objc optional func homeQuickToolsViewDidCollapseStateChanged(_ view: KMHomeQuickToolsView)
  11. @objc optional func homeQuickToolsViewDidManageTools(_ view: KMHomeQuickToolsView)
  12. @objc optional func homeQuickToolsViewDidItemClicked(_ view: KMHomeQuickToolsView, _ toolType: HomeQuickToolType)
  13. }
  14. public class KMHomeQuickToolsView: BaseXibView {
  15. @IBOutlet var contendBox: NSBox!
  16. @IBOutlet var titleLabel: NSTextField!
  17. @IBOutlet var moreButton: ComponentButton!
  18. @IBOutlet var itemsContendView: NSView!
  19. @IBOutlet var cardPDFTool1: ComponentCardPDFTool!
  20. @IBOutlet var cardPDFTool2: ComponentCardPDFTool!
  21. @IBOutlet var cardPDFTool3: ComponentCardPDFTool!
  22. @IBOutlet var cardPDFTool4: ComponentCardPDFTool!
  23. @IBOutlet var cardPDFTool5: ComponentCardPDFTool!
  24. @IBOutlet var cardPDFTool6: ComponentCardPDFTool!
  25. @IBOutlet var cardPDFTool7: ComponentCardPDFTool!
  26. @IBOutlet var cardPDFTool8: ComponentCardPDFTool!
  27. @IBOutlet var newTag1: ComponentTag!
  28. @IBOutlet var newTag2: ComponentTag!
  29. @IBOutlet var newTag3: ComponentTag!
  30. @IBOutlet var newTag4: ComponentTag!
  31. @IBOutlet var newTag5: ComponentTag!
  32. @IBOutlet var newTag6: ComponentTag!
  33. @IBOutlet var newTag7: ComponentTag!
  34. @IBOutlet var newTag8: ComponentTag!
  35. @IBOutlet var itemWidthConst: NSLayoutConstraint!
  36. @IBOutlet var itemHeightConst: NSLayoutConstraint!
  37. private var groupView: ComponentGroup!
  38. var itemIdentifys = ["CollapseToolsID", "ManageToolsID"]
  39. weak open var delegate: KMHomeQuickToolsViewDelegate?
  40. var isCollapse = false
  41. //MARK: - func
  42. public override func draw(_ dirtyRect: NSRect) {
  43. super.draw(dirtyRect)
  44. // Drawing code here.
  45. }
  46. public required init?(coder decoder: NSCoder) {
  47. super.init(coder: decoder)
  48. }
  49. override init(frame frameRect: NSRect) {
  50. super.init(frame: frameRect)
  51. }
  52. public override func awakeFromNib() {
  53. super.awakeFromNib()
  54. self.setupUI()
  55. }
  56. func setupUI() {
  57. titleLabel.stringValue = KMLocalizedString("Advanced Editing Tools")
  58. titleLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/1")
  59. titleLabel.font = ComponentLibrary.shared.font(forToken: "mac/body-m-bold")
  60. moreButton.properties = ComponentButtonProperty(type: .text_gray,
  61. size: .xxs,
  62. onlyIcon: true)
  63. moreButton.properties.propertyInfo.leftIcon_nor = NSImage(named: "quickTools_more_nor")
  64. moreButton.properties.propertyInfo.leftIcon_nor = NSImage(named: "quickTools_more_press")
  65. moreButton.reloadData()
  66. moreButton.setTarget(self, action: #selector(moreButtonClicked(_:)))
  67. let items = [cardPDFTool1, cardPDFTool2, cardPDFTool3, cardPDFTool4, cardPDFTool5, cardPDFTool6, cardPDFTool7, cardPDFTool8]
  68. for item in items {
  69. item!.delegate = self
  70. }
  71. }
  72. func refreshUI() {
  73. var showCount: CGFloat = 4 //每行显示的个数
  74. if CGRectGetWidth(itemsContendView.frame) < 168*2 + 1*16 {
  75. showCount = 1
  76. } else if CGRectGetWidth(itemsContendView.frame) < 168*3 + 2*16 {
  77. showCount = 2
  78. } else if CGRectGetWidth(itemsContendView.frame) < 168*4 + 3*16 {
  79. showCount = 3
  80. }
  81. print(showCount)
  82. var itemWidth = (CGRectGetWidth(self.itemsContendView.frame) - (showCount-1)*16 - 8)/showCount
  83. if itemWidth < 168 {
  84. itemWidth = 168
  85. }
  86. itemWidthConst.constant = itemWidth
  87. if let subviews = self.itemsContendView?.subviews {
  88. for view in subviews {
  89. if view is ComponentCardPDFTool {
  90. let index = subviews.firstIndex(of: view) ?? 0
  91. if index > KMNHomeQuickToolManager.defaultManager.quickToolsItemMutableArray.count - 1 {
  92. view.isHidden = true
  93. } else {
  94. if CGRectGetMinX(view.frame) > CGRectGetWidth(itemsContendView.frame) {
  95. view.isHidden = true
  96. } else {
  97. view.isHidden = false
  98. }
  99. }
  100. }
  101. }
  102. }
  103. }
  104. public func reloadData() {
  105. if KMNHomeQuickToolManager.defaultManager.collapseTools {
  106. itemHeightConst.constant = 60
  107. } else {
  108. itemHeightConst.constant = 88
  109. }
  110. self.refreshUI()
  111. let items = [cardPDFTool1, cardPDFTool2, cardPDFTool3, cardPDFTool4, cardPDFTool5, cardPDFTool6, cardPDFTool7, cardPDFTool8]
  112. let tagItems = [newTag1, newTag2, newTag3, newTag4, newTag5, newTag6, newTag7, newTag8]
  113. for tagItem in tagItems {
  114. tagItem?.isHidden = true
  115. tagItem?.properties = ComponentTagProperty(tagType: .mark_New, size: .m, text: KMLocalizedString("new"))
  116. }
  117. for item in items {
  118. var index = items.firstIndex(of: item) ?? 0
  119. if index > KMNHomeQuickToolManager.defaultManager.quickToolsItemMutableArray.count - 1 {
  120. index = 0
  121. item?.isHidden = true
  122. continue
  123. }
  124. item?.isHidden = false
  125. let toolType = HomeQuickToolType(rawValue: KMNHomeQuickToolManager.defaultManager.quickToolsItemMutableArray[index])!
  126. let toolMode = KMNHomeQuickToolMode.toolModeData(type: toolType)
  127. if let cardItem = item {
  128. cardItem.properties = ComponentCardPDFToolProperty(collapse: KMNHomeQuickToolManager.defaultManager.collapseTools,
  129. state: .normal,
  130. icon: NSImage(named: toolMode.toolImageName),
  131. text: toolMode.toolTitle,
  132. subText: toolMode.toolSubtitle)
  133. cardItem.properties.customData = toolMode
  134. }
  135. if toolMode.isNewState {
  136. let tagItem = tagItems[index]
  137. tagItem?.isHidden = false
  138. }
  139. }
  140. }
  141. //MARK: - Action
  142. @objc func moreButtonClicked(_ sender: NSView) {
  143. self.showThumbRightMenu(point: CGPointZero)
  144. }
  145. func showThumbRightMenu(point: CGPoint) {
  146. var viewHeight: CGFloat = 8
  147. var menuItemArr: [ComponentMenuitemProperty] = []
  148. var items = ["Collapse All", "Manage Quick Tools"]
  149. if KMNHomeQuickToolManager.defaultManager.collapseTools {
  150. items = ["Expand All", "Manage Quick Tools"]
  151. }
  152. for index in 0...items.count-1 {
  153. let properties_Menuitem: ComponentMenuitemProperty = ComponentMenuitemProperty(multipleSelect: false,
  154. itemSelected: false,
  155. isDisabled: false,
  156. keyEquivalent: nil,
  157. text: KMLocalizedString(items[index]),
  158. identifier: itemIdentifys[index])
  159. menuItemArr.append(properties_Menuitem)
  160. viewHeight += 36
  161. }
  162. if groupView == nil {
  163. groupView = ComponentGroup.createFromNib(in: ComponentLibrary.shared.componentBundle())
  164. }
  165. groupView.groupDelegate = self
  166. groupView?.frame = CGRectMake(0, 0, 160, viewHeight)
  167. groupView.updateGroupInfo(menuItemArr)
  168. if let superview = moreButton.superview {
  169. var point = superview.convert(moreButton.frame.origin, to: nil)
  170. point.y -= viewHeight
  171. point.y -= 10
  172. groupView.showWithPoint(point, relativeTo: moreButton)
  173. }
  174. }
  175. }
  176. //MARK: -
  177. extension KMHomeQuickToolsView: ComponentCardPDFToolDelegate {
  178. public func componentCardPDFToolDidClicked(_ view: ComponentCardPDFTool) {
  179. if view.properties.customData is KMNHomeQuickToolMode {
  180. let toolMode: KMNHomeQuickToolMode = view.properties.customData as! KMNHomeQuickToolMode
  181. delegate?.homeQuickToolsViewDidItemClicked?(self, toolMode.quickToolType)
  182. }
  183. }
  184. }
  185. //MARK: - ComponentGroupDelegate
  186. extension KMHomeQuickToolsView: ComponentGroupDelegate {
  187. public func componentGroupDidDismiss(group: ComponentGroup?) {
  188. moreButton.properties.state = .normal
  189. moreButton.reloadData()
  190. }
  191. public func componentGroupDidSelect(group: ComponentGroup?, menuItemProperty: ComponentMenuitemProperty?) {
  192. if menuItemProperty?.identifier == "CollapseToolsID" {
  193. KMNHomeQuickToolManager.defaultManager.collapseTools = !KMNHomeQuickToolManager.defaultManager.collapseTools
  194. self.delegate?.homeQuickToolsViewDidCollapseStateChanged?(self)
  195. } else if menuItemProperty?.identifier == "ManageToolsID" {
  196. self.delegate?.homeQuickToolsViewDidManageTools?(self)
  197. }
  198. }
  199. }