KMAdvertisementShowView.swift 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //
  2. // KMAdvertisementShowView.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lizhe on 2023/10/26.
  6. //
  7. import Cocoa
  8. typealias KMAdvertisementShowViewDidSelect = (_ view: KMAdvertisementShowView, _ item: KMAdvertisementItemInfo) -> Void
  9. class KMAdvertisementShowView: NSView {
  10. @IBOutlet var contentView: NSView!
  11. @IBOutlet weak var iconImage: NSImageView!
  12. @IBOutlet weak var closeButton: NSButton!
  13. @IBOutlet weak var contentButton: NSButton!
  14. var didSelect: KMAdvertisementShowViewDidSelect?
  15. open var inputData: KMAdvertisementItem? {
  16. didSet {
  17. self.reloadData()
  18. }
  19. }
  20. convenience init (data: [KMAdvertisementItem], superView: NSView) {
  21. self.init(frame: superView.bounds)
  22. superView.addSubview(self)
  23. self.autoresizingMask = [.height , .width]
  24. }
  25. public override init(frame frameRect: NSRect) {
  26. super.init(frame: frameRect)
  27. initContentView()
  28. setup()
  29. }
  30. // MARK: 初始化
  31. public required init?(coder decoder: NSCoder) {
  32. super.init(coder: decoder)
  33. initContentView()
  34. setup()
  35. }
  36. private func initContentView() {
  37. //绑定xib
  38. let resource = NSNib(nibNamed: String(describing: self.classForCoder.self),
  39. bundle: Bundle(for: self.classForCoder.self))!
  40. resource.instantiate(withOwner: self, topLevelObjects: nil)
  41. addSubview(contentView)
  42. contentView.translatesAutoresizingMaskIntoConstraints = false
  43. NSLayoutConstraint.activate([
  44. contentView.topAnchor.constraint(equalTo: topAnchor),
  45. contentView.leftAnchor.constraint(equalTo: leftAnchor),
  46. contentView.rightAnchor.constraint(equalTo: rightAnchor),
  47. contentView.bottomAnchor.constraint(equalTo: bottomAnchor)])
  48. contentView.updateConstraintsForSubtreeIfNeeded()
  49. }
  50. func setup() {
  51. self.contentView.isHidden = true
  52. // self.backgroundColor(NSColor.black)
  53. }
  54. func reloadData() {
  55. guard let model = inputData?.content?.first else {
  56. self.contentView.isHidden = true
  57. return
  58. }
  59. self.contentView.isHidden = false
  60. let url = URL(string: KMAdvertisementModelTransition.transitionImagePath(image: model.image, highlight: false))
  61. self.iconImage.image = KMAdvertisementImage.imageWithURL(url: url, completion: { [weak self] image in
  62. self?.iconImage.image = image
  63. })
  64. }
  65. @IBAction func contentButtonAction(_ sender: Any) {
  66. guard let model = inputData?.content?.first else { return }
  67. self.didSelect?(self, model)
  68. }
  69. @IBAction func closeButtonAction(_ sender: Any) {
  70. self.removeFromSuperview()
  71. }
  72. }