123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- //
- // KMDesignBaseView.swift
- // PDF Master
- //
- // Created by tangchao on 2023/6/7.
- //
- import Cocoa
- @objc enum KMDesignSize: Int {
- case large = 0 // 大
- case medium = 1 // 中
- case small = 2 // 小
- }
- typealias KMDesignTokenKey = String
- let KMDesignTokenNone: KMDesignTokenKey = ""
- private let KMHorizDividerTokenKey: KMDesignTokenKey = "divider.horiz"
- private let KMVertDividerTokenKey: KMDesignTokenKey = "divider.vert"
- // 组件 基类
- class KMDesignBaseView: NSView {
- public static let horDividerToken = KMHorizDividerTokenKey
- public static let verDividerToken = KMVertDividerTokenKey
-
- var height: CGFloat = 0.0
- var horizontalPadding: CGFloat = 0.0
- var borderColor: NSColor = .clear
- var borderRadius: CGFloat = 4.0
- var borderWidth: CGFloat = 1.0
- var fillColor: NSColor = .clear
-
- var textColor: NSColor = .black
- var fontName: String = "SFProText-Regular"
- var fontSize: CGFloat = 0.0
-
- var state: KMDesignTokenState = .Norm {
- didSet {
- self.updateUI(state: self.state)
- }
- }
- var kmEnabled = true {
- didSet {
- if (self.kmEnabled) {
- self.state = .Norm
- } else {
- self.state = .Disabled
- }
- }
- }
-
- override init(frame frameRect: NSRect) {
- super.init(frame: frameRect)
-
- self.initSubViews()
- self.initDefaultValue()
- }
-
- required init?(coder: NSCoder) {
- super.init(coder: coder)
-
- self.initSubViews()
- self.initDefaultValue()
- }
-
- func initSubViews() {}
- func initDefaultValue() {}
-
- func find_bgToken(state: KMDesignTokenState) -> KMDesignTokenKey { return KMDesignTokenNone }
- func find_textToken(state: KMDesignTokenState) -> KMDesignTokenKey { return KMDesignTokenNone }
- func find_spaceToken(state: KMDesignTokenState) -> KMDesignTokenKey { return KMDesignTokenNone }
-
- func updateUI(state: KMDesignTokenState) {
- let bgToken = self.find_bgToken(state: state)
- if (bgToken.isEmpty) {
- KMPrint("bgToken invalid.")
- } else {
- let model = KMDesignToken.shared.tokenUsesModel(withToken: bgToken)
- if let data = model.border as? String {
- self.borderColor = NSColor.km_init(hex: data)
- }
- if let data = model.borderRadius as? String, let _value = Float(data) {
- self.borderRadius = CGFloat(_value)
- }
- if let data = model.borderWidth as? String, let _value = Float(data) {
- self.borderWidth = CGFloat(_value)
- }
- if let data = model.fill as? String {
- self.fillColor = NSColor.km_init(hex: data)
- }
- }
-
- let textToken = self.find_textToken(state: state)
- if (!textToken.isEmpty) {
- let model = KMDesignToken.shared.tokenUsesModel(withToken: textToken)
- if let data = model.fill as? String {
- self.textColor = NSColor.km_init(hex: data)
- }
-
- let tModel = KMDesignToken.shared.typography(withToken: textToken)
- if let _fontSize = Float(tModel.fontSize) {
- self.fontSize = CGFloat(_fontSize)
- }
- var fontName: String = ""
- fontName.append(tModel.fontFamily.isEmpty ? "SFProText" : tModel.fontFamily)
- fontName.append("-")
- fontName.append(tModel.fontWeight.isEmpty ? "Regular" : tModel.fontWeight)
- // 去掉多余的空格
- fontName = fontName.replacingOccurrences(of: " ", with: "")
- self.fontName = fontName
- }
-
- }
-
- func canHover() -> Bool {
- if (self.state == .Norm || self.state == .Hov || self.state == .None) {
- return true
- }
- return false
- }
-
- // MARK: - divider
-
- func horDividerHeight() -> CGFloat {
- let height = KMDesignToken.shared.height(withToken: KMHorizDividerTokenKey)
- if let _height = Float(height) {
- return CGFloat(_height)
- }
- return 0
- }
- func horDividerColor() -> NSColor {
- return KMDesignToken.shared.fill(withToken: KMHorizDividerTokenKey)
- }
-
- func verDividerWidth() -> CGFloat {
- let width = KMDesignToken.shared.width(withToken: KMVertDividerTokenKey)
- if let _width = Float(width) {
- return CGFloat(_width)
- }
- return 0
- }
- func verDividerColor() -> NSColor {
- return KMDesignToken.shared.fill(withToken: KMVertDividerTokenKey)
- }
- }
|