KMAIRewritingVC.swift 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. //
  2. // KMAIRewritingVC.swift
  3. // PDF Master
  4. //
  5. // Created by wanjun on 2023/5/22.
  6. //
  7. import Cocoa
  8. @objc enum KMAIRewritingState : Int {
  9. case AIRewriting = 0
  10. case AIErrorCorrection
  11. }
  12. class KMAIRewritingVC: NSViewController {
  13. @IBOutlet weak var aiRewritingLabel: NSTextField!
  14. @IBOutlet weak var aiRewritingSubLabel: NSTextField!
  15. @IBOutlet weak var rewritingBox: KMBox!
  16. @IBOutlet weak var rewritingLabel: NSTextField!
  17. @IBOutlet weak var rewritingButton: NSButton!
  18. @IBOutlet weak var rewritingTextBox: NSBox!
  19. @IBOutlet weak var rewritingTextField: NSTextField!
  20. @IBOutlet weak var wordsLabel: NSTextField!
  21. @IBOutlet weak var resultLabel: NSTextField!
  22. @IBOutlet weak var copyBox: KMBox!
  23. @IBOutlet weak var copyLabel: NSTextField!
  24. @IBOutlet weak var copyButton: NSButton!
  25. @IBOutlet weak var resultTextBox: NSBox!
  26. @IBOutlet weak var resultTextView: NSTextView!
  27. @IBOutlet weak var emptyImageView: NSImageView!
  28. @IBOutlet weak var errorView: NSView!
  29. @IBOutlet weak var errorImageView: NSImageView!
  30. @IBOutlet weak var errorLabel: NSTextField!
  31. var state: KMAIRewritingState = .AIRewriting
  32. var _uiState: KMAITranslationState = .None
  33. var aiRewritingStr: String = ""
  34. var aiErrorCorrectionStr: String = ""
  35. var aiRewritingResultStr: String = ""
  36. var aiErrorCorrectionResultStr: String = ""
  37. var aiRewritingErrorStr: String = ""
  38. var aiErrorCorrectionErrorStr: String = ""
  39. var progressController: SKProgressController?
  40. override func viewDidLoad() {
  41. super.viewDidLoad()
  42. // Do view setup here.
  43. self.initLocalization()
  44. self.initializeUI()
  45. }
  46. // MARK: initialize
  47. func initializeUI() -> Void {
  48. self.aiRewritingLabel.textColor = NSColor(hex: "#252629")
  49. self.aiRewritingLabel.font = NSFont.SFProTextSemibold(20.0)
  50. self.aiRewritingSubLabel.textColor = NSColor(hex: "#94989C")
  51. self.aiRewritingSubLabel.font = NSFont.SFProTextRegular(14.0)
  52. self.rewritingBox.fillColor = NSColor(hex: "#BDDFFD")
  53. self.rewritingBox.cornerRadius = 4.0
  54. self.rewritingLabel.textColor = NSColor(hex: "#F7F8FA")
  55. self.rewritingLabel.font = NSFont.SFProTextRegular(14.0)
  56. self.rewritingTextBox.borderColor = NSColor(hex: "#1770F4")
  57. self.wordsLabel.textColor = NSColor(hex: "#94989C")
  58. self.wordsLabel.font = NSFont.SFProTextRegular(14.0)
  59. self.resultLabel.textColor = NSColor(hex: "#252629")
  60. self.resultLabel.font = NSFont.SFProTextSemibold(20.0)
  61. self.copyBox.borderColor = NSColor(hex: "#DFE1E5")
  62. self.copyBox.fillColor = NSColor(hex: "#EDEEF0")
  63. self.copyBox.cornerRadius = 4.0
  64. self.copyBox.borderWidth = 1.0
  65. self.copyLabel.textColor = NSColor(hex: "#B1B3B8")
  66. self.copyLabel.font = NSFont.SFProTextRegular(14.0)
  67. self.resultTextBox.borderColor = NSColor(hex: "#DFE1E5")
  68. self.errorView.isHidden = true
  69. self.errorImageView.image = NSImage(named: "icon_error")
  70. self.errorLabel.textColor = NSColor(hex: "#F3465B")
  71. self.errorLabel.font = NSFont.SFProTextRegular(12.0)
  72. // if self.state == .AIRewriting {
  73. // self.emptyImageView.image = NSImage(named: "ic_ai_rewriting_empty")
  74. // } else if self.state == .AIErrorCorrection {
  75. // self.emptyImageView.image = NSImage(named: "ic_ai_ErrorCorrection_empty")
  76. // }
  77. }
  78. func initLocalization() -> Void {
  79. switch self.state {
  80. case .AIRewriting:
  81. self.aiRewritingLabel.stringValue = NSLocalizedString("AI Rewriting", comment: "")
  82. self.aiRewritingSubLabel.stringValue = NSLocalizedString("Limited to 150 characters per session, 5 times per month", comment: "")
  83. self.rewritingLabel.stringValue = NSLocalizedString("Rewrite", comment: "")
  84. self.resultLabel.stringValue = NSLocalizedString("Result", comment: "")
  85. self.copyLabel.stringValue = NSLocalizedString("Copy", comment: "")
  86. self.rewritingTextField.placeholderString = NSLocalizedString("Please enter text content here...", comment: "")
  87. self.wordsLabel.stringValue = NSLocalizedString("0/150", comment: "")
  88. self.rewritingTextField.stringValue = self.aiRewritingStr
  89. self.resultTextView.string = self.aiRewritingResultStr
  90. self.emptyImageView.image = NSImage(named: "ic_ai_rewriting_empty")
  91. break
  92. case .AIErrorCorrection:
  93. self.aiRewritingLabel.stringValue = NSLocalizedString("AI Error Correction", comment: "")
  94. self.aiRewritingSubLabel.stringValue = NSLocalizedString("Limited to 150 characters per session, 5 times per month", comment: "")
  95. self.rewritingLabel.stringValue = NSLocalizedString("Correct", comment: "")
  96. self.resultLabel.stringValue = NSLocalizedString("Result", comment: "")
  97. self.copyLabel.stringValue = NSLocalizedString("Copy", comment: "")
  98. self.rewritingTextField.placeholderString = NSLocalizedString("Please enter text content here...", comment: "")
  99. self.wordsLabel.stringValue = NSLocalizedString("0/150", comment: "")
  100. self.rewritingTextField.stringValue = self.aiErrorCorrectionStr
  101. self.resultTextView.string = self.aiErrorCorrectionResultStr
  102. self.emptyImageView.image = NSImage(named: "ic_ai_ErrorCorrection_empty")
  103. break
  104. default:
  105. break
  106. }
  107. }
  108. // MARK: Get、Set
  109. var uiState: KMAITranslationState {
  110. get {
  111. return _uiState
  112. }
  113. set {
  114. _uiState = newValue
  115. self.refreshAIUI()
  116. }
  117. }
  118. // MARK: Private Methods
  119. func refreshAIUI() -> Void {
  120. self.wordsLabel.stringValue = String(format: "%d/150", self.rewritingTextField.stringValue.count)
  121. self.copyLabel.stringValue = NSLocalizedString("Copy", comment: "")
  122. if self.uiState == .CanAIRewriting || self.uiState == .CanAIErrorCorrection {
  123. self.rewritingBox.fillColor = NSColor(hex: "#1770F4")
  124. self.rewritingBox.borderWidth = 0.0
  125. self.rewritingLabel.textColor = NSColor(hex: "#FFFFFF")
  126. self.copyBox.fillColor = NSColor(hex: "#EDEEF0")
  127. self.copyBox.borderWidth = 1.0
  128. self.copyBox.borderColor = NSColor(hex: "#DFE1E5")
  129. self.copyLabel.textColor = NSColor(hex: "#B1B3B8")
  130. self.wordsLabel.textColor = NSColor(hex: "#94989C")
  131. self.resultTextBox.fillColor = NSColor(hex: "#FFFFFF")
  132. self.resultTextBox.borderWidth = 1.0
  133. self.rewritingBox.borderColor = NSColor(hex: "#DFE1E5")
  134. self.emptyImageView.isHidden = false
  135. self.errorView.isHidden = true
  136. } else if self.uiState == .CanCopy {
  137. self.rewritingBox.fillColor = NSColor(hex: "#1770F4")
  138. self.rewritingBox.borderWidth = 0.0
  139. self.rewritingLabel.textColor = NSColor(hex: "#FFFFFF")
  140. self.copyBox.fillColor = NSColor(hex: "#FFFFFF")
  141. self.copyBox.borderWidth = 1.0
  142. self.copyBox.borderColor = NSColor(hex: "#DFE1E5")
  143. self.copyLabel.textColor = NSColor(hex: "#252629")
  144. self.wordsLabel.textColor = NSColor(hex: "#94989C")
  145. self.resultTextBox.fillColor = NSColor(hex: "#FFFFFF")
  146. self.resultTextBox.borderWidth = 1.0
  147. self.rewritingBox.borderColor = NSColor(hex: "#DFE1E5")
  148. self.emptyImageView.isHidden = true
  149. self.errorView.isHidden = true
  150. } else if self.uiState == .Copyed {
  151. self.rewritingBox.fillColor = NSColor(hex: "#1770F4")
  152. self.rewritingBox.borderWidth = 0.0
  153. self.rewritingLabel.textColor = NSColor(hex: "#FFFFFF")
  154. self.copyBox.fillColor = NSColor(hex: "#36383B")
  155. self.copyBox.borderWidth = 0.0
  156. self.copyLabel.textColor = NSColor(hex: "#FFFFFF")
  157. self.copyLabel.stringValue = NSLocalizedString("Copied", comment: "")
  158. self.wordsLabel.textColor = NSColor(hex: "#94989C")
  159. self.resultTextBox.fillColor = NSColor(hex: "#FFFFFF")
  160. self.resultTextBox.borderWidth = 1.0
  161. self.rewritingBox.borderColor = NSColor(hex: "#DFE1E5")
  162. self.emptyImageView.isHidden = true
  163. self.errorView.isHidden = true
  164. } else if self.uiState == .Exceed { // 是否超过
  165. self.rewritingBox.fillColor = NSColor(hex: "#1770F4")
  166. self.rewritingBox.borderWidth = 0.0
  167. self.rewritingLabel.textColor = NSColor(hex: "#FFFFFF")
  168. self.copyBox.fillColor = NSColor(hex: "#EDEEF0")
  169. self.copyBox.borderWidth = 1.0
  170. self.copyBox.borderColor = NSColor(hex: "#DFE1E5")
  171. self.copyLabel.textColor = NSColor(hex: "#B1B3B8")
  172. self.wordsLabel.textColor = NSColor(hex: "#F3465B")
  173. self.resultTextBox.fillColor = NSColor(hex: "#FFFFFF")
  174. self.resultTextBox.borderWidth = 1.0
  175. self.rewritingBox.borderColor = NSColor(hex: "#F3465B")
  176. self.emptyImageView.isHidden = true
  177. self.errorView.isHidden = false
  178. self.errorLabel.stringValue = NSLocalizedString("Limit to 150 characters at a time", comment: "")
  179. } else if self.uiState == .Error {
  180. self.rewritingBox.fillColor = NSColor(hex: "#1770F4")
  181. self.rewritingBox.borderWidth = 0.0
  182. self.rewritingLabel.textColor = NSColor(hex: "#FFFFFF")
  183. self.copyBox.fillColor = NSColor(hex: "#EDEEF0")
  184. self.copyBox.borderWidth = 1.0
  185. self.copyBox.borderColor = NSColor(hex: "#DFE1E5")
  186. self.copyLabel.textColor = NSColor(hex: "#B1B3B8")
  187. if self.rewritingTextField.stringValue.count > 150 {
  188. self.wordsLabel.textColor = NSColor(hex: "#F3465B")
  189. } else {
  190. self.wordsLabel.textColor = NSColor(hex: "#94989C")
  191. }
  192. self.resultTextBox.fillColor = NSColor(hex: "#FFFFFF")
  193. self.resultTextBox.borderWidth = 1.0
  194. self.rewritingBox.borderColor = NSColor(hex: "#F3465B")
  195. self.emptyImageView.isHidden = true
  196. self.errorView.isHidden = false
  197. if self.state == .AIRewriting {
  198. self.errorLabel.stringValue = self.aiRewritingErrorStr
  199. } else if self.state == .AIErrorCorrection {
  200. self.errorLabel.stringValue = self.aiErrorCorrectionErrorStr
  201. }
  202. } else {
  203. self.rewritingBox.fillColor = NSColor(hex: "#BDDFFD")
  204. self.rewritingBox.borderWidth = 0.0
  205. self.rewritingLabel.textColor = NSColor(hex: "#F7F8FA")
  206. self.copyBox.fillColor = NSColor(hex: "#EDEEF0")
  207. self.copyBox.borderWidth = 1.0
  208. self.copyBox.borderColor = NSColor(hex: "#DFE1E5")
  209. self.copyLabel.textColor = NSColor(hex: "#B1B3B8")
  210. self.wordsLabel.textColor = NSColor(hex: "#94989C")
  211. self.resultTextBox.fillColor = NSColor(hex: "#FFFFFF")
  212. self.resultTextBox.borderWidth = 1.0
  213. self.rewritingBox.borderColor = NSColor(hex: "#DFE1E5")
  214. self.emptyImageView.isHidden = false
  215. self.errorView.isHidden = true
  216. }
  217. }
  218. func acquisition_uiState() -> Void {
  219. if self.state == .AIRewriting {
  220. if (self.aiRewritingStr.count > 0) && (self.aiRewritingResultStr.count > 0) {
  221. let pasteboard = NSPasteboard.general
  222. if pasteboard.string(forType: .string) != nil {
  223. if pasteboard.string(forType: .string)!.count > 0 {
  224. if pasteboard.string(forType: .string)! == self.aiRewritingResultStr {
  225. self.uiState = .Copyed
  226. } else {
  227. self.uiState = .CanCopy
  228. }
  229. } else {
  230. self.uiState = .CanCopy
  231. }
  232. } else {
  233. self.uiState = .CanCopy
  234. }
  235. } else if (self.aiRewritingStr.count > 0) {
  236. if self.aiRewritingStr.count > 150 {
  237. self.uiState = .Exceed
  238. } else {
  239. self.uiState = .CanAIRewriting
  240. }
  241. } else {
  242. if self.aiRewritingErrorStr.count > 0 {
  243. self.uiState = .Error
  244. } else {
  245. self.uiState = .None
  246. }
  247. }
  248. } else if self.state == .AIErrorCorrection {
  249. if (self.aiErrorCorrectionStr.count > 0) && (self.aiErrorCorrectionResultStr.count > 0) {
  250. let pasteboard = NSPasteboard.general
  251. if pasteboard.string(forType: .string) != nil {
  252. if pasteboard.string(forType: .string)!.count > 0 {
  253. if pasteboard.string(forType: .string)! == self.aiErrorCorrectionResultStr {
  254. self.uiState = .Copyed
  255. } else {
  256. self.uiState = .CanCopy
  257. }
  258. } else {
  259. self.uiState = .CanCopy
  260. }
  261. } else {
  262. self.uiState = .CanCopy
  263. }
  264. } else if (self.aiErrorCorrectionStr.count > 0) {
  265. if self.aiErrorCorrectionStr.count > 150 {
  266. self.uiState = .Exceed
  267. } else {
  268. self.uiState = .CanAIRewriting
  269. }
  270. } else {
  271. if self.aiErrorCorrectionErrorStr.count > 0 {
  272. self.uiState = .Error
  273. } else {
  274. self.uiState = .None
  275. }
  276. }
  277. }
  278. }
  279. func showProgressWindow() {
  280. let progress = SKProgressController()
  281. if self.state == .AIRewriting {
  282. progress.message = NSLocalizedString("Rewriting...", comment: "")
  283. } else if self.state == . AIErrorCorrection {
  284. progress.message = NSLocalizedString("Error Correcting...", comment: "")
  285. }
  286. progress.window?.backgroundColor = NSColor(hex: "#36383B")
  287. progress.window?.contentView?.wantsLayer = true
  288. progress.window?.contentView?.layer?.backgroundColor = NSColor(hex: "#36383B").cgColor
  289. progress.progressField.textColor = NSColor.white
  290. progress.closeBlock = { [unowned self] in
  291. }
  292. self.progressController = progress
  293. self.view.window?.beginSheet(progress.window!)
  294. }
  295. func hiddenProgressWindow() {
  296. if (self.progressController != nil) {
  297. self.view.window?.endSheet((self.progressController?.window)!)
  298. self.progressController = nil
  299. }
  300. }
  301. // MARK: Action
  302. @IBAction func aiRewritingAction(_ sender: NSButton) {
  303. if self.uiState == .CanAIRewriting || self.uiState == .CanAIErrorCorrection || self.uiState == .CanCopy || self.uiState == .Copyed || self.uiState == .Exceed || self.uiState == .Error {
  304. if !KMLightMemberManager.manager.isLogin() {
  305. KMLoginWindowController.show(window: NSApp.mainWindow!)
  306. return
  307. }
  308. self.errorView.isHidden = true
  309. DispatchQueue.main.async {
  310. self.showProgressWindow()
  311. }
  312. let infoDictionary = Bundle .main.infoDictionary!
  313. let majorVersion = infoDictionary["CFBundleShortVersionString"]
  314. KMRequestServerManager.manager.aiRewriting(content: self.rewritingTextField.stringValue, version: "1.0.1", state: self.state) { [unowned self] success, result in
  315. self.hiddenProgressWindow()
  316. if success {
  317. let result: NSDictionary = result!.result
  318. self.uiState = .CanCopy
  319. self.resultTextView.string = ""
  320. self.resultTextView.string = result["content"] as? String ?? ""
  321. if self.state == .AIRewriting {
  322. self.aiRewritingResultStr = result["content"] as? String ?? ""
  323. self.aiRewritingErrorStr = ""
  324. } else if self.state == .AIErrorCorrection {
  325. self.aiErrorCorrectionResultStr = result["content"] as? String ?? ""
  326. self.aiErrorCorrectionErrorStr = ""
  327. }
  328. } else {
  329. let result: String = result!.message
  330. DispatchQueue.main.async {
  331. self.resultTextView.string = ""
  332. self.errorLabel.stringValue = result
  333. if self.state == .AIRewriting {
  334. self.aiRewritingErrorStr = result
  335. } else if self.state == .AIErrorCorrection {
  336. self.aiErrorCorrectionErrorStr = result
  337. }
  338. self.uiState = .Error
  339. }
  340. }
  341. }
  342. }
  343. }
  344. @IBAction func copyAction(_ sender: NSButton) {
  345. if self.uiState == .CanCopy || self.uiState == .Copyed {
  346. if !KMLightMemberManager.manager.isLogin() {
  347. KMLoginWindowController.show(window: NSApp.mainWindow!)
  348. return
  349. }
  350. let pasteboard = NSPasteboard.general
  351. pasteboard.clearContents()
  352. pasteboard.setString(self.resultTextView.string, forType: .string)
  353. self.uiState = .Copyed
  354. }
  355. }
  356. }
  357. extension KMAIRewritingVC: NSTextFieldDelegate {
  358. func controlTextDidEndEditing(_ obj: Notification) {
  359. let object = obj.object as! NSTextField
  360. }
  361. func controlTextDidChange(_ obj: Notification) {
  362. let object = obj.object as! NSTextField
  363. self.wordsLabel.stringValue = String(format: "%d/150", object.stringValue.count)
  364. if object.stringValue.count > 0 {
  365. if self.state == .AIRewriting {
  366. self.uiState = .CanAIRewriting
  367. } else if self.state == .AIErrorCorrection {
  368. self.uiState = .CanAIErrorCorrection
  369. }
  370. } else {
  371. self.uiState = .None
  372. }
  373. if object.stringValue.count > 150 {
  374. self.uiState = .Exceed
  375. if self.state == .AIRewriting {
  376. self.aiRewritingErrorStr = NSLocalizedString("Limit to 150 characters at a time", comment: "")
  377. } else if self.state == .AIErrorCorrection {
  378. self.aiErrorCorrectionErrorStr = NSLocalizedString("Limit to 150 characters at a time", comment: "")
  379. }
  380. } else {
  381. if self.state == .AIRewriting {
  382. self.aiRewritingErrorStr = ""
  383. } else if self.state == .AIErrorCorrection {
  384. self.aiErrorCorrectionErrorStr = ""
  385. }
  386. }
  387. if self.state == .AIRewriting {
  388. self.aiRewritingStr = object.stringValue
  389. self.aiRewritingResultStr = ""
  390. self.resultTextView.string = ""
  391. } else if self.state == .AIErrorCorrection {
  392. self.aiErrorCorrectionStr = object.stringValue
  393. self.aiErrorCorrectionResultStr = ""
  394. self.resultTextView.string = ""
  395. }
  396. }
  397. }