KMWatermarkCollectionViewItem.swift 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //
  2. // KMWatermarkCollectionViewItem.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/11/3.
  6. //
  7. import Cocoa
  8. class KMWatermarkCollectionViewItem: NSCollectionViewItem {
  9. var waterMarkOprateCallback: ((KMWatermarkOperateType, KMWatermarkModel?) -> Void)?
  10. var backgroundOperateCallback: ((KMBackgroundOperateType, KMBackgroundModel?) -> Void)?
  11. @IBOutlet var topBaseView: NSView!
  12. @IBOutlet var optionView: NSView!
  13. @IBOutlet var editButton: KMCoverButton!
  14. @IBOutlet var deleteButton: KMCoverButton!
  15. @IBOutlet var watermarkNameLabel: NSTextField!
  16. @IBOutlet var watermarkImageView: NSImageView!
  17. @IBOutlet var labelContainerView: NSView!
  18. private var _waterMark: KMWatermarkModel?
  19. private var _background: KMBackgroundModel?
  20. deinit {
  21. KMPrint("KMWatermarkCollectionViewItem deinit.")
  22. }
  23. override func viewDidLoad() {
  24. super.viewDidLoad()
  25. // Do view setup here.
  26. self._configuUI()
  27. self._addTrackingArea()
  28. }
  29. override var isSelected: Bool {
  30. didSet {
  31. if (self.isSelected) {
  32. self.imageView?.layer?.borderColor = KMAppearance.Interactive.a0Color().cgColor
  33. self.labelContainerView.layer?.backgroundColor = KMAppearance.Interactive.a0Color().cgColor
  34. self.watermarkNameLabel.textColor = KMAppearance.Layout.w0Color()
  35. self.topBaseView.layer?.borderColor = KMAppearance.Interactive.a0Color().cgColor
  36. } else {
  37. self.imageView?.layer?.borderColor = KMAppearance.Layout.h2Color().cgColor
  38. self.labelContainerView.layer?.backgroundColor = .clear
  39. self.watermarkNameLabel.textColor = KMAppearance.Layout.h0Color()
  40. self.topBaseView.layer?.borderColor = KMAppearance.Layout.h2Color().cgColor
  41. }
  42. }
  43. }
  44. func updateInterface(_ waterMark: KMWatermarkModel) {
  45. self._waterMark = waterMark;
  46. if waterMark.isFront {
  47. self.watermarkImageView.image = NSImage(named: "KMImageNameUXIconVipWatermarkImg")
  48. self.imageView!.image = KMWatermarkModel.drawImageAtpageRect(rect: self.watermarkImageView.bounds, data: waterMark) // KMWatermarkManager.drawImageAtpageRect:self.watermarkImageView.bounds watermarkDatas:waterMark];
  49. } else {
  50. self.imageView?.image = NSImage(named: "KMImageNameUXIconVipWatermarkImg")
  51. self.watermarkImageView.image = KMWatermarkModel.drawImageAtpageRect(rect: self.watermarkImageView.bounds, data: waterMark)//[KMPDFWatermarkObject drawImageAtpageRect:self.watermarkImageView.bounds watermarkDatas:waterMark];
  52. }
  53. self.watermarkNameLabel.stringValue = waterMark.watermarkID
  54. }
  55. func updateBackgroundInterface(_ background: KMBackgroundModel) {
  56. self._background = background
  57. if (background.type == .color) {
  58. self.watermarkImageView.image = nil
  59. self.imageView?.image = nil
  60. self.watermarkImageView.wantsLayer = true
  61. self.watermarkImageView.layer?.backgroundColor = background.color?.cgColor
  62. } else {
  63. self.watermarkImageView.image = background.image
  64. self.watermarkImageView.layer?.backgroundColor = NSColor.clear.cgColor
  65. }
  66. self.watermarkNameLabel.stringValue = background.backgroundID
  67. }
  68. //编辑水印
  69. @IBAction @objc func buttonClicked_EditWatermark(_ sender: AnyObject?) {
  70. if (self.waterMarkOprateCallback != nil) {
  71. self.waterMarkOprateCallback!(.Edit, self._waterMark)
  72. }
  73. if (self.backgroundOperateCallback != nil) {
  74. self.backgroundOperateCallback!(.Edit, self._background)
  75. }
  76. }
  77. //删除水印
  78. @IBAction @objc func buttonClicked_DeleteWatermark(_ sender: AnyObject?) {
  79. if (self.waterMarkOprateCallback != nil) {
  80. self.waterMarkOprateCallback!(.Delete, self._waterMark)
  81. }
  82. if (self.backgroundOperateCallback != nil) {
  83. self.backgroundOperateCallback!(.Delete, self._background)
  84. }
  85. }
  86. override func mouseEntered(with event: NSEvent) {
  87. if let data = self.view.window?.isKeyWindow, data {
  88. self.optionView.isHidden = false
  89. self.topBaseView.layer?.borderColor = KMAppearance.Interactive.a0Color().cgColor
  90. }
  91. }
  92. override func mouseExited(with event: NSEvent) {
  93. if let data = self.view.window?.isKeyWindow, data {
  94. self.optionView.isHidden = true
  95. if(self.isSelected) {
  96. self.topBaseView.layer?.borderColor = KMAppearance.Interactive.a0Color().cgColor
  97. } else {
  98. self.topBaseView.layer?.borderColor = KMAppearance.Layout.h2Color().cgColor
  99. }
  100. }
  101. }
  102. }
  103. // MARK: - Private Methods
  104. extension KMWatermarkCollectionViewItem {
  105. private func _configuUI() {
  106. self.imageView?.wantsLayer = true
  107. self.imageView?.layer?.borderWidth = 1.0
  108. self.topBaseView.wantsLayer = true
  109. self.topBaseView.layer?.borderWidth = 1.0
  110. self.topBaseView.layer?.borderColor = KMAppearance.Layout.h2Color().cgColor
  111. self.optionView.wantsLayer = true
  112. // self.optionView.layer.backgroundColor = [KMAppearance KMColor_Layout_L_1].CGColor;
  113. self.optionView.isHidden = true
  114. self.labelContainerView.wantsLayer = true
  115. self.labelContainerView.layer?.cornerRadius = 1.0
  116. self.imageView?.layer?.borderColor = KMAppearance.Layout.h2Color().cgColor
  117. self.editButton.coverAction = { [unowned self] cbutton, cation in
  118. if (cation == .enter) {
  119. self.editButton.image = NSImage(named: KMImageNameUXIconBtnEditHov)
  120. } else {
  121. self.editButton.image = NSImage(named: KMImageNameUXIconBtnEditNor)
  122. }
  123. }
  124. self.deleteButton.coverAction = { [unowned self] cbutton, cation in
  125. if (cation == .enter) {
  126. self.deleteButton.image = NSImage(named: KMImageNameUXIconBtnCloseHov)
  127. } else {
  128. self.deleteButton.image = NSImage(named: KMImageNameUXIconBtnCloseNor)
  129. }
  130. };
  131. }
  132. private func _addTrackingArea() {
  133. let trackingArea = NSTrackingArea(rect: self.view.bounds, options: [.mouseEnteredAndExited, .inVisibleRect, .activeAlways, .mouseMoved], owner: self)
  134. self.view.addTrackingArea(trackingArea)
  135. }
  136. }