// // TrialSuccessWindowController.swift // PDF Reader Pro // // Created by Niehaoyu on 2024/7/25. // import Cocoa class TrialSuccessWindowController: NSWindowController { @IBOutlet var contendBox: NSBox! @IBOutlet var infoContendBox: NSBox! @IBOutlet var tipImage: NSImageView! @IBOutlet var titleLabel: NSTextField! @IBOutlet var subTitleLabel: NSTextField! var gradientView: GradientView? deinit { DistributedNotificationCenter.default().removeObserver(self) } convenience init() { self.init(windowNibName: "TrialSuccessWindowController") } override func windowDidLoad() { super.windowDidLoad() // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file. self.gradientView = GradientView.init(frame: self.contendBox.bounds) self.gradientView?.autoresizingMask = [.width, .height] self.gradientView?.wantsLayer = true self.contendBox.contentView?.addSubview(self.gradientView!, positioned: .below, relativeTo: self.titleLabel) self.titleLabel.font = NSFont.UbuntuBoldFontWithSize(20) self.subTitleLabel.font = NSFont.UbuntuRegularFontWithSize(14) self.titleLabel.stringValue = NSLocalizedString("Apply for a free trial successfully!", comment: "") self.subTitleLabel.stringValue = NSLocalizedString("Please go to the mailbox to check the trial information.", comment: "") self.refreshViewColor() DistributedNotificationCenter.default().addObserver(self, selector: #selector(themeChange), name: NSNotification.Name(rawValue: "AppleInterfaceThemeChangedNotification"), object: nil) } func refreshViewColor() { if KMAppearance.isDarkMode() { self.contendBox.fillColor = NSColor(red: 14/255, green: 17/255, blue: 20/255, alpha: 1) self.infoContendBox.fillColor = NSColor(red: 14/255, green: 17/255, blue: 20/255, alpha: 1) self.gradientView?.isHidden = true } else { self.infoContendBox.fillColor = NSColor(red: 232/255, green: 233/255, blue: 1, alpha: 1) self.infoContendBox.fillColor = NSColor(red: 248/255, green: 249/255, blue: 1, alpha: 1) self.gradientView?.isHidden = false } } override func showWindow(_ sender: Any?) { super.showWindow(sender) var frame = self.window?.frame let infoFrame = self.infoContendBox.frame frame?.size.height = max(250, infoFrame.size.height + 58) self.window?.setFrame(frame!, display: true) } @objc func themeChange() { DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) { self.refreshViewColor() } } } class GradientView: NSView { override func draw(_ dirtyRect: NSRect) { super.draw(dirtyRect) setupGradient() } private func setupGradient() { let gradientLayer = CAGradientLayer() gradientLayer.frame = self.bounds gradientLayer.colors = [NSColor(red: 232/255, green: 233/255, blue: 255/255, alpha: 1).cgColor, NSColor(red: 248/255, green: 248/255, blue: 255/255, alpha: 1).cgColor] // 设置渐变颜色 gradientLayer.startPoint = CGPoint(x: 1.0, y: 0.0) gradientLayer.endPoint = CGPoint(x: 1.0, y: 1.0) // 移除之前的渐变层(如果存在) if let existingLayer = self.layer?.sublayers?.first(where: { $0 is CAGradientLayer }) { existingLayer.removeFromSuperlayer() } self.wantsLayer = true self.layer?.addSublayer(gradientLayer) gradientLayer.frame = self.bounds // 确保渐变层的大小与视图一致 } override func viewDidMoveToSuperview() { super.viewDidMoveToSuperview() setupGradient() } override func layout() { super.layout() if let gradientLayer = self.layer?.sublayers?.first as? CAGradientLayer { gradientLayer.frame = self.bounds // 重新设置渐变层的大小 } } }