KMHomePopViewController.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. //
  2. // KMHomePopViewController.swift
  3. // PDF Master
  4. //
  5. // Created by wanjun on 2022/10/17.
  6. //
  7. import Cocoa
  8. typealias popCellViewDownCallback = (_ downEntered: Bool, _ count: String) -> Void
  9. class KMHomePopViewController: NSViewController {
  10. @IBOutlet weak var customBox: NSBox!
  11. @IBOutlet weak var customBoxWidthLayoutConstraint: NSLayoutConstraint!
  12. @IBOutlet weak var customBoxHeightLayoutConstraint: NSLayoutConstraint!
  13. var downCallback: popCellViewDownCallback?
  14. var popCellViewDownString: String?
  15. var popCellCount: Int?
  16. var dataArr: [String]?
  17. var KMHorizontalLine: String = "KMHorizontalLine"
  18. var background : NSColor = .clear // 背景颜色
  19. var background_hov : NSColor = .clear // 背景颜色
  20. var background_sel : NSColor = .clear // 背景颜色
  21. var background_disabled : NSColor = .clear // 背景颜色
  22. var cornerRadius : Float = 0.0 // 边框圆角
  23. var cornerRadius_hov : Float = 0.0 // 边框圆角
  24. var cornerRadius_sel : Float = 0.0 // 边框圆角
  25. var cornerRadius_disabled : Float = 0.0 // 边框圆角
  26. var lineHeight : CGFloat = 20.0 // 默认 内容行高
  27. var lineHeight_hov : CGFloat = 20.0 // 默认 内容行高
  28. var lineHeight_sel : CGFloat = 20.0 // 默认 内容行高
  29. var lineHeight_disabled : CGFloat = 20.0 // 默认 内容行高
  30. var font : NSFont = NSFont.systemFont(ofSize: 14.0) // 内容字体
  31. var font_hov : NSFont = NSFont.systemFont(ofSize: 14.0) // 内容字体
  32. var font_sel : NSFont = NSFont.systemFont(ofSize: 14.0) // 内容字体
  33. var font_disabled : NSFont = NSFont.systemFont(ofSize: 14.0) // 内容字体
  34. var _state: KMDesignTokenState = .Norm
  35. var enabled: Bool = true // 是否可点击
  36. var canHover: Bool = true // 是否可悬浮
  37. var disItems: [String] = []
  38. var selectedItems: [String] = []
  39. func initWithPopViewDataArr(_ popViewDataArr: [String]) -> Self {
  40. // self.dataArr = popViewDataArr.reverseObjectEnumerator().allObjects as NSArray
  41. self.dataArr = popViewDataArr.reversed()
  42. return self
  43. }
  44. override func viewDidLoad() {
  45. super.viewDidLoad()
  46. customBox.fillColor = NSColor(hex: "#FFFFFF")
  47. // self.updateUI()
  48. }
  49. override func viewDidAppear() {
  50. super.viewDidAppear()
  51. self.updateUI()
  52. }
  53. // MARK: Private
  54. func updateUI() {
  55. var widthMax: Float = 0;
  56. let subViews: [NSView] = self.customBox.contentView!.subviews
  57. for subView in subViews {
  58. subView.removeFromSuperview()
  59. }
  60. for string in self.dataArr ?? [] {
  61. if !(string as AnyObject).isEqual(to: KMHorizontalLine) {
  62. let width = self.cellContentAdaptiveWidth(string)
  63. if widthMax < width {
  64. widthMax = width
  65. }
  66. }
  67. }
  68. var formTopFloat: Float = 4.0
  69. // for i in (0..<dataArr!.count).reversed() {
  70. for string in dataArr?.reversed() ?? [] {
  71. if (string as AnyObject).isEqual(to: KMHorizontalLine) {
  72. self.createHonrizontalLineWithFrame(CGRect(x: 12.0, y: CGFloat(formTopFloat), width: CGFloat(widthMax)+23, height: 11))
  73. formTopFloat += 11
  74. } else {
  75. popCellViewDownString = string
  76. // self.createPopViewCellLabelWithFrame(CGRect(x: 4.0, y: CGFloat(formTopFloat), width: CGFloat(widthMax)+47, height: 32), stringValue: string)
  77. createPopViewCellLabelWithFrame(formTopFloat, stringValue: string)
  78. formTopFloat += 32;
  79. }
  80. }
  81. customBoxWidthLayoutConstraint.constant = CGFloat(widthMax+47)
  82. customBoxHeightLayoutConstraint.constant = CGFloat(formTopFloat+4.0)
  83. }
  84. func createHonrizontalLineWithFrame(_ frame: CGRect) {
  85. let box: NSBox = NSBox.init(frame: frame)
  86. box.boxType = .custom
  87. box.borderWidth = 0.0
  88. box.contentViewMargins = NSMakeSize(0, 0)
  89. customBox.contentView?.addSubview(box)
  90. let lineBox = NSBox.init(frame: CGRect(x: 0, y: 6, width: frame.width, height: 1))
  91. lineBox.boxType = .separator
  92. box.addSubview(lineBox)
  93. }
  94. // func createPopViewCellLabelWithFrame(_ frame: CGRect, stringValue: String) {
  95. // let box: KMBox = KMBox(frame: frame)
  96. // box.boxType = .custom
  97. // box.borderWidth = 0.0
  98. // box.contentViewMargins = NSMakeSize(0, 0)
  99. // customBox.contentView?.addSubview(box)
  100. //
  101. // let boxLabel: NSTextField = NSTextField.init()
  102. // boxLabel.isEditable = false
  103. // boxLabel.isBordered = false
  104. // boxLabel.stringValue = stringValue
  105. // boxLabel.font = NSFont.systemFont(ofSize: 14.0)
  106. // boxLabel.translatesAutoresizingMaskIntoConstraints = false
  107. // boxLabel.backgroundColor = NSColor.clear
  108. // boxLabel.textColor = NSColor.labelColor
  109. // box.contentView?.addSubview(boxLabel)
  110. //
  111. // box.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-21-[boxLabel]-21-|", metrics: nil, views:["boxLabel": boxLabel]))
  112. // box.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-1-[boxLabel]-1-|", metrics: nil, views:["boxLabel": boxLabel]))
  113. //
  114. // box.contentView?.addConstraint(NSLayoutConstraint(item: boxLabel, attribute:.centerY , relatedBy: .equal, toItem: box.contentView, attribute: .centerY, multiplier: 1, constant: 0))
  115. // box.moveCallback = {(mouseEntered: Bool, mouseBox: KMBox) -> Void in
  116. // if mouseEntered {
  117. // if #available(macOS 10.14, *) {
  118. // box.fillColor = NSColor.controlAccentColor
  119. // boxLabel.textColor = NSColor.white
  120. // } else {
  121. // box.fillColor = NSColor.init(red: 71/255.0, green: 126/255.0, blue: 222/255.0, alpha: 1.0)
  122. // boxLabel.textColor = NSColor.white
  123. // }
  124. // } else {
  125. // box.fillColor = NSColor.clear
  126. // boxLabel.textColor = NSColor.labelColor
  127. // }
  128. // }
  129. // box.downCallback = {(downEntered: Bool, mouseBox: KMBox) -> Void in
  130. // if downEntered {
  131. // if let callback = self.downCallback {
  132. // callback(true, stringValue)
  133. // }
  134. // }
  135. // }
  136. // }
  137. func createPopViewCellLabelWithFrame(_ mas_top: Float, stringValue: String) {
  138. var isDisabled = false
  139. if disItems.contains(stringValue) {
  140. isDisabled = true
  141. }
  142. var isSelected = false
  143. if (isDisabled == false && self.selectedItems.contains(stringValue)) {
  144. isSelected = true
  145. }
  146. let box: KMBox = KMBox(frame: NSZeroRect)
  147. box.boxType = .custom
  148. box.borderWidth = 0.0
  149. box.contentViewMargins = NSMakeSize(0, 0)
  150. box.cornerRadius = 4.0
  151. customBox.contentView?.addSubview(box)
  152. box.mas_makeConstraints { (make) in
  153. make?.left.equalTo()(4.0)
  154. make?.right.equalTo()(-4.0)
  155. make?.height.equalTo()(32.0)
  156. make?.top.equalTo()(customBox.mas_top)?.offset()(CGFloat(mas_top))
  157. }
  158. // let dropdownVC = KMDesignDropdown.init(nibName: "KMDesignDropdown", bundle: nil)
  159. // box.contentView = dropdownVC.view
  160. // dropdownVC.dropdown(bg: "dropdown.m.bg.norm", text: "dropdown.m.mac-text.def")
  161. // dropdownVC.dropdown(bg: "dropdown.m.bg.hov", text: "dropdown.m.mac-text.def", state: .Hov)
  162. // dropdownVC.dropdown(bg: "dropdown.m.bg.sel", text: "dropdown.m.mac-text.sel", state: .Sel)
  163. // dropdownVC.dropdown(bg: "dropdown.m.bg.dis", text: "dropdown.m.mac-text.dis", state: .Disabled)
  164. // dropdownVC.stringValue = str
  165. let boxLabel: NSTextField = NSTextField.init()
  166. boxLabel.isEditable = false
  167. boxLabel.isBordered = false
  168. boxLabel.stringValue = stringValue
  169. boxLabel.font = NSFont.systemFont(ofSize: 14.0)
  170. boxLabel.translatesAutoresizingMaskIntoConstraints = false
  171. boxLabel.backgroundColor = NSColor.clear
  172. boxLabel.textColor = NSColor(hex: "#252629")
  173. box.contentView?.addSubview(boxLabel)
  174. boxLabel.mas_makeConstraints { (make) in
  175. make?.centerY.equalTo()(0.0)
  176. make?.left.equalTo()(8.0)
  177. }
  178. let textTypography = KMDesignToken.shared.typography(withToken: "dropdown.m.mac-text.def")
  179. var fontFamily: String = textTypography.fontFamily
  180. let fontWeight: String = textTypography.fontWeight
  181. if fontFamily.contains(" ") {
  182. fontFamily = fontFamily.replacingOccurrences(of: " ", with: "")
  183. }
  184. if fontWeight != "" {
  185. fontFamily = String(format: "%@-%@", fontFamily, fontWeight)
  186. }
  187. boxLabel.font = NSFont(name: fontFamily, size: textTypography.fontSize.stringToCGFloat()) ?? NSFont.systemFont(ofSize: textTypography.fontSize.stringToCGFloat())
  188. let paragraphStyle = NSMutableParagraphStyle()
  189. paragraphStyle.lineSpacing = textTypography.lineHeight.stringToCGFloat()
  190. boxLabel.attributedStringValue = NSAttributedString(string: stringValue, attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle])
  191. box.moveCallback = {(mouseEntered: Bool, mouseBox: KMBox) -> Void in
  192. if !isDisabled {
  193. if isSelected { // 选中没有 hover 效果
  194. return
  195. }
  196. if mouseEntered {
  197. mouseBox.fillColor = KMDesignToken.shared.fill(withToken: "dropdown.m.bg.hov")
  198. } else {
  199. mouseBox.fillColor = NSColor.clear
  200. }
  201. }
  202. }
  203. box.downCallback = {(downEntered, mouseBox, event) -> Void in
  204. if !isDisabled {
  205. if downEntered {
  206. mouseBox.fillColor = KMDesignToken.shared.fill(withToken: "dropdown.m.bg.sel")
  207. boxLabel.textColor = KMDesignToken.shared.fill(withToken: "dropdown.m.mac-text.sel")
  208. if let callback = self.downCallback {
  209. callback(true, stringValue)
  210. }
  211. } else {
  212. mouseBox.fillColor = KMDesignToken.shared.fill(withToken: "dropdown.m.bg.norm")
  213. boxLabel.textColor = KMDesignToken.shared.fill(withToken: "dropdown.m.mac-text.def")
  214. }
  215. }
  216. }
  217. if isDisabled {
  218. box.fillColor = KMDesignToken.shared.fill(withToken: "dropdown.m.bg.dis")
  219. boxLabel.textColor = KMDesignToken.shared.fill(withToken: "dropdown.m.mac-text.dis")
  220. } else if (isSelected) {
  221. box.fillColor = KMDesignToken.shared.fill(withToken: "dropdown.m.bg.sel")
  222. boxLabel.textColor = KMDesignToken.shared.fill(withToken: "dropdown.m.mac-text.sel")
  223. }
  224. }
  225. func cellContentAdaptiveWidth(_ content: String) -> Float {
  226. if content.isEmpty {
  227. return 0
  228. }
  229. let attributes = [NSAttributedString.Key.font : NSFont.systemFont(ofSize: 14.0)]
  230. let rect : CGRect = content.boundingRect(with: CGSize(width: 0, height: 18),options: .usesLineFragmentOrigin, attributes: attributes,context:nil)
  231. return Float(rect.size.width)
  232. }
  233. func changePopViewCellData(_ count: Int, content: String) {
  234. let boxArray: Array<NSView> = customBox.contentView!.subviews
  235. for subView in boxArray {
  236. subView.removeFromSuperview()
  237. }
  238. var dataMutableArr: [String] = Array((dataArr?.reversed())!)
  239. dataMutableArr[count] = content
  240. dataArr = Array(dataMutableArr.reversed())
  241. self.updateUI()
  242. }
  243. }