Skip to content

Commit 5ca085c

Browse files
committed
Tweak the behavior of theme change in chat
1 parent adaac4f commit 5ca085c

5 files changed

Lines changed: 96 additions & 86 deletions

File tree

Core/Sources/ChatGPTChatTab/ChatPanel.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,10 +534,12 @@ struct ChatPanel_EmptyChat_Preview: PreviewProvider {
534534
struct ChatCodeSyntaxHighlighter: CodeSyntaxHighlighter {
535535
let brightMode: Bool
536536
let fontSize: Double
537+
let colorChange: Color?
537538

538-
init(brightMode: Bool, fontSize: Double) {
539+
init(brightMode: Bool, fontSize: Double, colorChange: Color?) {
539540
self.brightMode = brightMode
540541
self.fontSize = fontSize
542+
self.colorChange = colorChange
541543
}
542544

543545
func highlightCode(_ content: String, language: String?) -> Text {

Core/Sources/ChatGPTChatTab/Styles.swift

Lines changed: 90 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -37,75 +37,26 @@ extension View {
3737
var messageBubbleCornerRadius: Double { 8 }
3838

3939
func codeBlockLabelStyle() -> some View {
40-
CodeBlockLabelStyle(content: self)
41-
}
42-
43-
func codeBlockStyle(_ configuration: CodeBlockConfiguration) -> some View {
44-
CodeBlockStyle(configuration: configuration, content: self)
45-
}
46-
}
47-
48-
struct CodeBlockLabelStyle<Content: View>: View {
49-
@AppStorage(\.syncChatCodeHighlightTheme) var syncCodeHighlightTheme
50-
@AppStorage(\.codeForegroundColorLight) var codeForegroundColorLight
51-
@AppStorage(\.codeBackgroundColorLight) var codeBackgroundColorLight
52-
@AppStorage(\.codeForegroundColorDark) var codeForegroundColorDark
53-
@AppStorage(\.codeBackgroundColorDark) var codeBackgroundColorDark
54-
55-
let content: Content
56-
57-
var body: some View {
58-
content
59-
.relativeLineSpacing(.em(0.225))
40+
relativeLineSpacing(.em(0.225))
6041
.markdownTextStyle {
6142
FontFamilyVariant(.monospaced)
6243
FontSize(.em(0.85))
6344
}
6445
.padding(16)
6546
.padding(.top, 14)
6647
}
67-
}
68-
69-
struct CodeBlockStyle<Content: View>: View {
70-
@AppStorage(\.syncChatCodeHighlightTheme) var syncCodeHighlightTheme
71-
@AppStorage(\.codeForegroundColorLight) var codeForegroundColorLight
72-
@AppStorage(\.codeBackgroundColorLight) var codeBackgroundColorLight
73-
@AppStorage(\.codeForegroundColorDark) var codeForegroundColorDark
74-
@AppStorage(\.codeBackgroundColorDark) var codeBackgroundColorDark
75-
@Environment(\.colorScheme) var colorScheme
7648

77-
let configuration: CodeBlockConfiguration
78-
let content: Content
79-
80-
var body: some View {
81-
content
82-
.background({
83-
if syncCodeHighlightTheme {
84-
if colorScheme == .light, let color = codeBackgroundColorLight.value {
85-
return color.swiftUIColor
86-
} else if let color = codeBackgroundColorDark.value {
87-
return color.swiftUIColor
88-
}
89-
}
90-
91-
return Color(nsColor: .textBackgroundColor).opacity(0.7)
92-
}() as Color)
49+
func codeBlockStyle(
50+
_ configuration: CodeBlockConfiguration,
51+
backgroundColor: Color,
52+
labelColor: Color
53+
) -> some View {
54+
background(backgroundColor)
9355
.clipShape(RoundedRectangle(cornerRadius: 6))
9456
.overlay(alignment: .top) {
9557
HStack(alignment: .center) {
9658
Text(configuration.language ?? "code")
97-
.foregroundStyle({
98-
if syncCodeHighlightTheme {
99-
if colorScheme == .light,
100-
let color = codeForegroundColorLight.value
101-
{
102-
return color.swiftUIColor.opacity(0.5)
103-
} else if let color = codeForegroundColorDark.value {
104-
return color.swiftUIColor.opacity(0.5)
105-
}
106-
}
107-
return Color.secondary.opacity(0.7)
108-
}() as Color)
59+
.foregroundStyle(labelColor)
10960
.font(.callout)
11061
.padding(.leading, 8)
11162
.lineLimit(1)
@@ -123,8 +74,69 @@ struct CodeBlockStyle<Content: View>: View {
12374
}
12475
}
12576

77+
struct ThemedMarkdownText: View {
78+
@AppStorage(\.syncChatCodeHighlightTheme) var syncCodeHighlightTheme
79+
@AppStorage(\.codeForegroundColorLight) var codeForegroundColorLight
80+
@AppStorage(\.codeBackgroundColorLight) var codeBackgroundColorLight
81+
@AppStorage(\.codeForegroundColorDark) var codeForegroundColorDark
82+
@AppStorage(\.codeBackgroundColorDark) var codeBackgroundColorDark
83+
@AppStorage(\.chatFontSize) var chatFontSize
84+
@AppStorage(\.chatCodeFontSize) var chatCodeFontSize
85+
@Environment(\.colorScheme) var colorScheme
86+
87+
let text: String
88+
89+
init(_ text: String) {
90+
self.text = text
91+
}
92+
93+
var body: some View {
94+
Markdown(text)
95+
.textSelection(.enabled)
96+
.markdownTheme(.custom(
97+
fontSize: chatFontSize,
98+
codeBlockBackgroundColor: {
99+
if syncCodeHighlightTheme {
100+
if colorScheme == .light, let color = codeBackgroundColorLight.value {
101+
return color.swiftUIColor
102+
} else if let color = codeBackgroundColorDark.value {
103+
return color.swiftUIColor
104+
}
105+
}
106+
107+
return Color(nsColor: .textBackgroundColor).opacity(0.7)
108+
}(),
109+
codeBlockLabelColor: {
110+
if syncCodeHighlightTheme {
111+
if colorScheme == .light,
112+
let color = codeForegroundColorLight.value
113+
{
114+
return color.swiftUIColor.opacity(0.5)
115+
} else if let color = codeForegroundColorDark.value {
116+
return color.swiftUIColor.opacity(0.5)
117+
}
118+
}
119+
return Color.secondary.opacity(0.7)
120+
}()
121+
))
122+
.markdownCodeSyntaxHighlighter(
123+
ChatCodeSyntaxHighlighter(
124+
brightMode: colorScheme != .dark,
125+
fontSize: chatCodeFontSize,
126+
colorChange: colorScheme == .dark
127+
? codeForegroundColorDark.value?.swiftUIColor
128+
: codeForegroundColorLight.value?.swiftUIColor
129+
)
130+
)
131+
}
132+
}
133+
126134
extension MarkdownUI.Theme {
127-
static func custom(fontSize: Double) -> MarkdownUI.Theme {
135+
static func custom(
136+
fontSize: Double,
137+
codeBlockBackgroundColor: Color,
138+
codeBlockLabelColor: Color
139+
) -> MarkdownUI.Theme {
128140
.gitHub.text {
129141
ForegroundColor(.primary)
130142
BackgroundColor(Color.clear)
@@ -136,14 +148,22 @@ extension MarkdownUI.Theme {
136148
if wrapCode {
137149
configuration.label
138150
.codeBlockLabelStyle()
139-
.codeBlockStyle(configuration)
151+
.codeBlockStyle(
152+
configuration,
153+
backgroundColor: codeBlockBackgroundColor,
154+
labelColor: codeBlockLabelColor
155+
)
140156
} else {
141157
ScrollView(.horizontal) {
142158
configuration.label
143159
.codeBlockLabelStyle()
144160
}
145161
.workaroundForVerticalScrollingBugInMacOS()
146-
.codeBlockStyle(configuration)
162+
.codeBlockStyle(
163+
configuration,
164+
backgroundColor: codeBlockBackgroundColor,
165+
labelColor: codeBlockLabelColor
166+
)
147167
}
148168
}
149169
}
@@ -165,14 +185,22 @@ extension MarkdownUI.Theme {
165185
if wrapCode {
166186
configuration.label
167187
.codeBlockLabelStyle()
168-
.codeBlockStyle(configuration)
188+
.codeBlockStyle(
189+
configuration,
190+
backgroundColor: Color(nsColor: .textBackgroundColor).opacity(0.7),
191+
labelColor: Color.secondary.opacity(0.7)
192+
)
169193
} else {
170194
ScrollView(.horizontal) {
171195
configuration.label
172196
.codeBlockLabelStyle()
173197
}
174198
.workaroundForVerticalScrollingBugInMacOS()
175-
.codeBlockStyle(configuration)
199+
.codeBlockStyle(
200+
configuration,
201+
backgroundColor: Color(nsColor: .textBackgroundColor).opacity(0.7),
202+
labelColor: Color.secondary.opacity(0.7)
203+
)
176204
}
177205
}
178206
.table { configuration in

Core/Sources/ChatGPTChatTab/Views/BotMessage.swift

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ struct BotMessage: View {
1111
let references: [DisplayedChatMessage.Reference]
1212
let chat: StoreOf<Chat>
1313
@Environment(\.colorScheme) var colorScheme
14-
@AppStorage(\.chatFontSize) var chatFontSize
15-
@AppStorage(\.chatCodeFontSize) var chatCodeFontSize
1614

1715
@State var isReferencesPresented = false
1816
@State var isReferencesHovered = false
@@ -45,15 +43,7 @@ struct BotMessage: View {
4543
}
4644
}
4745

48-
Markdown(text)
49-
.textSelection(.enabled)
50-
.markdownTheme(.custom(fontSize: chatFontSize))
51-
.markdownCodeSyntaxHighlighter(
52-
ChatCodeSyntaxHighlighter(
53-
brightMode: colorScheme != .dark,
54-
fontSize: chatCodeFontSize
55-
)
56-
)
46+
ThemedMarkdownText(text)
5747
}
5848
.frame(alignment: .trailing)
5949
.padding()

Core/Sources/ChatGPTChatTab/Views/UserMessage.swift

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,9 @@ struct UserMessage: View {
99
let text: String
1010
let chat: StoreOf<Chat>
1111
@Environment(\.colorScheme) var colorScheme
12-
@AppStorage(\.chatFontSize) var chatFontSize
13-
@AppStorage(\.chatCodeFontSize) var chatCodeFontSize
1412

1513
var body: some View {
16-
Markdown(text)
17-
.textSelection(.enabled)
18-
.markdownTheme(.custom(fontSize: chatFontSize))
19-
.markdownCodeSyntaxHighlighter(
20-
ChatCodeSyntaxHighlighter(
21-
brightMode: colorScheme != .dark,
22-
fontSize: chatCodeFontSize
23-
)
24-
)
14+
ThemedMarkdownText(text)
2515
.frame(alignment: .leading)
2616
.padding()
2717
.background {

Pro

Submodule Pro updated from 2507d50 to 8f8facd

0 commit comments

Comments
 (0)