KMAITranslationWindowController.swift 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. //
  2. // KMAITranslationWindowController.swift
  3. // PDF Reader Pro
  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 = { [weak self] in
  277. self?.hiddenProgressWindow()
  278. }
  279. self.progressController = progress
  280. self.window?.beginSheet(progress.window!)
  281. }
  282. func hiddenProgressWindow() {
  283. if (self.progressController != nil) {
  284. self.window?.endSheet((self.progressController?.window)!)
  285. self.progressController = nil
  286. KMRequestServer.requestServer.task?.cancel()
  287. }
  288. }
  289. // MARK: Action
  290. @IBAction func fromLanguageAction(_ sender: NSButton) {
  291. self.languageAction(true)
  292. }
  293. @IBAction func toLanguageAction(_ sender: NSButton) {
  294. self.languageAction(false)
  295. }
  296. func languageAction(_ isFromLanguage: Bool) -> Void {
  297. if (self.popover != nil && self.popover!.isShown) {
  298. self.popover?.close()
  299. self.popover = nil
  300. return
  301. }
  302. var languages = self.fromLanguages
  303. if !isFromLanguage {
  304. languages = self.toLanguages
  305. }
  306. let vc: KMAILanguagePopVC = KMAILanguagePopVC().initWithPopViewDataArr(languages)
  307. let createFilePopover: NSPopover = NSPopover.init()
  308. self.popover = createFilePopover
  309. createFilePopover.contentViewController = vc
  310. createFilePopover.animates = true
  311. createFilePopover.behavior = .semitransient
  312. createFilePopover.setValue(true, forKey: "shouldHideAnchor")
  313. if isFromLanguage {
  314. vc.selectString = self.fromLabel.stringValue
  315. } else {
  316. vc.selectString = self.toLabel.stringValue
  317. }
  318. vc.downCallback = { [unowned self] (language: String) -> Void in
  319. createFilePopover.close()
  320. if isFromLanguage {
  321. self.fromLabel.stringValue = language
  322. self.fromStr = self.languageAbbreviation(language)
  323. } else {
  324. self.toLabel.stringValue = language
  325. self.toStr = self.languageAbbreviation(language)
  326. }
  327. self.toContentTextView.string = ""
  328. if self.fromContentTextField.stringValue.count > 0 {
  329. if self.fromContentTextField.stringValue.count > 150 {
  330. self.state = .Exceed
  331. } else {
  332. self.state = .CanTranslate
  333. }
  334. } else {
  335. self.state = .None
  336. }
  337. }
  338. if isFromLanguage {
  339. 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)
  340. vc.customBoxWidthLayoutConstraint.constant = fromBox.frame.width
  341. } else {
  342. 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)
  343. vc.customBoxWidthLayoutConstraint.constant = toBox.frame.width
  344. }
  345. }
  346. @IBAction func aiTranslationAction(_ sender: NSButton) {
  347. if self.state == .CanTranslate || self.state == .CanCopy || self.state == .Copyed || self.state == .Exceed || self.state == .Error {
  348. if !KMLightMemberManager.manager.isLogin() {
  349. KMLoginWindowController.show(window: NSApp.mainWindow!)
  350. return
  351. }
  352. self._aiTranslationAction(sender)
  353. // Task { @MainActor in
  354. // if await (KMLightMemberManager.manager.canPayFunction() == false) {
  355. // let _ = KMSubscribeWaterMarkWindowController.show(window: self.window!, type: .aiTranslate) { isSubscribeSuccess, _, isClose in
  356. // if (isClose) {
  357. // return
  358. // }
  359. // if (isSubscribeSuccess) {
  360. // self._aiTranslationAction(sender)
  361. // return
  362. // }
  363. // }
  364. // return
  365. // }
  366. // self._aiTranslationAction(sender)
  367. // }
  368. }
  369. }
  370. private func _aiTranslationAction(_ sender: NSButton) {
  371. DispatchQueue.main.async {
  372. self.showProgressWindow()
  373. }
  374. self.errorView.isHidden = true
  375. self.toContentTextView.string = ""
  376. let languages = [self.fromLabel.stringValue, self.toLabel.stringValue]
  377. UserDefaults.standard.set(languages, forKey: "KMAITranslationLanguageArrayKey")
  378. UserDefaults.standard.synchronize()
  379. self.fromStr = self.languageAbbreviation(self.fromLabel.stringValue)
  380. self.toStr = self.languageAbbreviation(self.toLabel.stringValue)
  381. let infoDictionary = Bundle .main.infoDictionary!
  382. let majorVersion = infoDictionary["CFBundleShortVersionString"]
  383. KMRequestServerManager.manager.aiTranslationTextTrans(q: self.fromContentTextField.stringValue, from: self.fromStr, to: self.toStr, version: majorVersion as! String) { [unowned self] success, result in
  384. DispatchQueue.main.async {
  385. self.hiddenProgressWindow()
  386. }
  387. if success {
  388. let result: NSDictionary = result!.result
  389. // let form: String = result["form"] as! String
  390. // let to: String = result["to"] as! String
  391. // let src: String = result["src"] as! String
  392. let dst: String = result["dst"] as! String
  393. DispatchQueue.main.async {
  394. self.state = .CanCopy
  395. self.toContentTextView.string = dst
  396. }
  397. } else {
  398. let result: String = result!.message
  399. DispatchQueue.main.async {
  400. self.errorLabel.stringValue = result
  401. self.state = .Error
  402. self.toContentTextView.string = ""
  403. }
  404. }
  405. }
  406. }
  407. @IBAction func copyAction(_ sender: NSButton) {
  408. if self.state == .CanCopy || self.state == .Copyed {
  409. if !KMLightMemberManager.manager.isLogin() {
  410. KMLoginWindowController.show(window: NSApp.mainWindow!)
  411. return
  412. }
  413. self._copyAction(sender)
  414. // Task { @MainActor in
  415. // if await (KMLightMemberManager.manager.canPayFunction() == false) {
  416. // let _ = KMSubscribeWaterMarkWindowController.show(window: self.window!, type: .aiTranslate) { isSubscribeSuccess, _, isClose in
  417. // if (isClose) {
  418. // return
  419. // }
  420. // if (isSubscribeSuccess) {
  421. // self._copyAction(sender)
  422. // return
  423. // }
  424. // }
  425. // return
  426. // }
  427. //
  428. // self._copyAction(sender)
  429. // }
  430. }
  431. }
  432. private func _copyAction(_ sender: NSButton) {
  433. let pasteboard = NSPasteboard.general
  434. pasteboard.clearContents()
  435. pasteboard.setString(self.toContentTextView.string, forType: .string)
  436. self.state = .Copyed
  437. }
  438. @IBAction func closeButtonAction(_ sender: NSButton) {
  439. if self.popover != nil {
  440. self.popover?.close()
  441. self.popover = nil
  442. }
  443. if self.popoverTips != nil {
  444. self.popoverTips?.close()
  445. self.popoverTips = nil
  446. }
  447. self.window?.close()
  448. }
  449. @IBAction func tipsButtonAction(_ sender: NSButton) {
  450. if (self.popoverTips != nil) {
  451. self.popoverTips?.close()
  452. self.popoverTips = nil
  453. return
  454. }
  455. let pop: NSPopover = NSPopover.init()
  456. self.popoverTips = pop
  457. let controller = KMSignatureHelpViewController.init()
  458. pop.contentViewController = controller
  459. controller.tipString = NSLocalizedString("Limit document size to 10M, document page number to 30, 10w characters per month.", comment: "")
  460. pop.setValue(true, forKey: "shouldHideAnchor")
  461. pop.show(relativeTo: NSRect(x: 0, y: -8, width: NSWidth(sender.bounds), height: NSHeight(sender.bounds)), of: sender, preferredEdge: .maxX)
  462. }
  463. }
  464. extension KMAITranslationWindowController: NSTextFieldDelegate {
  465. func controlTextDidEndEditing(_ obj: Notification) {
  466. _ = obj.object as! NSTextField
  467. }
  468. func controlTextDidChange(_ obj: Notification) {
  469. let object = obj.object as! NSTextField
  470. if object.stringValue.count > 0 {
  471. self.state = .CanTranslate
  472. } else {
  473. self.state = .None
  474. }
  475. if object.stringValue.count > 150 {
  476. self.state = .Exceed
  477. }
  478. self.toContentTextView.string = ""
  479. }
  480. }