ComponentAlertProperty.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //
  2. // ComponentAlertProperty.swift
  3. // KMComponentLibrary
  4. //
  5. // Created by Niehaoyu on 2024/9/12.
  6. //
  7. import Foundation
  8. import AppKit
  9. public class ComponentAlertProperty: NSObject {
  10. public var messageType: ComponentMessageType = .info
  11. public var title: String? //传nil时表示不显示Title
  12. public var subTitle: String? //副文本信息
  13. public var iconImage: NSImage?
  14. public var detailButtonString: String? //传nil时表示不显示
  15. public var cancelButtonString: String? //传nil时表示不显示
  16. public var propertyInfo = AlertPropertyInfo()
  17. public init(messageType: ComponentMessageType = .info,
  18. title: String? = nil,
  19. subTitle: String? = nil,
  20. iconImage: NSImage?
  21. detailButtonString: String? = nil,
  22. cancelButtonString: String? = nil) {
  23. self.messageType = messageType
  24. self.title = title
  25. self.subTitle = subTitle
  26. self.iconImage = iconImage
  27. self.detailButtonString = detailButtonString
  28. self.cancelButtonString = cancelButtonString
  29. }
  30. }
  31. public class AlertPropertyInfo: ComponentPropertyInfo {
  32. var lineBorderColor: NSColor?
  33. var subTextFont: NSFont = NSFont.labelFont(ofSize: 14)
  34. //MARK: - Function
  35. override init() {
  36. super.init()
  37. }
  38. }
  39. //MARK: - ComponentLibrary
  40. extension ComponentLibrary {
  41. func configAlertComponent(properties: ComponentAlertProperty) -> Void {
  42. var colorKey = "colorAlert/bg-info"
  43. var lineBorderColorKey = "colorPrimary/border1"
  44. if properties.messageType == .success {
  45. colorKey = "colorAlert/bg-success"
  46. lineBorderColorKey = "colorSuccess/border1"
  47. } else if properties.messageType == .warning {
  48. colorKey = "colorAlert/bg-warning"
  49. lineBorderColorKey = "colorWarning/border1"
  50. } else if properties.messageType == .error {
  51. colorKey = "colorAlert/bg-error"
  52. lineBorderColorKey = "colorError/border1"
  53. }
  54. if let value = ComponentLibrary.shared.getComponentValueFromKey(colorKey) {
  55. let currentValue = value as! [String : Any]
  56. properties.propertyInfo.color_nor = ComponentLibrary.shared.getColor(rgbaDict: currentValue)
  57. } else {
  58. properties.propertyInfo.color_nor = NSColor.clear
  59. }
  60. properties.propertyInfo.borderWidth = 0
  61. properties.propertyInfo.cornerRadius = 0
  62. if let value = ComponentLibrary.shared.getComponentValueFromKey(lineBorderColorKey) {
  63. let currentValue = value as! [String : Any]
  64. properties.propertyInfo.lineBorderColor = ComponentLibrary.shared.getColor(rgbaDict: currentValue)
  65. } else {
  66. properties.propertyInfo.lineBorderColor = NSColor.clear
  67. }
  68. //Text
  69. if let value = ComponentLibrary.shared.getComponentValueFromKey("colorText/1") {
  70. let currentValue = value as! [String : Any]
  71. properties.propertyInfo.textColor = ComponentLibrary.shared.getColor(rgbaDict: currentValue)
  72. } else {
  73. properties.propertyInfo.textColor = NSColor.clear
  74. }
  75. if let value = ComponentLibrary.shared.getComponentValueFromKey("mac/body-m-medium") {
  76. let currentValue = value as! [String : Any]
  77. if let fontSize = currentValue["fontSize"] as? Float,
  78. let fontFamily = currentValue["fontFamily"] as? String,
  79. let fontWeight = currentValue["fontWeight"] as? String {
  80. let attributeFontDescriptor = NSFontDescriptor(fontAttributes: [NSFontDescriptor.AttributeName.family: fontFamily, NSFontDescriptor.AttributeName.face: fontWeight])
  81. if let newFont = NSFont(descriptor: attributeFontDescriptor, size: CGFloat(fontSize)) {
  82. properties.propertyInfo.textFont = newFont
  83. }
  84. }
  85. }
  86. if let value = ComponentLibrary.shared.getComponentValueFromKey("mac/body-s-regular") {
  87. let currentValue = value as! [String : Any]
  88. if let fontSize = currentValue["fontSize"] as? Float,
  89. let fontFamily = currentValue["fontFamily"] as? String,
  90. let fontWeight = currentValue["fontWeight"] as? String {
  91. let attributeFontDescriptor = NSFontDescriptor(fontAttributes: [NSFontDescriptor.AttributeName.family: fontFamily, NSFontDescriptor.AttributeName.face: fontWeight])
  92. if let newFont = NSFont(descriptor: attributeFontDescriptor, size: CGFloat(fontSize)) {
  93. properties.propertyInfo.subTextFont = newFont
  94. }
  95. }
  96. }
  97. }
  98. }