123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493 |
- //
- // KMAIRewritingVC.swift
- // PDF Reader Pro
- //
- // 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 = .rewrite
-
- 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.km_init(hex: "#252629")
- self.aiRewritingLabel.font = NSFont.SFProTextSemiboldFont(20.0)
- self.aiRewritingSubLabel.textColor = NSColor.km_init(hex: "#94989C")
- self.aiRewritingSubLabel.font = NSFont.SFProTextRegularFont(14.0)
- self.rewritingBox.fillColor = NSColor.km_init(hex: "#BDDFFD")
- self.rewritingBox.cornerRadius = 4.0
- self.rewritingLabel.textColor = NSColor.km_init(hex: "#F7F8FA")
- self.rewritingLabel.font = NSFont.SFProTextRegularFont(14.0)
- self.rewritingTextBox.borderColor = NSColor.km_init(hex: "#1770F4")
- self.wordsLabel.textColor = NSColor.km_init(hex: "#94989C")
- self.wordsLabel.font = NSFont.SFProTextRegularFont(14.0)
- self.resultLabel.textColor = NSColor.km_init(hex: "#252629")
- self.resultLabel.font = NSFont.SFProTextSemiboldFont(20.0)
- self.copyBox.borderColor = NSColor.km_init(hex: "#DFE1E5")
- self.copyBox.fillColor = NSColor.km_init(hex: "#EDEEF0")
- self.copyBox.cornerRadius = 4.0
- self.copyBox.borderWidth = 1.0
- self.copyLabel.textColor = NSColor.km_init(hex: "#B1B3B8")
- self.copyLabel.font = NSFont.SFProTextRegularFont(14.0)
- self.resultTextBox.borderColor = NSColor.km_init(hex: "#DFE1E5")
-
- self.errorView.isHidden = true
- self.errorImageView.image = NSImage(named: "icon_error")
- self.errorLabel.textColor = NSColor.km_init(hex: "#F3465B")
- self.errorLabel.font = NSFont.SFProTextRegularFont(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 .rewrite:
- 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.km_init(hex: "#1770F4")
- self.rewritingBox.borderWidth = 0.0
- self.rewritingLabel.textColor = NSColor.km_init(hex: "#FFFFFF")
- self.copyBox.fillColor = NSColor.km_init(hex: "#EDEEF0")
- self.copyBox.borderWidth = 1.0
- self.copyBox.borderColor = NSColor.km_init(hex: "#DFE1E5")
- self.copyLabel.textColor = NSColor.km_init(hex: "#B1B3B8")
- self.wordsLabel.textColor = NSColor.km_init(hex: "#94989C")
- self.resultTextBox.fillColor = NSColor.km_init(hex: "#FFFFFF")
- self.resultTextBox.borderWidth = 1.0
- self.rewritingBox.borderColor = NSColor.km_init(hex: "#DFE1E5")
- self.emptyImageView.isHidden = false
- self.errorView.isHidden = true
- } else if self.uiState == .CanCopy {
- self.rewritingBox.fillColor = NSColor.km_init(hex: "#1770F4")
- self.rewritingBox.borderWidth = 0.0
- self.rewritingLabel.textColor = NSColor.km_init(hex: "#FFFFFF")
- self.copyBox.fillColor = NSColor.km_init(hex: "#FFFFFF")
- self.copyBox.borderWidth = 1.0
- self.copyBox.borderColor = NSColor.km_init(hex: "#DFE1E5")
- self.copyLabel.textColor = NSColor.km_init(hex: "#252629")
- self.wordsLabel.textColor = NSColor.km_init(hex: "#94989C")
- self.resultTextBox.fillColor = NSColor.km_init(hex: "#FFFFFF")
- self.resultTextBox.borderWidth = 1.0
- self.rewritingBox.borderColor = NSColor.km_init(hex: "#DFE1E5")
- self.emptyImageView.isHidden = true
- self.errorView.isHidden = true
- } else if self.uiState == .Copyed {
- self.rewritingBox.fillColor = NSColor.km_init(hex: "#1770F4")
- self.rewritingBox.borderWidth = 0.0
- self.rewritingLabel.textColor = NSColor.km_init(hex: "#FFFFFF")
- self.copyBox.fillColor = NSColor.km_init(hex: "#36383B")
- self.copyBox.borderWidth = 0.0
- self.copyLabel.textColor = NSColor.km_init(hex: "#FFFFFF")
- self.copyLabel.stringValue = NSLocalizedString("Copied", comment: "")
- self.wordsLabel.textColor = NSColor.km_init(hex: "#94989C")
- self.resultTextBox.fillColor = NSColor.km_init(hex: "#FFFFFF")
- self.resultTextBox.borderWidth = 1.0
- self.rewritingBox.borderColor = NSColor.km_init(hex: "#DFE1E5")
- self.emptyImageView.isHidden = true
- self.errorView.isHidden = true
- } else if self.uiState == .Exceed { // 是否超过
- self.rewritingBox.fillColor = NSColor.km_init(hex: "#1770F4")
- self.rewritingBox.borderWidth = 0.0
- self.rewritingLabel.textColor = NSColor.km_init(hex: "#FFFFFF")
- self.copyBox.fillColor = NSColor.km_init(hex: "#EDEEF0")
- self.copyBox.borderWidth = 1.0
- self.copyBox.borderColor = NSColor.km_init(hex: "#DFE1E5")
- self.copyLabel.textColor = NSColor.km_init(hex: "#B1B3B8")
- self.wordsLabel.textColor = NSColor.km_init(hex: "#F3465B")
- self.resultTextBox.fillColor = NSColor.km_init(hex: "#FFFFFF")
- self.resultTextBox.borderWidth = 1.0
- self.rewritingBox.borderColor = NSColor.km_init(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.km_init(hex: "#1770F4")
- self.rewritingBox.borderWidth = 0.0
- self.rewritingLabel.textColor = NSColor.km_init(hex: "#FFFFFF")
- self.copyBox.fillColor = NSColor.km_init(hex: "#EDEEF0")
- self.copyBox.borderWidth = 1.0
- self.copyBox.borderColor = NSColor.km_init(hex: "#DFE1E5")
- self.copyLabel.textColor = NSColor.km_init(hex: "#B1B3B8")
- if self.rewritingTextField.stringValue.count > 150 {
- self.wordsLabel.textColor = NSColor.km_init(hex: "#F3465B")
- } else {
- self.wordsLabel.textColor = NSColor.km_init(hex: "#94989C")
- }
- self.resultTextBox.fillColor = NSColor.km_init(hex: "#FFFFFF")
- self.resultTextBox.borderWidth = 1.0
- self.rewritingBox.borderColor = NSColor.km_init(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.km_init(hex: "#BDDFFD")
- self.rewritingBox.borderWidth = 0.0
- self.rewritingLabel.textColor = NSColor.km_init(hex: "#F7F8FA")
- self.copyBox.fillColor = NSColor.km_init(hex: "#EDEEF0")
- self.copyBox.borderWidth = 1.0
- self.copyBox.borderColor = NSColor.km_init(hex: "#DFE1E5")
- self.copyLabel.textColor = NSColor.km_init(hex: "#B1B3B8")
- self.wordsLabel.textColor = NSColor.km_init(hex: "#94989C")
- self.resultTextBox.fillColor = NSColor.km_init(hex: "#FFFFFF")
- self.resultTextBox.borderWidth = 1.0
- self.rewritingBox.borderColor = NSColor.km_init(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()
- progress.window?.backgroundColor = NSColor.km_init(hex: "#36383B")
- progress.window?.contentView?.wantsLayer = true
- progress.window?.contentView?.layer?.backgroundColor = NSColor.km_init(hex: "#36383B").cgColor
- progress.progressField.textColor = NSColor.white
- // if self.state == .AIRewriting {
- // progress.message = NSLocalizedString("Rewriting...", comment: "")
- // } else if self.state == . AIErrorCorrection {
- // progress.message = NSLocalizedString("Error Correcting...", comment: "")
- // }
- progress.closeBlock = { [unowned self] in
- self.hiddenProgressWindow()
- }
-
- 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
- KMRequestServer.requestServer.task?.cancel()
- }
- }
-
- // 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._aiRewritingAction(sender)
-
- // Task { @MainActor in
- // if await (KMLightMemberManager.manager.canPayFunction() == false) {
- // var type: KMSubscribeWaterMarkType = .aiRewrite
- // if (self.state == .AIErrorCorrection) {
- // type = .aiCorrect
- // }
- // let _ = KMSubscribeWaterMarkWindowController.show(window: self.view.window!, type: type) { isSub, _, isClose in
- // if (isClose) {
- // return
- // }
- // if (isSub) {
- // self._aiRewritingAction(sender)
- // return
- // }
- // }
- // return
- // }
- //
- // self._aiRewritingAction(sender)
- // }
- }
- }
-
- private func _aiRewritingAction(_ sender: NSButton) {
- self.errorView.isHidden = true
- DispatchQueue.main.async {
- self.showProgressWindow()
- }
- var majorVersion = "1.0.1"
- // if let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String {
- // majorVersion = appVersion
- // }
- KMRequestServerManager.manager.aiRewriting(content: self.rewritingTextField.stringValue, version: majorVersion as! String, 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
- }
- self._copyAction(sender)
- // Task { @MainActor in
- // if await (KMLightMemberManager.manager.canPayFunction() == false) {
- // let _ = KMSubscribeWaterMarkWindowController.show(window: self.view.window!, type: .aiRewrite) { isSub, _, isClose in
- // if (isClose) {
- // return
- // }
- // if (isSub) {
- // self._copyAction(sender)
- // return
- // }
- // }
- // return
- // }
- // self._copyAction(sender)
- // }
- }
- }
-
- private func _copyAction(_ sender: NSButton) {
- 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 = ""
- // }
- }
- }
|