1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021 |
- //
- // AIConfigWindowController.swift
- // PDF Reader Pro Edition
- //
- // Created by Niehaoyu on 2024/1/17.
- //
- import Cocoa
- @objcMembers class AIConfigWindowController: NSWindowController, NSTextFieldDelegate, NSTextViewDelegate {
- @IBOutlet weak var contendBox: NSBox!
- @IBOutlet weak var titleLabel: NSTextField!
- @IBOutlet weak var titleTipBtn: KMButton!
-
- @IBOutlet weak var uploadBGView: NSView!
- @IBOutlet weak var uploadItemBox: KMBox!
- @IBOutlet weak var uploadDesLabel: NSTextField!
- @IBOutlet weak var uploadPDFBtn: KMButton!
-
- @IBOutlet weak var fileBGView: NSView!
- @IBOutlet weak var fileEmptyImage: NSImageView!
- @IBOutlet weak var fileActionView: NSView!
- @IBOutlet weak var fileActionLabel: NSTextField!
- @IBOutlet weak var fileActionBtn: KMButton!
- @IBOutlet weak var filePDFBGView: NSView!
- @IBOutlet weak var filePDFHoverBtn: KMButton!
- @IBOutlet weak var pdfCoverView: NSView!
- @IBOutlet weak var pdfCoverImageView: NSImageView!
- @IBOutlet weak var removePDFBtn: NSButton!
- @IBOutlet weak var inputTextCountLabel: NSTextField!
- @IBOutlet weak var fileSizeTipView: NSView!
- @IBOutlet weak var fileSizeTipLabel: NSTextField!
- @IBOutlet weak var pdfNameLbl: NSTextField!
- @IBOutlet var fileEmptyTextView: NSTextView!
- @IBOutlet weak var filePlaceholdLabel: NSTextField!
-
-
- @IBOutlet weak var fileContendViewTopConst: NSLayoutConstraint!
-
- @IBOutlet weak var translateSepLineView: NSView!
- @IBOutlet weak var translateConfigView: NSView!
-
- @IBOutlet weak var fromLanguageView: NSView!
- @IBOutlet weak var fromLanguageLabel: NSTextField!
- @IBOutlet weak var fromLanguageBtn: NSButton!
-
- @IBOutlet weak var toLanguageView: NSView!
- @IBOutlet weak var toLanguageLabel: NSTextField!
- @IBOutlet weak var toLanguageBtn: NSButton!
-
- @IBOutlet weak var translateResultView: NSView!
- @IBOutlet weak var translateResultLabel: NSTextField!
- @IBOutlet weak var translateResultSubLabel: NSTextField!
-
-
- @IBOutlet weak var resultBGView: NSView!
- @IBOutlet weak var resultEmptyImage: NSImageView!
- // @IBOutlet weak var resultLabel: NSTextField!
- @IBOutlet var aiResultTextView: NSTextView!
-
- @IBOutlet weak var cancelBtn: NSButton!
- @IBOutlet weak var saveAsTextBtn: NSButton!
- @IBOutlet weak var saveAsPDFBtn: NSButton!
-
- static var currentWindowController: AIConfigWindowController!
-
- var popOver: NSPopover!
-
- var configType: AIConfigType = .none
-
- var cancelHandle: ((_ windowVC: AIConfigWindowController) -> Void)?
-
- var fileActionenAble: Bool = false
-
- var aiConvertFinish: Bool = true
- var filePath: String = ""
-
- var fromLanguage: String = ""
- var toLanguage: String = ""
-
- var translateFinish: Bool = false
- var translateFile: Bool = false
- var translateResultPath: String = ""
-
- @objc static func currentWC() -> AIConfigWindowController {
- if currentWindowController != nil {
- return currentWindowController
- } else {
- let configWC: AIConfigWindowController = AIConfigWindowController.init(windowNibName: "AIConfigWindowController")
- currentWindowController = configWC;
- return currentWindowController
- }
- }
-
- override func windowDidLoad() {
- super.windowDidLoad()
- // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
-
- self.titleLabel.font = NSFont.SFProTextSemiboldFont(13)
-
- self.titleTipBtn.mouseMoveCallback = {[unowned self] mouseEntered in
- if mouseEntered {
- var tipString = NSLocalizedString("Recommended file size: 10M or less.", comment: "")
- if self.configType == .reWriting {
- tipString = NSLocalizedString("No more than 2000 characters.", comment: "")
- } else if self.configType == .proofreading {
- tipString = NSLocalizedString("No more than 2000 characters.", comment: "")
- } else if self.configType == .translate {
- tipString = NSLocalizedString("1 credit for every 10,000 characters", comment: "")
- }
- let popViewController = KMToolbarItemPopViewController.init()
- if self.popOver == nil {
- self.popOver = NSPopover.init()
- }
- self.popOver.contentViewController = popViewController
- self.popOver.animates = false
- self.popOver.behavior = .semitransient
- self.popOver.contentSize = (popViewController.view.frame.size)
- popViewController.updateWithHelpTip(helpTip: tipString)
- self.popOver.show(relativeTo: self.titleTipBtn.frame, of: (self.window?.contentView)!, preferredEdge: .maxY)
- } else {
- self.popOver.close()
- }
- }
-
- self.uploadItemBox.borderWidth = 0
- self.uploadItemBox.cornerRadius = 1
- self.uploadItemBox.fillColor = KMAppearance.KMColor_Interactive_S0()
- self.uploadDesLabel.font = NSFont.SFProTextRegularFont(13)
- self.uploadDesLabel.textColor = KMAppearance.KMColor_Layout_H0()
- self.uploadDesLabel.stringValue = NSLocalizedString("Choose", comment: "")
-
- self.uploadPDFBtn.mouseMoveCallback = {[unowned self] mouseEntered in
- if mouseEntered {
- self.uploadItemBox.fillColor = KMAppearance.KMColor_Interactive_S_1()
- } else {
- self.uploadItemBox.fillColor = KMAppearance.KMColor_Interactive_S0()
- }
- }
-
- self.fileBGView.wantsLayer = true
- self.fileBGView.layer?.cornerRadius = 6
- self.fileBGView.layer?.masksToBounds = true
- self.fileBGView.layer?.borderWidth = 1
- self.fileBGView.layer?.borderColor = KMAppearance.KMColor_Layout_L_2().cgColor
-
- self.fileSizeTipView.wantsLayer = true
- self.fileSizeTipLabel.font = NSFont.SFProTextRegularFont(12)
- self.fileSizeTipLabel.textColor = KMAppearance.KMColor_Layout_H0()
- self.fileSizeTipLabel.stringValue = NSLocalizedString("Please upload a file smaller than 10M.", comment: "")
- self.fileSizeTipView.isHidden = true
-
- self.pdfNameLbl.font = NSFont.SFProTextRegularFont(12)
- self.pdfNameLbl.textColor = KMAppearance.KMColor_Layout_H0()
-
- self.resultBGView.wantsLayer = true
- self.resultBGView.layer?.cornerRadius = 6
- self.resultBGView.layer?.masksToBounds = true
-
- self.fileActionView.wantsLayer = true
- self.fileActionView.layer?.cornerRadius = 1
- self.fileActionView.layer?.masksToBounds = true
- self.fileActionLabel.font = NSFont.SFProTextRegularFont(13)
-
- self.fileActionBtn.mouseMoveCallback = {[unowned self] mouseEntered in
- if self.fileActionenAble == true {
- if mouseEntered {
- self.fileActionView.layer?.backgroundColor = KMAppearance.KMColor_Interactive_M_1().cgColor
- } else {
- self.fileActionView.layer?.backgroundColor = KMAppearance.KMColor_Interactive_M0().cgColor
- }
- }
- }
-
- self.translateSepLineView.wantsLayer = true
-
- self.pdfCoverView.wantsLayer = true
- self.pdfCoverView.layer?.cornerRadius = 2
- self.pdfCoverView.layer?.borderWidth = 2
-
- let countFormatter = TextFieldFormatter.init()
- countFormatter.setMaximumLength(2000)
- self.inputTextCountLabel.formatter = countFormatter
- self.inputTextCountLabel.delegate = self
-
- if KMAppearance.isDarkMode() {
- self.contendBox.fillColor = NSColor(red: 44/255, green: 44/255, blue: 44/255, alpha: 1)
- self.titleLabel.textColor = NSColor.white
- self.resultBGView.layer?.backgroundColor = NSColor(red: 49/255, green: 49/255, blue: 49/255, alpha: 1).cgColor
- self.inputTextCountLabel.textColor = KMAppearance.KMColor_Layout_W30()
- self.fileSizeTipView.layer?.backgroundColor = NSColor(red: 251/255, green: 166/255, blue: 0, alpha: 1).cgColor
- self.translateSepLineView.layer?.backgroundColor = NSColor.white.withAlphaComponent(0.05).cgColor
- self.translateResultView.layer?.backgroundColor = NSColor.white.withAlphaComponent(0.05).cgColor
-
- self.pdfCoverView.layer?.borderColor = NSColor.white.withAlphaComponent(0.2).cgColor
- self.pdfCoverView.layer?.backgroundColor = NSColor.white.withAlphaComponent(0.1).cgColor
- } else {
- self.contendBox.fillColor = NSColor.white
- self.titleLabel.textColor = NSColor.black
- self.resultBGView.layer?.backgroundColor = NSColor(red: 0, green: 0, blue: 0, alpha: 0.05).cgColor
- self.inputTextCountLabel.textColor = KMAppearance.KMColor_Layout_B30()
- self.fileSizeTipView.layer?.backgroundColor = NSColor(red: 253/255, green: 239/255, blue: 212/255, alpha: 1).cgColor
- self.translateSepLineView.layer?.backgroundColor = NSColor.black.withAlphaComponent(0.05).cgColor
- self.translateResultView.layer?.backgroundColor = NSColor.black.withAlphaComponent(0.05).cgColor
- self.pdfCoverView.layer?.borderColor = NSColor.black.withAlphaComponent(0.12).cgColor
- self.pdfCoverView.layer?.backgroundColor = NSColor.black.withAlphaComponent(0.04).cgColor
- }
-
- self.fileEmptyTextView.backgroundColor = NSColor.clear
- self.fileEmptyTextView.delegate = self
- self.fileEmptyTextView.isEditable = false
- self.fileEmptyTextView.frame = (self.fileEmptyTextView.enclosingScrollView?.contentView.bounds) ?? .zero
- self.fileEmptyTextView.autoresizingMask = [.width, .height]
-
- self.window?.makeFirstResponder(nil)
-
- var languages = ["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"]
- let menu = NSMenu.init()
- for idx in 0...languages.count-1 {
- let string = languages[idx]
- let menuItem = NSMenuItem.init(title: string, action: #selector(menuItemClick(_:)), keyEquivalent: "")
- menuItem.tag = 1000 + idx
- menu.addItem(menuItem)
- }
- self.fromLanguageView?.menu = menu
- self.fromLanguage = "Automatic"
-
- languages = ["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"]
- var toMenu = NSMenu.init()
- for idx in 0...languages.count-1 {
- let string = languages[idx]
- let menuItem = NSMenuItem.init(title: string, action: #selector(menuItemClick(_:)), keyEquivalent: "")
- menuItem.tag = 3000 + idx
- toMenu.addItem(menuItem)
- }
- self.toLanguageView?.menu = toMenu
- self.toLanguage = "English"
-
- self.fromLanguageLabel.font = NSFont.SFProTextRegularFont(13)
- self.toLanguageLabel.font = NSFont.SFProTextRegularFont(13)
- self.fromLanguageLabel.textColor = KMAppearance.KMColor_Layout_H0()
- self.toLanguageLabel.textColor = KMAppearance.KMColor_Layout_H0()
-
- self.fromLanguageLabel.stringValue = self.fromLanguage
- self.toLanguageLabel.stringValue = self.toLanguage
-
- self.removePDFBtn.isHidden = true
- self.filePDFHoverBtn.mouseMoveCallback = {[unowned self] mouseEntered in
- if mouseEntered {
- self.removePDFBtn.isHidden = false
- } else {
- self.removePDFBtn.isHidden = true
- }
- }
-
- self.cancelBtn.title = NSLocalizedString("Cancel", comment: "")
- self.saveAsTextBtn.title = NSLocalizedString("Save as TXT", comment: "")
- self.saveAsPDFBtn.title = NSLocalizedString("Save as PDF", comment: "")
- self.translateResultLabel.stringValue = NSLocalizedString("Translation Completion", comment: "")
- self.translateResultSubLabel.stringValue = NSLocalizedString("Click \"Save as PDF\" to save the result as a PDF file.", comment: "")
- self.translateResultView.isHidden = true
- self.translateResultView.wantsLayer = true
-
-
- if KMAppearance.isDarkMode() {
- self.translateResultLabel.textColor = NSColor.white
- self.translateResultSubLabel.textColor = NSColor.white
- } else {
- self.translateResultLabel.textColor = NSColor.black.withAlphaComponent(0.5)
- self.translateResultSubLabel.textColor = NSColor.black.withAlphaComponent(0.5)
- }
-
- self.translateResultLabel.font = NSFont.SFProTextSemiboldFont(13)
- self.translateResultSubLabel.font = NSFont.SFProTextRegularFont(13)
-
- self.aiResultTextView.font = NSFont.SFProTextRegularFont(14)
- self.aiResultTextView.textColor = KMAppearance.KMColor_Layout_H0()
- self.aiResultTextView.backgroundColor = NSColor.clear
- self.aiResultTextView.isEditable = false
-
- self.fileEmptyTextView.font = NSFont.SFProTextRegularFont(14)
- self.fileEmptyTextView.textColor = KMAppearance.KMColor_Layout_H0()
-
- }
-
- func getFileSize(atPath filePath : String) -> CGFloat {
- guard let dict = try? FileManager.default.attributesOfItem(atPath: filePath) as NSDictionary else {
- return 0
- }
- return CGFloat(dict.fileSize())
- }
-
- func refreshUI() -> Void {
- if self.configType == .summarize {
- self.titleLabel.stringValue = NSLocalizedString("AI Summarize", comment: "")
- self.fileEmptyImage.image = NSImage(named: "ai_summary_Empty")
- } else if self.configType == .reWriting {
- self.titleLabel.stringValue = NSLocalizedString("AI Rewrite", comment: "")
- self.fileEmptyImage.image = NSImage(named: "ai_rewriting_empty")
- } else if self.configType == .proofreading {
- self.titleLabel.stringValue = NSLocalizedString("AI Proofread", comment: "")
- self.fileEmptyImage.image = NSImage(named: "ai_proofreading_empty")
- } else if self.configType == .translate {
- self.titleLabel.stringValue = NSLocalizedString("AI Translate", comment: "")
- self.fileEmptyImage.image = NSImage(named: "ai_translate_empty")
- }
-
- self.translateResultView.isHidden = true
- self.translateConfigView.isHidden = true
- self.translateSepLineView.isHidden = true
-
- if self.configType == .reWriting ||
- self.configType == .proofreading {
- self.uploadBGView.isHidden = true
- self.fileContendViewTopConst.constant = 52
- self.filePDFBGView.isHidden = true
- } else {
- self.uploadBGView.isHidden = false
-
- self.fileContendViewTopConst.constant = 92
- self.filePDFBGView.isHidden = false
- if self.configType == .translate {
- self.fileContendViewTopConst.constant = 137
- self.translateConfigView.isHidden = false
- self.translateSepLineView.isHidden = false
- }
- }
-
- self.reloadData()
- }
-
- func clearData() -> Void {
- self.fileEmptyTextView.string = ""
- self.aiResultTextView.string = ""
- self.filePath = ""
-
- self.translateFinish = false
- self.translateFile = false
-
- self.cancelBtn.isEnabled = true
- }
-
- func reloadData() -> Void {
- self.clearData()
-
- self.fileEmptyTextView.isEditable = true
- self.inputTextCountLabel.isHidden = true
- if self.configType == .summarize {
- self.fileEmptyTextView.isEditable = false
- self.filePlaceholdLabel.placeholderString = NSLocalizedString("Start by uploading a document (pdf).", comment: "")
- } else if self.configType == .reWriting {
- self.inputTextCountLabel.isHidden = false
- self.filePlaceholdLabel.placeholderString = NSLocalizedString("Start by typing, pasting (⌘ + V) text", comment: "")
- } else if self.configType == .proofreading {
- self.inputTextCountLabel.isHidden = false
- self.filePlaceholdLabel.placeholderString = NSLocalizedString("Start by typing, pasting (⌘ + V) text", comment: "")
- } else if self.configType == .translate {
- self.inputTextCountLabel.isHidden = false
- self.filePlaceholdLabel.placeholderString = NSLocalizedString("Start by typing, pasting (⌘ + V) text, or uploading a document (pdf).", comment: "")
- }
-
- self.updateActionViewUI()
-
- self.updateResultViewUI()
-
- }
-
- //MARK: 更新左侧UI
- func updateActionViewUI() -> Void{
- self.fileSizeTipView.isHidden = true
-
- if self.filePath.count > 0 || self.fileEmptyTextView.string.count > 0 {
- self.enableActionView(true)
- self.filePlaceholdLabel.isHidden = true
- } else {
- self.enableActionView(false)
- self.filePlaceholdLabel.isHidden = false
- }
-
- if self.filePath.count > 0 {
- self.fileEmptyTextView.isHidden = true
- self.filePDFBGView.isHidden = false
-
- let document = CPDFDocument.init(url: URL(fileURLWithPath: self.filePath))
- if document?.isLocked == true || document == nil {
- self.filePath = ""
- self.updateActionViewUI()
- return
- }
- DispatchQueue.main.async {
- let page = document?.page(at: 0)
- let image = page?.thumbnail(of: page!.size)
- self.pdfCoverImageView.image = image
- }
-
- let filePathURL = URL(fileURLWithPath: self.filePath)
- let parentDirectory = filePathURL.lastPathComponent
- self.pdfNameLbl.stringValue = parentDirectory
-
- self.inputTextCountLabel.isHidden = true
- } else {
- self.fileEmptyTextView.isHidden = false
- self.filePDFBGView.isHidden = true
-
- self.pdfCoverImageView.image = nil
-
- self.inputTextCountLabel.isHidden = false
- if self.configType == .summarize {
- self.inputTextCountLabel.isHidden = true
- }
- }
-
- self.inputTextCountLabel.stringValue = String(format: "%ld", self.fileEmptyTextView.string.count) + "/2000"
- if self.fileEmptyTextView.string.count == 2000 {
- self.inputTextCountLabel.textColor = KMAppearance.KMColor_Status_Err()
- } else {
- if KMAppearance.isDarkMode() {
- self.inputTextCountLabel.textColor = KMAppearance.KMColor_Layout_W30()
- } else {
- self.inputTextCountLabel.textColor = KMAppearance.KMColor_Layout_B30()
- }
- }
-
- if self.aiResultTextView.string.count > 0 && (self.fileEmptyTextView.string.count > 0 || self.filePath.count > 0){
- self.fileActionLabel.stringValue = NSLocalizedString("Redo (1 credit)", comment: "")
- if self.configType == .translate && self.filePath.isEmpty == false {
- self.fileActionLabel.stringValue = NSLocalizedString("Redo", comment: "")
- }
- } else {
- if self.configType == .summarize {
- self.fileActionLabel.stringValue = NSLocalizedString("Summarize", comment: "") + NSLocalizedString("(1 credit)", comment: "")
- } else if self.configType == .reWriting {
- self.fileActionLabel.stringValue = NSLocalizedString("Rewrite", comment: "") + NSLocalizedString("(1 credit)", comment: "")
- } else if self.configType == .proofreading {
- self.fileActionLabel.stringValue = NSLocalizedString("Proofread", comment: "") + NSLocalizedString("(1 credit)", comment: "")
- } else if self.configType == .translate {
- self.fileActionLabel.stringValue = NSLocalizedString("Translate", comment: "") + NSLocalizedString("(1 credit)", comment: "")
- if self.filePath.isEmpty == false {
- self.fileActionLabel.stringValue = NSLocalizedString("Translate", comment: "")
- }
- }
- }
-
- }
-
- func enableActionView(_ enable: Bool) -> Void {
- if enable {
- self.fileActionView.layer?.backgroundColor = KMAppearance.KMColor_Interactive_M0().cgColor
- self.fileActionLabel.textColor = NSColor.white
- self.fileActionBtn.isEnabled = true
- } else {
- self.fileActionView.layer?.backgroundColor = KMAppearance.KMColor_Interactive_M0().withAlphaComponent(0.4).cgColor
- self.fileActionLabel.textColor = NSColor.white.withAlphaComponent(0.4)
- self.fileActionBtn.isEnabled = false
- }
- self.fileActionenAble = enable
-
- if self.filePath.count > 0 || self.fileEmptyTextView.string.count > 0 {
- self.filePlaceholdLabel.isHidden = true
- self.fileEmptyImage.isHidden = true
- } else {
- self.filePlaceholdLabel.isHidden = false
- self.fileEmptyImage.isHidden = false
- }
- }
-
- func updateResultViewUI() -> Void{
- if self.aiResultTextView.string.count > 0 {
- self.resultEmptyImage.isHidden = true
- self.saveAsTextBtn.isEnabled = true
- self.saveAsPDFBtn.isEnabled = true
- } else {
- self.resultEmptyImage.isHidden = false
- self.saveAsTextBtn.isEnabled = false
- self.saveAsPDFBtn.isEnabled = false
- }
-
- if self.configType == .translate && self.translateFinish == true {
- self.saveAsTextBtn.isEnabled = true
- self.saveAsPDFBtn.isEnabled = true
-
- if self.translateFile == true {
- self.translateResultView.isHidden = false
- self.saveAsTextBtn.isEnabled = false
- } else {
- self.translateResultView.isHidden = true
-
- }
- if translateResultView.isHidden == false {
- self.resultEmptyImage.isHidden = true
- }
- }
- }
-
- //MARK: WaitingView
- func addWaingView(_ view: NSView) -> Void {
- self.removeWaitingView(view)
-
- let wView = WaitingView.init(frame: view.bounds)
- wView.autoresizingMask = [.width, .height]
- view.addSubview(wView)
- wView.startAnimation()
- }
-
- func removeWaitingView(_ view: NSView) -> Void {
- for view in view.subviews {
- if view.className == WaitingView.className() {
- view.removeFromSuperview()
- break
- }
- }
- }
-
- func aiTranslateFinish() -> Void {
- self.aiConvertFinish = true
- self.removePDFBtn.isEnabled = true
- self.removeWaitingView(self.resultBGView)
- self.updateActionViewUI()
- }
-
- //MARK: AI-Action
- func aiSummarize() -> Void {
- if FileManager.default.fileExists(atPath: self.filePath) {
- self.aiResultTextView.string = ""
- self.addWaingView(self.resultBGView)
- self.enableActionView(false)
- self.removePDFBtn.isEnabled = false
- self.aiConvertFinish = false
- self.updateResultViewUI()
- // DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
- KMAIRequestServerManager.defaultManager.aiAction(content: self.filePath, state: .extractSummaryFile) { wrapper in
- DispatchQueue.main.async {
- self.aiConvertFinish = true
- self.removePDFBtn.isEnabled = true
- self.removeWaitingView(self.resultBGView)
- self.updateActionViewUI()
-
- let success = wrapper.success
- if success {
- let resultStr = wrapper.content
- self.aiResultTextView.string = resultStr
- self.updateActionViewUI()
- self.updateResultViewUI()
- } else {
- let alert = NSAlert()
- alert.alertStyle = .critical
- alert.messageText = wrapper.content
- alert.runModal()
- return
- }
- }
- }
- }
- }
-
- func aiReWriting() -> Void {
- self.aiResultTextView.string = ""
- self.addWaingView(self.resultBGView)
- self.enableActionView(false)
- self.removePDFBtn.isEnabled = false
- self.aiConvertFinish = false
- self.updateResultViewUI()
- // DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
- KMAIRequestServerManager.defaultManager.aiAction(content: self.fileEmptyTextView.string,
- state: .rewrite) { wrapper in
- DispatchQueue.main.async {
- self.aiConvertFinish = true
- self.removePDFBtn.isEnabled = true
- self.removeWaitingView(self.resultBGView)
- self.updateActionViewUI()
-
- let success = wrapper.success
- if success {
- let resultStr = wrapper.content
- self.aiResultTextView.string = resultStr
- self.updateActionViewUI()
- self.updateResultViewUI()
- } else {
- let alert = NSAlert()
- alert.alertStyle = .critical
- alert.messageText = wrapper.content
- alert.runModal()
- return
- }
- }
- }
- }
-
- func aiProofreading() -> Void {
- self.aiResultTextView.string = ""
- self.addWaingView(self.resultBGView)
- self.enableActionView(false)
- self.removePDFBtn.isEnabled = false
- self.aiConvertFinish = false
- self.updateResultViewUI()
- // DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
- KMAIRequestServerManager.defaultManager.aiAction(content: self.fileEmptyTextView.string,
- state: .correctTypos) { wrapper in
- DispatchQueue.main.async {
- self.aiConvertFinish = true
- self.removePDFBtn.isEnabled = true
- self.removeWaitingView(self.resultBGView)
- self.updateActionViewUI()
-
- let success = wrapper.success
- if success {
- let resultStr = wrapper.content
- self.aiResultTextView.string = resultStr
- self.updateActionViewUI()
- self.updateResultViewUI()
- } else {
- let alert = NSAlert()
- alert.alertStyle = .critical
- alert.messageText = wrapper.content
- alert.runModal()
- return
- }
- }
- }
- }
-
- func aiTranslate() -> Void {
-
- }
-
- @objc func menuItemClick(_ item: NSMenuItem) {
- if item.tag < 2000 {
- self.fromLanguage = item.title
- } else {
- self.toLanguage = item.title
- }
- self.fromLanguageLabel.stringValue = self.fromLanguage
- self.toLanguageLabel.stringValue = self.toLanguage
- }
-
- //MARK: - IBAction
- @IBAction func chooseFileAction(_ sender: KMButton) {
-
- let openPanel = NSOpenPanel()
- openPanel.canChooseDirectories = true
- openPanel.canChooseFiles = true
- openPanel.allowsMultipleSelection = false;
- openPanel.allowedFileTypes = ["pdf"]
- openPanel.beginSheetModal(for: self.window!) { result in
- if result == .OK {
- let fileURL = openPanel.urls.first
- let fileSize = self.getFileSize(atPath: fileURL!.path)
-
- if fileSize/(1024*1024) > 10 {
- self.fileSizeTipView.isHidden = false
- DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 5) {
- self.fileSizeTipView.isHidden = true
- }
- } else {
- self.filePath = fileURL!.path
- self.fileEmptyTextView.string = ""
- self.updateActionViewUI()
- }
- }
- }
- }
-
- @IBAction func chooseLanguageAction(_ sender: NSButton) {
- if sender == self.fromLanguageBtn {
- let menu = self.fromLanguageView?.menu
- menu?.popUp(positioning: menu?.item(at: 0), at: CGPoint(x: 0, y: 15), in: sender)
- } else if sender == self.toLanguageBtn {
- let menu = self.toLanguageView?.menu
- menu?.popUp(positioning: menu?.item(at: 0), at: CGPoint(x: 0, y: 15), in: sender)
- }
- }
-
- @IBAction func removePDFAction(_ sender: NSButton) {
- self.filePath = ""
- self.updateActionViewUI()
- self.updateResultViewUI()
- }
-
- @IBAction func startAIFunction(_ sender: KMButton) {
-
- let newStatus: Bool = KMCloudServer.isConnectionAvailable()
- if !newStatus {
- let alert = NSAlert()
- alert.alertStyle = .critical
- alert.messageText = NSLocalizedString("Please make sure your internet connection is available.", comment: "")
- alert.runModal()
- return
- }
-
- if self.configType == .summarize {
- self.aiSummarize()
-
- } else if self.configType == .reWriting {
- self.aiReWriting()
-
- } else if self.configType == .proofreading {
- self.aiProofreading()
-
- } else if self.configType == .translate {
- self.aiResultTextView.string = ""
- self.addWaingView(self.resultBGView)
- self.enableActionView(false)
- self.translateFinish = false
- self.translateResultView.isHidden = true
- self.updateResultViewUI()
-
- if self.filePath.count > 0 {
- self.translateFile = true
- self.removePDFBtn.isEnabled = false
- self.aiConvertFinish = false
- self.cancelBtn.isEnabled = false
-
- KMAIRequestServerManager.defaultManager.aiAction(content: self.filePath,
- state: .fileTranslate,
- from: self.fromLanguage,
- to: self.toLanguage) { wrapper in
- DispatchQueue.main.async {
-
- var success = wrapper.success
- let resultStr = wrapper.content
- if success == false && resultStr == "501" {
- success = true
- }
- if success {
- let translateTipWC = AITranslateTipWindowController.currentWC()
-
- let infoDict: NSDictionary = wrapper.result
- var credit: Int = 0
- if infoDict["credit"] != nil {
- credit = (infoDict["credit"] ?? "0") as! Int
- }
-
- var charCount: Int = 0
- if infoDict["charCount"] != nil {
- charCount = (infoDict["charCount"] ?? "0") as! Int
- }
-
- translateTipWC.creditValid = true
- if resultStr == "501" {
- translateTipWC.creditValid = false
- }
- translateTipWC.spendCredits = credit
- translateTipWC.totalChart = charCount
- translateTipWC.actionHandle = {[weak self] wc, result in
- if result == true {
- self?.window?.endSheet(wc.window!)
- KMAIRequestServerManager.defaultManager.aiTranslationFileTranslateHandle(fileKey: resultStr,
- from: self!.fromLanguage,
- to: self!.toLanguage) { wrapper in
- let success = wrapper.success
-
- if success {
- DispatchQueue.main.async {
- self?.aiTranslateFinish()
- self?.cancelBtn.isEnabled = true
-
- self!.translateFinish = true
- self!.translateResultView.isHidden = false
-
-
- let resultStr = wrapper.content
- self!.translateResultPath = resultStr
- self!.updateActionViewUI()
- self!.updateResultViewUI()
- }
- } else {
- DispatchQueue.main.async {
- self?.aiTranslateFinish()
- self?.cancelBtn.isEnabled = true
-
- let alert = NSAlert()
- alert.alertStyle = .critical
- alert.messageText = wrapper.content
- alert.runModal()
- }
- return
- }
- }
- } else {
- self?.cancelBtn.isEnabled = true
- self?.aiTranslateFinish()
- self?.window?.endSheet(wc.window!)
- }
- }
- self.window?.beginSheet(translateTipWC.window!)
- translateTipWC.reloadData()
- } else {
- self.cancelBtn.isEnabled = true
- self.aiConvertFinish = true
- self.removePDFBtn.isEnabled = true
- self.removeWaitingView(self.resultBGView)
- self.updateActionViewUI()
-
- let alert = NSAlert()
- alert.alertStyle = .critical
- alert.messageText = wrapper.content
- alert.runModal()
- return
- }
- }
- };
- } else {
- self.translateFile = false
- self.removePDFBtn.isEnabled = false
- self.aiConvertFinish = false
- // DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
- KMAIRequestServerManager.defaultManager.aiAction(content: self.fileEmptyTextView.string,
- state: .textTranslate,
- from: self.fromLanguage,
- to: self.toLanguage) { wrapper in
- DispatchQueue.main.async {
- self.aiConvertFinish = true
- self.removePDFBtn.isEnabled = true
- self.removeWaitingView(self.resultBGView)
- self.updateActionViewUI()
-
- let success = wrapper.success
- if success {
- self.translateFinish = true
- self.translateResultView.isHidden = false
-
- let resultStr = wrapper.content
- self.aiResultTextView.string = resultStr
-
- self.updateActionViewUI()
- self.updateResultViewUI()
- } else {
- let alert = NSAlert()
- alert.alertStyle = .critical
- alert.messageText = wrapper.content
- alert.runModal()
-
- self.updateResultViewUI()
- return
- }
- }
- };
- }
-
- }
- }
-
- @IBAction func languageChangeAction(_ sender: Any) {
- let curLan = self.fromLanguage
- self.fromLanguage = self.toLanguage
- self.toLanguage = curLan
-
-
- self.fromLanguageLabel.stringValue = self.fromLanguage
- self.toLanguageLabel.stringValue = self.toLanguage
-
- }
-
-
- @IBAction func cancelAction(_ sender: NSButton) {
- DispatchQueue.main.async {
- self.removeWaitingView(self.resultBGView)
- self.updateActionViewUI()
- }
- if self.aiConvertFinish == false {
- let alert = NSAlert()
- alert.alertStyle = .critical
- alert.messageText = NSLocalizedString("The task is already in process, if you cancel the process, AI Credit will not be returned, please confirm if you still need to cancel?", comment: "")
- alert.addButton(withTitle: NSLocalizedString("Back ", comment: ""))
- alert.addButton(withTitle: NSLocalizedString("Confirm Cancel", comment: ""))
- let response = alert.runModal()
- if response.rawValue == 1001 {
- self.confirmCancelAction()
- }
- return
- }
- if self.aiResultTextView.string.count > 0 {
- // if KMFunctionGuideWindowController.availableShow(.aiInfoResultSave) == true {
- // KMFunctionGuideWindowController.setDidShowFor(.aiInfoResultSave)
- // let alert = NSAlert()
- // alert.alertStyle = .critical
- // alert.messageText = NSLocalizedString("The results are not saved. Do you want to save them in other formats?", comment: "")
- // alert.addButton(withTitle: NSLocalizedString("Yes", comment: ""))
- // alert.addButton(withTitle: NSLocalizedString("No", comment: ""))
- // let response = alert.runModal()
- // if response.rawValue == 1001 {
- // self.cancelAction(self.cancelBtn)
- // }
- // return
- //
- // }
- }
-
- self.clearData()
-
- guard let callBack = self.cancelHandle else {
- return
- }
- callBack(self)
-
- }
-
- func confirmCancelAction() -> Void {
- self.clearData()
-
- guard let callBack = self.cancelHandle else {
- return
- }
- callBack(self)
- }
-
- @IBAction func saveAsTextAction(_ sender: Any) {
-
- let fileName = "Untitled"
- let outputSavePanel = NSSavePanel()
- outputSavePanel.allowedFileTypes = ["txt"]
- outputSavePanel.nameFieldStringValue = fileName
- let result = outputSavePanel.runModal()
- if result == .OK {
- let filePath = outputSavePanel.url?.path
- do {
- try self.aiResultTextView.string.write(to: outputSavePanel.url!, atomically: true, encoding: .utf8)
-
- NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: filePath!)])
- KMFunctionGuideWindowController.setDidShowFor(.aiInfoResultSave)
- } catch {
-
- }
-
- }
-
- }
-
- @IBAction func saveAsPDFAction(_ sender: Any) {
- let fileName = "Untitled"
- let outputSavePanel = NSSavePanel()
- outputSavePanel.allowedFileTypes = ["pdf"]
- outputSavePanel.nameFieldStringValue = fileName
- let result = outputSavePanel.runModal()
- if result == .OK {
- let filePath = outputSavePanel.url?.path
- do {
- if self.translateFile == true {
- do {
- if self.translateResultPath.count > 0 {
- if FileManager.default.fileExists(atPath: filePath!) {
- do {
- try FileManager.default.removeItem(atPath: filePath!)
- // print("删除旧文件成功")
- } catch {
- // print("删除旧文件失败:\(error)")
- }
- }
- try FileManager.default.copyItem(atPath: self.translateResultPath, toPath: filePath!)
- NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: filePath!)])
- }
- } catch {
-
- }
- } else {
- let text = self.aiResultTextView.string
- self.createPDF(from: text, saveTo: filePath!)
-
- NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: filePath!)])
- }
- KMFunctionGuideWindowController.setDidShowFor(.aiInfoResultSave)
- } catch {
-
- }
- }
- }
-
- func textDidChange(_ notification: Notification) {
- if let textView = notification.object as? NSTextView, textView == self.fileEmptyTextView {
- // 获取文本字段的当前字符数
- let currentText = textView.string
- let currentCount = currentText.count
-
- // 如果超过最大字符数,将文本截断为最大字符数,并设置回文本字段
- if currentCount > 2000 {
- let endIndex = currentText.index(currentText.startIndex, offsetBy: 2000)
- let truncatedText = String(currentText[..<endIndex])
- textView.string = truncatedText
- }
- }
- self.updateActionViewUI()
- }
-
- override func mouseDown(with event: NSEvent) {
- super.mouseDown(with: event)
-
- self.window?.makeFirstResponder(nil)
- }
-
- func createPDF(from text: String, saveTo path: String) {
- let attributedText = NSAttributedString(string: text)
- let pdfData = NSMutableData()
-
- let pdfConsumer = CGDataConsumer(data: pdfData)!
- var mediaBox = CGRect(x: 0, y: 0, width: 612, height: 792) // 设置页面尺寸,这里使用默认的美国信纸尺寸
-
- let pdfContext = CGContext(consumer: pdfConsumer, mediaBox: &mediaBox, nil)!
- pdfContext.beginPage(mediaBox: &mediaBox)
- pdfContext.textMatrix = .identity
- pdfContext.setShouldAntialias(true)
-
- let frame = mediaBox.insetBy(dx: 20, dy: 20) // 设置文字边距
-
- let framesetter = CTFramesetterCreateWithAttributedString(attributedText as CFAttributedString)
- let framePath = CGPath(rect: frame, transform: nil)
- let frameRef = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, attributedText.length), framePath, nil)
- CTFrameDraw(frameRef, pdfContext)
-
- pdfContext.endPage()
- pdfContext.closePDF()
-
- pdfData.write(toFile: path, atomically: true)
- print("PDF 文件已保存至:\(path)")
- }
-
-
-
- }
|