AITranslateTipWindowController.swift 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // AITranslateTipWindowController.swift
  3. // PDF Reader Pro Edition
  4. //
  5. // Created by Niehaoyu on 2024/2/29.
  6. //
  7. import Cocoa
  8. class AITranslateTipWindowController: NSWindowController {
  9. @IBOutlet weak var contendView: NSView!
  10. @IBOutlet weak var titleLabel1: NSTextField!
  11. @IBOutlet weak var titleBox: KMBox!
  12. @IBOutlet weak var creditsLabel: NSTextField!
  13. @IBOutlet weak var charactersLabel: NSTextField!
  14. @IBOutlet weak var creditTipLabel: NSTextField!
  15. @IBOutlet weak var errorTipImage: NSImageView!
  16. @IBOutlet weak var errorTipLabel: NSTextField!
  17. @IBOutlet weak var cancelBtn: NSButton!
  18. @IBOutlet weak var confirmBtn: NSButton!
  19. var creditValid: Bool = false
  20. var spendCredits: Int = 0
  21. var totalChart: Int = 0
  22. var actionHandle: ((_ wc: AITranslateTipWindowController, _ confirm: Bool) -> Void)?
  23. static var currentWindowController: AITranslateTipWindowController!
  24. deinit {
  25. DistributedNotificationCenter.default.removeObserver(self)
  26. }
  27. @objc static func currentWC() -> AITranslateTipWindowController {
  28. if currentWindowController != nil {
  29. return currentWindowController
  30. } else {
  31. let configWC: AITranslateTipWindowController = AITranslateTipWindowController.init(windowNibName: "AITranslateTipWindowController")
  32. currentWindowController = configWC;
  33. return currentWindowController
  34. }
  35. }
  36. override func windowDidLoad() {
  37. super.windowDidLoad()
  38. // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
  39. self.titleLabel1.font = NSFont.SFProTextRegularFont(13)
  40. self.creditsLabel.font = NSFont.SFProTextRegularFont(13)
  41. self.charactersLabel.font = NSFont.SFProTextRegularFont(13)
  42. self.creditTipLabel.font = NSFont.SFProTextRegularFont(13)
  43. self.titleLabel1.stringValue = NSLocalizedString("You will cost:", comment: "")
  44. self.creditTipLabel.stringValue = NSLocalizedString("1 credit for every 10,000 characters", comment: "")
  45. self.errorTipLabel.stringValue = NSLocalizedString("Insufficient credit", comment: "")
  46. self.cancelBtn.title = NSLocalizedString("Cancel", comment: "")
  47. self.confirmBtn.title = NSLocalizedString("Translate", comment: "")
  48. DistributedNotificationCenter.default().addObserver(self, selector: #selector(themeChange), name: NSNotification.Name(rawValue: "AppleInterfaceThemeChangedNotification"), object: nil)
  49. self.updateViewColor()
  50. self.reloadData()
  51. }
  52. func reloadData() {
  53. if self.creditValid == true {
  54. self.errorTipImage.isHidden = true
  55. self.errorTipLabel.isHidden = true
  56. self.confirmBtn.isEnabled = true
  57. } else {
  58. self.errorTipImage.isHidden = false
  59. self.errorTipLabel.isHidden = false
  60. self.confirmBtn.isEnabled = false
  61. }
  62. self.creditsLabel.stringValue = String(format: NSLocalizedString("%@ credits", comment: ""), String(format: "%d", self.spendCredits))
  63. self.charactersLabel.stringValue = String(format: NSLocalizedString("(Total %@ characters)", comment: ""), String(format: "%d", self.totalChart))
  64. }
  65. func updateViewColor() {
  66. self.titleBox.wantsLayer = true
  67. self.titleBox.layer!.borderWidth = 1
  68. if KMAppearance.isDarkMode() {
  69. self.titleLabel1.textColor = NSColor.white.withAlphaComponent(0.85)
  70. self.creditsLabel.textColor = NSColor.white.withAlphaComponent(0.85)
  71. self.charactersLabel.textColor = NSColor.white.withAlphaComponent(0.5)
  72. self.creditTipLabel.textColor = NSColor.white.withAlphaComponent(0.85)
  73. self.titleBox.fillColor = KMAppearance.KMColor_Layout_L1()
  74. self.titleBox.layer!.borderColor = NSColor.white.withAlphaComponent(0.1).cgColor
  75. } else {
  76. self.titleLabel1.textColor = NSColor.black.withAlphaComponent(0.85)
  77. self.creditsLabel.textColor = NSColor.black.withAlphaComponent(0.85)
  78. self.charactersLabel.textColor = NSColor.black.withAlphaComponent(0.5)
  79. self.creditTipLabel.textColor = NSColor.black.withAlphaComponent(0.85)
  80. self.titleBox.fillColor = KMAppearance.KMColor_Layout_L1()
  81. self.titleBox.layer!.borderColor = NSColor.black.withAlphaComponent(0.1).cgColor
  82. }
  83. self.titleBox.cornerRadius = 2
  84. self.titleBox.layer?.cornerRadius = 2
  85. self.errorTipLabel.textColor = KMAppearance.KMColor_Status_Err()
  86. }
  87. //MARK: IBAction
  88. @IBAction func cancelAction(_ sender: NSButton) {
  89. guard let callBack = self.actionHandle else {
  90. return
  91. }
  92. callBack(self, false)
  93. }
  94. @IBAction func confirmAction(_ sender: NSButton) {
  95. guard let callBack = self.actionHandle else {
  96. return
  97. }
  98. callBack(self, true)
  99. }
  100. @objc func themeChange() {
  101. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
  102. self.updateViewColor()
  103. }
  104. }
  105. }