// // AIUserInfoController.swift // PDF Reader Pro Edition // // Created by Niehaoyu on 2024/1/17. // import Cocoa @objcMembers class AIUserInfoController: NSViewController { @IBOutlet weak var contendBox: NSBox! @IBOutlet weak var titleLabel: NSTextField! @IBOutlet weak var whatNewlabel: NSTextField! @IBOutlet weak var emptyContendBox: NSBox! @IBOutlet weak var emptyTitleLabel: NSTextField! @IBOutlet weak var emptyBuyNowBox: KMBox! @IBOutlet weak var emptyBuyNowLabel: NSTextField! @IBOutlet weak var emptyRestoreBtn: HyperLinkButton! @IBOutlet weak var emptyBuyNowBtn: KMButton! @IBOutlet weak var emptyTipButton: KMButton! @IBOutlet weak var emptyShowAIBtn: NSButton! @IBOutlet weak var newFuncBtn: NSButton! @IBOutlet weak var creditsContendBox: NSBox! @IBOutlet weak var creditsInfoBox: KMBox! @IBOutlet weak var validCreditLabel: NSTextField! @IBOutlet weak var creditsBuyNowBox: KMBox! @IBOutlet weak var creditsBuyNowLabel: NSTextField! @IBOutlet weak var creditsRestoreBtn: HyperLinkButton! @IBOutlet weak var creditBuyNowBtn: KMButton! @IBOutlet weak var showAIRobotBtn: NSButton! @IBOutlet weak var remainLabel: NSTextField! var popOver: NSPopover! var guideHandle: ((_ vc: AIUserInfoController) -> Void)? var purchaseHandle: ((_ vc: AIUserInfoController) -> Void)? var enterLicenseHandle: ((_ vc: AIUserInfoController) -> Void)? deinit { NotificationCenter.default.removeObserver(self) } override func viewDidLoad() { super.viewDidLoad() // Do view setup here. newFuncBtn.isHidden = true whatNewlabel.isHidden = true self.view.wantsLayer = true if KMAppearance.isDarkMode() { self.view.layer?.backgroundColor = NSColor(red: 43/255, green: 43/255, blue: 43/255, alpha: 1).cgColor } else { self.view.layer?.backgroundColor = NSColor.white.cgColor } self.titleLabel.font = NSFont.SFProTextSemiboldFont(18) self.whatNewlabel.font = NSFont.SFProTextSemiboldFont(12) self.titleLabel.textColor = KMAppearance.KMColor_Layout_H0() self.whatNewlabel.textColor = KMAppearance.KMColor_Interactive_A0() self.titleLabel.stringValue = NSLocalizedString("AI Tools Title", comment: "") self.whatNewlabel.stringValue = NSLocalizedString("What‘s New", comment: "") + " →" self.creditsBuyNowLabel.stringValue = NSLocalizedString("Buy Now", comment: "") self.emptyContendBox.isHidden = true self.creditsContendBox.isHidden = true if KMMemberInfo.shared.isPermitAI == true { self.creditsContendBox.isHidden = false var viewRect = self.view.frame viewRect.size.height = 330 self.view.frame = viewRect } else { self.emptyContendBox.isHidden = false var viewRect = self.view.frame viewRect.size.height = 386 self.view.frame = viewRect } self.updateBtnShowState() self.updateEmptyCreditViewUI() self.updateCreditsViewUI() NotificationCenter.default.addObserver(self, selector: #selector(aiStatusInfoChangeNoti), name: NSNotification.Name(rawValue: kDeviceAIStatusChangeNotification), object: nil) self.updateCreditsViewInfo() self.emptyTipButton.mouseMoveCallback = {[unowned self] mouseEntered in if mouseEntered { let tipString = NSLocalizedString("Each use of the AI tools costs 1 or more credits.", comment: "") let popViewController = KMToolbarItemPopViewController.init() if self.popOver == nil { self.popOver = NSPopover.init() } self.popOver.contentViewController = popViewController self.popOver.animates = false self.popOver.behavior = .semitransient self.popOver.contentSize = (popViewController.view.frame.size) popViewController.updateWithHelpTip(helpTip: tipString) self.popOver.show(relativeTo: self.emptyTipButton.frame, of: self.emptyTipButton.superview!, preferredEdge: .maxY) } else { self.popOver.close() } } } func updateEmptyCreditViewUI() -> Void { self.emptyContendBox.cornerRadius = 8 if KMAppearance.isDarkMode() { self.emptyContendBox.fillColor = NSColor(red: 0, green: 0, blue: 0, alpha: 0.4) } else { self.emptyContendBox.fillColor = NSColor(red: 249/255, green: 247/255, blue: 250/255, alpha: 1) } self.emptyTitleLabel.textColor = KMAppearance.KMColor_Layout_H0() self.emptyBuyNowLabel.textColor = NSColor.white self.emptyBuyNowBox.fillColor = NSColor.clear self.emptyBuyNowBox.wantsLayer = true self.emptyBuyNowBox.layer?.cornerRadius = 20 self.emptyBuyNowBox.layer?.masksToBounds = true if KMMemberInfo.shared.ai_status == 1 { self.emptyBuyNowBox.isHidden = true } else { self.emptyBuyNowBox.isHidden = false } self.emptyBuyNowBtn.wantsLayer = true self.emptyBuyNowBtn.layer?.backgroundColor = NSColor.clear.cgColor self.emptyBuyNowBtn.mouseMoveCallback = {[weak self] mouseEntered in if mouseEntered { self?.emptyBuyNowBtn.layer?.backgroundColor = NSColor.white.withAlphaComponent(0.2).cgColor } else { self?.emptyBuyNowBtn.layer?.backgroundColor = NSColor.clear.cgColor } } self.emptyTitleLabel.font = NSFont.SFProTextSemiboldFont(14) self.emptyBuyNowLabel.font = NSFont.UbuntuMediumFontWithSize(16) self.emptyRestoreBtn.font = NSFont.SFProTextRegularFont(14) self.emptyTitleLabel.stringValue = NSLocalizedString("You have no credit", comment: "") self.emptyBuyNowLabel.stringValue = NSLocalizedString("Buy Now", comment: "") self.emptyRestoreBtn.title = NSLocalizedString("Enter AI License", comment: "") self.emptyRestoreBtn.setTitleColor(KMAppearance.KMColor_Interactive_M0()) self.emptyRestoreBtn.mouseMoveCallback = {[weak self] mouseEntered in if mouseEntered { self?.emptyRestoreBtn.setTitleColor(KMAppearance.KMColor_Interactive_M1()) } else { self?.emptyRestoreBtn.setTitleColor(KMAppearance.KMColor_Interactive_M0()) } } self.emptyShowAIBtn.title = NSLocalizedString("Show AI robot icon", comment: "") self.emptyShowAIBtn.setTitleColor(KMAppearance.KMColor_Layout_H0()) self.emptyShowAIBtn.state = AIInfoManager.default().showAIIcon ? .on : .off self.emptyShowAIBtn.isHidden = true } func updateCreditsViewUI() -> Void { self.creditsInfoBox.wantsLayer = true self.creditsInfoBox.layer?.cornerRadius = 8 self.creditsInfoBox.layer?.masksToBounds = true self.creditsContendBox.fillColor = NSColor.clear self.showAIRobotBtn.title = NSLocalizedString("Show AI robot icon", comment: "") self.showAIRobotBtn.setTitleColor(KMAppearance.KMColor_Layout_H0()) self.showAIRobotBtn.state = AIInfoManager.default().showAIIcon ? .on : .off self.showAIRobotBtn.isHidden = true if KMAppearance.isDarkMode() { self.creditsInfoBox.fillColor = NSColor(red: 0, green: 0, blue: 0, alpha: 0.4) } else { self.creditsInfoBox.fillColor = NSColor(red: 249/255, green: 247/255, blue: 250/255, alpha: 1) } self.creditsRestoreBtn.title = NSLocalizedString("Enter AI License", comment: "") self.creditsRestoreBtn.setTitleColor(KMAppearance.KMColor_Interactive_M0()) self.creditBuyNowBtn.wantsLayer = true self.creditBuyNowBtn.layer?.backgroundColor = NSColor.clear.cgColor self.creditBuyNowBtn.mouseMoveCallback = {[weak self] mouseEntered in if mouseEntered { self?.creditBuyNowBtn.layer?.backgroundColor = NSColor.white.withAlphaComponent(0.2).cgColor } else { self?.creditBuyNowBtn.layer?.backgroundColor = NSColor.clear.cgColor } } self.validCreditLabel.font = NSFont.UbuntuBoldFontWithSize(32) self.validCreditLabel.textColor = .blue self.remainLabel.font = NSFont.UbuntuBoldFontWithSize(20) // self.remainLabel.textColor = .black self.remainLabel.textColor = KMAppearance.KMColor_Layout_H0() self.creditsRestoreBtn.font = NSFont.SFProTextRegularFont(14) self.creditsBuyNowBox.fillColor = NSColor.clear self.creditsBuyNowBox.wantsLayer = true self.creditsBuyNowBox.layer?.cornerRadius = 18 self.creditsBuyNowBox.layer?.masksToBounds = true self.creditsBuyNowLabel.font = NSFont.UbuntuMediumFontWithSize(14) self.creditsBuyNowLabel.textColor = NSColor.white self.creditsRestoreBtn.setTitleColor(KMAppearance.KMColor_Interactive_M0()) self.updateBtnShowState() } func updateBtnShowState() { #if VERSION_DMG self.emptyRestoreBtn.isHidden = false #else self.emptyRestoreBtn.isHidden = true self.creditsRestoreBtn.isHidden = true if KMMemberInfo.shared.isPermitAI == true { self.creditsBuyNowBox.isHidden = true } else { self.creditsBuyNowBox.isHidden = false } #endif } func updateCreditsViewInfo() -> Void { let aiInfo: AIInfo = AIInfoManager.default().aiInfo self.validCreditLabel.stringValue = String(format: "%d", aiInfo.totalToken-aiInfo.usedTimes + Int32(KMMemberInfo.shared.aiPoint)) self.emptyContendBox.isHidden = true self.creditsContendBox.isHidden = true if KMMemberInfo.shared.isPermitAI == true { self.creditsContendBox.isHidden = false } else { self.emptyContendBox.isHidden = false } if KMMemberInfo.shared.isPermitAI == true { var viewRect = self.view.frame viewRect.size.height = 330 self.view.frame = viewRect } else { var viewRect = self.view.frame viewRect.size.height = 386 self.view.frame = viewRect } } //MARK: - IBAction @IBAction func newGuideAction(_ sender: Any) { guard let callBack = self.guideHandle else { return } callBack(self) } @IBAction func purchaseAction(_ sender: KMButton) { guard let callBack = self.purchaseHandle else { return } if KMMemberInfo.shared.isLogin == false { KMLoginWindowsController.shared.openWindow() { success in if KMMemberInfo.shared.aiPoint > 0 { } else { if success { callBack(self) } } } return } let newStatus: Bool = KMCloudServer.isConnectionAvailable() if !newStatus { let alert = NSAlert() alert.alertStyle = .critical alert.messageText = NSLocalizedString("Please make sure your internet connection is available.", comment: "") alert.runModal() return } callBack(self) } @IBAction func enterLicenseAction(_ sender: KMButton) { guard let callBack = self.enterLicenseHandle else { return } callBack(self) } @IBAction func showAIRobotAction(_ sender: NSButton) { if self.showAIRobotBtn.state == .on { AIInfoManager.default().showAIIcon = true } else { AIInfoManager.default().showAIIcon = false } NotificationCenter.default.post(name: NSNotification.Name(rawValue: kAIIconShowStateChangeNotification), object: nil) } @IBAction func emptyShowAIRototAction(_ sender: NSButton) { if self.emptyShowAIBtn.state == .on { AIInfoManager.default().showAIIcon = true } else { AIInfoManager.default().showAIIcon = false } NotificationCenter.default.post(name: NSNotification.Name(rawValue: kAIIconShowStateChangeNotification), object: nil) } @IBAction func creditRestoreAction(_ sender: NSButton) { guard let callBack = self.enterLicenseHandle else { return } callBack(self) } @objc func aiStatusInfoChangeNoti() { DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) { self.updateCreditsViewInfo() } } }