AIInfoInputView.swift 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. //
  2. // AIInfoInputView.swift
  3. // PDF Reader Pro Edition
  4. //
  5. // Created by Niehaoyu on 2024/4/17.
  6. //
  7. import Cocoa
  8. import PDFKit
  9. class customTextView: NSTextView {
  10. var firstResponderHandle: ((_ view: customTextView, _ isFirstResponder: Bool) -> Void)?
  11. override func becomeFirstResponder() -> Bool {
  12. guard let callBack = self.firstResponderHandle else {
  13. return super.becomeFirstResponder()
  14. }
  15. callBack(self, true)
  16. return super.becomeFirstResponder()
  17. }
  18. override func resignFirstResponder() -> Bool {
  19. guard let callBack = self.firstResponderHandle else {
  20. return super.resignFirstResponder()
  21. }
  22. callBack(self, false)
  23. return super.resignFirstResponder()
  24. }
  25. }
  26. protocol AIInfoInputViewDelegate: AnyObject {
  27. func ai_InputViewDidChooseCurFile(aiInputView: AIInfoInputView)
  28. }
  29. class AIInfoInputView: NSView, NibLoadable, NSTextFieldDelegate, NSTextViewDelegate {
  30. @IBOutlet weak var contendBox: NSBox!
  31. @IBOutlet weak var typeEmptyTipView: NSView!
  32. @IBOutlet weak var typeEmptyTipLabel: NSTextField!
  33. @IBOutlet weak var infoContendView: NSView!
  34. @IBOutlet weak var typeHeaderView: NSView!
  35. @IBOutlet weak var titleLabel: NSTextField!
  36. @IBOutlet weak var titleTipBtn: KMButton!
  37. @IBOutlet weak var stringClearBtn: NSButton!
  38. @IBOutlet weak var chooseFileBtnView: NSView!
  39. @IBOutlet weak var chooseCurFileBtn: NSButton!
  40. @IBOutlet weak var chooseFileBtn: NSButton!
  41. @IBOutlet weak var chooseFileViewTopConst: NSLayoutConstraint!
  42. @IBOutlet weak var fileSizeTipView: NSView!
  43. @IBOutlet weak var fileSizeTipLabel: NSTextField!
  44. @IBOutlet weak var placeholdLabel: NSTextField!
  45. @IBOutlet weak var placeholdLabelTopConst: NSLayoutConstraint!
  46. @IBOutlet weak var fileInfoContendView: NSView!
  47. @IBOutlet weak var fileInfoImg: NSImageView!
  48. @IBOutlet weak var fileNameLabel: NSTextField!
  49. @IBOutlet weak var fileSizeLabel: NSTextField!
  50. @IBOutlet weak var filePageCountLabel: NSTextField!
  51. @IBOutlet weak var removeChooseFileBtn: NSButton!
  52. @IBOutlet weak var fileInfoViewTopConst: NSLayoutConstraint!
  53. @IBOutlet weak var textScrollView: NSScrollView!
  54. @IBOutlet var fileEmptyTextView: customTextView!
  55. @IBOutlet weak var textScrollViewTopConst: NSLayoutConstraint!
  56. @IBOutlet weak var inputTextCountLabel: NSTextField!
  57. @IBOutlet weak var translateConfigView: NSView!
  58. @IBOutlet weak var fromLanguageView: NSView!
  59. @IBOutlet weak var fromLanguageLabel: NSTextField!
  60. @IBOutlet weak var fromLanguageBtn: NSButton!
  61. @IBOutlet weak var toLanguageView: NSView!
  62. @IBOutlet weak var toLanguageLabel: NSTextField!
  63. @IBOutlet weak var toLanguageBtn: NSButton!
  64. @IBOutlet weak var translateChangeBtn: KMButton!
  65. @IBOutlet weak var startButton: NSButton!
  66. weak var aiDelegate: AIInfoInputViewDelegate?
  67. var area: NSTrackingArea?
  68. var popOver: NSPopover!
  69. var aiConfigType: AIConfigType = .none
  70. var filePath: String = ""
  71. var fromLanguage: String = ""
  72. var toLanguage: String = ""
  73. var startAIHandle: ((_ view: AIInfoInputView) -> Void)?
  74. var inputFrameUpdateHandle: ((_ view: AIInfoInputView, _ stringSize: CGSize) -> Void)?
  75. override func draw(_ dirtyRect: NSRect) {
  76. super.draw(dirtyRect)
  77. // Drawing code here.
  78. }
  79. override func awakeFromNib() {
  80. super.awakeFromNib()
  81. self.contendBox.borderWidth = 1
  82. self.contendBox.cornerRadius = 7
  83. self.titleLabel.font = NSFont.SFProTextSemiboldFont(13)
  84. self.titleTipBtn.mouseMoveCallback = {[unowned self] mouseEntered in
  85. if mouseEntered {
  86. var tipString = NSLocalizedString("Recommended file size: 10M or less.", comment: "")
  87. if self.aiConfigType == .reWriting {
  88. tipString = NSLocalizedString("No more than 2000 characters.", comment: "")
  89. } else if self.aiConfigType == .proofreading {
  90. tipString = NSLocalizedString("No more than 2000 characters.", comment: "")
  91. } else if self.aiConfigType == .translate {
  92. tipString = NSLocalizedString("1 credit for every 10,000 characters; No more than 10M of a document. ", comment: "")
  93. }
  94. let popViewController = KMToolbarItemPopViewController.init()
  95. if self.popOver == nil {
  96. self.popOver = NSPopover.init()
  97. }
  98. self.popOver.contentViewController = popViewController
  99. self.popOver.animates = false
  100. self.popOver.behavior = .semitransient
  101. self.popOver.contentSize = (popViewController.view.frame.size)
  102. popViewController.updateWithHelpTip(helpTip: tipString)
  103. let origin = self.titleTipBtn.superview?.convert(self.titleTipBtn.frame.origin, to: self)
  104. var frame = self.titleTipBtn.frame
  105. frame.origin.x = origin!.x + 12
  106. frame.origin.y = origin!.y + 20
  107. self.popOver.show(relativeTo: frame, of: (self.window?.contentView)!, preferredEdge: .maxY)
  108. } else {
  109. self.popOver.close()
  110. }
  111. }
  112. let countFormatter = TextFieldFormatter.init()
  113. countFormatter.setMaximumLength(2000)
  114. self.inputTextCountLabel.formatter = countFormatter
  115. self.inputTextCountLabel.delegate = self
  116. self.fileEmptyTextView.font = NSFont.SFProTextRegularFont(14)
  117. self.fileEmptyTextView.delegate = self
  118. self.fileEmptyTextView.firstResponderHandle = {[unowned self] view, isFirstResponder in
  119. if isFirstResponder {
  120. if self.fileSizeTipView.isHidden == false {
  121. self.fileSizeTipView.isHidden = true
  122. NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(hideFileSizeTipView), object: nil)
  123. }
  124. }
  125. }
  126. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.3) {
  127. self.setUpTranslateUI()
  128. }
  129. self.fileSizeTipView.isHidden = true
  130. self.fileSizeTipView.wantsLayer = true
  131. self.fileSizeTipLabel.font = NSFont.SFProTextRegularFont(12)
  132. self.fileSizeTipLabel.textColor = KMAppearance.KMColor_Layout_H0()
  133. self.fileSizeTipLabel.stringValue = NSLocalizedString("Please upload a file smaller than 10M.", comment: "")
  134. self.fileSizeTipView.layer?.cornerRadius = 2
  135. self.fileSizeTipView.layer?.masksToBounds = true
  136. self.fileInfoContendView.wantsLayer = true
  137. self.fileInfoContendView.layer?.cornerRadius = 6
  138. self.fileInfoContendView.layer?.masksToBounds = true
  139. self.translateConfigView.wantsLayer = true
  140. self.translateConfigView.layer?.backgroundColor = NSColor.clear.cgColor
  141. self.typeEmptyTipLabel.stringValue = NSLocalizedString("Select the AI tool", comment: "")
  142. self.chooseCurFileBtn.title = NSLocalizedString("Current File", comment: "")
  143. self.chooseFileBtn.title = NSLocalizedString("Choose", comment: "")
  144. self.translateChangeBtn.mouseMoveCallback = {[unowned self] mouseEntered in
  145. if mouseEntered {
  146. self.translateChangeBtn.image = NSImage(named: "AIchange_hover")
  147. } else {
  148. self.translateChangeBtn.image = NSImage(named: "AIchange")
  149. }
  150. }
  151. self.updateCountLabelInfo()
  152. }
  153. override func updateTrackingAreas() {
  154. super.updateTrackingAreas()
  155. if let _area = self.area, _area.rect.isEmpty == false {
  156. if (_area.rect.equalTo(self.bounds)) {
  157. return
  158. }
  159. }
  160. if (self.area != nil) {
  161. self.removeTrackingArea(self.area!)
  162. self.area = nil
  163. }
  164. self.area = NSTrackingArea(rect: self.bounds, options: [.mouseEnteredAndExited, .mouseMoved, .activeAlways], owner: self)
  165. self.addTrackingArea(self.area!)
  166. }
  167. func setUpTranslateUI() {
  168. 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"]
  169. let menu = NSMenu.init()
  170. for idx in 0...languages.count-1 {
  171. let string = languages[idx]
  172. let menuItem = NSMenuItem.init(title: string, action: #selector(menuItemClick(_:)), keyEquivalent: "")
  173. menuItem.tag = 1000 + idx
  174. menuItem.target = self
  175. menu.addItem(menuItem)
  176. }
  177. self.fromLanguageView?.menu = menu
  178. self.fromLanguage = "Automatic"
  179. 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"]
  180. let toMenu = NSMenu.init()
  181. for idx in 0...languages.count-1 {
  182. let string = languages[idx]
  183. let menuItem = NSMenuItem.init(title: string, action: #selector(menuItemClick(_:)), keyEquivalent: "")
  184. menuItem.tag = 3000 + idx
  185. menuItem.target = self
  186. toMenu.addItem(menuItem)
  187. }
  188. self.toLanguageView?.menu = toMenu
  189. self.toLanguage = "English"
  190. self.fromLanguageLabel.font = NSFont.SFProTextRegularFont(13)
  191. self.toLanguageLabel.font = NSFont.SFProTextRegularFont(13)
  192. self.fromLanguageLabel.textColor = KMAppearance.KMColor_Layout_H0()
  193. self.toLanguageLabel.textColor = KMAppearance.KMColor_Layout_H0()
  194. self.fromLanguageLabel.stringValue = self.fromLanguage
  195. self.toLanguageLabel.stringValue = self.toLanguage
  196. }
  197. func aiFunctionTypeChanged() {
  198. self.refreshStringSize()
  199. }
  200. func reloadData() {
  201. if AIChatInfoManager.defaultManager.currentFilePath.isEmpty || AIChatInfoManager.defaultManager.isAILoading {
  202. self.chooseCurFileBtn.isEnabled = false
  203. } else {
  204. self.chooseCurFileBtn.isEnabled = true
  205. }
  206. if AIChatInfoManager.defaultManager.isAILoading {
  207. self.chooseFileBtn.isEnabled = false
  208. } else {
  209. self.chooseFileBtn.isEnabled = true
  210. }
  211. self.startButton.title = NSLocalizedString("Start (1 credit)", comment: "")
  212. if self.aiConfigType == .none {
  213. self.typeEmptyTipView.isHidden = false
  214. self.infoContendView.isHidden = true
  215. } else {
  216. self.typeEmptyTipView.isHidden = true
  217. self.infoContendView.isHidden = false
  218. self.chooseFileBtnView.isHidden = true
  219. self.inputTextCountLabel.isHidden = false
  220. self.placeholdLabel.isHidden = true
  221. self.textScrollView.isHidden = true
  222. self.translateConfigView.isHidden = true
  223. self.fileInfoContendView.isHidden = true
  224. self.removeChooseFileBtn.isHidden = true
  225. self.startButton.isEnabled = false
  226. self.placeholdLabelTopConst.constant = -2
  227. self.chooseFileViewTopConst.constant = 0
  228. self.textScrollViewTopConst.constant = 0
  229. self.fileInfoViewTopConst.constant = 8
  230. self.stringClearBtn.isHidden = true
  231. if self.fileEmptyTextView.string.isEmpty == false {
  232. self.stringClearBtn.isHidden = false
  233. }
  234. if self.aiConfigType == .summarize {
  235. self.inputTextCountLabel.isHidden = true
  236. self.placeholdLabel.isHidden = false
  237. self.chooseFileBtnView.isHidden = false
  238. self.placeholdLabel.stringValue = NSLocalizedString("Summarize the current file or click choose other files.", comment: "")
  239. self.placeholdLabelTopConst.constant = CGRectGetHeight(self.chooseFileBtnView.frame) + 4
  240. if self.filePath.isEmpty == false {
  241. self.fileInfoContendView.isHidden = false
  242. self.placeholdLabel.isHidden = true
  243. let filePathURL = URL(fileURLWithPath: self.filePath)
  244. self.fileNameLabel.stringValue = filePathURL.lastPathComponent
  245. self.fileSizeLabel.stringValue = self.fileSizeString(Float(self.getFileSize(atPath: self.filePath)))
  246. if let document = PDFDocument(url: URL(fileURLWithPath: filePath)) {
  247. self.filePageCountLabel.stringValue = String(format: "%d ", document.pageCount) + NSLocalizedString("Page", comment: "")
  248. if document.pageCount > 1 {
  249. self.filePageCountLabel.stringValue = String(format: "%d ", document.pageCount) + NSLocalizedString("Pages", comment: "")
  250. }
  251. } else {
  252. self.filePageCountLabel.stringValue = ""
  253. }
  254. self.startButton.isEnabled = true
  255. }
  256. } else if self.aiConfigType == .reWriting {
  257. self.textScrollView.isHidden = false
  258. self.placeholdLabel.isHidden = false
  259. if self.fileEmptyTextView.string.isEmpty == false {
  260. self.placeholdLabel.isHidden = true
  261. self.startButton.isEnabled = true
  262. }
  263. self.placeholdLabel.stringValue = NSLocalizedString("Enter or paste content here...", comment: "")
  264. } else if self.aiConfigType == .proofreading {
  265. self.textScrollView.isHidden = false
  266. self.placeholdLabel.isHidden = false
  267. if self.fileEmptyTextView.string.isEmpty == false {
  268. self.placeholdLabel.isHidden = true
  269. self.startButton.isEnabled = true
  270. }
  271. self.placeholdLabel.stringValue = NSLocalizedString("Enter or paste content here...", comment: "")
  272. } else if self.aiConfigType == .translate {
  273. self.textScrollView.isHidden = false
  274. self.translateConfigView.isHidden = false
  275. self.chooseFileBtnView.isHidden = false
  276. self.chooseFileViewTopConst.constant = 30
  277. self.placeholdLabel.isHidden = false
  278. self.placeholdLabelTopConst.constant = 58
  279. self.textScrollViewTopConst.constant = 58
  280. self.placeholdLabel.stringValue = NSLocalizedString("Enter or paste content here...", comment: "")
  281. if self.filePath.isEmpty == false {
  282. self.startButton.title = NSLocalizedString("Start", comment: "")
  283. self.fileEmptyTextView.string = ""
  284. self.fileInfoContendView.isHidden = false
  285. self.placeholdLabel.isHidden = true
  286. self.inputTextCountLabel.isHidden = true
  287. self.fileInfoViewTopConst.constant = 8
  288. let filePathURL = URL(fileURLWithPath: self.filePath)
  289. self.fileNameLabel.stringValue = filePathURL.lastPathComponent
  290. self.fileSizeLabel.stringValue = self.fileSizeString(Float(self.getFileSize(atPath: self.filePath)))
  291. if let document = PDFDocument(url: URL(fileURLWithPath: filePath)) {
  292. self.filePageCountLabel.stringValue = String(format: "%d ", document.pageCount) + NSLocalizedString("Page", comment: "")
  293. if document.pageCount > 1 {
  294. self.filePageCountLabel.stringValue = String(format: "%d ", document.pageCount) + NSLocalizedString("Pages", comment: "")
  295. }
  296. } else {
  297. self.filePageCountLabel.stringValue = ""
  298. }
  299. self.startButton.isEnabled = true
  300. } else {
  301. self.startButton.title = NSLocalizedString("Start (1 credit)", comment: "")
  302. self.textScrollView.isHidden = false
  303. if self.fileEmptyTextView.string.isEmpty {
  304. self.placeholdLabel.isHidden = false
  305. self.startButton.isEnabled = false
  306. } else {
  307. self.placeholdLabel.isHidden = true
  308. self.startButton.isEnabled = true
  309. }
  310. }
  311. }
  312. }
  313. self.refreshUI()
  314. }
  315. func refreshUI() {
  316. if KMAppearance.isDarkMode() {
  317. self.titleLabel.textColor = NSColor.white
  318. self.contendBox.fillColor = NSColor.white.withAlphaComponent(0.05)
  319. self.contendBox.borderColor = KMAppearance.KMColor_Interactive_M0().withAlphaComponent(1)
  320. self.fileSizeTipView.layer?.backgroundColor = NSColor(red: 251/255, green: 166/255, blue: 0, alpha: 1).cgColor
  321. self.typeEmptyTipLabel.textColor = KMAppearance.KMColor_Interactive_S1()
  322. self.placeholdLabel.textColor = KMAppearance.KMColor_Interactive_S1()
  323. self.toLanguageView.layer?.backgroundColor = NSColor(red: 110/255, green: 109/255, blue: 112/255, alpha: 1).cgColor
  324. self.fromLanguageView.layer?.backgroundColor = NSColor(red: 110/255, green: 109/255, blue: 112/255, alpha: 1).cgColor
  325. } else {
  326. self.titleLabel.textColor = NSColor.black
  327. self.contendBox.borderColor = NSColor(red: 201/255, green: 218/255, blue: 247/255, alpha: 1)
  328. self.fileSizeTipView.layer?.backgroundColor = NSColor(red: 253/255, green: 239/255, blue: 212/255, alpha: 1).cgColor
  329. self.typeEmptyTipLabel.textColor = KMAppearance.KMColor_Layout_B30()
  330. self.placeholdLabel.textColor = KMAppearance.KMColor_Layout_B30()
  331. self.toLanguageView.layer?.backgroundColor = NSColor(red: 236/255, green: 242/255, blue: 254/255, alpha: 1).cgColor
  332. self.fromLanguageView.layer?.backgroundColor = NSColor(red: 236/255, green: 242/255, blue: 254/255, alpha: 1).cgColor
  333. }
  334. if self.aiConfigType == .summarize {
  335. self.titleLabel.stringValue = "#" + NSLocalizedString("AI Summarize", comment: "")
  336. if KMAppearance.isDarkMode() {
  337. self.titleLabel.textColor = NSColor(red: 85/255, green: 245/255, blue: 1, alpha: 1)
  338. } else {
  339. self.titleLabel.textColor = NSColor(red: 0, green: 209/255, blue: 222/255, alpha: 1)
  340. }
  341. } else if self.aiConfigType == .reWriting {
  342. self.titleLabel.stringValue = "#" + NSLocalizedString("AI Rewrite", comment: "")
  343. if KMAppearance.isDarkMode() {
  344. self.titleLabel.textColor = NSColor(red: 255/255, green: 105/255, blue: 195/255, alpha: 1)
  345. } else {
  346. self.titleLabel.textColor = NSColor(red: 240/255, green: 28/255, blue: 155/255, alpha: 1)
  347. }
  348. } else if self.aiConfigType == .proofreading {
  349. self.titleLabel.stringValue = "#" + NSLocalizedString("AI Proofread", comment: "")
  350. if KMAppearance.isDarkMode() {
  351. self.titleLabel.textColor = NSColor(red: 194/255, green: 157/255, blue: 1, alpha: 1)
  352. } else {
  353. self.titleLabel.textColor = NSColor(red: 108/255, green: 28/255, blue: 240/255, alpha: 1)
  354. }
  355. } else if self.aiConfigType == .translate {
  356. self.titleLabel.stringValue = "#" + NSLocalizedString("AI Translate", comment: "")
  357. if KMAppearance.isDarkMode() {
  358. self.titleLabel.textColor = NSColor(red: 255/255, green: 152/255, blue: 77/255, alpha: 1)
  359. } else {
  360. self.titleLabel.textColor = NSColor(red: 240/255, green: 101/255, blue: 0, alpha: 1)
  361. }
  362. }
  363. self.placeholdLabel.font = NSFont.SFProTextRegularFont(14)
  364. self.fromLanguageView.wantsLayer = true
  365. self.toLanguageView.wantsLayer = true
  366. }
  367. func updateCountLabelInfo() {
  368. self.inputTextCountLabel.stringValue = String(format: "%ld", self.fileEmptyTextView.string.count) + "/2000"
  369. if self.fileEmptyTextView.string.count == 2000 {
  370. self.inputTextCountLabel.textColor = KMAppearance.KMColor_Status_Err()
  371. } else {
  372. if KMAppearance.isDarkMode() {
  373. self.inputTextCountLabel.textColor = KMAppearance.KMColor_Layout_W30()
  374. } else {
  375. self.inputTextCountLabel.textColor = KMAppearance.KMColor_Layout_B30()
  376. }
  377. }
  378. }
  379. func getFileSize(atPath filePath : String) -> CGFloat {
  380. guard let dict = try? FileManager.default.attributesOfItem(atPath: filePath) as NSDictionary else {
  381. return 0
  382. }
  383. return CGFloat(dict.fileSize())
  384. }
  385. func fileSizeString(_ fSize: Float) -> String {
  386. let fileSize = fSize / 1024
  387. let size = fileSize >= 1024 ? (fileSize < 1048576 ? fileSize/1024 : fileSize/1048576.0) : fileSize
  388. let unit = fileSize >= 1024 ? (fileSize < 1048576 ? "M" : "G") : "K"
  389. return String(format: "%0.1f %@", size, unit)
  390. }
  391. @objc func hideFileSizeTipView() {
  392. self.fileSizeTipView.isHidden = true
  393. }
  394. //MARK: IBAction
  395. @objc func menuItemClick(_ item: NSMenuItem) {
  396. if item.tag < 2000 {
  397. self.fromLanguage = item.title
  398. } else {
  399. self.toLanguage = item.title
  400. }
  401. self.fromLanguageLabel.stringValue = self.fromLanguage
  402. self.toLanguageLabel.stringValue = self.toLanguage
  403. }
  404. @IBAction func chooseCurFileAction(_ sender: Any) {
  405. self.aiDelegate?.ai_InputViewDidChooseCurFile(aiInputView: self)
  406. self.fileSizeTipView.isHidden = true
  407. let curFilePath = AIChatInfoManager.defaultManager.currentFilePath
  408. let fileSize = self.getFileSize(atPath: curFilePath)
  409. if fileSize/(1024*1024) > 10 {
  410. self.fileSizeTipView.isHidden = false
  411. NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(hideFileSizeTipView), object: nil)
  412. self.perform(#selector(hideFileSizeTipView), with: nil, afterDelay: 5)
  413. self.filePath = ""
  414. self.fileEmptyTextView.string = ""
  415. self.reloadData()
  416. } else {
  417. self.filePath = curFilePath
  418. self.fileEmptyTextView.string = ""
  419. self.reloadData()
  420. }
  421. self.mouseEntered(with: NSEvent())
  422. }
  423. @IBAction func summaryUploadAction(_ sender: NSButton) {
  424. self.fileSizeTipView.isHidden = true
  425. let openPanel = NSOpenPanel()
  426. openPanel.canChooseDirectories = false
  427. openPanel.canChooseFiles = true
  428. openPanel.allowsMultipleSelection = false;
  429. openPanel.allowedFileTypes = ["pdf","PDF"]
  430. openPanel.beginSheetModal(for: self.window!) { [self] result in
  431. if result == .OK {
  432. let fileURL = openPanel.urls.first
  433. let fileSize = self.getFileSize(atPath: fileURL!.path)
  434. if fileSize/(1024*1024) > 10 {
  435. self.fileSizeTipView.isHidden = false
  436. NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(hideFileSizeTipView), object: nil)
  437. self.perform(#selector(hideFileSizeTipView), with: nil, afterDelay: 5)
  438. self.filePath = ""
  439. self.fileEmptyTextView.string = ""
  440. self.reloadData()
  441. } else {
  442. self.filePath = fileURL!.path
  443. self.fileEmptyTextView.string = ""
  444. self.reloadData()
  445. }
  446. }
  447. }
  448. }
  449. @IBAction func chooseLanguageAction(_ sender: NSButton) {
  450. if sender == self.fromLanguageBtn {
  451. let menu = self.fromLanguageView?.menu
  452. menu?.popUp(positioning: menu?.item(at: 0), at: CGPoint(x: 0, y: 15), in: sender)
  453. } else if sender == self.toLanguageBtn {
  454. let menu = self.toLanguageView?.menu
  455. menu?.popUp(positioning: menu?.item(at: 0), at: CGPoint(x: 0, y: 15), in: sender)
  456. }
  457. }
  458. @IBAction func languageChangeAction(_ sender: Any) {
  459. let curLan = self.fromLanguage
  460. self.fromLanguage = self.toLanguage
  461. self.toLanguage = curLan
  462. self.fromLanguageLabel.stringValue = self.fromLanguage
  463. self.toLanguageLabel.stringValue = self.toLanguage
  464. }
  465. @IBAction func removeChooseFile(_ sender: NSButton) {
  466. if self.aiConfigType == .summarize ||
  467. self.aiConfigType == .translate {
  468. self.filePath = ""
  469. self.reloadData()
  470. self.updateCountLabelInfo()
  471. }
  472. }
  473. @IBAction func clearInputStringAction(_ sender: Any) {
  474. self.fileEmptyTextView.string = ""
  475. self.reloadData()
  476. }
  477. @IBAction func startAIAction(_ sender: NSButton) {
  478. let newStatus: Bool = KMCloudServer.isConnectionAvailable()
  479. if !newStatus {
  480. let alert = NSAlert()
  481. alert.alertStyle = .critical
  482. alert.messageText = NSLocalizedString("Please make sure your internet connection is available.", comment: "")
  483. alert.runModal()
  484. return
  485. }
  486. guard let callBack = self.startAIHandle else {
  487. return
  488. }
  489. callBack(self)
  490. if self.aiConfigType == .summarize {
  491. self.filePath = ""
  492. } else if self.aiConfigType == .reWriting {
  493. self.fileEmptyTextView.string = ""
  494. } else if self.aiConfigType == .proofreading {
  495. self.fileEmptyTextView.string = ""
  496. } else if self.aiConfigType == .translate {
  497. self.filePath = ""
  498. self.fileEmptyTextView.string = ""
  499. }
  500. self.reloadData()
  501. self.updateCountLabelInfo()
  502. self.refreshStringSize()
  503. }
  504. func sizeOfString(_ string: String, _ font: NSFont) -> (CGSize) {
  505. let attributes: [NSAttributedString.Key: Any] = [
  506. .font: font
  507. ]
  508. let size = (string as NSString).boundingRect(with: NSSize(width: CGRectGetWidth(self.textScrollView.frame), height: CGFloat(MAXFLOAT)),
  509. options: .usesLineFragmentOrigin,
  510. attributes: attributes,
  511. context: nil).size
  512. return size
  513. }
  514. func refreshStringSize() {
  515. var size = self.sizeOfString(self.fileEmptyTextView.string, self.fileEmptyTextView.font!)
  516. if self.aiConfigType == .summarize {
  517. if self.filePath.isEmpty == true {
  518. size = self.sizeOfString(self.placeholdLabel.stringValue, self.placeholdLabel.font!)
  519. }
  520. }
  521. guard let callBack = self.inputFrameUpdateHandle else {
  522. return
  523. }
  524. callBack(self, size)
  525. }
  526. //MARK: Deletegate
  527. func textShouldBeginEditing(_ textObject: NSText) -> Bool {
  528. print("textShouldBeginEditing")
  529. return true
  530. }
  531. func textDidChange(_ notification: Notification) {
  532. if let textView = notification.object as? NSTextView, textView == self.fileEmptyTextView {
  533. // 获取文本字段的当前字符数
  534. let currentText = textView.string
  535. let currentCount = currentText.count
  536. // 如果超过最大字符数,将文本截断为最大字符数,并设置回文本字段
  537. if currentCount > 2000 {
  538. let endIndex = currentText.index(currentText.startIndex, offsetBy: 2000)
  539. let truncatedText = String(currentText[..<endIndex])
  540. textView.string = truncatedText
  541. }
  542. self.refreshStringSize()
  543. }
  544. if self.aiConfigType == .reWriting {
  545. if self.fileEmptyTextView.string.isEmpty {
  546. self.placeholdLabel.isHidden = false
  547. self.startButton.isEnabled = false
  548. } else {
  549. self.placeholdLabel.isHidden = true
  550. self.startButton.isEnabled = true
  551. }
  552. } else if self.aiConfigType == .proofreading {
  553. if self.fileEmptyTextView.string.isEmpty {
  554. self.placeholdLabel.isHidden = false
  555. self.startButton.isEnabled = false
  556. } else {
  557. self.placeholdLabel.isHidden = true
  558. self.startButton.isEnabled = true
  559. }
  560. } else if self.aiConfigType == .translate {
  561. if self.fileEmptyTextView.string.isEmpty {
  562. self.placeholdLabel.isHidden = false
  563. self.startButton.isEnabled = false
  564. } else {
  565. self.placeholdLabel.isHidden = true
  566. self.startButton.isEnabled = true
  567. }
  568. }
  569. self.updateCountLabelInfo()
  570. self.stringClearBtn.isHidden = true
  571. if self.fileEmptyTextView.string.isEmpty == false {
  572. self.stringClearBtn.isHidden = false
  573. }
  574. }
  575. //MARK: MouseEvent
  576. override func mouseEntered(with event: NSEvent) {
  577. super.mouseEntered(with: event)
  578. if self.filePath.isEmpty == false {
  579. self.removeChooseFileBtn.isHidden = false
  580. if KMAppearance.isDarkMode() {
  581. self.fileInfoContendView.layer?.backgroundColor = NSColor.white.withAlphaComponent(0.05).cgColor
  582. } else {
  583. self.fileInfoContendView.layer?.backgroundColor = NSColor.black.withAlphaComponent(0.05).cgColor
  584. }
  585. } else {
  586. self.fileInfoContendView.layer?.backgroundColor = NSColor.clear.cgColor
  587. }
  588. }
  589. override func mouseExited(with event: NSEvent) {
  590. super.mouseExited(with: event)
  591. self.removeChooseFileBtn.isHidden = true
  592. self.fileInfoContendView.layer?.backgroundColor = NSColor.clear.cgColor
  593. }
  594. override func mouseMoved(with event: NSEvent) {
  595. super.mouseMoved(with: event)
  596. }
  597. override func mouseDown(with event: NSEvent) {
  598. super.mouseDown(with: event)
  599. self.fileSizeTipView.isHidden = true
  600. NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(hideFileSizeTipView), object: nil)
  601. }
  602. }