KMDesignDropdown.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // KMDesignDropdown.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2023/2/20.
  6. //
  7. import Cocoa
  8. class KMDesignDropdown: NSViewController {
  9. @IBOutlet weak var mainBox: KMMoveBox!
  10. @IBOutlet weak var button : NSButton!
  11. @IBOutlet weak var label : NSTextField!
  12. @IBOutlet weak var dropdownView : NSView!
  13. @IBOutlet weak var horizontalPadding: NSLayoutConstraint!
  14. var height: Float = 30.0
  15. var horizontalPadding_spacing: Float = 8.0
  16. var textColor: NSColor = .black // 内容颜色
  17. var textColor_hov: NSColor = .black // 内容颜色
  18. var textColor_sel: NSColor = .black // 内容颜色
  19. var textColor_disabled: NSColor = .black // 内容颜色
  20. var background: NSColor = .clear// 背景颜色
  21. var background_hov: NSColor = .clear// 背景颜色
  22. var background_sel: NSColor = .clear// 背景颜色
  23. var background_disabled: NSColor = .clear// 背景颜色
  24. var cornerRadius: Float = 0.0// 边框圆角
  25. var cornerRadius_hov: Float = 0.0// 边框圆角
  26. var cornerRadius_sel: Float = 0.0// 边框圆角
  27. var cornerRadius_disabled: Float = 0.0// 边框圆角
  28. var lineHeight: CGFloat = 20.0 // 默认 内容行高
  29. var lineHeight_hov: CGFloat = 20.0 // 默认 内容行高
  30. var lineHeight_sel: CGFloat = 20.0 // 默认 内容行高
  31. var lineHeight_disabled: CGFloat = 20.0 // 默认 内容行高
  32. var font: NSFont = NSFont.systemFont(ofSize: 14.0) // 内容字体
  33. var font_hov: NSFont = NSFont.systemFont(ofSize: 14.0) // 内容字体
  34. var font_sel: NSFont = NSFont.systemFont(ofSize: 14.0) // 内容字体
  35. var font_disabled: NSFont = NSFont.systemFont(ofSize: 14.0) // 内容字体
  36. var stringValue: String = ""// 内容
  37. var toolTip: String = "" // 提示文字
  38. // button 通用属性
  39. var action: Selector? // 点击事件
  40. var target: AnyObject? // 对象目标
  41. var enabled: Bool = true // 是否可点击
  42. var state: KMDesignTokenState = .Norm
  43. var canHover: Bool = true // 是否可悬浮
  44. var isHidden: Bool = false // 是否隐藏
  45. var editable: Bool = false //是否允许编辑
  46. override func viewDidLoad() {
  47. super.viewDidLoad()
  48. // Do view setup here.
  49. mainBox.contentView = dropdownView
  50. mainBox.move = { [weak self](mouseEntered: Bool) -> Void in
  51. if self != nil {
  52. if mouseEntered {
  53. if self!.state != .Sel && self!.canHover && (self!.state != .Disabled) {
  54. self!.state = .Hov
  55. self!.updateUI()
  56. }
  57. } else {
  58. if self!.state != .Sel && self!.canHover && (self!.state != .Disabled) {
  59. self!.state = .Norm
  60. self!.updateUI()
  61. }
  62. }
  63. }
  64. }
  65. }
  66. // MARK: Private Methods
  67. func updateUI() -> Void {
  68. let paragraphStyle = NSMutableParagraphStyle()
  69. if (state == .Norm) {
  70. mainBox.fillColor = background
  71. mainBox.cornerRadius = CGFloat(cornerRadius)
  72. label.textColor = textColor
  73. label.font = font
  74. paragraphStyle.lineSpacing = lineHeight
  75. } else if (state == .Hov) {
  76. mainBox.fillColor = background_hov
  77. mainBox.cornerRadius = CGFloat(cornerRadius_hov)
  78. label.textColor = textColor_hov
  79. label.font = font_hov
  80. paragraphStyle.lineSpacing = lineHeight_hov
  81. } else if (state == .Sel) {
  82. mainBox.fillColor = background_sel
  83. mainBox.cornerRadius = CGFloat(cornerRadius_sel)
  84. label.textColor = textColor_sel
  85. label.font = font_sel
  86. paragraphStyle.lineSpacing = lineHeight_sel
  87. } else if (state == .Disabled) {
  88. mainBox.fillColor = background_disabled
  89. mainBox.cornerRadius = CGFloat(cornerRadius_disabled)
  90. label.textColor = textColor_disabled
  91. label.font = font_disabled
  92. paragraphStyle.lineSpacing = lineHeight_disabled
  93. }
  94. if action != nil {
  95. button.action = action
  96. button.target = target
  97. }
  98. button.isEnabled = enabled
  99. label.attributedStringValue = NSAttributedString(string: stringValue, attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
  100. if toolTip != "" {
  101. button.toolTip = toolTip
  102. }
  103. self.view.isHidden = isHidden
  104. horizontalPadding.constant = CGFloat(horizontalPadding_spacing)
  105. }
  106. }