KMAdvertisementCollectionViewItem.swift 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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: KMAdvertisementModelItem! {
  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. }
  24. func reloadData() {
  25. self.titleLabel.stringValue = KMAdvertisementModelTransition.transitionLanguage(langeuage: model?.title?.language)
  26. self.titleLabel.font = NSFont.init(name: model?.title?.font?.name ?? "AppleSystemUIFont", size: CGFloat(model?.title?.font?.size ?? 13))
  27. self.contentView.toolTip = KMAdvertisementModelTransition.transitionLanguage(langeuage: model?.tips)
  28. self.contentView.layer?.cornerRadius = model.title?.background?.layer?.cornerRadius ?? 0
  29. self.updateColor(false)
  30. }
  31. func updateColor(_ highlight: Bool) {
  32. if model.title?.color != nil {
  33. self.titleLabel.textColor = KMAdvertisementModelTransition.transitionColor(color: (model.title?.color), highlight: highlight)
  34. }
  35. if model.title?.background?.color != nil {
  36. self.contentView.layer?.backgroundColor = KMAdvertisementModelTransition.transitionColor(color: (model.title?.background?.color)!, highlight: highlight).cgColor
  37. }
  38. if model.imageURL != nil {
  39. weak var weakSelf = self
  40. let url = URL(string: KMAdvertisementModelTransition.transitionImagePath(image: model.imageURL!, highlight: highlight))
  41. self.iconImageView.image = KMAdvertisementImage.imageWithURL(url: url, completion: { image in
  42. if weakSelf != nil {
  43. weakSelf!.iconImageView.image = image
  44. }
  45. })
  46. }
  47. }
  48. override func mouseEntered(with event: NSEvent) {
  49. self.updateColor(true)
  50. }
  51. override func mouseExited(with event: NSEvent) {
  52. self.updateColor(false)
  53. }
  54. }