KMAITranslationWindowController.swift 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. //
  2. // KMAITranslationWindowController.swift
  3. // PDF Master
  4. //
  5. // Created by wanjun on 2023/5/24.
  6. //
  7. import Cocoa
  8. @objc enum KMAITranslationState: Int {
  9. case None = 0
  10. case CanTranslate
  11. case CanAIRewriting
  12. case CanAIErrorCorrection
  13. case CanCopy
  14. case Copyed
  15. case Exceed
  16. case Error
  17. }
  18. class KMAITranslationWindowController: NSWindowController {
  19. @IBOutlet weak var closeButton: NSButton!
  20. @IBOutlet weak var aiTranslationTitleLabel: NSTextField!
  21. @IBOutlet weak var tipsButton: NSButton!
  22. @IBOutlet weak var translationLanguage: NSTextField!
  23. @IBOutlet weak var fromBox: NSBox!
  24. @IBOutlet weak var fromLabel: NSTextField!
  25. @IBOutlet weak var toBox: NSBox!
  26. @IBOutlet weak var toLabel: NSTextField!
  27. @IBOutlet weak var languageImageView: NSImageView!
  28. @IBOutlet weak var fromContentBox: NSBox!
  29. @IBOutlet weak var fromContentTextField: NSTextField!
  30. @IBOutlet weak var fromContentLabel: NSTextField!
  31. @IBOutlet weak var toContentBox: NSBox!
  32. @IBOutlet weak var toContentTextView: NSTextView!
  33. @IBOutlet weak var translateBox: KMBox!
  34. @IBOutlet weak var translateLabel: NSTextField!
  35. @IBOutlet weak var translateButton: NSButton!
  36. @IBOutlet weak var copyBox: KMBox!
  37. @IBOutlet weak var copyLabel: NSTextField!
  38. @IBOutlet weak var copyButton: NSButton!
  39. @IBOutlet weak var errorView: NSView!
  40. @IBOutlet weak var errorImageView: NSImageView!
  41. @IBOutlet weak var errorLabel: NSTextField!
  42. var fromStr: String = "auto"
  43. var toStr: String = "en"
  44. var fromLanguages: [String] = ["Automatic", "English", "Simplified Chinese", "Traditional Chinese", "Japanese", "Korean", "French", "Spanish", "Italian", "German", "Portuguese", "Russian", "Vietnamese", "Thai", "Arabic", "Greek", "Bulgarian", "Finnish", "Slovene", "Dutch", "Czech", "Swedish", "Polish", "Danish", "Romanian", "Hungarian"]
  45. var toLanguages: [String] = ["English", "Simplified Chinese", "Traditional Chinese", "Japanese", "Korean", "French", "Spanish", "Italian", "German", "Portuguese", "Russian", "Vietnamese", "Thai", "Arabic", "Greek", "Bulgarian", "Finnish", "Slovene", "Dutch", "Czech", "Swedish", "Polish", "Danish", "Romanian", "Hungarian"]
  46. var content: String = ""
  47. var popover: NSPopover?
  48. var popoverTips: NSPopover?
  49. var _state: KMAITranslationState = .None
  50. var progressController: SKProgressController?
  51. override func windowDidLoad() {
  52. super.windowDidLoad()
  53. self.window?.backgroundColor = .white
  54. self.initLocalization()
  55. self.initializeUI()
  56. }
  57. // MARK: initialize
  58. func initializeUI() -> Void {
  59. self.aiTranslationTitleLabel.textColor = NSColor.km_init(hex: "#252629")
  60. self.aiTranslationTitleLabel.font = NSFont.SFProTextSemiboldFont(16.0)
  61. self.translationLanguage.textColor = NSColor.km_init(hex: "#252629")
  62. self.translationLanguage.font = NSFont.SFProTextRegularFont(14.0)
  63. self.fromBox.fillColor = NSColor.km_init(hex: "#FFFFFF")
  64. self.fromBox.borderColor = NSColor.km_init(hex: "#DFE1E5")
  65. self.fromLabel.textColor = NSColor.km_init(hex: "#252629")
  66. self.fromLabel.font = NSFont.SFProTextRegularFont(14.0)
  67. self.toBox.fillColor = NSColor.km_init(hex: "#FFFFFF")
  68. self.toBox.borderColor = NSColor.km_init(hex: "#DFE1E5")
  69. self.toLabel.textColor = NSColor.km_init(hex: "#252629")
  70. self.toLabel.font = NSFont.SFProTextRegularFont(14.0)
  71. self.languageImageView.image = NSImage(named: "ic_transtate")
  72. self.fromContentBox.borderColor = NSColor.km_init(hex: "#1770F4")
  73. self.fromContentBox.fillColor = NSColor.km_init(hex: "#FFFFFF")
  74. self.fromContentTextField.textColor = NSColor.km_init(hex: "#252629")
  75. self.fromContentTextField.font = NSFont.SFProTextRegularFont(12.0)
  76. self.fromContentLabel.textColor = NSColor.km_init(hex: "#94989C")
  77. self.fromContentLabel.font = NSFont.SFProTextRegularFont(12.0)
  78. self.toContentBox.borderColor = NSColor.km_init(hex: "#DFE1E5")
  79. self.toContentBox.fillColor = NSColor.km_init(hex: "#FFFFFF")
  80. self.toContentTextView.textColor = .black
  81. self.toContentTextView.font = NSFont.SFProTextRegularFont(12.0)
  82. self.translateBox.fillColor = NSColor.km_init(hex: "#BDDFFD")
  83. self.translateBox.cornerRadius = 4.0
  84. self.translateLabel.textColor = NSColor.km_init(hex: "#FFFFFF")
  85. self.translateLabel.font = NSFont.SFProTextRegularFont(12.0)
  86. self.copyBox.fillColor = NSColor.km_init(hex: "#EDEEF0")
  87. self.copyBox.borderColor = NSColor.km_init(hex: "#DFE1E5")
  88. self.copyBox.borderWidth = 1.0
  89. self.copyBox.cornerRadius = 4.0
  90. self.copyLabel.textColor = NSColor.km_init(hex: "#F7F8FA")
  91. self.copyLabel.font = NSFont.SFProTextRegularFont(12.0)
  92. self.errorView.isHidden = true
  93. self.errorImageView.image = NSImage(named: "icon_error")
  94. self.errorLabel.textColor = NSColor.km_init(hex: "#F3465B")
  95. self.errorLabel.font = NSFont.SFProTextRegularFont(12.0)
  96. if self.content.count > 0 {
  97. self.fromContentTextField.stringValue = self.content
  98. if self.content.count > 150 {
  99. self.state = .Exceed
  100. } else {
  101. self.state = .CanTranslate
  102. }
  103. }
  104. }
  105. func initLocalization() -> Void {
  106. self.aiTranslationTitleLabel.stringValue = "AI Translation"
  107. self.translationLanguage.stringValue = "Translation Language:"
  108. self.translateLabel.stringValue = "Translate"
  109. self.copyLabel.stringValue = "Copy"
  110. let languageArr = UserDefaults.standard.array(forKey: "KMAITranslationLanguageArrayKey") as? [String] ?? []
  111. if (languageArr.count > 0) {
  112. self.fromLabel.stringValue = languageArr[0]
  113. self.toLabel.stringValue = languageArr[1]
  114. } else {
  115. self.fromLabel.stringValue = NSLocalizedString("Automatic", comment: "")
  116. self.toLabel.stringValue = NSLocalizedString("English", comment: "")
  117. let languages = [NSLocalizedString("Automatic", comment: ""), NSLocalizedString("English", comment: "")]
  118. UserDefaults.standard.set(languages, forKey: "KMAITranslationLanguageArrayKey")
  119. UserDefaults.standard.synchronize()
  120. }
  121. self.fromContentLabel.stringValue = NSLocalizedString("0/150", comment: "")
  122. }
  123. override func mouseDown(with event: NSEvent) {
  124. if self.popover != nil {
  125. self.popover?.close()
  126. self.popover = nil
  127. }
  128. if self.popoverTips != nil {
  129. self.popoverTips?.close()
  130. self.popoverTips = nil
  131. }
  132. }
  133. // MARK: Get、Set
  134. var state: KMAITranslationState {
  135. get {
  136. return _state
  137. }
  138. set {
  139. _state = newValue
  140. self.refreshAIUI()
  141. }
  142. }
  143. // MARK: Private Methods
  144. func languageAbbreviation(_ language: String) -> String {
  145. if language == "Automatic" {
  146. return "auto"
  147. } else if language == "English" {
  148. return "en"
  149. } else if language == "Simplified Chinese" {
  150. return "zh"
  151. } else if language == "Traditional Chinese" {
  152. return "cht"
  153. } else if language == "Japanese" {
  154. return "jp"
  155. } else if language == "Korean" {
  156. return "kor"
  157. } else if language == "French" {
  158. return "fra"
  159. } else if language == "Spanish" {
  160. return "spa"
  161. } else if language == "Italian" {
  162. return "it"
  163. } else if language == "German" {
  164. return "de"
  165. } else if language == "Portuguese" {
  166. return "pt"
  167. } else if language == "Russian" {
  168. return "ru"
  169. } else if language == "Vietnamese" {
  170. return "vie"
  171. } else if language == "Thai" {
  172. return "th"
  173. } else if language == "Arabic" {
  174. return "ara"
  175. } else if language == "Greek" {
  176. return "el"
  177. } else if language == "Bulgarian" {
  178. return "bul"
  179. } else if language == "Finnish" {
  180. return "fin"
  181. } else if language == "Slovene" {
  182. return "slo"
  183. } else if language == "Dutch" {
  184. return "nl"
  185. } else if language == "Czech" {
  186. return "cs"
  187. } else if language == "Swedish" {
  188. return "swe"
  189. } else if language == "Polish" {
  190. return "pl"
  191. } else if language == "Danish" {
  192. return "dan"
  193. } else if language == "Romanian" {
  194. return "rom"
  195. } else if language == "Hungarian" {
  196. return "hu"
  197. }
  198. return "auto"
  199. }
  200. func refreshAIUI() -> Void {
  201. self.fromContentLabel.stringValue = String(format: "%d/150", self.fromContentTextField.stringValue.count)
  202. self.copyLabel.stringValue = NSLocalizedString("Copy", comment: "")
  203. if self.state == .CanTranslate {
  204. self.fromContentLabel.textColor = NSColor.km_init(hex: "#94989C")
  205. self.translateBox.fillColor = NSColor.km_init(hex: "#1770F4")
  206. self.translateBox.borderWidth = 0.0
  207. self.translateLabel.textColor = NSColor.km_init(hex: "#FFFFFF")
  208. self.copyBox.fillColor = NSColor.km_init(hex: "#EDEEF0")
  209. self.copyBox.borderWidth = 1.0
  210. self.copyBox.borderColor = NSColor.km_init(hex: "#DFE1E5")
  211. self.copyLabel.textColor = NSColor.km_init(hex: "#B1B3B8")
  212. self.errorView.isHidden = true
  213. } else if self.state == .CanCopy {
  214. self.fromContentLabel.textColor = NSColor.km_init(hex: "#94989C")
  215. self.translateBox.fillColor = NSColor.km_init(hex: "#1770F4")
  216. self.translateBox.borderWidth = 0.0
  217. self.translateLabel.textColor = NSColor.km_init(hex: "#FFFFFF")
  218. self.copyBox.fillColor = NSColor.km_init(hex: "#FFFFFF")
  219. self.copyBox.borderWidth = 1.0
  220. self.copyBox.borderColor = NSColor.km_init(hex: "#DFE1E5")
  221. self.copyLabel.textColor = NSColor.km_init(hex: "#252629")
  222. self.errorView.isHidden = true
  223. } else if self.state == .Copyed {
  224. self.fromContentLabel.textColor = NSColor.km_init(hex: "#94989C")
  225. self.translateBox.fillColor = NSColor.km_init(hex: "#1770F4")
  226. self.translateBox.borderWidth = 0.0
  227. self.translateLabel.textColor = NSColor.km_init(hex: "#FFFFFF")
  228. self.copyBox.fillColor = NSColor.km_init(hex: "#36383B")
  229. self.copyBox.borderWidth = 0.0
  230. self.copyLabel.textColor = NSColor.km_init(hex: "#FFFFFF")
  231. self.copyLabel.stringValue = NSLocalizedString("Copied", comment: "")
  232. self.errorView.isHidden = true
  233. } else if self.state == .Exceed { // 是否超过
  234. self.fromContentLabel.textColor = NSColor.km_init(hex: "#F3465B")
  235. self.translateBox.fillColor = NSColor.km_init(hex: "#1770F4")
  236. self.translateBox.borderWidth = 0.0
  237. self.translateLabel.textColor = NSColor.km_init(hex: "#FFFFFF")
  238. self.copyBox.fillColor = NSColor.km_init(hex: "#EDEEF0")
  239. self.copyBox.borderWidth = 1.0
  240. self.copyBox.borderColor = NSColor.km_init(hex: "#DFE1E5")
  241. self.copyLabel.textColor = NSColor.km_init(hex: "#B1B3B8")
  242. self.errorView.isHidden = true
  243. } else if self.state == .Error {
  244. if self.fromContentTextField.stringValue.count > 150 {
  245. self.fromContentLabel.textColor = NSColor.km_init(hex: "#F3465B")
  246. } else {
  247. self.fromContentLabel.textColor = NSColor.km_init(hex: "#94989C")
  248. }
  249. self.translateBox.fillColor = NSColor.km_init(hex: "#1770F4")
  250. self.translateBox.borderWidth = 0.0
  251. self.translateLabel.textColor = NSColor.km_init(hex: "#FFFFFF")
  252. self.copyBox.fillColor = NSColor.km_init(hex: "#EDEEF0")
  253. self.copyBox.borderWidth = 1.0
  254. self.copyBox.borderColor = NSColor.km_init(hex: "#DFE1E5")
  255. self.copyLabel.textColor = NSColor.km_init(hex: "#B1B3B8")
  256. self.errorView.isHidden = false
  257. } else {
  258. self.fromContentLabel.textColor = NSColor.km_init(hex: "#94989C")
  259. self.translateBox.fillColor = NSColor.km_init(hex: "#BDDFFD")
  260. self.translateBox.borderWidth = 0.0
  261. self.translateLabel.textColor = NSColor.km_init(hex: "#F7F8FA")
  262. self.copyBox.fillColor = NSColor.km_init(hex: "#EDEEF0")
  263. self.copyBox.borderWidth = 1.0
  264. self.copyBox.borderColor = NSColor.km_init(hex: "#DFE1E5")
  265. self.copyLabel.textColor = NSColor.km_init(hex: "#B1B3B8")
  266. self.errorView.isHidden = true
  267. }
  268. }
  269. func showProgressWindow() {
  270. let progress = SKProgressController()
  271. progress.window?.backgroundColor = NSColor.km_init(hex: "#36383B")
  272. progress.window?.contentView?.wantsLayer = true
  273. progress.window?.contentView?.layer?.backgroundColor = NSColor.km_init(hex: "#36383B").cgColor
  274. progress.progressField.textColor = NSColor.white
  275. progress.message = NSLocalizedString("Translating...", comment: "")
  276. progress.closeBlock = {}
  277. self.progressController = progress
  278. self.window?.beginSheet(progress.window!)
  279. }
  280. func hiddenProgressWindow() {
  281. if (self.progressController != nil) {
  282. self.window?.endSheet((self.progressController?.window)!)
  283. self.progressController = nil
  284. }
  285. }
  286. // MARK: Action
  287. @IBAction func fromLanguageAction(_ sender: NSButton) {
  288. self.languageAction(true)
  289. }
  290. @IBAction func toLanguageAction(_ sender: NSButton) {
  291. self.languageAction(false)
  292. }
  293. func languageAction(_ isFromLanguage: Bool) -> Void {
  294. if (self.popover != nil && self.popover!.isShown) {
  295. self.popover?.close()
  296. self.popover = nil
  297. return
  298. }
  299. var languages = self.fromLanguages
  300. if !isFromLanguage {
  301. languages = self.toLanguages
  302. }
  303. let vc: KMAILanguagePopVC = KMAILanguagePopVC().initWithPopViewDataArr(languages)
  304. let createFilePopover: NSPopover = NSPopover.init()
  305. self.popover = createFilePopover
  306. createFilePopover.contentViewController = vc
  307. createFilePopover.animates = true
  308. createFilePopover.behavior = .semitransient
  309. createFilePopover.setValue(true, forKey: "shouldHideAnchor")
  310. if isFromLanguage {
  311. vc.selectString = self.fromLabel.stringValue
  312. } else {
  313. vc.selectString = self.toLabel.stringValue
  314. }
  315. vc.downCallback = { [unowned self] (language: String) -> Void in
  316. createFilePopover.close()
  317. if isFromLanguage {
  318. self.fromLabel.stringValue = language
  319. self.fromStr = self.languageAbbreviation(language)
  320. } else {
  321. self.toLabel.stringValue = language
  322. self.toStr = self.languageAbbreviation(language)
  323. }
  324. self.toContentTextView.string = ""
  325. if self.fromContentTextField.stringValue.count > 0 {
  326. if self.fromContentTextField.stringValue.count > 150 {
  327. self.state = .Exceed
  328. } else {
  329. self.state = .CanTranslate
  330. }
  331. } else {
  332. self.state = .None
  333. }
  334. }
  335. if isFromLanguage {
  336. createFilePopover.show(relativeTo: CGRect(x: fromBox.bounds.origin.x, y: 10, width: fromBox.bounds.size.width, height: fromBox.bounds.size.height), of: fromBox, preferredEdge: .maxY)
  337. vc.customBoxWidthLayoutConstraint.constant = fromBox.frame.width
  338. } else {
  339. createFilePopover.show(relativeTo: CGRect(x: toBox.bounds.origin.x, y: 10, width: toBox.bounds.size.width, height: toBox.bounds.size.height), of: toBox, preferredEdge: .maxY)
  340. vc.customBoxWidthLayoutConstraint.constant = toBox.frame.width
  341. }
  342. }
  343. @IBAction func aiTranslationAction(_ sender: NSButton) {
  344. if self.state == .CanTranslate || self.state == .CanCopy || self.state == .Copyed || self.state == .Exceed || self.state == .Error {
  345. if !KMLightMemberManager.manager.isLogin() {
  346. KMLoginWindowController.show(window: NSApp.mainWindow!)
  347. return
  348. }
  349. self._aiTranslationAction(sender)
  350. // Task { @MainActor in
  351. // if await (KMLightMemberManager.manager.canPayFunction() == false) {
  352. // let _ = KMSubscribeWaterMarkWindowController.show(window: self.window!, type: .aiTranslate) { isSubscribeSuccess, _, isClose in
  353. // if (isClose) {
  354. // return
  355. // }
  356. // if (isSubscribeSuccess) {
  357. // self._aiTranslationAction(sender)
  358. // return
  359. // }
  360. // }
  361. // return
  362. // }
  363. // self._aiTranslationAction(sender)
  364. // }
  365. }
  366. }
  367. private func _aiTranslationAction(_ sender: NSButton) {
  368. DispatchQueue.main.async {
  369. self.showProgressWindow()
  370. }
  371. self.errorView.isHidden = true
  372. self.toContentTextView.string = ""
  373. let languages = [self.fromLabel.stringValue, self.toLabel.stringValue]
  374. UserDefaults.standard.set(languages, forKey: "KMAITranslationLanguageArrayKey")
  375. UserDefaults.standard.synchronize()
  376. self.fromStr = self.languageAbbreviation(self.fromLabel.stringValue)
  377. self.toStr = self.languageAbbreviation(self.toLabel.stringValue)
  378. let infoDictionary = Bundle .main.infoDictionary!
  379. let majorVersion = infoDictionary["CFBundleShortVersionString"]
  380. KMRequestServerManager.manager.aiTranslationTextTrans(q: self.fromContentTextField.stringValue, from: self.fromStr, to: self.toStr, version: majorVersion as! String) { [unowned self] success, result in
  381. DispatchQueue.main.async {
  382. self.hiddenProgressWindow()
  383. }
  384. if success {
  385. let result: NSDictionary = result!.result
  386. // let form: String = result["form"] as! String
  387. // let to: String = result["to"] as! String
  388. // let src: String = result["src"] as! String
  389. let dst: String = result["dst"] as! String
  390. DispatchQueue.main.async {
  391. self.state = .CanCopy
  392. self.toContentTextView.string = dst
  393. }
  394. } else {
  395. let result: String = result!.message
  396. DispatchQueue.main.async {
  397. self.errorLabel.stringValue = result
  398. self.state = .Error
  399. self.toContentTextView.string = ""
  400. }
  401. }
  402. }
  403. }
  404. @IBAction func copyAction(_ sender: NSButton) {
  405. if self.state == .CanCopy || self.state == .Copyed {
  406. if !KMLightMemberManager.manager.isLogin() {
  407. KMLoginWindowController.show(window: NSApp.mainWindow!)
  408. return
  409. }
  410. self._copyAction(sender)
  411. // Task { @MainActor in
  412. // if await (KMLightMemberManager.manager.canPayFunction() == false) {
  413. // let _ = KMSubscribeWaterMarkWindowController.show(window: self.window!, type: .aiTranslate) { isSubscribeSuccess, _, isClose in
  414. // if (isClose) {
  415. // return
  416. // }
  417. // if (isSubscribeSuccess) {
  418. // self._copyAction(sender)
  419. // return
  420. // }
  421. // }
  422. // return
  423. // }
  424. //
  425. // self._copyAction(sender)
  426. // }
  427. }
  428. }
  429. private func _copyAction(_ sender: NSButton) {
  430. let pasteboard = NSPasteboard.general
  431. pasteboard.clearContents()
  432. pasteboard.setString(self.toContentTextView.string, forType: .string)
  433. self.state = .Copyed
  434. }
  435. @IBAction func closeButtonAction(_ sender: NSButton) {
  436. if self.popover != nil {
  437. self.popover?.close()
  438. self.popover = nil
  439. }
  440. if self.popoverTips != nil {
  441. self.popoverTips?.close()
  442. self.popoverTips = nil
  443. }
  444. self.window?.close()
  445. }
  446. @IBAction func tipsButtonAction(_ sender: NSButton) {
  447. if (self.popoverTips != nil) {
  448. self.popoverTips?.close()
  449. self.popoverTips = nil
  450. return
  451. }
  452. let pop: NSPopover = NSPopover.init()
  453. self.popoverTips = pop
  454. let controller = KMSignatureHelpViewController.init()
  455. pop.contentViewController = controller
  456. controller.tipString = NSLocalizedString("Limit document size to 10M, document page number to 30, 10w characters per month.", comment: "")
  457. pop.setValue(true, forKey: "shouldHideAnchor")
  458. pop.show(relativeTo: NSRect(x: 0, y: -8, width: NSWidth(sender.bounds), height: NSHeight(sender.bounds)), of: sender, preferredEdge: .maxX)
  459. }
  460. }
  461. extension KMAITranslationWindowController: NSTextFieldDelegate {
  462. func controlTextDidEndEditing(_ obj: Notification) {
  463. _ = obj.object as! NSTextField
  464. }
  465. func controlTextDidChange(_ obj: Notification) {
  466. let object = obj.object as! NSTextField
  467. if object.stringValue.count > 0 {
  468. self.state = .CanTranslate
  469. } else {
  470. self.state = .None
  471. }
  472. if object.stringValue.count > 150 {
  473. self.state = .Exceed
  474. }
  475. self.toContentTextView.string = ""
  476. }
  477. }