AIUserInfoController.swift 13 KB

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