KMBatesTemplateItem.swift 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. //
  2. // KMBatesTemplateItem.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/11/8.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. @objc protocol KMBatesTemplateItemDelegate: AnyObject {
  10. @objc optional func kmBatesTemplateItemDidEdit(_ view: KMBatesTemplateItem)
  11. @objc optional func kmBatesTemplateItemDidDelete(_ view: KMBatesTemplateItem)
  12. }
  13. class KMBatesTemplateItem: NSCollectionViewItem {
  14. @IBOutlet var watermarkItem: ComponentCardWatermark!
  15. @IBOutlet var infoContendView: NSView!
  16. @IBOutlet var titleLabel: NSTextField!
  17. var dataModel: KMBatesModel?
  18. weak open var delegate: KMBatesTemplateItemDelegate?
  19. var watermarkProperty: ComponentCardWMProperty = ComponentCardWMProperty(state: .normal, icon: nil, text: "", editIcon: NSImage(named: "watermark_edit"), deleteIcon: NSImage(named: "pageEdit_delete"))
  20. override func viewDidLoad() {
  21. super.viewDidLoad()
  22. // Do view setup here.
  23. view.wantsLayer = true
  24. view.layer?.backgroundColor = ComponentLibrary.shared.getComponentColorFromKey("colorFill/hight").cgColor
  25. view.layer?.borderWidth = 1
  26. view.layer?.borderColor = ComponentLibrary.shared.getComponentColorFromKey("colorBorder/divider").cgColor
  27. if let value = ComponentLibrary.shared.getComponentValueFromKey("radius/s") {
  28. let currentValue = value as! CGFloat
  29. view.layer?.cornerRadius = currentValue
  30. }
  31. watermarkItem.properties = watermarkProperty
  32. watermarkItem.delegate = self
  33. titleLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  34. titleLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  35. }
  36. //Setter
  37. override var isSelected: Bool {
  38. get{
  39. return super.isSelected
  40. }
  41. set{
  42. super.isSelected = newValue
  43. refresh()
  44. }
  45. }
  46. private func refresh() {
  47. guard let dataModel = dataModel else { return }
  48. watermarkItem.properties.state = isSelected ? .pressed : .normal
  49. watermarkItem.reloadData()
  50. }
  51. func reloadData() {
  52. guard let dataModel = dataModel else { return }
  53. watermarkItem.properties.state = isSelected ? .pressed : .normal
  54. watermarkItem.reloadData()
  55. titleLabel.stringValue = dataModel.name
  56. infoContendView.wantsLayer = true
  57. infoContendView.layer?.masksToBounds = true
  58. let subviews = infoContendView.subviews
  59. for view in subviews {
  60. view.removeFromSuperview()
  61. }
  62. var yValue = CGRectGetHeight(infoContendView.frame)
  63. if dataModel.topLeftString.count > 0 {
  64. yValue -= 24
  65. let view = getPositionView(0, dataModel.topLeftString)
  66. view.frame = CGRectMake(0, yValue, CGRectGetWidth(infoContendView.frame), 24)
  67. view.autoresizingMask = [.width, .minYMargin]
  68. infoContendView.addSubview(view)
  69. yValue -= 8
  70. }
  71. if dataModel.topCenterString.count > 0 {
  72. yValue -= 24
  73. let view = getPositionView(1, dataModel.topCenterString)
  74. view.frame = CGRectMake(0, yValue, CGRectGetWidth(infoContendView.frame), 24)
  75. view.autoresizingMask = [.width, .minYMargin]
  76. infoContendView.addSubview(view)
  77. yValue -= 8
  78. }
  79. if dataModel.topRightString.count > 0 {
  80. yValue -= 24
  81. let view = getPositionView(2, dataModel.topRightString)
  82. view.frame = CGRectMake(0, yValue, CGRectGetWidth(infoContendView.frame), 24)
  83. view.autoresizingMask = [.width, .minYMargin]
  84. infoContendView.addSubview(view)
  85. yValue -= 8
  86. }
  87. if dataModel.bottomLeftString.count > 0 {
  88. yValue -= 24
  89. let view = getPositionView(3, dataModel.bottomLeftString)
  90. view.frame = CGRectMake(0, yValue, CGRectGetWidth(infoContendView.frame), 24)
  91. view.autoresizingMask = [.width, .minYMargin]
  92. infoContendView.addSubview(view)
  93. yValue -= 8
  94. }
  95. if dataModel.bottomCenterString.count > 0 {
  96. yValue -= 24
  97. let view = getPositionView(4, dataModel.bottomCenterString)
  98. view.frame = CGRectMake(0, yValue, CGRectGetWidth(infoContendView.frame), 24)
  99. view.autoresizingMask = [.width, .minYMargin]
  100. infoContendView.addSubview(view)
  101. yValue -= 8
  102. }
  103. if dataModel.bottomRightString.count > 0 {
  104. yValue -= 24
  105. let view = getPositionView(5, dataModel.bottomRightString)
  106. view.frame = CGRectMake(0, yValue, CGRectGetWidth(infoContendView.frame), 24)
  107. view.autoresizingMask = [.width, .minYMargin]
  108. infoContendView.addSubview(view)
  109. yValue -= 8
  110. }
  111. }
  112. func getPositionView(_ type: Int, _ text: String) -> NSView {
  113. let view = NSView(frame: CGRectMake(0, 0, 200, 24))
  114. let label = NSTextField(frame: CGRectMake(56, 0, 40, 20))
  115. label.stringValue = text
  116. label.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/1")
  117. label.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-regular")
  118. label.sizeToFit()
  119. label.frame = CGRectMake(56, (24-label.frame.size.height)/2, label.frame.size.width, label.frame.size.height)
  120. label.autoresizingMask = [.maxXMargin, .minYMargin]
  121. label.isBordered = false
  122. label.drawsBackground = false
  123. label.isEditable = false
  124. view.addSubview(label)
  125. let position = ComponentCPosition()
  126. position.frame = CGRectMake(0, 0, 48, 24)
  127. position.properties = ComponentCPositionProperty(rowCount: 2, columnCount: 3, dash: true, isDisable: true)
  128. if type == 0 {
  129. position.properties.selRow = 1
  130. position.properties.selColumn = 0
  131. } else if type == 1 {
  132. position.properties.selRow = 1
  133. position.properties.selColumn = 1
  134. } else if type == 2 {
  135. position.properties.selRow = 1
  136. position.properties.selColumn = 2
  137. } else if type == 3 {
  138. position.properties.selRow = 0
  139. position.properties.selColumn = 0
  140. } else if type == 4 {
  141. position.properties.selRow = 0
  142. position.properties.selColumn = 1
  143. } else if type == 5 {
  144. position.properties.selRow = 0
  145. position.properties.selColumn = 2
  146. }
  147. position.reloadData()
  148. view.addSubview(position)
  149. return view
  150. }
  151. }
  152. //MARK: - ComponentCardWatermarkDelegate
  153. extension KMBatesTemplateItem: ComponentCardWatermarkDelegate {
  154. func componentSliderDidEdit(_ view: ComponentCardWatermark) {
  155. delegate?.kmBatesTemplateItemDidEdit?(self)
  156. }
  157. func componentSliderDidDelete(_ view: ComponentCardWatermark) {
  158. delegate?.kmBatesTemplateItemDidDelete?(self)
  159. }
  160. }