TrialSuccessWindowController.swift 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // TrialSuccessWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/7/25.
  6. //
  7. import Cocoa
  8. class TrialSuccessWindowController: NSWindowController {
  9. @IBOutlet var contendBox: NSBox!
  10. @IBOutlet var infoContendBox: NSBox!
  11. @IBOutlet var tipImage: NSImageView!
  12. @IBOutlet var titleLabel: NSTextField!
  13. @IBOutlet var subTitleLabel: NSTextField!
  14. var gradientView: GradientView?
  15. deinit {
  16. DistributedNotificationCenter.default().removeObserver(self)
  17. }
  18. convenience init() {
  19. self.init(windowNibName: "TrialSuccessWindowController")
  20. }
  21. override func windowDidLoad() {
  22. super.windowDidLoad()
  23. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
  24. self.gradientView = GradientView.init(frame: self.contendBox.bounds)
  25. self.gradientView?.autoresizingMask = [.width, .height]
  26. self.gradientView?.wantsLayer = true
  27. self.contendBox.contentView?.addSubview(self.gradientView!, positioned: .below, relativeTo: self.titleLabel)
  28. self.titleLabel.font = NSFont.UbuntuBoldFontWithSize(20)
  29. self.subTitleLabel.font = NSFont.UbuntuRegularFontWithSize(14)
  30. self.titleLabel.stringValue = NSLocalizedString("Apply for a free trial successfully!", comment: "")
  31. self.subTitleLabel.stringValue = NSLocalizedString("Please go to the mailbox to check the trial information.", comment: "")
  32. self.refreshViewColor()
  33. DistributedNotificationCenter.default().addObserver(self, selector: #selector(themeChange), name: NSNotification.Name(rawValue: "AppleInterfaceThemeChangedNotification"), object: nil)
  34. }
  35. func refreshViewColor() {
  36. if KMAppearance.isDarkMode() {
  37. self.contendBox.fillColor = NSColor(red: 14/255, green: 17/255, blue: 20/255, alpha: 1)
  38. self.infoContendBox.fillColor = NSColor(red: 14/255, green: 17/255, blue: 20/255, alpha: 1)
  39. self.gradientView?.isHidden = true
  40. } else {
  41. self.infoContendBox.fillColor = NSColor(red: 232/255, green: 233/255, blue: 1, alpha: 1)
  42. self.infoContendBox.fillColor = NSColor(red: 248/255, green: 249/255, blue: 1, alpha: 1)
  43. self.gradientView?.isHidden = false
  44. }
  45. }
  46. override func showWindow(_ sender: Any?) {
  47. super.showWindow(sender)
  48. var frame = self.window?.frame
  49. let infoFrame = self.infoContendBox.frame
  50. frame?.size.height = max(250, infoFrame.size.height + 58)
  51. self.window?.setFrame(frame!, display: true)
  52. }
  53. @objc func themeChange() {
  54. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
  55. self.refreshViewColor()
  56. }
  57. }
  58. }
  59. class GradientView: NSView {
  60. override func draw(_ dirtyRect: NSRect) {
  61. super.draw(dirtyRect)
  62. setupGradient()
  63. }
  64. private func setupGradient() {
  65. let gradientLayer = CAGradientLayer()
  66. gradientLayer.frame = self.bounds
  67. gradientLayer.colors = [NSColor(red: 232/255, green: 233/255, blue: 255/255, alpha: 1).cgColor,
  68. NSColor(red: 248/255, green: 248/255, blue: 255/255, alpha: 1).cgColor] // 设置渐变颜色
  69. gradientLayer.startPoint = CGPoint(x: 1.0, y: 0.0)
  70. gradientLayer.endPoint = CGPoint(x: 1.0, y: 1.0)
  71. // 移除之前的渐变层(如果存在)
  72. if let existingLayer = self.layer?.sublayers?.first(where: { $0 is CAGradientLayer }) {
  73. existingLayer.removeFromSuperlayer()
  74. }
  75. self.wantsLayer = true
  76. self.layer?.addSublayer(gradientLayer)
  77. gradientLayer.frame = self.bounds // 确保渐变层的大小与视图一致
  78. }
  79. override func viewDidMoveToSuperview() {
  80. super.viewDidMoveToSuperview()
  81. setupGradient()
  82. }
  83. override func layout() {
  84. super.layout()
  85. if let gradientLayer = self.layer?.sublayers?.first as? CAGradientLayer {
  86. gradientLayer.frame = self.bounds // 重新设置渐变层的大小
  87. }
  88. }
  89. }