KMAdvertisementCollectionViewItem.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. //
  22. override func viewWillAppear() {
  23. super.viewWillAppear()
  24. self.view.addTrackingRect(self.view.bounds, owner: self, userData: nil, assumeInside: false)
  25. }
  26. override func viewDidLoad() {
  27. super.viewDidLoad()
  28. // Do view setup here.
  29. // self.view.addTrackingRect(self.view.bounds, owner: self, userData: nil, assumeInside: false)
  30. self.contentView.wantsLayer = true
  31. self.contentView.layer?.masksToBounds = true
  32. self.updateColor(false)
  33. // self.contentView.layer?.backgroundColor = NSColor.init(red: 38.0/255.0, green: 40/255.0, blue: 43/255.0, alpha: 1).cgColor
  34. }
  35. func reloadData() {
  36. self.titleLabel.stringValue = KMAdvertisementModelTransition.transitionLanguage(langeuage: model?.name)
  37. // self.titleLabel.font = NSFont.init(name: model?.title?.font?.name ?? "AppleSystemUIFont", size: CGFloat(model?.title?.font?.size ?? 13))
  38. self.contentView.toolTip = KMAdvertisementModelTransition.transitionLanguage(langeuage: model?.tooltip)
  39. self.contentView.layer?.cornerRadius = 4
  40. self.updateColor(false)
  41. }
  42. func updateColor(_ highlight: Bool) {
  43. // self.view.appearance = NSAppearance(named: .aqua)
  44. if highlight {
  45. if KMAdvertisementConfig.isDarkModel() {
  46. self.titleLabel.textColor = NSColor.km_init(hex: "#4E7EDB")
  47. self.contentView.backgroundColor(NSColor.km_init(hex: "#FFFFFF", alpha: 0.05))
  48. } else {
  49. self.titleLabel.textColor = NSColor.km_init(hex: "#273C62")
  50. self.contentView.backgroundColor(NSColor.km_init(hex: "#000000", alpha: 0.05))
  51. }
  52. } else {
  53. if KMAdvertisementConfig.isDarkModel() {
  54. self.contentView.backgroundColor(NSColor.km_init(hex: "#26282B"))
  55. self.titleLabel.textColor = NSColor.km_init(hex: "#C8C9CC")
  56. } else {
  57. self.contentView.backgroundColor(NSColor.km_init(hex: "#FCFDFF"))
  58. self.titleLabel.textColor = NSColor.km_init(hex: "#42464D")
  59. }
  60. }
  61. let url = URL(string: KMAdvertisementModelTransition.transitionImagePath(image: model?.image, highlight: highlight))
  62. self.iconImageView.image = KMAdvertisementImage.imageWithURL(url: url, completion: { [weak self] image in
  63. self?.iconImageView.image = image
  64. })
  65. }
  66. // func updateColor(_ highlight: Bool) {
  67. //
  68. // if highlight {
  69. // self.titleLabel.textColor = KMAppearance.Layout.mColor()
  70. // self.contentView.layer?.backgroundColor = KMAppearance.Status.hovColor().cgColor
  71. // } else {
  72. // self.titleLabel.textColor = KMAppearance.Layout.h1Color()
  73. // self.contentView.layer?.backgroundColor = KMAppearance.Layout.l0Color().cgColor
  74. // }
  75. //
  76. // let url = URL(string: KMAdvertisementModelTransition.transitionImagePath(image: model?.image, highlight: highlight))
  77. // self.iconImageView.image = KMAdvertisementImage.imageWithURL(url: url, completion: { [weak self] image in
  78. // self?.iconImageView.image = image
  79. // })
  80. // }
  81. override func mouseEntered(with event: NSEvent) {
  82. self.updateColor(true)
  83. }
  84. override func mouseExited(with event: NSEvent) {
  85. self.updateColor(false)
  86. }
  87. }