123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- //
- // ComponentButton.swift
- // KMComponentLibrary
- //
- // Created by Niehaoyu on 2024/8/22.
- //
- import Cocoa
- import AppKit
- public class ComponentButton: ComponentBaseXibView {
- @IBOutlet var contendBox: NSBox!
- @IBOutlet var leftIcon: NSImageView!
- @IBOutlet var label: NSTextField!
- @IBOutlet var rightIcon: NSImageView!
-
- @IBOutlet var leftImgLeftConst: NSLayoutConstraint!
- @IBOutlet var leftImgWidthConst: NSLayoutConstraint!
- @IBOutlet var rightImgWidthConst: NSLayoutConstraint!
- @IBOutlet var rightImgRightConst: NSLayoutConstraint!
-
- // MARK: Private Property
- private var _properties : ComponentButtonProperty = ComponentButtonProperty()
-
- private var _toolTip: String = "" // 提示文字
-
- private var action: Selector? // 点击事件
- private weak var target: AnyObject? // 对象目标
-
- private var mouseDownEnable: Bool = false
-
- open var keyEquivalent: String = ""
- open var keyEquivalentModifierMask: NSEvent.ModifierFlags = NSEvent.ModifierFlags()
-
- // MARK: - 初始化
-
- 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()
- label.wantsLayer = true
- }
-
-
- // MARK: - Set & Get
-
- public var properties : ComponentButtonProperty {
- get {
- return _properties
- }
- set {
- _properties = newValue
-
- ComponentLibrary.shared.configButtonComponent(properties: _properties)
-
- reloadData()
- }
- }
-
-
- //MARK: - setupUI
-
- func setupUI() {
- leftIcon.isHidden = true
- label.isHidden = true
- rightIcon.isHidden = true
-
- if properties.onlyIcon == true {
- leftIcon.isHidden = false
- } else {
- label.isHidden = false
- if properties.showLeftIcon == true &&
- properties.showRightIcon == true {
-
- leftIcon.isHidden = false
- rightIcon.isHidden = false
-
- } else if properties.showLeftIcon == true &&
- properties.showRightIcon == false {
- leftIcon.isHidden = false
-
- } else if properties.showLeftIcon == false &&
- properties.showRightIcon == true {
- rightIcon.isHidden = false
-
- } else {
-
- }
- }
- }
-
- func refreshUI() {
- var fillColor = properties.propertyInfo.color_nor
- var borderWidth = properties.propertyInfo.borderWidth
- var borderColor = properties.propertyInfo.borderColor_nor
-
- var textColor = properties.propertyInfo.textColor
-
- var leftImage = properties.icon
- var rightImage = properties.icon
- if properties.state == .normal {
- if let image = properties.propertyInfo.leftIcon_nor {
- leftImage = image
- }
- if let image = properties.propertyInfo.rightIcon_nor {
- rightImage = image
- }
- } else if properties.state == .hover {
- fillColor = properties.propertyInfo.color_hov
- borderWidth = properties.propertyInfo.borderWidth_hov
- borderColor = properties.propertyInfo.borderColor_hov
- textColor = properties.propertyInfo.textColor_hov
-
- if let image = properties.propertyInfo.leftIcon_hov {
- leftImage = image
- }
- if let image = properties.propertyInfo.rightIcon_hov {
- rightImage = image
- }
- } else if properties.state == .pressed {
- fillColor = properties.propertyInfo.color_active
- borderWidth = properties.propertyInfo.borderWidth_active
- borderColor = properties.propertyInfo.borderColor_active
- textColor = properties.propertyInfo.textColor_Active
-
- if let image = properties.propertyInfo.leftIcon_press {
- leftImage = image
- }
- if let image = properties.propertyInfo.rightIcon_press {
- rightImage = image
- }
-
- }
- if properties.isDisabled == true {
- fillColor = properties.propertyInfo.color_dis
- borderWidth = properties.propertyInfo.borderWidth
- borderColor = properties.propertyInfo.borderColor_dis
- textColor = properties.propertyInfo.textColor_dis
-
- if let image = properties.propertyInfo.leftIcon_dis {
- leftImage = image
- } else {
- if let image = properties.propertyInfo.leftIcon_nor?.filled(with: textColor) {
- leftImage = image
- } else if let image = properties.icon?.filled(with: textColor) {
- leftImage = image
- }
- }
- if let image = properties.propertyInfo.rightIcon_dis {
- rightImage = image
- }
- }
-
- contendBox.fillColor = fillColor
- contendBox.borderColor = borderColor
- contendBox.cornerRadius = properties.propertyInfo.cornerRadius
- contendBox.borderWidth = borderWidth
-
- label.textColor = textColor
-
- label.font = properties.propertyInfo.textFont
-
- if let image = leftImage {
- leftIcon.image = image
- }
-
- if let image = rightImage {
- rightIcon.image = image
- }
-
- label.stringValue = properties.buttonText ?? ""
-
- // let paragraphStyle = NSMutableParagraphStyle()
- // paragraphStyle.maximumLineHeight = 24
- // if properties.size == .l {
- // paragraphStyle.maximumLineHeight = 20
- // } else if properties.size == .m {
- // paragraphStyle.maximumLineHeight = 18
- // } else if properties.size == .s {
- // paragraphStyle.maximumLineHeight = 18
- // }
- // let attributedString = NSAttributedString(string: self.label.stringValue,
- // attributes: [.paragraphStyle: paragraphStyle,
- // NSAttributedString.Key.font : label.font as Any,
- // NSAttributedString.Key.foregroundColor : label.textColor as Any,
- //
- // ])
- // label.attributedStringValue = attributedString
-
- leftImgWidthConst.constant = properties.propertyInfo.imageWidth
- rightImgWidthConst.constant = leftImgWidthConst.constant
-
- if properties.onlyIcon == true {
- properties.propertyInfo.viewWidth = properties.propertyInfo.viewHeight
- let viewWidth = properties.propertyInfo.viewWidth
- leftImgLeftConst.constant = (viewWidth - leftImgWidthConst.constant)/2
-
- } else {
- let viewWidth = frame.size.width
- let imageWidth = properties.propertyInfo.imageWidth
- let layoutGap = properties.propertyInfo.layout_gap
-
- label.sizeToFit()
- var labelRect = label.frame
- labelRect.origin.y = (CGRectGetHeight(frame) - labelRect.size.height)/2
-
- if properties.showLeftIcon == true && properties.showRightIcon == true {
- leftIcon.isHidden = false
- rightIcon.isHidden = false
-
- labelRect.origin.x = (viewWidth - labelRect.size.width - imageWidth*2 - layoutGap*2)/2.0 + imageWidth + layoutGap
- } else if properties.showLeftIcon == true && properties.showRightIcon == false {
- leftIcon.isHidden = false
-
- labelRect.origin.x = (viewWidth - labelRect.size.width - imageWidth - layoutGap)/2.0 + imageWidth + layoutGap
- } else if properties.showLeftIcon == false && properties.showRightIcon == true {
- rightIcon.isHidden = false
-
- labelRect.origin.x = (viewWidth - labelRect.size.width - imageWidth - layoutGap)/2.0
- } else {
- labelRect.origin.x = (viewWidth - labelRect.size.width)/2.0
- }
- label.frame = labelRect
- leftImgLeftConst.constant = CGRectGetMinX(labelRect) - leftImgWidthConst.constant - layoutGap
- rightImgRightConst.constant = frame.size.width - CGRectGetMaxX(labelRect) - leftImgWidthConst.constant - layoutGap
- }
- leftIcon.autoresizingMask = [.minXMargin, .maxXMargin, .minYMargin, .maxYMargin]
-
- }
-
- public func reloadData() {
- DispatchQueue.main.async {
-
- self.setupUI()
-
- self.refreshUI()
- }
- }
-
- //MARK: - Public Method
-
- public func setTarget(_ target: AnyObject?, action: Selector?) {
- self.target = target!
- self.action = action!
- }
-
- func sizeOfString(_ string: String, _ font: NSFont, _ width: CGFloat) -> (CGSize) {
-
- let paragraphStyle = NSMutableParagraphStyle()
- paragraphStyle.lineBreakMode = .byWordWrapping
-
- let attributes: [NSAttributedString.Key: Any] = [
- .font: font,
- .paragraphStyle:paragraphStyle
- ]
- let size = (string as NSString).boundingRect(with: NSSize(width: width, height: CGFloat(MAXFLOAT)),
- options: .usesLineFragmentOrigin,
- attributes: attributes,
- context: nil).size
- return size
- }
-
- //MARK: - MouseEvent
- public override func mouseEntered(with event: NSEvent) {
- super.mouseEntered(with: event)
-
- if properties.isDisabled == false && 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.isDisabled == false && properties.state != .pressed {
- properties.state = .normal
- }
-
- refreshUI()
- }
-
- public override func mouseDown(with event: NSEvent) {
-
- if properties.isDisabled == false &&
- properties.state != .pressed {
-
- properties.state = .pressed
-
- refreshUI()
- }
- }
-
- public override func mouseUp(with event: NSEvent) {
-
- if properties.isDisabled == false {
- if properties.keepPressState == false {
- properties.state = .hover
-
- refreshUI()
- }
- window?.makeFirstResponder(self)
- if let target = target, let action = action {
- _ = target.perform(action, with: self)
- }
- }
- }
-
- }
|