Skip to content

Commit e5b3d8f

Browse files
committed
Make code blocks in chat horizontally scrollable
1 parent e164e8e commit e5b3d8f

1 file changed

Lines changed: 61 additions & 23 deletions

File tree

Core/Sources/ChatGPTChatTab/Styles.swift

Lines changed: 61 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,31 +41,34 @@ extension MarkdownUI.Theme {
4141
FontSize(fontSize)
4242
}
4343
.codeBlock { configuration in
44-
configuration.label
45-
.relativeLineSpacing(.em(0.225))
46-
.markdownTextStyle {
47-
FontFamilyVariant(.monospaced)
48-
FontSize(.em(0.85))
49-
}
50-
.padding(16)
51-
.padding(.top, 14)
52-
.background(Color(nsColor: .textBackgroundColor).opacity(0.7))
53-
.clipShape(RoundedRectangle(cornerRadius: 6))
54-
.overlay(alignment: .top) {
55-
HStack(alignment: .center) {
56-
Text(configuration.language ?? "code")
57-
.foregroundStyle(.tertiary)
58-
.font(.callout)
59-
.padding(.leading, 8)
60-
.lineLimit(1)
61-
Spacer()
62-
CopyButton {
63-
NSPasteboard.general.clearContents()
64-
NSPasteboard.general.setString(configuration.content, forType: .string)
65-
}
44+
ScrollView(.horizontal) {
45+
configuration.label
46+
.relativeLineSpacing(.em(0.225))
47+
.markdownTextStyle {
48+
FontFamilyVariant(.monospaced)
49+
FontSize(.em(0.85))
50+
}
51+
.padding(16)
52+
.padding(.top, 14)
53+
}
54+
.workaroundForVerticalScrollingBugInMacOS()
55+
.background(Color(nsColor: .textBackgroundColor).opacity(0.7))
56+
.clipShape(RoundedRectangle(cornerRadius: 6))
57+
.overlay(alignment: .top) {
58+
HStack(alignment: .center) {
59+
Text(configuration.language ?? "code")
60+
.foregroundStyle(.tertiary)
61+
.font(.callout)
62+
.padding(.leading, 8)
63+
.lineLimit(1)
64+
Spacer()
65+
CopyButton {
66+
NSPasteboard.general.clearContents()
67+
NSPasteboard.general.setString(configuration.content, forType: .string)
6668
}
6769
}
68-
.markdownMargin(top: 4, bottom: 16)
70+
}
71+
.markdownMargin(top: 4, bottom: 16)
6972
}
7073
}
7174

@@ -98,3 +101,38 @@ extension MarkdownUI.Theme {
98101
}
99102
}
100103

104+
final class VerticalScrollingFixHostingView<Content>: NSHostingView<Content> where Content: View {
105+
override func wantsForwardedScrollEvents(for axis: NSEvent.GestureAxis) -> Bool {
106+
return axis == .vertical
107+
}
108+
}
109+
110+
struct VerticalScrollingFixViewRepresentable<Content>: NSViewRepresentable where Content: View {
111+
let content: Content
112+
113+
func makeNSView(context: Context) -> NSHostingView<Content> {
114+
return VerticalScrollingFixHostingView<Content>(rootView: content)
115+
}
116+
117+
func updateNSView(_ nsView: NSHostingView<Content>, context: Context) {}
118+
}
119+
120+
struct VerticalScrollingFixWrapper<Content>: View where Content: View {
121+
let content: () -> Content
122+
123+
init(@ViewBuilder content: @escaping () -> Content) {
124+
self.content = content
125+
}
126+
127+
var body: some View {
128+
VerticalScrollingFixViewRepresentable(content: self.content())
129+
}
130+
}
131+
132+
extension View {
133+
/// https://stackoverflow.com/questions/64920744/swiftui-nested-scrollviews-problem-on-macos
134+
@ViewBuilder func workaroundForVerticalScrollingBugInMacOS() -> some View {
135+
VerticalScrollingFixWrapper { self }
136+
}
137+
}
138+

0 commit comments

Comments
 (0)