// // KMAIRewritingVC.swift // PDF Master // // Created by wanjun on 2023/5/22. // import Cocoa @objc enum KMAIRewritingState : Int { case AIRewriting = 0 case AIErrorCorrection } class KMAIRewritingVC: NSViewController { @IBOutlet weak var aiRewritingLabel: NSTextField! @IBOutlet weak var aiRewritingSubLabel: NSTextField! @IBOutlet weak var rewritingBox: KMBox! @IBOutlet weak var rewritingLabel: NSTextField! @IBOutlet weak var rewritingButton: NSButton! @IBOutlet weak var rewritingTextBox: NSBox! @IBOutlet weak var rewritingTextField: NSTextField! @IBOutlet weak var wordsLabel: NSTextField! @IBOutlet weak var resultLabel: NSTextField! @IBOutlet weak var copyBox: KMBox! @IBOutlet weak var copyLabel: NSTextField! @IBOutlet weak var copyButton: NSButton! @IBOutlet weak var resultTextBox: NSBox! @IBOutlet weak var resultTextView: NSTextView! @IBOutlet weak var emptyImageView: NSImageView! @IBOutlet weak var errorView: NSView! @IBOutlet weak var errorImageView: NSImageView! @IBOutlet weak var errorLabel: NSTextField! var state: KMAIRewritingState = .AIRewriting 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() // Do view setup here. self.initLocalization() self.initializeUI() } // MARK: initialize func initializeUI() -> Void { self.aiRewritingLabel.textColor = NSColor(hex: "#252629") self.aiRewritingLabel.font = NSFont.SFProTextSemibold(20.0) self.aiRewritingSubLabel.textColor = NSColor(hex: "#94989C") self.aiRewritingSubLabel.font = NSFont.SFProTextRegular(14.0) self.rewritingBox.fillColor = NSColor(hex: "#BDDFFD") self.rewritingBox.cornerRadius = 4.0 self.rewritingLabel.textColor = NSColor(hex: "#F7F8FA") self.rewritingLabel.font = NSFont.SFProTextRegular(14.0) self.rewritingTextBox.borderColor = NSColor(hex: "#1770F4") self.wordsLabel.textColor = NSColor(hex: "#94989C") self.wordsLabel.font = NSFont.SFProTextRegular(14.0) self.resultLabel.textColor = NSColor(hex: "#252629") self.resultLabel.font = NSFont.SFProTextSemibold(20.0) self.copyBox.borderColor = NSColor(hex: "#DFE1E5") self.copyBox.fillColor = NSColor(hex: "#EDEEF0") self.copyBox.cornerRadius = 4.0 self.copyBox.borderWidth = 1.0 self.copyLabel.textColor = NSColor(hex: "#B1B3B8") self.copyLabel.font = NSFont.SFProTextRegular(14.0) self.resultTextBox.borderColor = NSColor(hex: "#DFE1E5") self.errorView.isHidden = true 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 { switch self.state { case .AIRewriting: self.aiRewritingLabel.stringValue = NSLocalizedString("AI Rewriting", comment: "") self.aiRewritingSubLabel.stringValue = NSLocalizedString("Limited to 150 characters per session, 5 times per month", comment: "") self.rewritingLabel.stringValue = NSLocalizedString("Rewrite", comment: "") self.resultLabel.stringValue = NSLocalizedString("Result", comment: "") self.copyLabel.stringValue = NSLocalizedString("Copy", comment: "") 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 self.emptyImageView.image = NSImage(named: "ic_ai_rewriting_empty") break case .AIErrorCorrection: self.aiRewritingLabel.stringValue = NSLocalizedString("AI Error Correction", comment: "") self.aiRewritingSubLabel.stringValue = NSLocalizedString("Limited to 150 characters per session, 5 times per month", comment: "") self.rewritingLabel.stringValue = NSLocalizedString("Correct", comment: "") self.resultLabel.stringValue = NSLocalizedString("Result", comment: "") self.copyLabel.stringValue = NSLocalizedString("Copy", comment: "") 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 self.emptyImageView.image = NSImage(named: "ic_ai_ErrorCorrection_empty") 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 to 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) != nil { if pasteboard.string(forType: .string)!.count > 0 { if pasteboard.string(forType: .string)! == self.aiRewritingResultStr { self.uiState = .Copyed } else { self.uiState = .CanCopy } } else { self.uiState = .CanCopy } } else { self.uiState = .CanCopy } } 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) != nil { if pasteboard.string(forType: .string)!.count > 0 { if pasteboard.string(forType: .string)! == self.aiErrorCorrectionResultStr { self.uiState = .Copyed } else { self.uiState = .CanCopy } } else { self.uiState = .CanCopy } } else { self.uiState = .CanCopy } } 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("Rewriting...", comment: "") } else if self.state == . AIErrorCorrection { progress.message = NSLocalizedString("Error Correcting...", 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.uiState == .CanAIRewriting || self.uiState == .CanAIErrorCorrection || self.uiState == .CanCopy || self.uiState == .Copyed || self.uiState == .Exceed || self.uiState == .Error { if !KMLightMemberManager.manager.isLogin() { KMLoginWindowController.show(window: NSApp.mainWindow!) return } self.errorView.isHidden = true 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.hiddenProgressWindow() if success { 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 { let result: String = result!.message 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.uiState == .CanCopy || self.uiState == .Copyed { if !KMLightMemberManager.manager.isLogin() { KMLoginWindowController.show(window: NSApp.mainWindow!) return } let pasteboard = NSPasteboard.general pasteboard.clearContents() pasteboard.setString(self.resultTextView.string, forType: .string) self.uiState = .Copyed } } } extension KMAIRewritingVC: NSTextFieldDelegate { func controlTextDidEndEditing(_ obj: Notification) { let object = obj.object as! NSTextField } func controlTextDidChange(_ obj: Notification) { let object = obj.object as! NSTextField self.wordsLabel.stringValue = String(format: "%d/150", object.stringValue.count) if object.stringValue.count > 0 { if self.state == .AIRewriting { self.uiState = .CanAIRewriting } else if self.state == .AIErrorCorrection { self.uiState = .CanAIErrorCorrection } } else { self.uiState = .None } if object.stringValue.count > 150 { self.uiState = .Exceed if self.state == .AIRewriting { self.aiRewritingErrorStr = NSLocalizedString("Limit to 150 characters at a time", comment: "") } else if self.state == .AIErrorCorrection { self.aiErrorCorrectionErrorStr = NSLocalizedString("Limit to 150 characters at a time", comment: "") } } else { if self.state == .AIRewriting { self.aiRewritingErrorStr = "" } else if self.state == .AIErrorCorrection { self.aiErrorCorrectionErrorStr = "" } } if self.state == .AIRewriting { self.aiRewritingStr = object.stringValue self.aiRewritingResultStr = "" self.resultTextView.string = "" } else if self.state == .AIErrorCorrection { self.aiErrorCorrectionStr = object.stringValue self.aiErrorCorrectionResultStr = "" self.resultTextView.string = "" } } }