// // KMAdvertisementCollectionViewItem.swift // KMAdvertisement // // Created by lizhe on 2022/11/28. // import Cocoa class KMAdvertisementCollectionViewItem: NSCollectionViewItem { @IBOutlet weak var iconImageView: NSImageView! @IBOutlet weak var titleLabel: NSTextField! @IBOutlet weak var contentView: NSView! var model: KMAdvertisementModelItem! { 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.contentView.layer?.backgroundColor = KMAdvertisementModelTransition.transitionColor(color: (model.title?.background?.color)!, highlight: highlight).cgColor } if model.imageURL != nil { weak var weakSelf = self let url = URL(string: KMAdvertisementModelTransition.transitionImagePath(image: model.imageURL!, highlight: highlight)) self.iconImageView.image = KMAdvertisementImage.imageWithURL(url: url, completion: { image in if weakSelf != nil { weakSelf!.iconImageView.image = image } }) } } override func mouseEntered(with event: NSEvent) { self.updateColor(true) } override func mouseExited(with event: NSEvent) { self.updateColor(false) } }