KMBGTemplateItem.swift 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. @objc optional func kmNBGTemplateItemDidUpdateSelectedState(_ view: KMBGTemplateItem)
  13. }
  14. class KMBGTemplateItem: NSCollectionViewItem {
  15. @IBOutlet var watermarkItem: ComponentCardWatermark!
  16. var backgroundModel: KMBackgroundModel?
  17. var itemSelected: Bool = false
  18. weak open var delegate: KMBGTemplateItemDelegate?
  19. var watermarkProperty: ComponentCardWMProperty = ComponentCardWMProperty(state: .normal, icon: nil, text: "Sample Text", editIcon: NSImage(named: "watermark_edit"), deleteIcon: NSImage(named: "pageEdit_delete"))
  20. override func viewDidLoad() {
  21. super.viewDidLoad()
  22. // Do view setup here.
  23. watermarkItem.properties = watermarkProperty
  24. watermarkItem.delegate = self
  25. // watermarkItem.setTarget(self, action: #selector(itemClicked(_:)))
  26. }
  27. //Setter
  28. override var isSelected: Bool {
  29. get{
  30. return super.isSelected
  31. }
  32. set{
  33. super.isSelected = newValue
  34. reloadData()
  35. }
  36. }
  37. func reloadData() {
  38. watermarkItem.properties.icon = backgroundModel?.modelPreviewImage()
  39. watermarkItem.properties.state = isSelected ? .pressed : .normal
  40. watermarkItem.properties.text = backgroundModel?.name
  41. watermarkItem.reloadData()
  42. }
  43. @objc func itemClicked(_ item: ComponentCardWatermark) {
  44. itemSelected = !itemSelected
  45. reloadData()
  46. delegate?.kmNBGTemplateItemDidUpdateSelectedState?(self)
  47. }
  48. }
  49. //MARK: - ComponentCardWatermarkDelegate
  50. extension KMBGTemplateItem: ComponentCardWatermarkDelegate {
  51. func componentSliderDidEdit(_ view: ComponentCardWatermark) {
  52. delegate?.kmNBGTemplateItemDidEdit?(self)
  53. }
  54. func componentSliderDidDelete(_ view: ComponentCardWatermark) {
  55. delegate?.kmNBGTemplateItemDidDelete?(self)
  56. }
  57. }