123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- //
- // AITranslateTipWindowController.swift
- // PDF Reader Pro Edition
- //
- // Created by Niehaoyu on 2024/2/29.
- //
- import Cocoa
- class AITranslateTipWindowController: NSWindowController {
- @IBOutlet weak var contendView: NSView!
- @IBOutlet weak var titleLabel1: NSTextField!
- @IBOutlet weak var titleBox: KMBox!
- @IBOutlet weak var creditsLabel: NSTextField!
- @IBOutlet weak var charactersLabel: NSTextField!
-
- @IBOutlet weak var creditTipLabel: NSTextField!
-
- @IBOutlet weak var errorTipImage: NSImageView!
- @IBOutlet weak var errorTipLabel: NSTextField!
-
- @IBOutlet weak var cancelBtn: NSButton!
- @IBOutlet weak var confirmBtn: NSButton!
-
- var creditValid: Bool = false
- var spendCredits: Int = 0
- var totalChart: Int = 0
-
- var actionHandle: ((_ wc: AITranslateTipWindowController, _ confirm: Bool) -> Void)?
-
- static var currentWindowController: AITranslateTipWindowController!
-
-
- deinit {
- DistributedNotificationCenter.default.removeObserver(self)
- }
-
- @objc static func currentWC() -> AITranslateTipWindowController {
- if currentWindowController != nil {
- return currentWindowController
- } else {
- let configWC: AITranslateTipWindowController = AITranslateTipWindowController.init(windowNibName: "AITranslateTipWindowController")
- currentWindowController = configWC;
- return currentWindowController
- }
- }
-
- 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.titleLabel1.font = NSFont.SFProTextRegularFont(13)
- self.creditsLabel.font = NSFont.SFProTextRegularFont(13)
- self.charactersLabel.font = NSFont.SFProTextRegularFont(13)
- self.creditTipLabel.font = NSFont.SFProTextRegularFont(13)
-
- self.titleLabel1.stringValue = NSLocalizedString("You will cost:", comment: "")
-
- self.creditTipLabel.stringValue = NSLocalizedString("2 credit for every 10,000 characters", comment: "")
- self.errorTipLabel.stringValue = NSLocalizedString("Insufficient credit", comment: "")
-
- self.cancelBtn.title = NSLocalizedString("Cancel", comment: "")
- self.confirmBtn.title = NSLocalizedString("Translate", comment: "")
-
- DistributedNotificationCenter.default().addObserver(self, selector: #selector(themeChange), name: NSNotification.Name(rawValue: "AppleInterfaceThemeChangedNotification"), object: nil)
- self.updateViewColor()
- self.reloadData()
-
- }
-
- func reloadData() {
- if self.creditValid == true {
- self.errorTipImage.isHidden = true
- self.errorTipLabel.isHidden = true
- self.confirmBtn.isEnabled = true
-
- } else {
- self.errorTipImage.isHidden = false
- self.errorTipLabel.isHidden = false
- self.confirmBtn.isEnabled = false
- }
-
- self.creditsLabel.stringValue = String(format: NSLocalizedString("%@ credits", comment: ""), String(format: "%d", self.spendCredits))
- self.charactersLabel.stringValue = String(format: NSLocalizedString("(Total %@ characters)", comment: ""), String(format: "%d", self.totalChart))
- }
-
- func updateViewColor() {
- self.titleBox.wantsLayer = true
- self.titleBox.layer!.borderWidth = 1
-
- if KMAppearance.isDarkMode() {
- self.titleLabel1.textColor = NSColor.white.withAlphaComponent(0.85)
- self.creditsLabel.textColor = NSColor.white.withAlphaComponent(0.85)
- self.charactersLabel.textColor = NSColor.white.withAlphaComponent(0.5)
- self.creditTipLabel.textColor = NSColor.white.withAlphaComponent(0.85)
-
- self.titleBox.fillColor = KMAppearance.KMColor_Layout_L1()
- self.titleBox.layer!.borderColor = NSColor.white.withAlphaComponent(0.1).cgColor
- } else {
- self.titleLabel1.textColor = NSColor.black.withAlphaComponent(0.85)
- self.creditsLabel.textColor = NSColor.black.withAlphaComponent(0.85)
- self.charactersLabel.textColor = NSColor.black.withAlphaComponent(0.5)
- self.creditTipLabel.textColor = NSColor.black.withAlphaComponent(0.85)
-
- self.titleBox.fillColor = KMAppearance.KMColor_Layout_L1()
- self.titleBox.layer!.borderColor = NSColor.black.withAlphaComponent(0.1).cgColor
-
- }
- self.titleBox.cornerRadius = 2
- self.titleBox.layer?.cornerRadius = 2
-
- self.errorTipLabel.textColor = KMAppearance.KMColor_Status_Err()
-
- }
-
- //MARK: IBAction
- @IBAction func cancelAction(_ sender: NSButton) {
- guard let callBack = self.actionHandle else {
- return
- }
- callBack(self, false)
- }
-
- @IBAction func confirmAction(_ sender: NSButton) {
- guard let callBack = self.actionHandle else {
- return
- }
- callBack(self, true)
- }
-
-
- @objc func themeChange() {
- DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
- self.updateViewColor()
- }
- }
-
-
- }
|