KMAdvertisementCollectionViewItem.swift 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. //
  2. // KMAdvertisementCollectionViewItem.swift
  3. // KMAdvertisement
  4. //
  5. // Created by lizhe on 2022/11/28.
  6. //
  7. import Cocoa
  8. class KMAdvertisementCollectionViewItem: NSCollectionViewItem {
  9. @IBOutlet weak var iconImageView: NSImageView!
  10. @IBOutlet weak var titleLabel: NSTextField!
  11. @IBOutlet weak var contentView: NSView!
  12. var model: KMAdvertisementItemInfo? {
  13. didSet {
  14. self.reloadData()
  15. }
  16. }
  17. override func viewDidLoad() {
  18. super.viewDidLoad()
  19. // Do view setup here.
  20. self.view.addTrackingRect(self.view.bounds, owner: self, userData: nil, assumeInside: false)
  21. self.contentView.wantsLayer = true
  22. self.contentView.layer?.masksToBounds = true
  23. self.updateColor(false)
  24. // self.contentView.layer?.backgroundColor = NSColor.init(red: 38.0/255.0, green: 40/255.0, blue: 43/255.0, alpha: 1).cgColor
  25. }
  26. func reloadData() {
  27. self.titleLabel.stringValue = KMAdvertisementModelTransition.transitionLanguage(langeuage: model?.name)
  28. // self.titleLabel.font = NSFont.init(name: model?.title?.font?.name ?? "AppleSystemUIFont", size: CGFloat(model?.title?.font?.size ?? 13))
  29. self.contentView.toolTip = KMAdvertisementModelTransition.transitionLanguage(langeuage: model?.tooltip)
  30. self.contentView.layer?.cornerRadius = 8
  31. self.updateColor(false)
  32. }
  33. func updateColor(_ highlight: Bool) {
  34. if highlight {
  35. self.titleLabel.textColor = KMAppearance.Layout.mColor()
  36. self.contentView.layer?.backgroundColor = KMAppearance.Status.hovColor().cgColor
  37. } else {
  38. self.titleLabel.textColor = KMAppearance.Layout.h1Color()
  39. self.contentView.layer?.backgroundColor = KMAppearance.Layout.l0Color().cgColor
  40. }
  41. let url = URL(string: KMAdvertisementModelTransition.transitionImagePath(image: model?.image, highlight: highlight))
  42. self.iconImageView.image = KMAdvertisementImage.imageWithURL(url: url, completion: { [weak self] image in
  43. self?.iconImageView.image = image
  44. })
  45. }
  46. override func mouseEntered(with event: NSEvent) {
  47. self.updateColor(true)
  48. }
  49. override func mouseExited(with event: NSEvent) {
  50. self.updateColor(false)
  51. }
  52. }