|
@@ -8,6 +8,12 @@
|
|
|
import Cocoa
|
|
|
import AppKit
|
|
|
|
|
|
+@objc public protocol ComponentDropdownToolDelegate: AnyObject {
|
|
|
+
|
|
|
+ @objc optional func componentDropdownToolDidClicked(_ view: ComponentDropdownTool, menuItem: ComponentMenuitemProperty?)
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
public class ComponentDropdownTool: ComponentBaseXibView {
|
|
|
|
|
|
@IBOutlet var contendBox: NSBox!
|
|
@@ -15,11 +21,12 @@ public class ComponentDropdownTool: ComponentBaseXibView {
|
|
|
@IBOutlet var dropImage: NSImageView!
|
|
|
@IBOutlet var titleLabel: NSTextField!
|
|
|
|
|
|
- private var _properties : ComponentDropdownToolProperties = ComponentDropdownToolProperties()
|
|
|
-
|
|
|
- private var action: Selector? // 点击事件
|
|
|
- private weak var target: AnyObject? // 对象目标
|
|
|
-
|
|
|
+ private var _properties : ComponentDropdownToolProperty = ComponentDropdownToolProperty()
|
|
|
+
|
|
|
+ private var groupView: ComponentGroup!
|
|
|
+
|
|
|
+ weak open var delegate: ComponentDropdownToolDelegate?
|
|
|
+
|
|
|
public override func draw(_ dirtyRect: NSRect) {
|
|
|
super.draw(dirtyRect)
|
|
|
|
|
@@ -47,7 +54,7 @@ public class ComponentDropdownTool: ComponentBaseXibView {
|
|
|
}
|
|
|
|
|
|
//MARK: - Setter and Getter
|
|
|
- public var properties : ComponentDropdownToolProperties {
|
|
|
+ public var properties : ComponentDropdownToolProperty {
|
|
|
get {
|
|
|
return _properties
|
|
|
}
|
|
@@ -56,98 +63,99 @@ public class ComponentDropdownTool: ComponentBaseXibView {
|
|
|
|
|
|
ComponentLibrary.shared.configDropdownToolComponent(properties: _properties)
|
|
|
|
|
|
- self.setupUI()
|
|
|
+ setupUI()
|
|
|
|
|
|
- self.refreshUI()
|
|
|
+ refreshUI()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
//MARK: - SetupUI
|
|
|
func setupUI() {
|
|
|
-
|
|
|
- if let image = self.properties.leftIcon {
|
|
|
- self.iconImage.image = image
|
|
|
- self.iconImage.isHidden = false
|
|
|
+
|
|
|
+ if let image = properties.leftIcon {
|
|
|
+ iconImage.image = image
|
|
|
+ iconImage.isHidden = false
|
|
|
} else {
|
|
|
- self.iconImage.isHidden = true
|
|
|
+ iconImage.isHidden = true
|
|
|
}
|
|
|
|
|
|
- if self.properties.showDropdown == true {
|
|
|
- self.dropImage.isHidden = false
|
|
|
+ if properties.showDropdown == true {
|
|
|
+ dropImage.isHidden = false
|
|
|
} else {
|
|
|
- self.dropImage.isHidden = true
|
|
|
+ dropImage.isHidden = true
|
|
|
}
|
|
|
|
|
|
- self.titleLabel.stringValue = self.properties.text
|
|
|
-
|
|
|
- self.titleLabel.sizeToFit()
|
|
|
- var labelRect = self.titleLabel.frame
|
|
|
- if self.iconImage.isHidden {
|
|
|
- labelRect.origin.x = 4
|
|
|
+ if let text = properties.text {
|
|
|
+ titleLabel.stringValue = text
|
|
|
+ titleLabel.isHidden = false
|
|
|
+
|
|
|
+ titleLabel.sizeToFit()
|
|
|
+ var labelRect = titleLabel.frame
|
|
|
+ if iconImage.isHidden {
|
|
|
+ labelRect.origin.x = 4
|
|
|
+ } else {
|
|
|
+ labelRect.origin.x = 28
|
|
|
+ }
|
|
|
+ labelRect.origin.y = CGRectGetHeight(frame)/2 - CGRectGetHeight(labelRect)/2
|
|
|
+ titleLabel.frame = labelRect
|
|
|
+
|
|
|
} else {
|
|
|
- labelRect.origin.x = 28
|
|
|
+ titleLabel.isHidden = true
|
|
|
}
|
|
|
- labelRect.origin.y = CGRectGetHeight(self.frame)/2 - CGRectGetHeight(labelRect)/2
|
|
|
- self.titleLabel.frame = labelRect
|
|
|
|
|
|
}
|
|
|
|
|
|
func refreshUI() {
|
|
|
|
|
|
- self.contendBox.cornerRadius = self.properties.propertyInfo.cornerRadius
|
|
|
- self.contendBox.borderWidth = self.properties.propertyInfo.borderWidth
|
|
|
- self.contendBox.borderColor = self.properties.propertyInfo.borderColor_nor
|
|
|
+ contendBox.cornerRadius = properties.propertyInfo.cornerRadius
|
|
|
+ contendBox.borderWidth = properties.propertyInfo.borderWidth
|
|
|
+ contendBox.borderColor = properties.propertyInfo.borderColor_nor
|
|
|
|
|
|
var fillColor: NSColor?
|
|
|
var textColor: NSColor?
|
|
|
|
|
|
- if self.properties.state == .normal {
|
|
|
- fillColor = self.properties.propertyInfo.color_nor
|
|
|
- textColor = self.properties.propertyInfo.textColor
|
|
|
- } else if self.properties.state == .hover {
|
|
|
- fillColor = self.properties.propertyInfo.color_hov
|
|
|
- textColor = self.properties.propertyInfo.textColor_hov
|
|
|
- } else if self.properties.state == .pressed {
|
|
|
- fillColor = self.properties.propertyInfo.color_active
|
|
|
- textColor = self.properties.propertyInfo.textColor_Active
|
|
|
+ if properties.state == .normal {
|
|
|
+ fillColor = properties.propertyInfo.color_nor
|
|
|
+ textColor = properties.propertyInfo.textColor
|
|
|
+ } else if properties.state == .hover {
|
|
|
+ fillColor = properties.propertyInfo.color_hov
|
|
|
+ textColor = properties.propertyInfo.textColor_hov
|
|
|
+ } else if properties.state == .pressed {
|
|
|
+ fillColor = properties.propertyInfo.color_active
|
|
|
+ textColor = properties.propertyInfo.textColor_Active
|
|
|
}
|
|
|
- if self.properties.isDisabled == true {
|
|
|
- fillColor = self.properties.propertyInfo.color_dis
|
|
|
- textColor = self.properties.propertyInfo.textColor_dis
|
|
|
+ if properties.isDisabled == true {
|
|
|
+ fillColor = properties.propertyInfo.color_dis
|
|
|
+ textColor = properties.propertyInfo.textColor_dis
|
|
|
}
|
|
|
|
|
|
if let color = fillColor {
|
|
|
- self.contendBox.fillColor = color
|
|
|
+ contendBox.fillColor = color
|
|
|
}
|
|
|
if let color = textColor {
|
|
|
- self.titleLabel.textColor = color
|
|
|
+ titleLabel.textColor = color
|
|
|
}
|
|
|
- self.titleLabel.font = properties.propertyInfo.textFont
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+ titleLabel.font = properties.propertyInfo.textFont
|
|
|
+
|
|
|
}
|
|
|
|
|
|
//MARK: - Public Method
|
|
|
public func reloadData() {
|
|
|
- self.refreshUI()
|
|
|
+ setupUI()
|
|
|
+
|
|
|
+ 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 self.properties.state != .pressed {
|
|
|
- self.properties.state = .hover
|
|
|
+ if properties.state != .pressed {
|
|
|
+ properties.state = .hover
|
|
|
}
|
|
|
|
|
|
- self.refreshUI()
|
|
|
+ refreshUI()
|
|
|
}
|
|
|
|
|
|
public override func mouseMoved(with event: NSEvent) {
|
|
@@ -158,34 +166,67 @@ public class ComponentDropdownTool: ComponentBaseXibView {
|
|
|
public override func mouseExited(with event: NSEvent) {
|
|
|
super.mouseExited(with: event)
|
|
|
|
|
|
- if self.properties.state != .pressed {
|
|
|
- self.properties.state = .normal
|
|
|
+ if properties.state != .pressed {
|
|
|
+ properties.state = .normal
|
|
|
}
|
|
|
|
|
|
- self.refreshUI()
|
|
|
+ refreshUI()
|
|
|
}
|
|
|
|
|
|
public override func mouseDown(with event: NSEvent) {
|
|
|
super.mouseDown(with: event)
|
|
|
|
|
|
- if self.properties.state != .pressed {
|
|
|
- self.properties.state = .pressed
|
|
|
+ if properties.state != .pressed {
|
|
|
+ properties.state = .pressed
|
|
|
} else {
|
|
|
- self.properties.state = .hover
|
|
|
+ properties.state = .hover
|
|
|
}
|
|
|
- self.refreshUI()
|
|
|
+ refreshUI()
|
|
|
|
|
|
}
|
|
|
|
|
|
public override func mouseUp(with event: NSEvent) {
|
|
|
super.mouseUp(with: event)
|
|
|
|
|
|
- if self.properties.isDisabled == false {
|
|
|
- if let target = target, let action = action {
|
|
|
- _ = target.perform(action, with: self)
|
|
|
+ guard let items = properties.menuItemArr else {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if groupView == nil {
|
|
|
+ groupView = ComponentGroup.createFromNib(in: ComponentLibrary.shared.componentBundle())
|
|
|
+ }
|
|
|
+ var viewHeight: CGFloat = 8
|
|
|
+ for item in items {
|
|
|
+ if item.type == .normal {
|
|
|
+ viewHeight += 36
|
|
|
+ } else if item.type == .divider {
|
|
|
+ viewHeight += 8
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ var point = convert(contendBox.frame.origin, to: window?.contentView)
|
|
|
+ point.y -= viewHeight
|
|
|
+ point.y -= 4
|
|
|
+
|
|
|
+ groupView.groupDelegate = self
|
|
|
+ groupView?.frame = CGRectMake(0, 0, 232, viewHeight)
|
|
|
+ groupView.updateGroupInfo(items)
|
|
|
+ groupView.showWithPoint(point, relativeTo: contendBox)
|
|
|
+
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+//MARK: - ComponentGroupDelegate
|
|
|
+extension ComponentDropdownTool: ComponentGroupDelegate {
|
|
|
+ public func componentGroupDidDismiss(group: ComponentGroup?) {
|
|
|
+ properties.state = .normal
|
|
|
+
|
|
|
+ refreshUI()
|
|
|
+ }
|
|
|
+
|
|
|
+ public func componentGroupDidSelect(group: ComponentGroup?, menuItemProperty: ComponentMenuitemProperty?) {
|
|
|
+ delegate?.componentDropdownToolDidClicked?(self, menuItem: menuItemProperty)
|
|
|
+ }
|
|
|
+}
|