|
@@ -12,9 +12,6 @@ import Cocoa
|
|
|
case AIErrorCorrection
|
|
|
}
|
|
|
|
|
|
-//class KMAIRewritingVC: NSViewController {
|
|
|
-//}
|
|
|
-
|
|
|
class KMAIRewritingVC: NSViewController {
|
|
|
|
|
|
@IBOutlet weak var aiRewritingLabel: NSTextField!
|
|
@@ -39,16 +36,19 @@ class KMAIRewritingVC: NSViewController {
|
|
|
@IBOutlet weak var errorView: NSView!
|
|
|
@IBOutlet weak var errorImageView: NSImageView!
|
|
|
@IBOutlet weak var errorLabel: NSTextField!
|
|
|
-
|
|
|
- @IBOutlet weak var progressView: NSProgressIndicator!
|
|
|
|
|
|
var state: KMAIRewritingState = .AIRewriting
|
|
|
|
|
|
- var isExceed: Bool = false
|
|
|
- var canRewriting: Bool = false
|
|
|
- var canCopy: Bool = false
|
|
|
+ var _uiState: KMAITranslationState = .None
|
|
|
+
|
|
|
var aiRewritingStr: String = ""
|
|
|
var aiErrorCorrectionStr: String = ""
|
|
|
+ var aiRewritingResultStr: String = ""
|
|
|
+ var aiErrorCorrectionResultStr: String = ""
|
|
|
+ var aiRewritingErrorStr: String = ""
|
|
|
+ var aiErrorCorrectionErrorStr: String = ""
|
|
|
+
|
|
|
+ var progressController: SKProgressController?
|
|
|
|
|
|
override func viewDidLoad() {
|
|
|
super.viewDidLoad()
|
|
@@ -61,8 +61,6 @@ class KMAIRewritingVC: NSViewController {
|
|
|
// MARK: initialize
|
|
|
|
|
|
func initializeUI() -> Void {
|
|
|
- self.progressView.isHidden = true
|
|
|
-
|
|
|
self.aiRewritingLabel.textColor = NSColor(hex: "#252629")
|
|
|
self.aiRewritingLabel.font = NSFont.SFProTextSemibold(20.0)
|
|
|
self.aiRewritingSubLabel.textColor = NSColor(hex: "#94989C")
|
|
@@ -89,6 +87,12 @@ class KMAIRewritingVC: NSViewController {
|
|
|
self.errorImageView.image = NSImage(named: "icon_error")
|
|
|
self.errorLabel.textColor = NSColor(hex: "#F3465B")
|
|
|
self.errorLabel.font = NSFont.SFProTextRegular(12.0)
|
|
|
+
|
|
|
+ if self.state == .AIRewriting {
|
|
|
+ self.emptyImageView.image = NSImage(named: "ic_ai_rewriting_empty")
|
|
|
+ } else if self.state == .AIErrorCorrection {
|
|
|
+ self.emptyImageView.image = NSImage(named: "ic_ai_ErrorCorrection_empty")
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
func initLocalization() -> Void {
|
|
@@ -102,6 +106,7 @@ class KMAIRewritingVC: NSViewController {
|
|
|
self.rewritingTextField.placeholderString = NSLocalizedString("Please enter text content here...", comment: "")
|
|
|
self.wordsLabel.stringValue = NSLocalizedString("0/150", comment: "")
|
|
|
self.rewritingTextField.stringValue = self.aiRewritingStr
|
|
|
+ self.resultTextView.string = self.aiRewritingResultStr
|
|
|
break
|
|
|
case .AIErrorCorrection:
|
|
|
self.aiRewritingLabel.stringValue = NSLocalizedString("AI Error Correction", comment: "")
|
|
@@ -112,51 +117,261 @@ class KMAIRewritingVC: NSViewController {
|
|
|
self.rewritingTextField.placeholderString = NSLocalizedString("Please enter text content here...", comment: "")
|
|
|
self.wordsLabel.stringValue = NSLocalizedString("0/150", comment: "")
|
|
|
self.rewritingTextField.stringValue = self.aiErrorCorrectionStr
|
|
|
+ self.resultTextView.string = self.aiErrorCorrectionResultStr
|
|
|
break
|
|
|
default:
|
|
|
break
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ // MARK: Get、Set
|
|
|
+
|
|
|
+ var uiState: KMAITranslationState {
|
|
|
+ get {
|
|
|
+ return _uiState
|
|
|
+ }
|
|
|
+ set {
|
|
|
+ _uiState = newValue
|
|
|
+
|
|
|
+ self.refreshAIUI()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // MARK: Private Methods
|
|
|
+
|
|
|
+ func refreshAIUI() -> Void {
|
|
|
+ self.wordsLabel.stringValue = String(format: "%d/150", self.rewritingTextField.stringValue.count)
|
|
|
+ self.copyLabel.stringValue = NSLocalizedString("Copy", comment: "")
|
|
|
+
|
|
|
+ if self.uiState == .CanAIRewriting || self.uiState == .CanAIErrorCorrection {
|
|
|
+ self.rewritingBox.fillColor = NSColor(hex: "#1770F4")
|
|
|
+ self.rewritingBox.borderWidth = 0.0
|
|
|
+ self.rewritingLabel.textColor = NSColor(hex: "#FFFFFF")
|
|
|
+ self.copyBox.fillColor = NSColor(hex: "#EDEEF0")
|
|
|
+ self.copyBox.borderWidth = 1.0
|
|
|
+ self.copyBox.borderColor = NSColor(hex: "#DFE1E5")
|
|
|
+ self.copyLabel.textColor = NSColor(hex: "#B1B3B8")
|
|
|
+ self.wordsLabel.textColor = NSColor(hex: "#94989C")
|
|
|
+ self.resultTextBox.fillColor = NSColor(hex: "#FFFFFF")
|
|
|
+ self.resultTextBox.borderWidth = 1.0
|
|
|
+ self.rewritingBox.borderColor = NSColor(hex: "#DFE1E5")
|
|
|
+ self.emptyImageView.isHidden = false
|
|
|
+ self.errorView.isHidden = true
|
|
|
+ } else if self.uiState == .CanCopy {
|
|
|
+ self.rewritingBox.fillColor = NSColor(hex: "#1770F4")
|
|
|
+ self.rewritingBox.borderWidth = 0.0
|
|
|
+ self.rewritingLabel.textColor = NSColor(hex: "#FFFFFF")
|
|
|
+ self.copyBox.fillColor = NSColor(hex: "#FFFFFF")
|
|
|
+ self.copyBox.borderWidth = 1.0
|
|
|
+ self.copyBox.borderColor = NSColor(hex: "#DFE1E5")
|
|
|
+ self.copyLabel.textColor = NSColor(hex: "#252629")
|
|
|
+ self.wordsLabel.textColor = NSColor(hex: "#94989C")
|
|
|
+ self.resultTextBox.fillColor = NSColor(hex: "#FFFFFF")
|
|
|
+ self.resultTextBox.borderWidth = 1.0
|
|
|
+ self.rewritingBox.borderColor = NSColor(hex: "#DFE1E5")
|
|
|
+ self.emptyImageView.isHidden = true
|
|
|
+ self.errorView.isHidden = true
|
|
|
+ } else if self.uiState == .Copyed {
|
|
|
+ self.rewritingBox.fillColor = NSColor(hex: "#1770F4")
|
|
|
+ self.rewritingBox.borderWidth = 0.0
|
|
|
+ self.rewritingLabel.textColor = NSColor(hex: "#FFFFFF")
|
|
|
+ self.copyBox.fillColor = NSColor(hex: "#36383B")
|
|
|
+ self.copyBox.borderWidth = 0.0
|
|
|
+ self.copyLabel.textColor = NSColor(hex: "#FFFFFF")
|
|
|
+ self.copyLabel.stringValue = NSLocalizedString("Copied", comment: "")
|
|
|
+ self.wordsLabel.textColor = NSColor(hex: "#94989C")
|
|
|
+ self.resultTextBox.fillColor = NSColor(hex: "#FFFFFF")
|
|
|
+ self.resultTextBox.borderWidth = 1.0
|
|
|
+ self.rewritingBox.borderColor = NSColor(hex: "#DFE1E5")
|
|
|
+ self.emptyImageView.isHidden = true
|
|
|
+ self.errorView.isHidden = true
|
|
|
+ } else if self.uiState == .Exceed { // 是否超过
|
|
|
+ self.rewritingBox.fillColor = NSColor(hex: "#1770F4")
|
|
|
+ self.rewritingBox.borderWidth = 0.0
|
|
|
+ self.rewritingLabel.textColor = NSColor(hex: "#FFFFFF")
|
|
|
+ self.copyBox.fillColor = NSColor(hex: "#EDEEF0")
|
|
|
+ self.copyBox.borderWidth = 1.0
|
|
|
+ self.copyBox.borderColor = NSColor(hex: "#DFE1E5")
|
|
|
+ self.copyLabel.textColor = NSColor(hex: "#B1B3B8")
|
|
|
+ self.wordsLabel.textColor = NSColor(hex: "#F3465B")
|
|
|
+ self.resultTextBox.fillColor = NSColor(hex: "#FFFFFF")
|
|
|
+ self.resultTextBox.borderWidth = 1.0
|
|
|
+ self.rewritingBox.borderColor = NSColor(hex: "#F3465B")
|
|
|
+ self.emptyImageView.isHidden = true
|
|
|
+ self.errorView.isHidden = false
|
|
|
+ self.errorLabel.stringValue = NSLocalizedString("Limit 150 characters at a time", comment: "")
|
|
|
+ } else if self.uiState == .Error {
|
|
|
+ self.rewritingBox.fillColor = NSColor(hex: "#1770F4")
|
|
|
+ self.rewritingBox.borderWidth = 0.0
|
|
|
+ self.rewritingLabel.textColor = NSColor(hex: "#FFFFFF")
|
|
|
+ self.copyBox.fillColor = NSColor(hex: "#EDEEF0")
|
|
|
+ self.copyBox.borderWidth = 1.0
|
|
|
+ self.copyBox.borderColor = NSColor(hex: "#DFE1E5")
|
|
|
+ self.copyLabel.textColor = NSColor(hex: "#B1B3B8")
|
|
|
+ if self.rewritingTextField.stringValue.count > 150 {
|
|
|
+ self.wordsLabel.textColor = NSColor(hex: "#F3465B")
|
|
|
+ } else {
|
|
|
+ self.wordsLabel.textColor = NSColor(hex: "#94989C")
|
|
|
+ }
|
|
|
+ self.resultTextBox.fillColor = NSColor(hex: "#FFFFFF")
|
|
|
+ self.resultTextBox.borderWidth = 1.0
|
|
|
+ self.rewritingBox.borderColor = NSColor(hex: "#F3465B")
|
|
|
+ self.emptyImageView.isHidden = true
|
|
|
+ self.errorView.isHidden = false
|
|
|
+ if self.state == .AIRewriting {
|
|
|
+ self.errorLabel.stringValue = self.aiRewritingErrorStr
|
|
|
+ } else if self.state == .AIErrorCorrection {
|
|
|
+ self.errorLabel.stringValue = self.aiErrorCorrectionErrorStr
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ self.rewritingBox.fillColor = NSColor(hex: "#BDDFFD")
|
|
|
+ self.rewritingBox.borderWidth = 0.0
|
|
|
+ self.rewritingLabel.textColor = NSColor(hex: "#F7F8FA")
|
|
|
+ self.copyBox.fillColor = NSColor(hex: "#EDEEF0")
|
|
|
+ self.copyBox.borderWidth = 1.0
|
|
|
+ self.copyBox.borderColor = NSColor(hex: "#DFE1E5")
|
|
|
+ self.copyLabel.textColor = NSColor(hex: "#B1B3B8")
|
|
|
+ self.wordsLabel.textColor = NSColor(hex: "#94989C")
|
|
|
+ self.resultTextBox.fillColor = NSColor(hex: "#FFFFFF")
|
|
|
+ self.resultTextBox.borderWidth = 1.0
|
|
|
+ self.rewritingBox.borderColor = NSColor(hex: "#DFE1E5")
|
|
|
+ self.emptyImageView.isHidden = false
|
|
|
+ self.errorView.isHidden = true
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func acquisition_uiState() -> Void {
|
|
|
+ if self.state == .AIRewriting {
|
|
|
+ if (self.aiRewritingStr.count > 0) && (self.aiRewritingResultStr.count > 0) {
|
|
|
+ let pasteboard = NSPasteboard.general
|
|
|
+ if pasteboard.string(forType: .string)!.count > 0 {
|
|
|
+ if pasteboard.string(forType: .string)! == self.aiRewritingResultStr {
|
|
|
+ self.uiState = .CanCopy
|
|
|
+ } else {
|
|
|
+ self.uiState = .Copyed
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ self.uiState = .Copyed
|
|
|
+ }
|
|
|
+ } else if (self.aiRewritingStr.count > 0) {
|
|
|
+ if self.aiRewritingStr.count > 150 {
|
|
|
+ self.uiState = .Exceed
|
|
|
+ } else {
|
|
|
+ self.uiState = .CanAIRewriting
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if self.aiRewritingErrorStr.count > 0 {
|
|
|
+ self.uiState = .Error
|
|
|
+ } else {
|
|
|
+ self.uiState = .None
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else if self.state == .AIErrorCorrection {
|
|
|
+ if (self.aiErrorCorrectionStr.count > 0) && (self.aiErrorCorrectionResultStr.count > 0) {
|
|
|
+ let pasteboard = NSPasteboard.general
|
|
|
+ if pasteboard.string(forType: .string)!.count > 0 {
|
|
|
+ if pasteboard.string(forType: .string)! == self.aiErrorCorrectionResultStr {
|
|
|
+ self.uiState = .CanCopy
|
|
|
+ } else {
|
|
|
+ self.uiState = .Copyed
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ self.uiState = .Copyed
|
|
|
+ }
|
|
|
+ } else if (self.aiErrorCorrectionStr.count > 0) {
|
|
|
+ if self.aiErrorCorrectionStr.count > 150 {
|
|
|
+ self.uiState = .Exceed
|
|
|
+ } else {
|
|
|
+ self.uiState = .CanAIRewriting
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if self.aiErrorCorrectionErrorStr.count > 0 {
|
|
|
+ self.uiState = .Error
|
|
|
+ } else {
|
|
|
+ self.uiState = .None
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func showProgressWindow() {
|
|
|
+ let progress = SKProgressController()
|
|
|
+ if self.state == .AIRewriting {
|
|
|
+ progress.message = NSLocalizedString("Under Rewrite...", comment: "")
|
|
|
+ } else if self.state == . AIErrorCorrection {
|
|
|
+ progress.message = NSLocalizedString("Error Correction...", comment: "")
|
|
|
+ }
|
|
|
+ progress.window?.backgroundColor = NSColor(hex: "#36383B")
|
|
|
+ progress.window?.contentView?.wantsLayer = true
|
|
|
+ progress.window?.contentView?.layer?.backgroundColor = NSColor(hex: "#36383B").cgColor
|
|
|
+ progress.progressField.textColor = NSColor.white
|
|
|
+
|
|
|
+ progress.closeBlock = { [unowned self] in
|
|
|
+ }
|
|
|
+
|
|
|
+ self.progressController = progress
|
|
|
+ self.view.window?.beginSheet(progress.window!)
|
|
|
+ }
|
|
|
+
|
|
|
+ func hiddenProgressWindow() {
|
|
|
+ if (self.progressController != nil) {
|
|
|
+ self.view.window?.endSheet((self.progressController?.window)!)
|
|
|
+ self.progressController = nil
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
// MARK: Action
|
|
|
|
|
|
@IBAction func aiRewritingAction(_ sender: NSButton) {
|
|
|
- if self.canRewriting {
|
|
|
+ if self.uiState == .CanAIRewriting || self.uiState == .CanAIErrorCorrection || self.uiState == .CanCopy || self.uiState == .Copyed || self.uiState == .Exceed || self.uiState == .Error {
|
|
|
self.errorView.isHidden = true
|
|
|
- self.progressView.isHidden = false
|
|
|
-
|
|
|
+ DispatchQueue.main.async {
|
|
|
+ self.showProgressWindow()
|
|
|
+ }
|
|
|
+
|
|
|
let infoDictionary = Bundle .main.infoDictionary!
|
|
|
let majorVersion = infoDictionary["CFBundleShortVersionString"]
|
|
|
KMRequestServerManager.manager.aiRewriting(content: self.rewritingTextField.stringValue, version: "1.0.1", state: self.state) { [unowned self] success, result in
|
|
|
- self.progressView.isHidden = true
|
|
|
- let result: NSDictionary = result!.result
|
|
|
+ self.hiddenProgressWindow()
|
|
|
+
|
|
|
if success {
|
|
|
- self.copyBox.fillColor = NSColor(hex: "#FFFFFF")
|
|
|
- self.copyBox.borderColor = NSColor(hex: "#DFE1E5")
|
|
|
- self.copyLabel.textColor = NSColor(hex: "#252629")
|
|
|
- self.canCopy = true
|
|
|
+ let result: NSDictionary = result!.result
|
|
|
+ self.uiState = .CanCopy
|
|
|
|
|
|
self.resultTextView.string = ""
|
|
|
self.resultTextView.string = result["content"] as? String ?? ""
|
|
|
+ if self.state == .AIRewriting {
|
|
|
+ self.aiRewritingResultStr = result["content"] as? String ?? ""
|
|
|
+ self.aiRewritingErrorStr = ""
|
|
|
+ } else if self.state == .AIErrorCorrection {
|
|
|
+ self.aiErrorCorrectionResultStr = result["content"] as? String ?? ""
|
|
|
+ self.aiErrorCorrectionErrorStr = ""
|
|
|
+ }
|
|
|
} else {
|
|
|
- self.copyBox.borderColor = NSColor(hex: "#DFE1E5")
|
|
|
- self.copyBox.fillColor = NSColor(hex: "#EDEEF0")
|
|
|
- self.canCopy = false
|
|
|
+ let result: String = result!.message
|
|
|
|
|
|
- self.resultTextView.string = ""
|
|
|
-
|
|
|
- self.errorView.isHidden = false
|
|
|
- self.errorLabel.stringValue = "error"
|
|
|
+ DispatchQueue.main.async {
|
|
|
+ self.resultTextView.string = ""
|
|
|
+ self.errorLabel.stringValue = result
|
|
|
+ if self.state == .AIRewriting {
|
|
|
+ self.aiRewritingErrorStr = result
|
|
|
+ } else if self.state == .AIErrorCorrection {
|
|
|
+ self.aiErrorCorrectionErrorStr = result
|
|
|
+ }
|
|
|
+ self.uiState = .Error
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@IBAction func copyAction(_ sender: NSButton) {
|
|
|
- if self.canCopy {
|
|
|
+ if self.uiState == .CanCopy || self.uiState == .Copyed {
|
|
|
let pasteboard = NSPasteboard.general
|
|
|
pasteboard.clearContents()
|
|
|
pasteboard.setString(self.resultTextView.string, forType: .string)
|
|
|
+
|
|
|
+ self.uiState = .Copyed
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -172,21 +387,28 @@ extension KMAIRewritingVC: NSTextFieldDelegate {
|
|
|
self.wordsLabel.stringValue = String(format: "%d/150", object.stringValue.count)
|
|
|
|
|
|
if object.stringValue.count > 0 {
|
|
|
- self.rewritingBox.fillColor = NSColor(hex: "#1770F4")
|
|
|
- self.rewritingLabel.textColor = NSColor(hex: "#FFFFFF")
|
|
|
- self.canRewriting = true
|
|
|
+ if self.state == .AIRewriting {
|
|
|
+ self.uiState = .CanAIRewriting
|
|
|
+ } else if self.state == .AIErrorCorrection {
|
|
|
+ self.uiState = .CanAIErrorCorrection
|
|
|
+ }
|
|
|
} else {
|
|
|
- self.rewritingBox.fillColor = NSColor(hex: "#BDDFFD")
|
|
|
- self.rewritingLabel.textColor = NSColor(hex: "#F7F8FA")
|
|
|
- self.canRewriting = false
|
|
|
+ self.uiState = .None
|
|
|
}
|
|
|
|
|
|
- if object.stringValue.count >= 150 {
|
|
|
- self.wordsLabel.textColor = NSColor(hex: "#F3465B")
|
|
|
- self.isExceed = true
|
|
|
+ if object.stringValue.count > 150 {
|
|
|
+ self.uiState = .Exceed
|
|
|
+ if self.state == .AIRewriting {
|
|
|
+ self.aiRewritingErrorStr = NSLocalizedString("Limit 150 characters at a time", comment: "")
|
|
|
+ } else if self.state == .AIErrorCorrection {
|
|
|
+ self.aiErrorCorrectionErrorStr = NSLocalizedString("Limit 150 characters at a time", comment: "")
|
|
|
+ }
|
|
|
} else {
|
|
|
- self.wordsLabel.textColor = NSColor(hex: "#94989C")
|
|
|
- self.isExceed = false
|
|
|
+ if self.state == .AIRewriting {
|
|
|
+ self.aiRewritingErrorStr = ""
|
|
|
+ } else if self.state == .AIErrorCorrection {
|
|
|
+ self.aiErrorCorrectionErrorStr = ""
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
if self.state == .AIRewriting {
|