KMAITranslationWindowController.swift 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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(hex: "#252629")
  60. self.aiTranslationTitleLabel.font = NSFont.SFProTextSemibold(16.0)
  61. self.translationLanguage.textColor = NSColor(hex: "#252629")
  62. self.translationLanguage.font = NSFont.SFProTextRegular(14.0)
  63. self.fromBox.fillColor = NSColor(hex: "#FFFFFF")
  64. self.fromBox.borderColor = NSColor(hex: "#DFE1E5")
  65. self.fromLabel.textColor = NSColor(hex: "#252629")
  66. self.fromLabel.font = NSFont.SFProTextRegular(14.0)
  67. self.toBox.fillColor = NSColor(hex: "#FFFFFF")
  68. self.toBox.borderColor = NSColor(hex: "#DFE1E5")
  69. self.toLabel.textColor = NSColor(hex: "#252629")
  70. self.toLabel.font = NSFont.SFProTextRegular(14.0)
  71. self.languageImageView.image = NSImage(named: "ic_transtate")
  72. self.fromContentBox.borderColor = NSColor(hex: "#1770F4")
  73. self.fromContentBox.fillColor = NSColor(hex: "#FFFFFF")
  74. self.fromContentTextField.textColor = NSColor(hex: "#252629")
  75. self.fromContentTextField.font = NSFont.SFProTextRegular(12.0)
  76. self.fromContentLabel.textColor = NSColor(hex: "#94989C")
  77. self.fromContentLabel.font = NSFont.SFProTextRegular(12.0)
  78. self.toContentBox.borderColor = NSColor(hex: "#DFE1E5")
  79. self.toContentBox.fillColor = NSColor(hex: "#FFFFFF")
  80. self.toContentTextView.textColor = NSColor(hex: "#252629")
  81. self.toContentTextView.font = NSFont.SFProTextRegular(12.0)
  82. self.translateBox.fillColor = NSColor(hex: "#BDDFFD")
  83. self.translateBox.cornerRadius = 4.0
  84. self.translateLabel.textColor = NSColor(hex: "#FFFFFF")
  85. self.translateLabel.font = NSFont.SFProTextRegular(12.0)
  86. self.copyBox.fillColor = NSColor(hex: "#EDEEF0")
  87. self.copyBox.borderColor = NSColor(hex: "#DFE1E5")
  88. self.copyBox.borderWidth = 1.0
  89. self.copyBox.cornerRadius = 4.0
  90. self.copyLabel.textColor = NSColor(hex: "#F7F8FA")
  91. self.copyLabel.font = NSFont.SFProTextRegular(12.0)
  92. self.errorView.isHidden = true
  93. self.errorImageView.image = NSImage(named: "icon_error")
  94. self.errorLabel.textColor = NSColor(hex: "#F3465B")
  95. self.errorLabel.font = NSFont.SFProTextRegular(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. self.fromLabel.stringValue = "Automatic"
  111. self.toLabel.stringValue = "English"
  112. self.fromContentLabel.stringValue = NSLocalizedString("0/150", comment: "")
  113. }
  114. override func mouseDown(with event: NSEvent) {
  115. if self.popover != nil {
  116. self.popover?.close()
  117. self.popover = nil
  118. }
  119. if self.popoverTips != nil {
  120. self.popoverTips?.close()
  121. self.popoverTips = nil
  122. }
  123. }
  124. // MARK: Get、Set
  125. var state: KMAITranslationState {
  126. get {
  127. return _state
  128. }
  129. set {
  130. _state = newValue
  131. self.refreshAIUI()
  132. }
  133. }
  134. // MARK: Private Methods
  135. func languageAbbreviation(_ language: String) -> String {
  136. if language == "Automatic" {
  137. return "auto"
  138. } else if language == "English" {
  139. return "en"
  140. } else if language == "Simplified Chinese" {
  141. return "zh"
  142. } else if language == "Traditional Chinese" {
  143. return "cht"
  144. } else if language == "Japanese" {
  145. return "jp"
  146. } else if language == "Korean" {
  147. return "kor"
  148. } else if language == "French" {
  149. return "fra"
  150. } else if language == "Spanish" {
  151. return "spa"
  152. } else if language == "Italian" {
  153. return "it"
  154. } else if language == "German" {
  155. return "de"
  156. } else if language == "Portuguese" {
  157. return "pt"
  158. } else if language == "Russian" {
  159. return "ru"
  160. } else if language == "Vietnamese" {
  161. return "vie"
  162. } else if language == "Thai" {
  163. return "th"
  164. } else if language == "Arabic" {
  165. return "ara"
  166. } else if language == "Greek" {
  167. return "el"
  168. } else if language == "Bulgarian" {
  169. return "bul"
  170. } else if language == "Finnish" {
  171. return "fin"
  172. } else if language == "Slovene" {
  173. return "slo"
  174. } else if language == "Dutch" {
  175. return "nl"
  176. } else if language == "Czech" {
  177. return "cs"
  178. } else if language == "Swedish" {
  179. return "swe"
  180. } else if language == "Polish" {
  181. return "pl"
  182. } else if language == "Danish" {
  183. return "dan"
  184. } else if language == "Romanian" {
  185. return "rom"
  186. } else if language == "Hungarian" {
  187. return "hu"
  188. }
  189. return "auto"
  190. }
  191. func refreshAIUI() -> Void {
  192. self.fromContentLabel.stringValue = String(format: "%d/150", self.fromContentTextField.stringValue.count)
  193. self.copyLabel.stringValue = NSLocalizedString("Copy", comment: "")
  194. if self.state == .CanTranslate {
  195. self.fromContentLabel.textColor = NSColor(hex: "#94989C")
  196. self.translateBox.fillColor = NSColor(hex: "#1770F4")
  197. self.translateBox.borderWidth = 0.0
  198. self.translateLabel.textColor = NSColor(hex: "#FFFFFF")
  199. self.copyBox.fillColor = NSColor(hex: "#EDEEF0")
  200. self.copyBox.borderWidth = 1.0
  201. self.copyBox.borderColor = NSColor(hex: "#DFE1E5")
  202. self.copyLabel.textColor = NSColor(hex: "#B1B3B8")
  203. self.errorView.isHidden = true
  204. } else if self.state == .CanCopy {
  205. self.fromContentLabel.textColor = NSColor(hex: "#94989C")
  206. self.translateBox.fillColor = NSColor(hex: "#1770F4")
  207. self.translateBox.borderWidth = 0.0
  208. self.translateLabel.textColor = NSColor(hex: "#FFFFFF")
  209. self.copyBox.fillColor = NSColor(hex: "#FFFFFF")
  210. self.copyBox.borderWidth = 1.0
  211. self.copyBox.borderColor = NSColor(hex: "#DFE1E5")
  212. self.copyLabel.textColor = NSColor(hex: "#252629")
  213. self.errorView.isHidden = true
  214. } else if self.state == .Copyed {
  215. self.fromContentLabel.textColor = NSColor(hex: "#94989C")
  216. self.translateBox.fillColor = NSColor(hex: "#1770F4")
  217. self.translateBox.borderWidth = 0.0
  218. self.translateLabel.textColor = NSColor(hex: "#FFFFFF")
  219. self.copyBox.fillColor = NSColor(hex: "#36383B")
  220. self.copyBox.borderWidth = 0.0
  221. self.copyLabel.textColor = NSColor(hex: "#FFFFFF")
  222. self.copyLabel.stringValue = NSLocalizedString("Copied", comment: "")
  223. self.errorView.isHidden = true
  224. } else if self.state == .Exceed { // 是否超过
  225. self.fromContentLabel.textColor = NSColor(hex: "#F3465B")
  226. self.translateBox.fillColor = NSColor(hex: "#1770F4")
  227. self.translateBox.borderWidth = 0.0
  228. self.translateLabel.textColor = NSColor(hex: "#FFFFFF")
  229. self.copyBox.fillColor = NSColor(hex: "#EDEEF0")
  230. self.copyBox.borderWidth = 1.0
  231. self.copyBox.borderColor = NSColor(hex: "#DFE1E5")
  232. self.copyLabel.textColor = NSColor(hex: "#B1B3B8")
  233. self.errorView.isHidden = true
  234. } else if self.state == .Error {
  235. if self.fromContentTextField.stringValue.count > 150 {
  236. self.fromContentLabel.textColor = NSColor(hex: "#F3465B")
  237. } else {
  238. self.fromContentLabel.textColor = NSColor(hex: "#94989C")
  239. }
  240. self.translateBox.fillColor = NSColor(hex: "#1770F4")
  241. self.translateBox.borderWidth = 0.0
  242. self.translateLabel.textColor = NSColor(hex: "#FFFFFF")
  243. self.copyBox.fillColor = NSColor(hex: "#EDEEF0")
  244. self.copyBox.borderWidth = 1.0
  245. self.copyBox.borderColor = NSColor(hex: "#DFE1E5")
  246. self.copyLabel.textColor = NSColor(hex: "#B1B3B8")
  247. self.errorView.isHidden = false
  248. } else {
  249. self.fromContentLabel.textColor = NSColor(hex: "#94989C")
  250. self.translateBox.fillColor = NSColor(hex: "#BDDFFD")
  251. self.translateBox.borderWidth = 0.0
  252. self.translateLabel.textColor = NSColor(hex: "#F7F8FA")
  253. self.copyBox.fillColor = NSColor(hex: "#EDEEF0")
  254. self.copyBox.borderWidth = 1.0
  255. self.copyBox.borderColor = NSColor(hex: "#DFE1E5")
  256. self.copyLabel.textColor = NSColor(hex: "#B1B3B8")
  257. self.errorView.isHidden = true
  258. }
  259. }
  260. func showProgressWindow() {
  261. let progress = SKProgressController()
  262. progress.message = NSLocalizedString("Translating...", comment: "")
  263. progress.window?.backgroundColor = NSColor(hex: "#36383B")
  264. progress.window?.contentView?.wantsLayer = true
  265. progress.window?.contentView?.layer?.backgroundColor = NSColor(hex: "#36383B").cgColor
  266. progress.progressField.textColor = NSColor.white
  267. progress.closeBlock = { [unowned self] in
  268. }
  269. self.progressController = progress
  270. self.window?.beginSheet(progress.window!)
  271. }
  272. func hiddenProgressWindow() {
  273. if (self.progressController != nil) {
  274. self.window?.endSheet((self.progressController?.window)!)
  275. self.progressController = nil
  276. }
  277. }
  278. // MARK: Action
  279. @IBAction func fromLanguageAction(_ sender: NSButton) {
  280. self.languageAction(true)
  281. }
  282. @IBAction func toLanguageAction(_ sender: NSButton) {
  283. self.languageAction(false)
  284. }
  285. func languageAction(_ isFromLanguage: Bool) -> Void {
  286. if (self.popover != nil && self.popover!.isShown) {
  287. self.popover?.close()
  288. self.popover = nil
  289. return
  290. }
  291. var languages = self.fromLanguages
  292. if !isFromLanguage {
  293. languages = self.toLanguages
  294. }
  295. let vc: KMAILanguagePopVC = KMAILanguagePopVC().initWithPopViewDataArr(languages)
  296. let createFilePopover: NSPopover = NSPopover.init()
  297. self.popover = createFilePopover
  298. createFilePopover.contentViewController = vc
  299. createFilePopover.animates = true
  300. createFilePopover.behavior = .semitransient
  301. createFilePopover.setValue(true, forKey: "shouldHideAnchor")
  302. if isFromLanguage {
  303. vc.selectString = self.fromLabel.stringValue
  304. } else {
  305. vc.selectString = self.toLabel.stringValue
  306. }
  307. vc.downCallback = { [unowned self] (language: String) -> Void in
  308. createFilePopover.close()
  309. if isFromLanguage {
  310. self.fromLabel.stringValue = language
  311. self.fromStr = self.languageAbbreviation(language)
  312. } else {
  313. self.toLabel.stringValue = language
  314. self.toStr = self.languageAbbreviation(language)
  315. }
  316. self.toContentTextView.string = ""
  317. if self.fromContentTextField.stringValue.count > 0 {
  318. if self.fromContentTextField.stringValue.count > 150 {
  319. self.state = .Exceed
  320. } else {
  321. self.state = .CanTranslate
  322. }
  323. } else {
  324. self.state = .None
  325. }
  326. }
  327. if isFromLanguage {
  328. 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)
  329. vc.customBoxWidthLayoutConstraint.constant = fromBox.frame.width
  330. } else {
  331. 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)
  332. vc.customBoxWidthLayoutConstraint.constant = toBox.frame.width
  333. }
  334. }
  335. @IBAction func aiTranslationAction(_ sender: NSButton) {
  336. if self.state == .CanTranslate || self.state == .CanCopy || self.state == .Copyed || self.state == .Exceed || self.state == .Error {
  337. if !KMLightMemberManager.manager.isLogin() {
  338. KMLoginWindowController.show(window: NSApp.mainWindow!)
  339. return
  340. }
  341. DispatchQueue.main.async {
  342. self.showProgressWindow()
  343. }
  344. self.errorView.isHidden = true
  345. self.toContentTextView.string = ""
  346. let infoDictionary = Bundle .main.infoDictionary!
  347. let majorVersion = infoDictionary["CFBundleShortVersionString"]
  348. KMRequestServerManager.manager.aiTranslationTextTrans(q: self.fromContentTextField.stringValue, from: self.fromStr, to: self.toStr, version: "1.0.1") { [unowned self] success, result in
  349. DispatchQueue.main.async {
  350. self.hiddenProgressWindow()
  351. }
  352. if success {
  353. let result: NSDictionary = result!.result
  354. let form: String = result["form"] as! String
  355. let to: String = result["to"] as! String
  356. let src: String = result["src"] as! String
  357. let dst: String = result["dst"] as! String
  358. DispatchQueue.main.async {
  359. self.state = .CanCopy
  360. self.toContentTextView.string = dst
  361. }
  362. } else {
  363. let result: String = result!.message
  364. DispatchQueue.main.async {
  365. self.errorLabel.stringValue = result
  366. self.state = .Error
  367. self.toContentTextView.string = ""
  368. }
  369. }
  370. }
  371. }
  372. }
  373. @IBAction func copyAction(_ sender: NSButton) {
  374. if self.state == .CanCopy || self.state == .Copyed {
  375. if !KMLightMemberManager.manager.isLogin() {
  376. KMLoginWindowController.show(window: NSApp.mainWindow!)
  377. return
  378. }
  379. let pasteboard = NSPasteboard.general
  380. pasteboard.clearContents()
  381. pasteboard.setString(self.toContentTextView.string, forType: .string)
  382. self.state = .Copyed
  383. }
  384. }
  385. @IBAction func closeButtonAction(_ sender: NSButton) {
  386. if self.popover != nil {
  387. self.popover?.close()
  388. self.popover = nil
  389. }
  390. if self.popoverTips != nil {
  391. self.popoverTips?.close()
  392. self.popoverTips = nil
  393. }
  394. self.window?.close()
  395. }
  396. @IBAction func tipsButtonAction(_ sender: NSButton) {
  397. if (self.popoverTips != nil) {
  398. self.popoverTips?.close()
  399. self.popoverTips = nil
  400. return
  401. }
  402. let pop: NSPopover = NSPopover.init()
  403. self.popoverTips = pop
  404. let controller = KMSignatureHelpViewController.init()
  405. pop.contentViewController = controller
  406. controller.tipString = NSLocalizedString("Limit document size to 10M, document page number to 30, 10w characters per month.", comment: "")
  407. pop.setValue(true, forKey: "shouldHideAnchor")
  408. pop.show(relativeTo: NSRect(x: 0, y: -8, width: NSWidth(sender.bounds), height: NSHeight(sender.bounds)), of: sender, preferredEdge: .maxX)
  409. }
  410. }
  411. extension KMAITranslationWindowController: NSTextFieldDelegate {
  412. func controlTextDidEndEditing(_ obj: Notification) {
  413. let object = obj.object as! NSTextField
  414. }
  415. func controlTextDidChange(_ obj: Notification) {
  416. let object = obj.object as! NSTextField
  417. if object.stringValue.count > 0 {
  418. self.state = .CanTranslate
  419. } else {
  420. self.state = .None
  421. }
  422. if object.stringValue.count > 150 {
  423. self.state = .Exceed
  424. }
  425. self.toContentTextView.string = ""
  426. }
  427. }