KMDesignBaseView.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //
  2. // KMDesignBaseView.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2023/6/7.
  6. //
  7. import Cocoa
  8. @objc enum KMDesignSize: Int {
  9. case large = 0 // 大
  10. case medium = 1 // 中
  11. case small = 2 // 小
  12. }
  13. typealias KMDesignTokenKey = String
  14. let KMDesignTokenNone: KMDesignTokenKey = ""
  15. private let KMHorizDividerTokenKey: KMDesignTokenKey = "divider.horiz"
  16. private let KMVertDividerTokenKey: KMDesignTokenKey = "divider.vert"
  17. // 组件 基类
  18. class KMDesignBaseView: NSView {
  19. public static let horDividerToken = KMHorizDividerTokenKey
  20. public static let verDividerToken = KMVertDividerTokenKey
  21. var height: CGFloat = 0.0
  22. var horizontalPadding: CGFloat = 0.0
  23. var borderColor: NSColor = .clear
  24. var borderRadius: CGFloat = 4.0
  25. var borderWidth: CGFloat = 1.0
  26. var fillColor: NSColor = .clear
  27. var textColor: NSColor = .black
  28. var fontName: String = "SFProText-Regular"
  29. var fontSize: CGFloat = 0.0
  30. var state: KMDesignTokenState = .Norm {
  31. didSet {
  32. self.updateUI(state: self.state)
  33. }
  34. }
  35. var kmEnabled = true {
  36. didSet {
  37. if (self.kmEnabled) {
  38. self.state = .Norm
  39. } else {
  40. self.state = .Disabled
  41. }
  42. }
  43. }
  44. override init(frame frameRect: NSRect) {
  45. super.init(frame: frameRect)
  46. self.initSubViews()
  47. self.initDefaultValue()
  48. }
  49. required init?(coder: NSCoder) {
  50. super.init(coder: coder)
  51. self.initSubViews()
  52. self.initDefaultValue()
  53. }
  54. func initSubViews() {}
  55. func initDefaultValue() {}
  56. func find_bgToken(state: KMDesignTokenState) -> KMDesignTokenKey { return KMDesignTokenNone }
  57. func find_textToken(state: KMDesignTokenState) -> KMDesignTokenKey { return KMDesignTokenNone }
  58. func find_spaceToken(state: KMDesignTokenState) -> KMDesignTokenKey { return KMDesignTokenNone }
  59. func updateUI(state: KMDesignTokenState) {
  60. let bgToken = self.find_bgToken(state: state)
  61. if (bgToken.isEmpty) {
  62. KMPrint("bgToken invalid.")
  63. } else {
  64. let model = KMDesignToken.shared.tokenUsesModel(withToken: bgToken)
  65. if let data = model.border as? String {
  66. self.borderColor = NSColor.km_init(hex: data)
  67. }
  68. if let data = model.borderRadius as? String, let _value = Float(data) {
  69. self.borderRadius = CGFloat(_value)
  70. }
  71. if let data = model.borderWidth as? String, let _value = Float(data) {
  72. self.borderWidth = CGFloat(_value)
  73. }
  74. if let data = model.fill as? String {
  75. self.fillColor = NSColor.km_init(hex: data)
  76. }
  77. }
  78. let textToken = self.find_textToken(state: state)
  79. if (!textToken.isEmpty) {
  80. let model = KMDesignToken.shared.tokenUsesModel(withToken: textToken)
  81. if let data = model.fill as? String {
  82. self.textColor = NSColor.km_init(hex: data)
  83. }
  84. let tModel = KMDesignToken.shared.typography(withToken: textToken)
  85. if let _fontSize = Float(tModel.fontSize) {
  86. self.fontSize = CGFloat(_fontSize)
  87. }
  88. var fontName: String = ""
  89. fontName.append(tModel.fontFamily.isEmpty ? "SFProText" : tModel.fontFamily)
  90. fontName.append("-")
  91. fontName.append(tModel.fontWeight.isEmpty ? "Regular" : tModel.fontWeight)
  92. // 去掉多余的空格
  93. fontName = fontName.replacingOccurrences(of: " ", with: "")
  94. self.fontName = fontName
  95. }
  96. }
  97. func canHover() -> Bool {
  98. if (self.state == .Norm || self.state == .Hov || self.state == .None) {
  99. return true
  100. }
  101. return false
  102. }
  103. // MARK: - divider
  104. func horDividerHeight() -> CGFloat {
  105. let height = KMDesignToken.shared.height(withToken: KMHorizDividerTokenKey)
  106. if let _height = Float(height) {
  107. return CGFloat(_height)
  108. }
  109. return 0
  110. }
  111. func horDividerColor() -> NSColor {
  112. return KMDesignToken.shared.fill(withToken: KMHorizDividerTokenKey)
  113. }
  114. func verDividerWidth() -> CGFloat {
  115. let width = KMDesignToken.shared.width(withToken: KMVertDividerTokenKey)
  116. if let _width = Float(width) {
  117. return CGFloat(_width)
  118. }
  119. return 0
  120. }
  121. func verDividerColor() -> NSColor {
  122. return KMDesignToken.shared.fill(withToken: KMVertDividerTokenKey)
  123. }
  124. }