KMAdvertisementCollectionViewItem.swift 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. //
  2. // KMAdvertisementCollectionViewItem.swift
  3. // KMAdvertisement
  4. //
  5. // Created by lizhe on 2022/11/28.
  6. //
  7. import Cocoa
  8. import Kingfisher
  9. class KMAdvertisementCollectionViewItem: NSCollectionViewItem {
  10. @IBOutlet weak var iconImageView: NSImageView!
  11. @IBOutlet weak var titleLabel: NSTextField!
  12. @IBOutlet weak var contentView: NSView!
  13. var model: KMAdvertisementModel.Section.Content! {
  14. didSet {
  15. self.reloadData()
  16. }
  17. }
  18. override func viewDidLoad() {
  19. super.viewDidLoad()
  20. // Do view setup here.
  21. self.view.addTrackingRect(self.view.bounds, owner: self, userData: nil, assumeInside: false)
  22. self.contentView.wantsLayer = true
  23. self.contentView.layer?.masksToBounds = true
  24. }
  25. func reloadData() {
  26. self.titleLabel.stringValue = KMAdvertisementModelTransition.transitionLanguage(langeuage: model?.title?.language)
  27. self.titleLabel.font = NSFont.init(name: model?.title?.font?.name ?? "AppleSystemUIFont", size: CGFloat(model?.title?.font?.size ?? 13))
  28. self.contentView.toolTip = KMAdvertisementModelTransition.transitionLanguage(langeuage: model?.tips)
  29. self.contentView.layer?.cornerRadius = model.title?.background?.layer?.cornerRadius ?? 0
  30. self.updateColor(false)
  31. }
  32. func updateColor(_ highlight: Bool) {
  33. if model.title?.color != nil {
  34. self.titleLabel.textColor = KMAdvertisementModelTransition.transitionColor(color: (model.title?.color)!, highlight: highlight)
  35. }
  36. if model.title?.background?.color != nil {
  37. self.iconImageView.layer?.backgroundColor = KMAdvertisementModelTransition.transitionColor(color: (model.title?.background?.color)!, highlight: highlight).cgColor
  38. }
  39. if model.imageURL != nil {
  40. self.iconImageView?.kf.setImage(with: URL(string: KMAdvertisementModelTransition.transitionImagePath(image: model.imageURL!, highlight: highlight)))
  41. }
  42. }
  43. override func mouseEntered(with event: NSEvent) {
  44. self.updateColor(true)
  45. }
  46. override func mouseExited(with event: NSEvent) {
  47. self.updateColor(false)
  48. }
  49. }