KMDesignPropertySelector.swift 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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: KMMoveBox!
  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. self.propertySelector(bg: "property-selector.icon-btn.bg.norm")
  55. self.propertySelector(bg: "property-selector.icon-btn.bg.hov", state: .Hov)
  56. self.propertySelector(bg: "property-selector.icon-btn.bg.sel", state: .Sel)
  57. } else if (propertySelectorType == .Color_Icon_Btn) {
  58. self.mainBox.contentView = colorIconView
  59. self.iconBox.cornerRadius = iconBoxWidth_spacing.constant/2
  60. self.iconBox.borderWidth = 1.0
  61. self.iconBox.borderColor = NSColor.km_init(hex: "#000000", alpha: 0.1)
  62. self.propertySelector(bg: "property-selector.icon-btn.bg.norm")
  63. self.propertySelector(bg: "property-selector.icon-btn.bg.hov", state: .Hov)
  64. self.propertySelector(bg: "property-selector.icon-btn.bg.sel", state: .Sel)
  65. }
  66. mainBox.move = { [weak self](mouseEntered: Bool) -> Void in
  67. if mouseEntered {
  68. if self!.state != .Sel && self!.canHover && (self!.state != .Disabled) {
  69. self!.state = .Hov
  70. }
  71. } else {
  72. if self!.state != .Sel && self!.canHover && (self!.state != .Disabled) {
  73. self!.state = .Norm
  74. }
  75. }
  76. }
  77. }
  78. // MARK: Get、Set
  79. var state: KMDesignTokenState {
  80. get {
  81. return _state
  82. }
  83. set {
  84. _state = newValue
  85. updateUI()
  86. }
  87. }
  88. var fillColor: NSColor {
  89. get {
  90. return _fillColor
  91. }
  92. set {
  93. _fillColor = newValue
  94. self.iconBox.fillColor = _fillColor
  95. if _fillColor == NSColor.clear {
  96. self.iconBox.isClear = true
  97. } else {
  98. self.iconBox.isClear = false
  99. }
  100. }
  101. }
  102. var image: NSImage {
  103. get {
  104. if (_image == nil) {
  105. _image = NSImage(named: "KMFileIcon")!
  106. }
  107. return _image
  108. }
  109. set {
  110. _image = newValue
  111. icon.image = _image
  112. }
  113. }
  114. var image_hov: NSImage {
  115. get {
  116. if (_image_hov == nil) {
  117. _image_hov = image
  118. }
  119. return _image_hov!
  120. }
  121. set {
  122. _image_hov = newValue
  123. icon.image = _image_hov
  124. }
  125. }
  126. var image_sel: NSImage {
  127. get {
  128. if (_image_sel == nil) {
  129. _image_sel = image
  130. }
  131. return _image_sel!
  132. }
  133. set {
  134. _image_sel = newValue
  135. icon.image = _image_sel
  136. }
  137. }
  138. var action: Selector {
  139. get {
  140. return _action!
  141. }
  142. set {
  143. _action = newValue
  144. if _action != nil {
  145. button.action = _action
  146. }
  147. }
  148. }
  149. var target: AnyObject {
  150. get {
  151. return _target!
  152. }
  153. set {
  154. _target = newValue
  155. if _target != nil {
  156. button.target = _target
  157. }
  158. }
  159. }
  160. // MARK: Private Methods
  161. func updateUI() -> Void {
  162. if (state == .Norm) {
  163. self.mainBox.fillColor = self.background
  164. self.mainBox.borderColor = self.borderColor
  165. self.mainBox.borderWidth = CGFloat(self.borderWidth)
  166. self.icon.image = self.image
  167. } else if (state == .Hov) {
  168. self.mainBox.fillColor = self.background_hov
  169. self.mainBox.borderColor = self.borderColor_hov
  170. self.mainBox.borderWidth = CGFloat(self.borderWidth_hov)
  171. self.mainBox.cornerRadius = CGFloat(self.cornerRadius_hov)
  172. self.icon.image = self.image_hov
  173. } else if (state == .Sel) {
  174. self.mainBox.fillColor = self.background_sel
  175. self.mainBox.borderWidth = CGFloat(self.borderWidth_sel)
  176. self.mainBox.cornerRadius = CGFloat(self.cornerRadius_hov)
  177. self.mainBox.borderColor = self.borderColor_sel
  178. self.icon.image = self.image_sel
  179. } else if (state == .Disabled) {
  180. self.mainBox.fillColor = self.background_disabled
  181. self.mainBox.borderWidth = 0.0
  182. self.mainBox.cornerRadius = CGFloat(self.cornerRadius_disabled)
  183. self.mainBox.borderColor = self.borderColor_disabled
  184. self.icon.image = self._image_disabled
  185. }
  186. }
  187. }