|
| 1 | +import Combine |
| 2 | +import DebounceFunction |
| 3 | +import Foundation |
| 4 | +import MarkdownUI |
| 5 | +import SharedUIComponents |
| 6 | +import SwiftUI |
| 7 | + |
| 8 | +/// Use this instead of the built in ``CodeBlockView`` to highlight code blocks asynchronously, |
| 9 | +/// so that the UI doesn't freeze when rendering large code blocks. |
| 10 | +struct AsyncCodeBlockView: View { |
| 11 | + class Storage: ObservableObject { |
| 12 | + static let queue = DispatchQueue( |
| 13 | + label: "chat-code-block-highlight", |
| 14 | + qos: .userInteractive |
| 15 | + ) |
| 16 | + |
| 17 | + @Published var highlighted: AttributedString? |
| 18 | + var debounceFunction: DebounceFunction<AsyncCodeBlockView>? |
| 19 | + |
| 20 | + init() { |
| 21 | + self.debounceFunction = .init(duration: 0.5, block: { [weak self] view in |
| 22 | + self?.highlight(for: view) |
| 23 | + }) |
| 24 | + } |
| 25 | + |
| 26 | + func highlight(debounce: Bool, for view: AsyncCodeBlockView) { |
| 27 | + if debounce { |
| 28 | + Task { await debounceFunction?(view) } |
| 29 | + } else { |
| 30 | + highlight(for: view) |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + func highlight(for view: AsyncCodeBlockView) { |
| 35 | + let content = view.content |
| 36 | + let language = view.fenceInfo ?? "" |
| 37 | + let brightMode = view.colorScheme != .dark |
| 38 | + let font = view.font |
| 39 | + Self.queue.async { |
| 40 | + let content = highlightedCodeBlock( |
| 41 | + code: content, |
| 42 | + language: language, |
| 43 | + scenario: "chat", |
| 44 | + brightMode: brightMode, |
| 45 | + font: font |
| 46 | + ) |
| 47 | + let string = AttributedString(content) |
| 48 | + DispatchQueue.main.async { |
| 49 | + self.highlighted = string |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + let fenceInfo: String? |
| 56 | + let content: String |
| 57 | + let font: NSFont |
| 58 | + |
| 59 | + @Environment(\.colorScheme) var colorScheme |
| 60 | + @StateObject var storage = Storage() |
| 61 | + @AppStorage(\.syncChatCodeHighlightTheme) var syncCodeHighlightTheme |
| 62 | + @AppStorage(\.codeForegroundColorLight) var codeForegroundColorLight |
| 63 | + @AppStorage(\.codeBackgroundColorLight) var codeBackgroundColorLight |
| 64 | + @AppStorage(\.codeForegroundColorDark) var codeForegroundColorDark |
| 65 | + @AppStorage(\.codeBackgroundColorDark) var codeBackgroundColorDark |
| 66 | + |
| 67 | + init(fenceInfo: String?, content: String, font: NSFont) { |
| 68 | + self.fenceInfo = fenceInfo |
| 69 | + self.content = content.hasSuffix("\n") ? String(content.dropLast()) : content |
| 70 | + self.font = font |
| 71 | + } |
| 72 | + |
| 73 | + var body: some View { |
| 74 | + Group { |
| 75 | + if let highlighted = storage.highlighted { |
| 76 | + Text(highlighted) |
| 77 | + } else { |
| 78 | + Text(content).font(.init(font)) |
| 79 | + } |
| 80 | + } |
| 81 | + .onAppear { |
| 82 | + storage.highlight(debounce: false, for: self) |
| 83 | + } |
| 84 | + .onChange(of: colorScheme) { _ in |
| 85 | + storage.highlight(debounce: false, for: self) |
| 86 | + } |
| 87 | + .onChange(of: syncCodeHighlightTheme) { _ in |
| 88 | + storage.highlight(debounce: true, for: self) |
| 89 | + } |
| 90 | + .onChange(of: codeForegroundColorLight) { _ in |
| 91 | + storage.highlight(debounce: true, for: self) |
| 92 | + } |
| 93 | + .onChange(of: codeBackgroundColorLight) { _ in |
| 94 | + storage.highlight(debounce: true, for: self) |
| 95 | + } |
| 96 | + .onChange(of: codeForegroundColorDark) { _ in |
| 97 | + storage.highlight(debounce: true, for: self) |
| 98 | + } |
| 99 | + .onChange(of: codeBackgroundColorDark) { _ in |
| 100 | + storage.highlight(debounce: true, for: self) |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | + |
0 commit comments