KMDesignPropertySelector.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //
  2. // KMDesignPropertySelector.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2023/2/23.
  6. //
  7. import Cocoa
  8. @objc enum PropertySelectorType : Int {
  9. case Icon_Btn = 0
  10. case Color_Icon_Btn
  11. case Line_Style
  12. }
  13. class KMDesignPropertyColorBox: NSBox {
  14. var isClear: Bool = false
  15. override func draw(_ dirtyRect: NSRect) {
  16. super.draw(dirtyRect)
  17. if isClear {
  18. let path = NSBezierPath()
  19. let startPoint = NSPoint(x: 3, y: 3)
  20. path.move(to: startPoint)
  21. // Set the end point of the path
  22. let endPoint = NSPoint(x: super.frame.width-3, y: super.frame.height-3)
  23. path.line(to: endPoint)
  24. // Draw the path
  25. NSColor.km_init(hex: "#F3465B").setStroke()
  26. path.lineWidth = 1
  27. path.stroke()
  28. }
  29. }
  30. }
  31. class KMDesignPropertySelector: KMDesignBase {
  32. @IBOutlet weak var mainBox: NSBox!
  33. @IBOutlet weak var button: NSButton!
  34. @IBOutlet weak var iconView: NSView!
  35. @IBOutlet weak var icon: NSImageView!
  36. @IBOutlet weak var colorIconView: NSView!
  37. @IBOutlet weak var iconBox: KMDesignPropertyColorBox!
  38. @IBOutlet weak var iconBoxHeight_spacing: NSLayoutConstraint!
  39. @IBOutlet weak var iconBoxWidth_spacing: NSLayoutConstraint!
  40. var propertySelectorType: PropertySelectorType = .Icon_Btn
  41. var _fillColor: NSColor = .clear
  42. init(withType type: PropertySelectorType) {
  43. super.init(nibName: "KMDesignPropertySelector", bundle: nil)
  44. self.propertySelectorType = type
  45. }
  46. required init?(coder: NSCoder) {
  47. fatalError("init(coder:) has not been implemented")
  48. }
  49. override func viewDidLoad() {
  50. super.viewDidLoad()
  51. // Do view setup here.
  52. if (propertySelectorType == .Icon_Btn) {
  53. self.mainBox.contentView = iconView
  54. } else if (propertySelectorType == .Color_Icon_Btn) {
  55. self.mainBox.contentView = colorIconView
  56. self.iconBox.cornerRadius = iconBoxWidth_spacing.constant/2
  57. self.iconBox.borderWidth = 1.0
  58. self.iconBox.borderColor = NSColor.km_init(hex: "#000000", alpha: 0.1)
  59. }
  60. }
  61. // MARK: Get、Set
  62. var state: KMDesignTokenState = .Norm
  63. var fillColor: NSColor {
  64. get {
  65. return _fillColor
  66. }
  67. set {
  68. _fillColor = newValue
  69. self.iconBox.fillColor = _fillColor
  70. if _fillColor == NSColor.clear {
  71. self.iconBox.isClear = true
  72. } else {
  73. self.iconBox.isClear = false
  74. }
  75. }
  76. }
  77. var image: NSImage {
  78. get {
  79. if (_image == nil) {
  80. _image = NSImage(named: "KMFileIcon")!
  81. }
  82. return _image
  83. }
  84. set {
  85. _image = newValue
  86. icon.image = _image
  87. }
  88. }
  89. var image_hov: NSImage {
  90. get {
  91. if (_image_hov == nil) {
  92. _image_hov = image
  93. }
  94. return _image_hov!
  95. }
  96. set {
  97. _image_hov = newValue
  98. icon.image = _image_hov
  99. }
  100. }
  101. var image_sel: NSImage {
  102. get {
  103. if (_image_sel == nil) {
  104. _image_sel = image
  105. }
  106. return _image_sel!
  107. }
  108. set {
  109. _image_sel = newValue
  110. icon.image = _image_sel
  111. }
  112. }
  113. var action: Selector {
  114. get {
  115. return _action!
  116. }
  117. set {
  118. _action = newValue
  119. if _action != nil {
  120. button.action = _action
  121. }
  122. }
  123. }
  124. var target: AnyObject {
  125. get {
  126. return _target!
  127. }
  128. set {
  129. _target = newValue
  130. if _target != nil {
  131. button.target = _target
  132. }
  133. }
  134. }
  135. // MARK: Private Methods
  136. func updateUI() -> Void {
  137. if (state == .Norm) {
  138. self.mainBox.fillColor = self.background
  139. self.mainBox.borderColor = self.borderColor
  140. self.mainBox.borderWidth = CGFloat(self.borderWidth)
  141. self.icon.image = self.image
  142. } else if (state == .Hov) {
  143. self.mainBox.fillColor = self.background_hov
  144. self.mainBox.borderColor = self.borderColor_hov
  145. self.mainBox.borderWidth = CGFloat(self.borderWidth_hov)
  146. self.mainBox.cornerRadius = CGFloat(self.cornerRadius_hov)
  147. self.icon.image = self.image_hov
  148. } else if (state == .Sel) {
  149. self.mainBox.fillColor = self.background_sel
  150. self.mainBox.borderWidth = CGFloat(self.borderWidth_sel)
  151. self.mainBox.cornerRadius = CGFloat(self.cornerRadius_hov)
  152. self.mainBox.borderColor = self.borderColor_sel
  153. self.icon.image = self.image_sel
  154. } else if (state == .Disabled) {
  155. self.mainBox.fillColor = self.background_disabled
  156. self.mainBox.borderWidth = 0.0
  157. self.mainBox.cornerRadius = CGFloat(self.cornerRadius_disabled)
  158. self.mainBox.borderColor = self.borderColor_disabled
  159. self.icon.image = self._image_disabled
  160. }
  161. }
  162. }