1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- //
- // KMAdvertisementCollectionViewItem.swift
- // KMAdvertisement
- //
- // Created by lizhe on 2022/11/28.
- //
- import Cocoa
- import Kingfisher
- class KMAdvertisementCollectionViewItem: NSCollectionViewItem {
- @IBOutlet weak var iconImageView: NSImageView!
- @IBOutlet weak var titleLabel: NSTextField!
-
- @IBOutlet weak var contentView: NSView!
- var model: KMAdvertisementModel.Section.Content! {
- didSet {
- self.reloadData()
- }
- }
-
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do view setup here.
- self.view.addTrackingRect(self.view.bounds, owner: self, userData: nil, assumeInside: false)
- self.contentView.wantsLayer = true
- self.contentView.layer?.masksToBounds = true
- }
-
- func reloadData() {
- self.titleLabel.stringValue = KMAdvertisementModelTransition.transitionLanguage(langeuage: model?.title?.language)
- self.titleLabel.font = NSFont.init(name: model?.title?.font?.name ?? "AppleSystemUIFont", size: CGFloat(model?.title?.font?.size ?? 13))
- self.contentView.toolTip = KMAdvertisementModelTransition.transitionLanguage(langeuage: model?.tips)
- self.contentView.layer?.cornerRadius = model.title?.background?.layer?.cornerRadius ?? 0
- self.updateColor(false)
- }
-
- func updateColor(_ highlight: Bool) {
-
- if model.title?.color != nil {
- self.titleLabel.textColor = KMAdvertisementModelTransition.transitionColor(color: (model.title?.color)!, highlight: highlight)
- }
-
- if model.title?.background?.color != nil {
- self.iconImageView.layer?.backgroundColor = KMAdvertisementModelTransition.transitionColor(color: (model.title?.background?.color)!, highlight: highlight).cgColor
- }
-
- if model.imageURL != nil {
- self.iconImageView?.kf.setImage(with: URL(string: KMAdvertisementModelTransition.transitionImagePath(image: model.imageURL!, highlight: highlight)))
- }
- }
-
- override func mouseEntered(with event: NSEvent) {
- self.updateColor(true)
- }
- override func mouseExited(with event: NSEvent) {
- self.updateColor(false)
- }
- }
|