KMAdvertisementCollectionViewItem.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 = 4
  31. self.updateColor(false)
  32. }
  33. func updateColor(_ highlight: Bool) {
  34. // self.view.appearance = NSAppearance(named: .aqua)
  35. if highlight {
  36. if KMAdvertisementConfig.isDarkModel() {
  37. self.titleLabel.textColor = NSColor.km_init(hex: "#4E7EDB")
  38. self.contentView.backgroundColor(NSColor.km_init(hex: "#FFFFFF", alpha: 0.05))
  39. } else {
  40. self.titleLabel.textColor = NSColor.km_init(hex: "#273C62")
  41. self.contentView.backgroundColor(NSColor.km_init(hex: "#000000", alpha: 0.05))
  42. }
  43. } else {
  44. if KMAdvertisementConfig.isDarkModel() {
  45. self.contentView.backgroundColor(NSColor.km_init(hex: "#26282B"))
  46. self.titleLabel.textColor = NSColor.km_init(hex: "#C8C9CC")
  47. } else {
  48. self.contentView.backgroundColor(NSColor.km_init(hex: "#FCFDFF"))
  49. self.titleLabel.textColor = NSColor.km_init(hex: "#42464D")
  50. }
  51. }
  52. let url = URL(string: KMAdvertisementModelTransition.transitionImagePath(image: model?.image, highlight: highlight))
  53. self.iconImageView.image = KMAdvertisementImage.imageWithURL(url: url, completion: { [weak self] image in
  54. self?.iconImageView.image = image
  55. })
  56. }
  57. // func updateColor(_ highlight: Bool) {
  58. //
  59. // if highlight {
  60. // self.titleLabel.textColor = KMAppearance.Layout.mColor()
  61. // self.contentView.layer?.backgroundColor = KMAppearance.Status.hovColor().cgColor
  62. // } else {
  63. // self.titleLabel.textColor = KMAppearance.Layout.h1Color()
  64. // self.contentView.layer?.backgroundColor = KMAppearance.Layout.l0Color().cgColor
  65. // }
  66. //
  67. // let url = URL(string: KMAdvertisementModelTransition.transitionImagePath(image: model?.image, highlight: highlight))
  68. // self.iconImageView.image = KMAdvertisementImage.imageWithURL(url: url, completion: { [weak self] image in
  69. // self?.iconImageView.image = image
  70. // })
  71. // }
  72. override func mouseEntered(with event: NSEvent) {
  73. self.updateColor(true)
  74. }
  75. override func mouseExited(with event: NSEvent) {
  76. self.updateColor(false)
  77. }
  78. }