ComponentSideBarItem.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // ComponentSideBarItem.swift
  3. // KMComponentLibrary
  4. //
  5. // Created by Niehaoyu on 2024/9/20.
  6. //
  7. import Cocoa
  8. import AppKit
  9. public class ComponentSideBarItem: ComponentBaseXibView {
  10. @IBOutlet var contendBox: NSBox!
  11. @IBOutlet var iconImage: NSImageView!
  12. private var _properties : ComponentSideBarItemProperty = ComponentSideBarItemProperty()
  13. private var action: Selector? // 点击事件
  14. private weak var target: AnyObject? // 对象目标
  15. public override func draw(_ dirtyRect: NSRect) {
  16. super.draw(dirtyRect)
  17. }
  18. // MARK: 初始化
  19. deinit {
  20. NotificationCenter.default.removeObserver(self)
  21. }
  22. public required init?(coder decoder: NSCoder) {
  23. super.init(coder: decoder)
  24. }
  25. override init(frame frameRect: NSRect) {
  26. super.init(frame: frameRect)
  27. }
  28. public override func awakeFromNib() {
  29. super.awakeFromNib()
  30. }
  31. //MARK: - Setter and Getter
  32. public var properties : ComponentSideBarItemProperty {
  33. get {
  34. return _properties
  35. }
  36. set {
  37. _properties = newValue
  38. ComponentLibrary.shared.configSidebarItemComponent(properties: _properties)
  39. setupUI()
  40. refreshUI()
  41. }
  42. }
  43. //MARK: - SetupUI
  44. func setupUI() {
  45. iconImage.image = properties.icon
  46. }
  47. func refreshUI() {
  48. contendBox.cornerRadius = properties.propertyInfo.cornerRadius
  49. contendBox.borderWidth = properties.propertyInfo.borderWidth
  50. contendBox.borderColor = properties.propertyInfo.borderColor_nor
  51. var fillColor: NSColor?
  52. if properties.state == .normal {
  53. fillColor = properties.propertyInfo.color_nor
  54. } else if properties.state == .hover {
  55. fillColor = properties.propertyInfo.color_hov
  56. } else if properties.state == .pressed {
  57. fillColor = properties.propertyInfo.color_active
  58. }
  59. if let color = fillColor {
  60. contendBox.fillColor = color
  61. }
  62. }
  63. //MARK: - Public Method
  64. public func reloadData() {
  65. refreshUI()
  66. }
  67. public func setTarget(_ target: AnyObject?, action: Selector?) {
  68. self.target = target!
  69. self.action = action!
  70. }
  71. //MARK: - MouseEvent
  72. public override func mouseEntered(with event: NSEvent) {
  73. super.mouseEntered(with: event)
  74. if properties.state != .pressed {
  75. properties.state = .hover
  76. }
  77. refreshUI()
  78. }
  79. public override func mouseMoved(with event: NSEvent) {
  80. super.mouseMoved(with: event)
  81. }
  82. public override func mouseExited(with event: NSEvent) {
  83. super.mouseExited(with: event)
  84. if properties.state != .pressed {
  85. properties.state = .normal
  86. }
  87. refreshUI()
  88. }
  89. public override func mouseDown(with event: NSEvent) {
  90. super.mouseDown(with: event)
  91. }
  92. public override func mouseUp(with event: NSEvent) {
  93. super.mouseUp(with: event)
  94. if let target = target, let action = action {
  95. _ = target.perform(action, with: self)
  96. }
  97. }
  98. }