ComponentCardPDFTool.swift 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //
  2. // ComponentCard.swift
  3. // KMComponentLibrary
  4. //
  5. // Created by Niehaoyu on 2024/9/14.
  6. //
  7. import Cocoa
  8. import AppKit
  9. @objc public protocol ComponentCardPDFToolDelegate: AnyObject {
  10. //点击回调
  11. @objc optional func componentCardPDFToolDidClicked(_ view: ComponentCardPDFTool)
  12. //选中状态更新结束后回调
  13. @objc optional func componentCardPDFToolDidSelectedStateUpdated(_ view: ComponentCardPDFTool)
  14. }
  15. public class ComponentCardPDFTool: ComponentBaseXibView {
  16. @IBOutlet var contendBox: NSBox!
  17. @IBOutlet var iconImage: NSImageView!
  18. @IBOutlet var titleLabel: NSTextField!
  19. @IBOutlet var subTitleLabel: NSTextField!
  20. @IBOutlet var checkBox: ComponentCheckBox!
  21. @IBOutlet var iconWidthConst: NSLayoutConstraint!
  22. private var _properties : ComponentCardPDFToolProperty = ComponentCardPDFToolProperty()
  23. weak open var delegate: ComponentCardPDFToolDelegate?
  24. public override func draw(_ dirtyRect: NSRect) {
  25. super.draw(dirtyRect)
  26. }
  27. // MARK: 初始化
  28. deinit {
  29. NotificationCenter.default.removeObserver(self)
  30. }
  31. public required init?(coder decoder: NSCoder) {
  32. super.init(coder: decoder)
  33. }
  34. override init(frame frameRect: NSRect) {
  35. super.init(frame: frameRect)
  36. }
  37. public override func awakeFromNib() {
  38. super.awakeFromNib()
  39. self.iconImage.wantsLayer = true
  40. self.iconImage.layer?.masksToBounds = true
  41. checkBox.properties = ComponentCheckBoxProperty(size: .xxs, state: .normal, isDisabled: false, showhelp: false, text: nil, checkboxType: .normal)
  42. checkBox.setTarget(self, action: #selector(checkBoxAction(_:)))
  43. }
  44. //MARK: - Setter and Getter
  45. public var properties : ComponentCardPDFToolProperty {
  46. get {
  47. return _properties
  48. }
  49. set {
  50. _properties = newValue
  51. ComponentLibrary.shared.configCardPDFToolComponent(properties: _properties)
  52. self.setupUI()
  53. self.refreshUI()
  54. }
  55. }
  56. //MARK: - SetupUI
  57. func setupUI() {
  58. if properties.selectedAble == true {
  59. self.iconWidthConst.constant = 24
  60. self.iconImage.layer?.cornerRadius = 12
  61. } else {
  62. self.iconWidthConst.constant = 36
  63. self.iconImage.layer?.cornerRadius = 18
  64. }
  65. self.iconImage.image = self.properties.icon
  66. self.titleLabel.stringValue = self.properties.text ?? ""
  67. self.subTitleLabel.stringValue = self.properties.subText ?? ""
  68. if self.properties.collapse == true {
  69. self.subTitleLabel.isHidden = true
  70. self.titleLabel.frame = CGRectMake(CGRectGetMaxX(self.iconImage.frame)+8, CGRectGetHeight(self.frame)/2-CGRectGetHeight(self.titleLabel.frame)/2, CGRectGetWidth(self.titleLabel.frame), CGRectGetHeight(self.titleLabel.frame))
  71. self.titleLabel.autoresizingMask = [.maxXMargin, .maxYMargin]
  72. } else {
  73. self.titleLabel.frame = CGRectMake(CGRectGetMaxX(self.iconImage.frame)+8, CGRectGetHeight(self.frame)-16-CGRectGetHeight(self.titleLabel.frame), CGRectGetWidth(self.titleLabel.frame), CGRectGetHeight(self.titleLabel.frame))
  74. self.titleLabel.autoresizingMask = [.minYMargin, .maxXMargin, .maxYMargin]
  75. self.subTitleLabel.isHidden = false
  76. self.subTitleLabel.frame = CGRectMake(self.iconWidthConst.constant+8+12, CGRectGetMinY(self.titleLabel.frame)-4-38, CGRectGetWidth(self.frame)-self.iconWidthConst.constant-20-12-12, 38)
  77. self.subTitleLabel.autoresizingMask = [.minYMargin, .maxXMargin, .maxYMargin, .width]
  78. }
  79. }
  80. func refreshUI() {
  81. self.contendBox.cornerRadius = self.properties.propertyInfo.cornerRadius
  82. self.contendBox.fillColor = properties.propertyInfo.color_nor
  83. self.titleLabel.textColor = properties.propertyInfo.textColor
  84. self.subTitleLabel.textColor = properties.propertyInfo.subTextColor
  85. self.subTitleLabel.font = properties.propertyInfo.textFont
  86. self.subTitleLabel.font = properties.propertyInfo.subTextFont
  87. if self.properties.state == .normal {
  88. self.contendBox.borderColor = properties.propertyInfo.borderColor_nor
  89. self.contendBox.borderWidth = self.properties.propertyInfo.borderWidth
  90. } else if self.properties.state == .hover {
  91. self.contendBox.borderColor = properties.propertyInfo.borderColor_hov
  92. self.contendBox.borderWidth = self.properties.propertyInfo.borderWidth_hov
  93. }
  94. if properties.selectedAble == true && properties.isSelected == true {
  95. contendBox.fillColor = properties.propertyInfo.color_active
  96. contendBox.borderColor = properties.propertyInfo.borderColor_hov
  97. contendBox.borderWidth = properties.propertyInfo.borderWidth
  98. }
  99. self.titleLabel.sizeToFit()
  100. checkBox.isHidden = properties.selectedAble ? false : true
  101. checkBox.properties.checkboxType = properties.isSelected ? .selected : .normal
  102. checkBox.reloadData()
  103. }
  104. //MARK: - Action
  105. @objc func checkBoxAction(_ sender: NSView) {
  106. if checkBox.properties.checkboxType == .normal {
  107. properties.isSelected = false
  108. } else if checkBox.properties.checkboxType == .selected {
  109. properties.isSelected = true
  110. }
  111. self.reloadData()
  112. self.delegate?.componentCardPDFToolDidSelectedStateUpdated?(self)
  113. }
  114. //MARK: - Public Method
  115. public func reloadData() {
  116. self.setupUI()
  117. self.refreshUI()
  118. }
  119. //MARK: - MouseEvent
  120. public override func mouseEntered(with event: NSEvent) {
  121. super.mouseEntered(with: event)
  122. if properties.enableMouseEnterExit {
  123. properties.state = .hover
  124. self.refreshUI()
  125. }
  126. }
  127. public override func mouseMoved(with event: NSEvent) {
  128. super.mouseMoved(with: event)
  129. if properties.enableMouseEnterExit {
  130. properties.state = .hover
  131. self.refreshUI()
  132. }
  133. }
  134. public override func mouseExited(with event: NSEvent) {
  135. super.mouseExited(with: event)
  136. if properties.enableMouseEnterExit {
  137. properties.state = .normal
  138. self.refreshUI()
  139. }
  140. }
  141. public override func mouseDown(with event: NSEvent) {
  142. super.mouseDown(with: event)
  143. self.refreshUI()
  144. }
  145. public override func mouseUp(with event: NSEvent) {
  146. super.mouseUp(with: event)
  147. self.delegate?.componentCardPDFToolDidClicked?(self)
  148. }
  149. }