KMAIRewritingVC.swift 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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.km_init(hex: "#252629")
  49. self.aiRewritingLabel.font = NSFont.SFProTextSemiboldFont(20.0)
  50. self.aiRewritingSubLabel.textColor = NSColor.km_init(hex: "#94989C")
  51. self.aiRewritingSubLabel.font = NSFont.SFProTextRegularFont(14.0)
  52. self.rewritingBox.fillColor = NSColor.km_init(hex: "#BDDFFD")
  53. self.rewritingBox.cornerRadius = 4.0
  54. self.rewritingLabel.textColor = NSColor.km_init(hex: "#F7F8FA")
  55. self.rewritingLabel.font = NSFont.SFProTextRegularFont(14.0)
  56. self.rewritingTextBox.borderColor = NSColor.km_init(hex: "#1770F4")
  57. self.wordsLabel.textColor = NSColor.km_init(hex: "#94989C")
  58. self.wordsLabel.font = NSFont.SFProTextRegularFont(14.0)
  59. self.resultLabel.textColor = NSColor.km_init(hex: "#252629")
  60. self.resultLabel.font = NSFont.SFProTextSemiboldFont(20.0)
  61. self.copyBox.borderColor = NSColor.km_init(hex: "#DFE1E5")
  62. self.copyBox.fillColor = NSColor.km_init(hex: "#EDEEF0")
  63. self.copyBox.cornerRadius = 4.0
  64. self.copyBox.borderWidth = 1.0
  65. self.copyLabel.textColor = NSColor.km_init(hex: "#B1B3B8")
  66. self.copyLabel.font = NSFont.SFProTextRegularFont(14.0)
  67. self.resultTextBox.borderColor = NSColor.km_init(hex: "#DFE1E5")
  68. self.errorView.isHidden = true
  69. self.errorImageView.image = NSImage(named: "icon_error")
  70. self.errorLabel.textColor = NSColor.km_init(hex: "#F3465B")
  71. self.errorLabel.font = NSFont.SFProTextRegularFont(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.km_init(hex: "#1770F4")
  124. self.rewritingBox.borderWidth = 0.0
  125. self.rewritingLabel.textColor = NSColor.km_init(hex: "#FFFFFF")
  126. self.copyBox.fillColor = NSColor.km_init(hex: "#EDEEF0")
  127. self.copyBox.borderWidth = 1.0
  128. self.copyBox.borderColor = NSColor.km_init(hex: "#DFE1E5")
  129. self.copyLabel.textColor = NSColor.km_init(hex: "#B1B3B8")
  130. self.wordsLabel.textColor = NSColor.km_init(hex: "#94989C")
  131. self.resultTextBox.fillColor = NSColor.km_init(hex: "#FFFFFF")
  132. self.resultTextBox.borderWidth = 1.0
  133. self.rewritingBox.borderColor = NSColor.km_init(hex: "#DFE1E5")
  134. self.emptyImageView.isHidden = false
  135. self.errorView.isHidden = true
  136. } else if self.uiState == .CanCopy {
  137. self.rewritingBox.fillColor = NSColor.km_init(hex: "#1770F4")
  138. self.rewritingBox.borderWidth = 0.0
  139. self.rewritingLabel.textColor = NSColor.km_init(hex: "#FFFFFF")
  140. self.copyBox.fillColor = NSColor.km_init(hex: "#FFFFFF")
  141. self.copyBox.borderWidth = 1.0
  142. self.copyBox.borderColor = NSColor.km_init(hex: "#DFE1E5")
  143. self.copyLabel.textColor = NSColor.km_init(hex: "#252629")
  144. self.wordsLabel.textColor = NSColor.km_init(hex: "#94989C")
  145. self.resultTextBox.fillColor = NSColor.km_init(hex: "#FFFFFF")
  146. self.resultTextBox.borderWidth = 1.0
  147. self.rewritingBox.borderColor = NSColor.km_init(hex: "#DFE1E5")
  148. self.emptyImageView.isHidden = true
  149. self.errorView.isHidden = true
  150. } else if self.uiState == .Copyed {
  151. self.rewritingBox.fillColor = NSColor.km_init(hex: "#1770F4")
  152. self.rewritingBox.borderWidth = 0.0
  153. self.rewritingLabel.textColor = NSColor.km_init(hex: "#FFFFFF")
  154. self.copyBox.fillColor = NSColor.km_init(hex: "#36383B")
  155. self.copyBox.borderWidth = 0.0
  156. self.copyLabel.textColor = NSColor.km_init(hex: "#FFFFFF")
  157. self.copyLabel.stringValue = NSLocalizedString("Copied", comment: "")
  158. self.wordsLabel.textColor = NSColor.km_init(hex: "#94989C")
  159. self.resultTextBox.fillColor = NSColor.km_init(hex: "#FFFFFF")
  160. self.resultTextBox.borderWidth = 1.0
  161. self.rewritingBox.borderColor = NSColor.km_init(hex: "#DFE1E5")
  162. self.emptyImageView.isHidden = true
  163. self.errorView.isHidden = true
  164. } else if self.uiState == .Exceed { // 是否超过
  165. self.rewritingBox.fillColor = NSColor.km_init(hex: "#1770F4")
  166. self.rewritingBox.borderWidth = 0.0
  167. self.rewritingLabel.textColor = NSColor.km_init(hex: "#FFFFFF")
  168. self.copyBox.fillColor = NSColor.km_init(hex: "#EDEEF0")
  169. self.copyBox.borderWidth = 1.0
  170. self.copyBox.borderColor = NSColor.km_init(hex: "#DFE1E5")
  171. self.copyLabel.textColor = NSColor.km_init(hex: "#B1B3B8")
  172. self.wordsLabel.textColor = NSColor.km_init(hex: "#F3465B")
  173. self.resultTextBox.fillColor = NSColor.km_init(hex: "#FFFFFF")
  174. self.resultTextBox.borderWidth = 1.0
  175. self.rewritingBox.borderColor = NSColor.km_init(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.km_init(hex: "#1770F4")
  181. self.rewritingBox.borderWidth = 0.0
  182. self.rewritingLabel.textColor = NSColor.km_init(hex: "#FFFFFF")
  183. self.copyBox.fillColor = NSColor.km_init(hex: "#EDEEF0")
  184. self.copyBox.borderWidth = 1.0
  185. self.copyBox.borderColor = NSColor.km_init(hex: "#DFE1E5")
  186. self.copyLabel.textColor = NSColor.km_init(hex: "#B1B3B8")
  187. if self.rewritingTextField.stringValue.count > 150 {
  188. self.wordsLabel.textColor = NSColor.km_init(hex: "#F3465B")
  189. } else {
  190. self.wordsLabel.textColor = NSColor.km_init(hex: "#94989C")
  191. }
  192. self.resultTextBox.fillColor = NSColor.km_init(hex: "#FFFFFF")
  193. self.resultTextBox.borderWidth = 1.0
  194. self.rewritingBox.borderColor = NSColor.km_init(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.km_init(hex: "#BDDFFD")
  204. self.rewritingBox.borderWidth = 0.0
  205. self.rewritingLabel.textColor = NSColor.km_init(hex: "#F7F8FA")
  206. self.copyBox.fillColor = NSColor.km_init(hex: "#EDEEF0")
  207. self.copyBox.borderWidth = 1.0
  208. self.copyBox.borderColor = NSColor.km_init(hex: "#DFE1E5")
  209. self.copyLabel.textColor = NSColor.km_init(hex: "#B1B3B8")
  210. self.wordsLabel.textColor = NSColor.km_init(hex: "#94989C")
  211. self.resultTextBox.fillColor = NSColor.km_init(hex: "#FFFFFF")
  212. self.resultTextBox.borderWidth = 1.0
  213. self.rewritingBox.borderColor = NSColor.km_init(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.km_init(hex: "#36383B")
  287. progress.window?.contentView?.wantsLayer = true
  288. progress.window?.contentView?.layer?.backgroundColor = NSColor.km_init(hex: "#36383B").cgColor
  289. progress.progressField.textColor = NSColor.white
  290. progress.closeBlock = { [unowned self] in
  291. self.hiddenProgressWindow()
  292. }
  293. self.progressController = progress
  294. self.view.window?.beginSheet(progress.window!)
  295. }
  296. func hiddenProgressWindow() {
  297. if (self.progressController != nil) {
  298. self.view.window?.endSheet((self.progressController?.window)!)
  299. self.progressController = nil
  300. KMRequestServer.requestServer.task?.cancel()
  301. }
  302. }
  303. // MARK: Action
  304. @IBAction func aiRewritingAction(_ sender: NSButton) {
  305. if self.uiState == .CanAIRewriting || self.uiState == .CanAIErrorCorrection || self.uiState == .CanCopy || self.uiState == .Copyed || self.uiState == .Exceed || self.uiState == .Error {
  306. if !KMLightMemberManager.manager.isLogin() {
  307. KMLoginWindowController.show(window: NSApp.mainWindow!)
  308. return
  309. }
  310. self._aiRewritingAction(sender)
  311. // Task { @MainActor in
  312. // if await (KMLightMemberManager.manager.canPayFunction() == false) {
  313. // var type: KMSubscribeWaterMarkType = .aiRewrite
  314. // if (self.state == .AIErrorCorrection) {
  315. // type = .aiCorrect
  316. // }
  317. // let _ = KMSubscribeWaterMarkWindowController.show(window: self.view.window!, type: type) { isSub, _, isClose in
  318. // if (isClose) {
  319. // return
  320. // }
  321. // if (isSub) {
  322. // self._aiRewritingAction(sender)
  323. // return
  324. // }
  325. // }
  326. // return
  327. // }
  328. //
  329. // self._aiRewritingAction(sender)
  330. // }
  331. }
  332. }
  333. private func _aiRewritingAction(_ sender: NSButton) {
  334. self.errorView.isHidden = true
  335. DispatchQueue.main.async {
  336. self.showProgressWindow()
  337. }
  338. var majorVersion = "1.0.1"
  339. // if let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String {
  340. // majorVersion = appVersion
  341. // }
  342. KMRequestServerManager.manager.aiRewriting(content: self.rewritingTextField.stringValue, version: majorVersion as! String, state: self.state) { [unowned self] success, result in
  343. self.hiddenProgressWindow()
  344. if success {
  345. let result: NSDictionary = result!.result
  346. self.uiState = .CanCopy
  347. self.resultTextView.string = ""
  348. self.resultTextView.string = result["content"] as? String ?? ""
  349. if self.state == .AIRewriting {
  350. self.aiRewritingResultStr = result["content"] as? String ?? ""
  351. self.aiRewritingErrorStr = ""
  352. } else if self.state == .AIErrorCorrection {
  353. self.aiErrorCorrectionResultStr = result["content"] as? String ?? ""
  354. self.aiErrorCorrectionErrorStr = ""
  355. }
  356. } else {
  357. let result: String = result!.message
  358. DispatchQueue.main.async {
  359. self.resultTextView.string = ""
  360. self.errorLabel.stringValue = result
  361. if self.state == .AIRewriting {
  362. self.aiRewritingErrorStr = result
  363. } else if self.state == .AIErrorCorrection {
  364. self.aiErrorCorrectionErrorStr = result
  365. }
  366. self.uiState = .Error
  367. }
  368. }
  369. }
  370. }
  371. @IBAction func copyAction(_ sender: NSButton) {
  372. if self.uiState == .CanCopy || self.uiState == .Copyed {
  373. if !KMLightMemberManager.manager.isLogin() {
  374. KMLoginWindowController.show(window: NSApp.mainWindow!)
  375. return
  376. }
  377. self._copyAction(sender)
  378. // Task { @MainActor in
  379. // if await (KMLightMemberManager.manager.canPayFunction() == false) {
  380. // let _ = KMSubscribeWaterMarkWindowController.show(window: self.view.window!, type: .aiRewrite) { isSub, _, isClose in
  381. // if (isClose) {
  382. // return
  383. // }
  384. // if (isSub) {
  385. // self._copyAction(sender)
  386. // return
  387. // }
  388. // }
  389. // return
  390. // }
  391. // self._copyAction(sender)
  392. // }
  393. }
  394. }
  395. private func _copyAction(_ sender: NSButton) {
  396. let pasteboard = NSPasteboard.general
  397. pasteboard.clearContents()
  398. pasteboard.setString(self.resultTextView.string, forType: .string)
  399. self.uiState = .Copyed
  400. }
  401. }
  402. extension KMAIRewritingVC: NSTextFieldDelegate {
  403. func controlTextDidEndEditing(_ obj: Notification) {
  404. let object = obj.object as! NSTextField
  405. }
  406. func controlTextDidChange(_ obj: Notification) {
  407. let object = obj.object as! NSTextField
  408. self.wordsLabel.stringValue = String(format: "%d/150", object.stringValue.count)
  409. if object.stringValue.count > 0 {
  410. if self.state == .AIRewriting {
  411. self.uiState = .CanAIRewriting
  412. } else if self.state == .AIErrorCorrection {
  413. self.uiState = .CanAIErrorCorrection
  414. }
  415. } else {
  416. self.uiState = .None
  417. }
  418. if object.stringValue.count > 150 {
  419. self.uiState = .Exceed
  420. if self.state == .AIRewriting {
  421. self.aiRewritingErrorStr = NSLocalizedString("Limit to 150 characters at a time", comment: "")
  422. } else if self.state == .AIErrorCorrection {
  423. self.aiErrorCorrectionErrorStr = NSLocalizedString("Limit to 150 characters at a time", comment: "")
  424. }
  425. } else {
  426. if self.state == .AIRewriting {
  427. self.aiRewritingErrorStr = ""
  428. } else if self.state == .AIErrorCorrection {
  429. self.aiErrorCorrectionErrorStr = ""
  430. }
  431. }
  432. if self.state == .AIRewriting {
  433. self.aiRewritingStr = object.stringValue
  434. self.aiRewritingResultStr = ""
  435. self.resultTextView.string = ""
  436. } else if self.state == .AIErrorCorrection {
  437. self.aiErrorCorrectionStr = object.stringValue
  438. self.aiErrorCorrectionResultStr = ""
  439. self.resultTextView.string = ""
  440. }
  441. }
  442. }