KMTextImageButtonVC.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. //
  2. // KMTextImageButtonVC.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by wanjun on 2023/2/3.
  6. //
  7. import Cocoa
  8. @objc enum KMTextImageButtonType : Int {
  9. case None = 0
  10. case Up
  11. case Left
  12. case Down
  13. case Right
  14. }
  15. typealias moveCallback = (_ mouseEntered: Bool) -> Void
  16. class KMMoveBox: NSBox {
  17. var move: moveCallback?
  18. override func viewDidMoveToWindow() {
  19. super.viewDidMoveToWindow()
  20. if self.window != nil {
  21. self.addTrackingRect(self.bounds, owner: self, userData: nil, assumeInside: false)
  22. }
  23. }
  24. override func setFrameSize(_ newSize: NSSize) {
  25. super.setFrameSize(newSize)
  26. self.addTrackingRect(NSMakeRect(0, 0, newSize.width, newSize.height), owner: self, userData: nil, assumeInside: false)
  27. }
  28. override func mouseEntered(with event: NSEvent) {
  29. if let callback = move {
  30. callback(true)
  31. }
  32. }
  33. override func mouseExited(with event: NSEvent) {
  34. if let callback = move {
  35. callback(false)
  36. }
  37. }
  38. }
  39. class KMTextImageButtonVC: NSViewController {
  40. @IBOutlet weak var mainBox: KMMoveBox!
  41. @IBOutlet weak var upView: NSView!
  42. @IBOutlet weak var leftView: NSView!
  43. @IBOutlet weak var downView: NSView!
  44. @IBOutlet weak var rightView: NSView!
  45. @IBOutlet weak var upImageView: NSImageView!
  46. @IBOutlet weak var leftImageView: NSImageView!
  47. @IBOutlet weak var downImageView: NSImageView!
  48. @IBOutlet weak var rightImageView: NSImageView!
  49. @IBOutlet weak var upTextfield: NSTextField!
  50. @IBOutlet weak var leftTextfield: NSTextField!
  51. @IBOutlet weak var downTextfield: NSTextField!
  52. @IBOutlet weak var rightTextfield: NSTextField!
  53. @IBOutlet weak var button: NSButton!
  54. @IBOutlet weak var upPadding_spacing: NSLayoutConstraint!
  55. @IBOutlet weak var upImageHeight: NSLayoutConstraint!
  56. @IBOutlet weak var upImageWidth: NSLayoutConstraint!
  57. @IBOutlet weak var upItem_spacing: NSLayoutConstraint!
  58. @IBOutlet weak var leftPadding_spacing: NSLayoutConstraint!
  59. @IBOutlet weak var leftImageHeight: NSLayoutConstraint!
  60. @IBOutlet weak var leftImageWidth: NSLayoutConstraint!
  61. @IBOutlet weak var leftItem_spacing: NSLayoutConstraint!
  62. @IBOutlet weak var downPadding_spacing: NSLayoutConstraint!
  63. @IBOutlet weak var downImageHeight: NSLayoutConstraint!
  64. @IBOutlet weak var downImageWidth: NSLayoutConstraint!
  65. @IBOutlet weak var downItem_spacing: NSLayoutConstraint!
  66. @IBOutlet weak var rightPadding_spacing: NSLayoutConstraint!
  67. @IBOutlet weak var rightImageHeight: NSLayoutConstraint!
  68. @IBOutlet weak var rightImageWidth: NSLayoutConstraint!
  69. @IBOutlet weak var rightItem_spacing: NSLayoutConstraint!
  70. var image: NSImage = NSImage(named: "KMRadioButtonUnSelect")! // 默认 imageView 图片
  71. var image_hover: NSImage = NSImage(named: "KMRadioButtonUnSelect")! // 悬浮 imageView 图片
  72. var image_click: NSImage = NSImage(named: "KMRadioButtonUnSelect")! // 选中 imageView 图片
  73. var stringValue: String = ""// 内容
  74. var textColor: NSColor = .black // 默认内容颜色
  75. var textColor_hover: NSColor = .black // 悬浮内容颜色
  76. var textColor_click: NSColor = .black // 选中内容颜色
  77. var font: NSFont = NSFont.systemFont(ofSize: 14.0) // 默认 内容字体
  78. var lineHeight: CGFloat = 20.0 // 默认 内容行高
  79. var lineHeight_hover: CGFloat = 20.0 // 悬浮 内容行高
  80. var lineHeight_click: CGFloat = 20.0 // 选中 内容行高
  81. var background: NSColor = .clear// 默认背景颜色
  82. var background_hover: NSColor = .clear// 悬浮背景颜色
  83. var background_click: NSColor = .clear// 选中背景颜色
  84. var borderWidth: Float = 1.0// 默认 边框宽度
  85. var borderWidth_hover: Float = 1.0// 悬浮 边框宽度
  86. var borderWidth_click: Float = 1.0// 选中 边框宽度
  87. var borderColor: NSColor = .clear// 边框颜色
  88. var borderColor_hover: NSColor = .clear// 悬浮边框颜色
  89. var borderColor_click: NSColor = .clear// 选中边框颜色
  90. var cornerRadius: Float = 0.0// 默认 边框圆角
  91. var cornerRadius_hover: Float = 0.0// 悬浮 边框圆角
  92. var cornerRadius_click: Float = 0.0// 选中 边框圆角
  93. var imageWidth: Float = 20.0// 图片宽度
  94. var imageHeight: Float = 20.0// 图片高度
  95. var paddingSpacing: Float = 16.0
  96. var itemSpacing: Float = 8.0
  97. var action: Selector? // 点击事件
  98. var target: AnyObject? // 对象目标
  99. var enabled: Bool = true // 是否可点击
  100. var editable: Bool = false //是否允许编辑
  101. var canHover: Bool = true // 是否可悬浮
  102. var type: KMTextImageButtonType = .Left
  103. var state: KMDesignTokenState = .Norm
  104. override func viewDidLoad() {
  105. super.viewDidLoad()
  106. // Do view setup here.
  107. updateUI()
  108. mainBox.move = { [weak self](mouseEntered: Bool) -> Void in
  109. if self != nil {
  110. if mouseEntered {
  111. if self!.state != .Sel && self!.canHover {
  112. self!.state = .Hov
  113. self!.updateUI()
  114. }
  115. } else {
  116. if self!.state != .Sel && self!.canHover {
  117. self!.state = .Norm
  118. self!.updateUI()
  119. }
  120. }
  121. }
  122. }
  123. }
  124. func updateUI() -> Void {
  125. let paragraphStyle = NSMutableParagraphStyle()
  126. if type == .Up {
  127. mainBox.contentView = upView
  128. upTextfield.isEditable = editable
  129. upTextfield.attributedStringValue = NSAttributedString(string: stringValue, attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
  130. upTextfield.font = font
  131. } else if type == .Left {
  132. mainBox.contentView = leftView
  133. leftTextfield.isEditable = editable
  134. leftTextfield.attributedStringValue = NSAttributedString(string: stringValue, attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
  135. leftTextfield.font = font
  136. } else if type == .Down {
  137. mainBox.contentView = downView
  138. downTextfield.isEditable = editable
  139. downTextfield.attributedStringValue = NSAttributedString(string: stringValue, attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
  140. downTextfield.font = font
  141. } else if type == .Right {
  142. mainBox.contentView = rightView
  143. rightTextfield.isEditable = editable
  144. rightTextfield.attributedStringValue = NSAttributedString(string: stringValue, attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
  145. rightTextfield.font = font
  146. }
  147. button.isEnabled = enabled
  148. if state == .Norm {
  149. mainBox.fillColor = background
  150. mainBox.borderColor = borderColor
  151. if type == .Up {
  152. upImageView.image = image
  153. upTextfield.textColor = textColor
  154. } else if type == .Left {
  155. leftImageView.image = image
  156. leftTextfield.textColor = textColor
  157. } else if type == .Down {
  158. downImageView.image = image
  159. downTextfield.textColor = textColor
  160. } else if type == .Right {
  161. rightImageView.image = image
  162. rightTextfield.textColor = textColor
  163. }
  164. mainBox.borderWidth = CGFloat(borderWidth)
  165. mainBox.cornerRadius = CGFloat(cornerRadius)
  166. paragraphStyle.lineSpacing = lineHeight
  167. } else if state == .Hov {
  168. mainBox.fillColor = background_hover
  169. mainBox.borderColor = borderColor_hover
  170. if type == .Up {
  171. upImageView.image = image_hover
  172. upTextfield.textColor = textColor_hover
  173. } else if type == .Left {
  174. leftImageView.image = image_hover
  175. leftTextfield.textColor = textColor_hover
  176. } else if type == .Down {
  177. downImageView.image = image_hover
  178. downTextfield.textColor = textColor_hover
  179. } else if type == .Right {
  180. rightImageView.image = image_hover
  181. rightTextfield.textColor = textColor_hover
  182. }
  183. mainBox.borderWidth = CGFloat(borderWidth_hover)
  184. mainBox.cornerRadius = CGFloat(cornerRadius_hover)
  185. paragraphStyle.lineSpacing = lineHeight_hover
  186. } else if state == .Sel {
  187. mainBox.fillColor = background_click
  188. mainBox.borderColor = borderColor_click
  189. if type == .Up {
  190. upImageView.image = image_click
  191. upTextfield.textColor = textColor_click
  192. } else if type == .Left {
  193. leftImageView.image = image_click
  194. leftTextfield.textColor = textColor_click
  195. } else if type == .Down {
  196. downImageView.image = image_click
  197. downTextfield.textColor = textColor_click
  198. } else if type == .Right {
  199. rightImageView.image = image_click
  200. rightTextfield.textColor = textColor_click
  201. }
  202. mainBox.borderWidth = CGFloat(borderWidth_click)
  203. mainBox.cornerRadius = CGFloat(cornerRadius_click)
  204. paragraphStyle.lineSpacing = lineHeight_click
  205. }
  206. button.target = target
  207. button.action = action
  208. upImageHeight.constant = CGFloat(imageHeight)
  209. upImageWidth.constant = CGFloat(imageWidth)
  210. leftImageHeight.constant = CGFloat(imageHeight)
  211. leftImageWidth.constant = CGFloat(imageWidth)
  212. downImageHeight.constant = CGFloat(imageHeight)
  213. downImageWidth.constant = CGFloat(imageWidth)
  214. rightImageHeight.constant = CGFloat(imageHeight)
  215. rightImageWidth.constant = CGFloat(imageWidth)
  216. if type == .Up {
  217. upPadding_spacing.constant = CGFloat(paddingSpacing)
  218. upItem_spacing.constant = CGFloat(itemSpacing)
  219. } else if type == .Left {
  220. leftPadding_spacing.constant = CGFloat(paddingSpacing)
  221. leftItem_spacing.constant = CGFloat(itemSpacing)
  222. } else if type == .Down {
  223. downPadding_spacing.constant = CGFloat(paddingSpacing)
  224. downItem_spacing.constant = CGFloat(itemSpacing)
  225. } else if type == .Right {
  226. rightPadding_spacing.constant = CGFloat(paddingSpacing)
  227. rightItem_spacing.constant = CGFloat(itemSpacing)
  228. }
  229. }
  230. // MARK: Private Methods
  231. }