123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- //
- // ComponentSideBarItem.swift
- // KMComponentLibrary
- //
- // Created by Niehaoyu on 2024/9/20.
- //
- import Cocoa
- import AppKit
- public class ComponentSideBarItem: ComponentBaseXibView {
-
- @IBOutlet var contendBox: NSBox!
- @IBOutlet var iconImage: NSImageView!
-
- private var _properties : ComponentSideBarItemProperty = ComponentSideBarItemProperty()
-
- private var action: Selector? // 点击事件
- private weak var target: AnyObject? // 对象目标
-
- public override func draw(_ dirtyRect: NSRect) {
- super.draw(dirtyRect)
-
- }
-
- // MARK: 初始化
- deinit {
- NotificationCenter.default.removeObserver(self)
-
- }
-
- 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()
-
- }
-
- //MARK: - Setter and Getter
- public var properties : ComponentSideBarItemProperty {
- get {
- return _properties
- }
- set {
- _properties = newValue
-
- ComponentLibrary.shared.configSidebarItemComponent(properties: _properties)
-
- setupUI()
-
- refreshUI()
- }
- }
-
- //MARK: - SetupUI
- func setupUI() {
- iconImage.image = properties.icon
- }
-
- func refreshUI() {
-
- contendBox.cornerRadius = properties.propertyInfo.cornerRadius
- contendBox.borderWidth = properties.propertyInfo.borderWidth
- contendBox.borderColor = properties.propertyInfo.borderColor_nor
-
- var fillColor: NSColor?
-
- if properties.state == .normal {
- fillColor = properties.propertyInfo.color_nor
- } else if properties.state == .hover {
- fillColor = properties.propertyInfo.color_hov
- } else if properties.state == .pressed {
- fillColor = properties.propertyInfo.color_active
- }
-
- if let color = fillColor {
- contendBox.fillColor = color
- }
-
- }
-
- //MARK: - Public Method
- public func reloadData() {
- refreshUI()
-
- }
-
- public func setTarget(_ target: AnyObject?, action: Selector?) {
- self.target = target!
- self.action = action!
- }
-
- //MARK: - MouseEvent
- public override func mouseEntered(with event: NSEvent) {
- super.mouseEntered(with: event)
-
- if properties.state != .pressed {
- properties.state = .hover
- }
-
- refreshUI()
- }
-
- public override func mouseMoved(with event: NSEvent) {
- super.mouseMoved(with: event)
-
- }
-
- public override func mouseExited(with event: NSEvent) {
- super.mouseExited(with: event)
-
- if properties.state != .pressed {
- properties.state = .normal
- }
-
- refreshUI()
- }
-
- public override func mouseDown(with event: NSEvent) {
- super.mouseDown(with: event)
-
- }
-
- public override func mouseUp(with event: NSEvent) {
- super.mouseUp(with: event)
-
- if let target = target, let action = action {
- _ = target.perform(action, with: self)
- }
- }
- }
|