AIUserInfoController.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. //
  2. // AIUserInfoController.swift
  3. // PDF Reader Pro Edition
  4. //
  5. // Created by Niehaoyu on 2024/1/17.
  6. //
  7. import Cocoa
  8. @objcMembers class AIUserInfoController: NSViewController {
  9. @IBOutlet weak var contendBox: NSBox!
  10. @IBOutlet weak var titleLabel: NSTextField!
  11. @IBOutlet weak var whatNewlabel: NSTextField!
  12. @IBOutlet weak var emptyContendBox: NSBox!
  13. @IBOutlet weak var emptyTitleLabel: NSTextField!
  14. @IBOutlet weak var emptyBuyNowBox: KMBox!
  15. @IBOutlet weak var emptyBuyNowLabel: NSTextField!
  16. @IBOutlet weak var emptyRestoreBtn: HyperLinkButton!
  17. @IBOutlet weak var emptyBuyNowBtn: KMButton!
  18. @IBOutlet weak var emptyTipButton: KMButton!
  19. @IBOutlet weak var emptyShowAIBtn: NSButton!
  20. @IBOutlet weak var creditsContendBox: NSBox!
  21. @IBOutlet weak var creditsInfoBox: KMBox!
  22. @IBOutlet weak var validCreditLabel: NSTextField!
  23. @IBOutlet weak var creditsBuyNowBox: KMBox!
  24. @IBOutlet weak var creditsBuyNowLabel: NSTextField!
  25. @IBOutlet weak var creditsRestoreBtn: HyperLinkButton!
  26. @IBOutlet weak var creditBuyNowBtn: KMButton!
  27. @IBOutlet weak var showAIRobotBtn: NSButton!
  28. @IBOutlet weak var remainLabel: NSTextField!
  29. var popOver: NSPopover!
  30. var guideHandle: ((_ vc: AIUserInfoController) -> Void)?
  31. var purchaseHandle: ((_ vc: AIUserInfoController) -> Void)?
  32. var enterLicenseHandle: ((_ vc: AIUserInfoController) -> Void)?
  33. deinit {
  34. NotificationCenter.default.removeObserver(self)
  35. }
  36. override func viewDidLoad() {
  37. super.viewDidLoad()
  38. // Do view setup here.
  39. self.view.wantsLayer = true
  40. if KMAppearance.isDarkMode() {
  41. self.view.layer?.backgroundColor = NSColor(red: 43/255, green: 43/255, blue: 43/255, alpha: 1).cgColor
  42. } else {
  43. self.view.layer?.backgroundColor = NSColor.white.cgColor
  44. }
  45. self.titleLabel.font = NSFont.SFProTextSemiboldFont(18)
  46. self.whatNewlabel.font = NSFont.SFProTextSemiboldFont(12)
  47. self.titleLabel.textColor = KMAppearance.KMColor_Layout_H0()
  48. self.whatNewlabel.textColor = KMAppearance.KMColor_Interactive_A0()
  49. self.titleLabel.stringValue = NSLocalizedString("AI Tools Title", comment: "")
  50. self.whatNewlabel.stringValue = NSLocalizedString("What‘s New", comment: "") + " →"
  51. self.creditsBuyNowLabel.stringValue = NSLocalizedString("Buy Now", comment: "")
  52. self.emptyContendBox.isHidden = true
  53. self.creditsContendBox.isHidden = true
  54. self.emptyShowAIBtn.isHidden = true
  55. if KMMemberInfo.shared.isPermitAI == true {
  56. self.creditsContendBox.isHidden = false
  57. var viewRect = self.view.frame
  58. viewRect.size.height = 330
  59. self.view.frame = viewRect
  60. } else {
  61. self.emptyContendBox.isHidden = false
  62. self.emptyShowAIBtn.isHidden = false
  63. var viewRect = self.view.frame
  64. viewRect.size.height = 386
  65. self.view.frame = viewRect
  66. }
  67. self.updateBtnShowState()
  68. self.updateEmptyCreditViewUI()
  69. self.updateCreditsViewUI()
  70. NotificationCenter.default.addObserver(self, selector: #selector(aiStatusInfoChangeNoti), name: NSNotification.Name(rawValue: kDeviceAIStatusChangeNotification), object: nil)
  71. self.updateCreditsViewInfo()
  72. self.emptyTipButton.mouseMoveCallback = {[unowned self] mouseEntered in
  73. if mouseEntered {
  74. let tipString = NSLocalizedString("Each use of the AI tools costs 1 or more credits.", comment: "")
  75. let popViewController = KMToolbarItemPopViewController.init()
  76. if self.popOver == nil {
  77. self.popOver = NSPopover.init()
  78. }
  79. self.popOver.contentViewController = popViewController
  80. self.popOver.animates = false
  81. self.popOver.behavior = .semitransient
  82. self.popOver.contentSize = (popViewController.view.frame.size)
  83. popViewController.updateWithHelpTip(helpTip: tipString)
  84. self.popOver.show(relativeTo: self.emptyTipButton.frame, of: self.emptyTipButton.superview!, preferredEdge: .maxY)
  85. } else {
  86. self.popOver.close()
  87. }
  88. }
  89. }
  90. func updateEmptyCreditViewUI() -> Void {
  91. self.emptyContendBox.cornerRadius = 8
  92. if KMAppearance.isDarkMode() {
  93. self.emptyContendBox.fillColor = NSColor(red: 0, green: 0, blue: 0, alpha: 0.4)
  94. } else {
  95. self.emptyContendBox.fillColor = NSColor(red: 249/255, green: 247/255, blue: 250/255, alpha: 1)
  96. }
  97. self.emptyTitleLabel.textColor = KMAppearance.KMColor_Layout_H0()
  98. self.emptyBuyNowLabel.textColor = NSColor.white
  99. self.emptyBuyNowBox.fillColor = NSColor.clear
  100. self.emptyBuyNowBox.wantsLayer = true
  101. self.emptyBuyNowBox.layer?.cornerRadius = 20
  102. self.emptyBuyNowBox.layer?.masksToBounds = true
  103. if KMMemberInfo.shared.ai_status == 1 {
  104. self.emptyBuyNowBox.isHidden = true
  105. } else {
  106. self.emptyBuyNowBox.isHidden = false
  107. }
  108. self.emptyBuyNowBtn.wantsLayer = true
  109. self.emptyBuyNowBtn.layer?.backgroundColor = NSColor.clear.cgColor
  110. self.emptyBuyNowBtn.mouseMoveCallback = {[weak self] mouseEntered in
  111. if mouseEntered {
  112. self?.emptyBuyNowBtn.layer?.backgroundColor = NSColor.white.withAlphaComponent(0.2).cgColor
  113. } else {
  114. self?.emptyBuyNowBtn.layer?.backgroundColor = NSColor.clear.cgColor
  115. }
  116. }
  117. self.emptyTitleLabel.font = NSFont.SFProTextSemiboldFont(14)
  118. self.emptyBuyNowLabel.font = NSFont.UbuntuMediumFontWithSize(16)
  119. self.emptyRestoreBtn.font = NSFont.SFProTextRegularFont(14)
  120. self.emptyTitleLabel.stringValue = NSLocalizedString("You have no credit", comment: "")
  121. self.emptyBuyNowLabel.stringValue = NSLocalizedString("Buy Now", comment: "")
  122. self.emptyRestoreBtn.title = NSLocalizedString("Enter AI License", comment: "")
  123. self.emptyRestoreBtn.setTitleColor(KMAppearance.KMColor_Interactive_M0())
  124. self.emptyRestoreBtn.mouseMoveCallback = {[weak self] mouseEntered in
  125. if mouseEntered {
  126. self?.emptyRestoreBtn.setTitleColor(KMAppearance.KMColor_Interactive_M1())
  127. } else {
  128. self?.emptyRestoreBtn.setTitleColor(KMAppearance.KMColor_Interactive_M0())
  129. }
  130. }
  131. self.emptyShowAIBtn.title = NSLocalizedString("Show AI robot icon", comment: "")
  132. self.emptyShowAIBtn.setTitleColor(KMAppearance.KMColor_Layout_H0())
  133. self.emptyShowAIBtn.state = AIInfoManager.default().showAIIcon ? .on : .off
  134. self.emptyShowAIBtn.isHidden = true
  135. }
  136. func updateCreditsViewUI() -> Void {
  137. self.creditsInfoBox.wantsLayer = true
  138. self.creditsInfoBox.layer?.cornerRadius = 8
  139. self.creditsInfoBox.layer?.masksToBounds = true
  140. self.creditsContendBox.fillColor = NSColor.clear
  141. self.showAIRobotBtn.title = NSLocalizedString("Show AI robot icon", comment: "")
  142. self.showAIRobotBtn.setTitleColor(KMAppearance.KMColor_Layout_H0())
  143. self.showAIRobotBtn.state = AIInfoManager.default().showAIIcon ? .on : .off
  144. self.showAIRobotBtn.isHidden = true
  145. if KMAppearance.isDarkMode() {
  146. self.creditsInfoBox.fillColor = NSColor(red: 0, green: 0, blue: 0, alpha: 0.4)
  147. } else {
  148. self.creditsInfoBox.fillColor = NSColor(red: 249/255, green: 247/255, blue: 250/255, alpha: 1)
  149. }
  150. self.creditsRestoreBtn.title = NSLocalizedString("Enter AI License", comment: "")
  151. self.creditsRestoreBtn.setTitleColor(KMAppearance.KMColor_Interactive_M0())
  152. self.creditBuyNowBtn.wantsLayer = true
  153. self.creditBuyNowBtn.layer?.backgroundColor = NSColor.clear.cgColor
  154. self.creditBuyNowBtn.mouseMoveCallback = {[weak self] mouseEntered in
  155. if mouseEntered {
  156. self?.creditBuyNowBtn.layer?.backgroundColor = NSColor.white.withAlphaComponent(0.2).cgColor
  157. } else {
  158. self?.creditBuyNowBtn.layer?.backgroundColor = NSColor.clear.cgColor
  159. }
  160. }
  161. self.validCreditLabel.font = NSFont.UbuntuBoldFontWithSize(32)
  162. self.validCreditLabel.textColor = .blue
  163. self.remainLabel.font = NSFont.UbuntuBoldFontWithSize(20)
  164. self.remainLabel.textColor = .black
  165. self.creditsRestoreBtn.font = NSFont.SFProTextRegularFont(14)
  166. self.creditsBuyNowBox.fillColor = NSColor.clear
  167. self.creditsBuyNowBox.wantsLayer = true
  168. self.creditsBuyNowBox.layer?.cornerRadius = 18
  169. self.creditsBuyNowBox.layer?.masksToBounds = true
  170. self.creditsBuyNowLabel.font = NSFont.UbuntuMediumFontWithSize(14)
  171. self.creditsBuyNowLabel.textColor = NSColor.white
  172. self.creditsRestoreBtn.setTitleColor(KMAppearance.KMColor_Interactive_M0())
  173. self.updateBtnShowState()
  174. }
  175. func updateBtnShowState() {
  176. #if VERSION_DMG
  177. self.emptyRestoreBtn.isHidden = false
  178. #else
  179. self.emptyRestoreBtn.isHidden = true
  180. self.creditsRestoreBtn.isHidden = true
  181. if KMMemberInfo.shared.isPermitAI == true {
  182. self.creditsBuyNowBox.isHidden = true
  183. } else {
  184. self.creditsBuyNowBox.isHidden = false
  185. }
  186. #endif
  187. }
  188. func updateCreditsViewInfo() -> Void {
  189. let aiInfo: AIInfo = AIInfoManager.default().aiInfo
  190. self.validCreditLabel.stringValue = String(format: "%d", aiInfo.totalToken-aiInfo.usedTimes + Int32(KMMemberInfo.shared.aiPoint))
  191. self.emptyContendBox.isHidden = true
  192. self.emptyShowAIBtn.isHidden = true
  193. self.creditsContendBox.isHidden = true
  194. if KMMemberInfo.shared.isPermitAI == true {
  195. self.creditsContendBox.isHidden = false
  196. } else {
  197. self.emptyContendBox.isHidden = false
  198. self.emptyShowAIBtn.isHidden = false
  199. }
  200. if KMMemberInfo.shared.isPermitAI == true {
  201. var viewRect = self.view.frame
  202. viewRect.size.height = 330
  203. self.view.frame = viewRect
  204. } else {
  205. var viewRect = self.view.frame
  206. viewRect.size.height = 386
  207. self.view.frame = viewRect
  208. }
  209. }
  210. //MARK: - IBAction
  211. @IBAction func newGuideAction(_ sender: Any) {
  212. guard let callBack = self.guideHandle else {
  213. return
  214. }
  215. callBack(self)
  216. }
  217. @IBAction func purchaseAction(_ sender: KMButton) {
  218. guard let callBack = self.purchaseHandle else {
  219. return
  220. }
  221. if KMMemberInfo.shared.isLogin == false {
  222. KMLoginWindowsController.shared.openWindow() { success in
  223. if success {
  224. callBack(self)
  225. }
  226. }
  227. return
  228. }
  229. let newStatus: Bool = KMCloudServer.isConnectionAvailable()
  230. if !newStatus {
  231. let alert = NSAlert()
  232. alert.alertStyle = .critical
  233. alert.messageText = NSLocalizedString("Please make sure your internet connection is available.", comment: "")
  234. alert.runModal()
  235. return
  236. }
  237. callBack(self)
  238. }
  239. @IBAction func enterLicenseAction(_ sender: KMButton) {
  240. guard let callBack = self.enterLicenseHandle else {
  241. return
  242. }
  243. callBack(self)
  244. }
  245. @IBAction func showAIRobotAction(_ sender: NSButton) {
  246. if self.showAIRobotBtn.state == .on {
  247. AIInfoManager.default().showAIIcon = true
  248. } else {
  249. AIInfoManager.default().showAIIcon = false
  250. }
  251. NotificationCenter.default.post(name: NSNotification.Name(rawValue: kAIIconShowStateChangeNotification), object: nil)
  252. }
  253. @IBAction func emptyShowAIRototAction(_ sender: NSButton) {
  254. if self.emptyShowAIBtn.state == .on {
  255. AIInfoManager.default().showAIIcon = true
  256. } else {
  257. AIInfoManager.default().showAIIcon = false
  258. }
  259. NotificationCenter.default.post(name: NSNotification.Name(rawValue: kAIIconShowStateChangeNotification), object: nil)
  260. }
  261. @IBAction func creditRestoreAction(_ sender: NSButton) {
  262. guard let callBack = self.enterLicenseHandle else {
  263. return
  264. }
  265. callBack(self)
  266. }
  267. @objc func aiStatusInfoChangeNoti() {
  268. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
  269. self.updateCreditsViewInfo()
  270. }
  271. }
  272. }