AIInfoInputView.swift 31 KB

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