AIChatView.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. //
  2. // AIChatView.swift
  3. // PDF Reader Pro Edition
  4. //
  5. // Created by Niehaoyu on 2024/4/17.
  6. //
  7. import Cocoa
  8. class AIChatView: NSView, NibLoadable {
  9. @IBOutlet weak var contendView: NSView!
  10. @IBOutlet weak var scrollView: NSScrollView!
  11. @IBOutlet weak var collectionView: NSCollectionView!
  12. var chooseConfigHandle: ((_ view: AIChatView, _ clickType: AIConfigType) -> Void)?
  13. var cancelAIHandle: ((_ itemView: AIChatView, _ chatInfoModel: AIChatInfoModel) -> Void)?
  14. var continueAITranslateHandle: ((_ itemView: AIChatView, _ chatInfoModel: AIChatInfoModel) -> Void)?
  15. var redoHandle: ((_ view: AIChatView, _ model: AIChatInfoModel) -> Void)?
  16. override func draw(_ dirtyRect: NSRect) {
  17. super.draw(dirtyRect)
  18. // Drawing code here.
  19. }
  20. override func layout() {
  21. super.layout()
  22. self.collectionView.reloadData()
  23. }
  24. override func awakeFromNib() {
  25. super.awakeFromNib()
  26. self.scrollView.backgroundColor = NSColor.clear
  27. self.scrollView.drawsBackground = false
  28. self.scrollView.scrollerStyle = .overlay
  29. self.collectionView.delegate = self
  30. self.collectionView.dataSource = self
  31. self.collectionView.backgroundColors = [NSColor.clear]
  32. self.collectionView.wantsLayer = true
  33. self.collectionView.layer?.backgroundColor = NSColor.clear.cgColor
  34. self.collectionView.register(AIChatDefaultTIpItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "AIChatDefaultTIpItemID"))
  35. self.collectionView.register(AIChatStringResultItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "AIChatStringResultItemID"))
  36. self.collectionView.register(AIChatFileInfoItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "AIChatFileInfoItemID"))
  37. self.collectionView.register(AIChatStringUploadItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "AIChatStringUploadItemID"))
  38. self.collectionView.register(AIChatTranslateResultItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "AIChatTranslateResultItemID"))
  39. }
  40. func reloadData() {
  41. self.collectionView.reloadData()
  42. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.15) {
  43. let indexPath = Set([IndexPath(item: AIChatInfoManager.defaultManager.modelsArrM.count-1, section: 0)])
  44. self.collectionView.scrollToItems(at: indexPath, scrollPosition: .bottom)
  45. }
  46. }
  47. func sizeOfString(_ string: String, _ font: NSFont, _ width: CGFloat) -> (CGSize) {
  48. let paragraphStyle = NSMutableParagraphStyle()
  49. paragraphStyle.lineBreakMode = .byWordWrapping
  50. let attributes: [NSAttributedString.Key: Any] = [
  51. .font: font,
  52. .paragraphStyle:paragraphStyle
  53. ]
  54. let size = (string as NSString).boundingRect(with: NSSize(width: width, height: CGFloat(MAXFLOAT)),
  55. options: .usesLineFragmentOrigin,
  56. attributes: attributes,
  57. context: nil).size
  58. return size
  59. }
  60. }
  61. extension AIChatView: NSCollectionViewDelegate {
  62. //当item被选中
  63. // public func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
  64. // print("点击")
  65. // let view = collectionView.item(at: indexPaths.first!) as! KMAdvertisementCollectionViewItem
  66. //
  67. // let content = view.model
  68. //
  69. // guard let callBack = didSelect else { return }
  70. //
  71. // content?.index = indexPaths.first!.item
  72. // callBack(self, content!)
  73. // }
  74. //
  75. // //当item取消选中
  76. // public func collectionView(_ collectionView: NSCollectionView, didDeselectItemsAt indexPaths: Set<IndexPath>) {
  77. // _ = collectionView.item(at: indexPaths.first!) as! KMAdvertisementCollectionViewItem
  78. // }
  79. }
  80. extension AIChatView: NSCollectionViewDataSource {
  81. public func numberOfSections(in collectionView: NSCollectionView) -> Int {
  82. return 1
  83. }
  84. public func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
  85. return AIChatInfoManager.defaultManager.modelsArrM.count
  86. }
  87. //返回对应的item自定义个体
  88. public func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
  89. if indexPath.item > AIChatInfoManager.defaultManager.modelsArrM.count {
  90. return NSCollectionViewItem()
  91. }
  92. let model = AIChatInfoManager.defaultManager.modelsArrM[indexPath.item]
  93. if model.infoType == .defaultTip {
  94. let view = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "AIChatDefaultTIpItemID"), for: indexPath) as! AIChatDefaultTIpItem
  95. view.clickHandle = {[unowned self] view, configType in
  96. guard let callBack = self.chooseConfigHandle else {
  97. return
  98. }
  99. callBack(self, configType)
  100. }
  101. view.reloadData()
  102. return view
  103. } else if model.infoType == .chatFileUpload {
  104. let view = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "AIChatFileInfoItemID"), for: indexPath) as! AIChatFileInfoItem
  105. view.chatInfoModel = model
  106. view.reloadData()
  107. return view
  108. } else if model.infoType == .chatStringUpload{
  109. let view = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "AIChatStringUploadItemID"), for: indexPath) as! AIChatStringUploadItem
  110. view.chatInfoModel = model
  111. view.reloadData()
  112. return view
  113. } else if model.infoType == .chatStringResult {
  114. let view = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "AIChatStringResultItemID"), for: indexPath) as! AIChatStringResultItem
  115. view.chatInfoModel = model
  116. view.redoHandle = {[unowned self] itemView, model in
  117. guard let callBack = self.redoHandle else {
  118. return
  119. }
  120. callBack(self, itemView.chatInfoModel)
  121. }
  122. view.copyHandle = {[unowned self] itemView, model in
  123. let pasteboard = NSPasteboard.general
  124. pasteboard.clearContents()
  125. pasteboard.setString(model.chatResult, forType: .string)
  126. let contextString = NSLocalizedString("Copy Successfully!", comment: "")
  127. _ = CustomAlertView.alertView(message: contextString, fromView: self, withStyle: .black)
  128. }
  129. view.reloadData()
  130. view.setUI()
  131. view.refreshViewColor()
  132. return view
  133. } else if model.infoType == .chatTranslateResult {
  134. let view = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "AIChatTranslateResultItemID"), for: indexPath) as! AIChatTranslateResultItem
  135. view.chatInfoModel = model
  136. view.reloadData()
  137. view.cancelAIHandle = {[unowned self] itemView in
  138. guard let callBack = self.cancelAIHandle else {
  139. return
  140. }
  141. callBack(self, view.chatInfoModel)
  142. }
  143. view.startAIHandle = {[unowned self] itemView in
  144. guard let callBack = self.continueAITranslateHandle else {
  145. return
  146. }
  147. callBack(self, view.chatInfoModel)
  148. }
  149. view.redoHandle = {[unowned self] itemView, model in
  150. guard let callBack = self.redoHandle else {
  151. return
  152. }
  153. callBack(self, itemView.chatInfoModel)
  154. }
  155. view.copyHandle = {[unowned self] itemView, model in
  156. let pasteboard = NSPasteboard.general
  157. pasteboard.clearContents()
  158. pasteboard.setString(model.chatResult, forType: .string)
  159. let contextString = NSLocalizedString("Copy Successfully!", comment: "")
  160. _ = CustomAlertView.alertView(message: contextString, fromView: self, withStyle: .black)
  161. }
  162. view.setUI()
  163. return view
  164. }
  165. return NSCollectionViewItem()
  166. }
  167. // public func collectionView(_ collectionView: NSCollectionView, viewForSupplementaryElementOfKind kind: NSCollectionView.SupplementaryElementKind, at indexPath: IndexPath) -> NSView {
  168. // var nibName: String?
  169. // var view = NSView()
  170. // if kind == NSCollectionView.elementKindSectionHeader {
  171. // nibName = "KMAdvertisementCollectionHeadView"
  172. //
  173. // view = collectionView.makeSupplementaryView(ofKind: kind, withIdentifier: NSUserInterfaceItemIdentifier(rawValue: nibName!), for: indexPath)
  174. // if let view = view as? KMAdvertisementCollectionHeadView {
  175. // let model = self.data[indexPath.section]
  176. // view.model = model
  177. // }
  178. // } else if kind == NSCollectionView.elementKindSectionFooter {
  179. // nibName = "KMAdvertisementCollectionHeadView"
  180. //
  181. // view = collectionView.makeSupplementaryView(ofKind: kind, withIdentifier: NSUserInterfaceItemIdentifier(rawValue: nibName!), for: indexPath)
  182. // }
  183. // return view
  184. // }
  185. }
  186. extension AIChatView: NSCollectionViewDelegateFlowLayout {
  187. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
  188. if indexPath.item > AIChatInfoManager.defaultManager.modelsArrM.count {
  189. return NSSize(width: 0.01, height: 0.01)
  190. }
  191. let model = AIChatInfoManager.defaultManager.modelsArrM[indexPath.item]
  192. if model.infoType == .defaultTip {
  193. return NSSize(width: CGRectGetWidth(self.collectionView.frame), height: 160)
  194. } else if model.infoType == .chatFileUpload {
  195. return NSSize(width: CGRectGetWidth(self.collectionView.frame), height: 64)
  196. } else if model.infoType == .chatStringUpload {
  197. let height = self.sizeOfString(model.uploadContent, NSFont.SFProTextRegularFont(14), 216).height
  198. return NSSize(width: CGRectGetWidth(self.collectionView.frame), height: height + 24)
  199. } else if model.infoType == .chatStringResult {
  200. if model.aiConfigType == .summarize ||
  201. model.aiConfigType == .reWriting ||
  202. model.aiConfigType == .proofreading {
  203. if model.chatInfoState == .stateSuccess {
  204. let height = self.sizeOfString(model.chatResult, NSFont.SFProTextRegularFont(14), 216).height
  205. return NSSize(width: CGRectGetWidth(self.collectionView.frame), height: 28+48+height)
  206. } else if model.chatInfoState == .stateFailed {
  207. let height = self.sizeOfString(model.chatResult, NSFont.SFProTextRegularFont(13), 198).height
  208. return NSSize(width: CGRectGetWidth(self.collectionView.frame), height: 28+16+height)
  209. }
  210. return NSSize(width: CGRectGetWidth(self.collectionView.frame), height: 155+28)
  211. }
  212. return NSSize(width: CGRectGetWidth(self.collectionView.frame), height: 164)
  213. } else if model.infoType == .chatTranslateResult {
  214. if model.aiConfigType == .translate {
  215. if model.chatInfoState == .stateLoading {
  216. return NSSize(width: CGRectGetWidth(self.collectionView.frame), height: 155+28)
  217. } else if model.chatInfoState == .stateInfoConfirm {
  218. if model.creditsValid == true {
  219. return NSSize(width: CGRectGetWidth(self.collectionView.frame), height: 200)
  220. } else {
  221. return NSSize(width: CGRectGetWidth(self.collectionView.frame), height: 228)
  222. }
  223. } else if model.chatInfoState == .stateCancel {
  224. return NSSize(width: CGRectGetWidth(self.collectionView.frame), height: 96)
  225. } else if model.chatInfoState == .stateFailed {
  226. let height = self.sizeOfString(model.chatResult, NSFont.SFProTextRegularFont(13), 198).height
  227. return NSSize(width: CGRectGetWidth(self.collectionView.frame), height: 40+28+height)
  228. } else if model.chatInfoState == .stateSuccess {
  229. if model.filePath.isEmpty == false {
  230. //文件
  231. return NSSize(width: CGRectGetWidth(self.collectionView.frame), height: 160)
  232. } else {
  233. //文字
  234. let height = self.sizeOfString(model.chatResult, NSFont.SFProTextRegularFont(13), 216).height
  235. return NSSize(width: CGRectGetWidth(self.collectionView.frame), height: 100+height)
  236. }
  237. }
  238. }
  239. return NSSize(width: CGRectGetWidth(self.collectionView.frame), height: 155+28)
  240. }
  241. return NSSize(width: 1, height: 1)
  242. }
  243. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> NSSize {
  244. return NSSize(width: 0, height: 0.01)
  245. }
  246. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, referenceSizeForFooterInSection section: Int) -> NSSize {
  247. return NSSize(width: 0, height: 0.01)
  248. }
  249. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  250. return 16
  251. }
  252. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, insetForSectionAt section: Int) -> NSEdgeInsets {
  253. return NSEdgeInsetsMake(13, 0, 30, 0)
  254. }
  255. }