KMNQuickToolCollectionViewItem.swift 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // KMNQuickToolCollectionViewItem.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by 丁林圭 on 2024/10/11.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class KMNQuickToolCollectionViewItem: NSCollectionViewItem, ComponentCardPDFToolDelegate {
  10. @IBOutlet var itemCardView: ComponentCardPDFTool!
  11. @IBOutlet var tagView: ComponentTag!
  12. var clickCallBack: ((Bool) -> Void)?
  13. public var isShowState: Bool = false {
  14. didSet {
  15. itemCardView.properties.isSelected = isShowState
  16. itemCardView.reloadData()
  17. }
  18. }
  19. public var isNewState: Bool = false {
  20. didSet {
  21. tagView.isHidden = !isNewState
  22. }
  23. }
  24. public var quickToolModel:KMNHomeQuickToolMode = KMNHomeQuickToolMode.toolModeData(type:.Batch) {
  25. didSet {
  26. let pdftoolProperty: ComponentCardPDFToolProperty = ComponentCardPDFToolProperty(collapse: true,
  27. state: .normal,
  28. icon: NSImage(named: quickToolModel.toolImageName),
  29. text: quickToolModel.toolTitle,
  30. subText: "",
  31. selectedAble: true,
  32. isSelected:false,
  33. identifyID: "",
  34. enableMouseEnterExit: false)
  35. itemCardView.properties = pdftoolProperty
  36. }
  37. }
  38. override func viewDidLoad() {
  39. super.viewDidLoad()
  40. self.view.wantsLayer = true
  41. itemCardView.delegate = self
  42. tagView.properties = ComponentTagProperty(tagType: .mark_New, size: .m, text: KMLocalizedString("new"))
  43. tagView.isHidden = true
  44. DispatchQueue.main.async {
  45. self.itemCardView.reloadData()
  46. }
  47. }
  48. //MARK: - ComponentCardPDFToolDelegate
  49. @objc func componentCardPDFToolDidClicked(_ view: ComponentCardPDFTool) {
  50. itemCardView.properties.isSelected = !itemCardView.properties.isSelected
  51. itemCardView.reloadData()
  52. clickCallBack?(itemCardView.properties.isSelected)
  53. }
  54. @objc func componentCardPDFToolDidSelectedStateUpdated(_ view: ComponentCardPDFTool) {
  55. clickCallBack?(itemCardView.properties.isSelected)
  56. }
  57. }
  58. extension NSView {
  59. func shakeAnimation() {
  60. let shake = CAKeyframeAnimation(keyPath: "position")
  61. shake.values = [
  62. NSValue(point: CGPoint(x: self.bounds.origin.x - 5, y: self.bounds.origin.y)),
  63. NSValue(point: CGPoint(x: self.bounds.origin.x + 5, y: self.bounds.origin.y)),
  64. NSValue(point: CGPoint(x: self.bounds.origin.x - 5, y: self.bounds.origin.y)),
  65. NSValue(point: CGPoint(x: self.bounds.origin.x + 5, y: self.bounds.origin.y)),
  66. NSValue(point: CGPoint(x: self.bounds.origin.x, y: self.bounds.origin.y))
  67. ]
  68. shake.keyTimes = [0, 0.25, 0.5, 0.75, 1]
  69. shake.duration = 0.5
  70. shake.isAdditive = true
  71. shake.timingFunctions = [CAMediaTimingFunction(name: .easeInEaseOut)]
  72. self.layer?.add(shake, forKey: "shake")
  73. }
  74. }