KMBGTemplateItem.swift 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. //
  2. // KMBGTemplateItem.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/11/7.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. @objc protocol KMBGTemplateItemDelegate: AnyObject {
  10. @objc optional func kmNBGTemplateItemDidEdit(_ view: KMBGTemplateItem)
  11. @objc optional func kmNBGTemplateItemDidDelete(_ view: KMBGTemplateItem)
  12. }
  13. class KMBGTemplateItem: NSCollectionViewItem {
  14. @IBOutlet var watermarkItem: ComponentCardWatermark!
  15. var backgroundModel: KMBackgroundModel?
  16. weak open var delegate: KMBGTemplateItemDelegate?
  17. var watermarkProperty: ComponentCardWMProperty = ComponentCardWMProperty(state: .normal, icon: nil, text: "Sample Text", editIcon: NSImage(named: "watermark_edit"), deleteIcon: NSImage(named: "pageEdit_delete"))
  18. override func viewDidLoad() {
  19. super.viewDidLoad()
  20. // Do view setup here.
  21. watermarkItem.properties = watermarkProperty
  22. watermarkItem.delegate = self
  23. }
  24. //Setter
  25. override var isSelected: Bool {
  26. get{
  27. return super.isSelected
  28. }
  29. set{
  30. super.isSelected = newValue
  31. reloadData()
  32. }
  33. }
  34. func reloadData() {
  35. watermarkItem.properties.icon = backgroundModel?.modelPreviewImage()
  36. watermarkItem.properties.state = isSelected ? .pressed : .normal
  37. watermarkItem.properties.text = backgroundModel?.name
  38. watermarkItem.reloadData()
  39. }
  40. }
  41. //MARK: - ComponentCardWatermarkDelegate
  42. extension KMBGTemplateItem: ComponentCardWatermarkDelegate {
  43. func componentSliderDidEdit(_ view: ComponentCardWatermark) {
  44. delegate?.kmNBGTemplateItemDidEdit?(self)
  45. }
  46. func componentSliderDidDelete(_ view: ComponentCardWatermark) {
  47. delegate?.kmNBGTemplateItemDidDelete?(self)
  48. }
  49. }