Skip to content

Commit 1c4f3fe

Browse files
committed
Merge branch 'feature/xcode-theme' into develop
2 parents 5b88887 + 5ca085c commit 1c4f3fe

17 files changed

Lines changed: 345 additions & 50 deletions

File tree

Core/Sources/ChatGPTChatTab/ChatPanel.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,16 +534,19 @@ 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 {
544546
let content = highlightedCodeBlock(
545547
code: content,
546548
language: language ?? "",
549+
scenario: "chat",
547550
brightMode: brightMode,
548551
fontSize: fontSize
549552
)

Core/Sources/ChatGPTChatTab/Styles.swift

Lines changed: 92 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,17 @@ extension View {
4646
.padding(.top, 14)
4747
}
4848

49-
func codeBlockStyle(_ configuration: CodeBlockConfiguration) -> some View {
50-
background(Color(nsColor: .textBackgroundColor).opacity(0.7))
49+
func codeBlockStyle(
50+
_ configuration: CodeBlockConfiguration,
51+
backgroundColor: Color,
52+
labelColor: Color
53+
) -> some View {
54+
background(backgroundColor)
5155
.clipShape(RoundedRectangle(cornerRadius: 6))
5256
.overlay(alignment: .top) {
5357
HStack(alignment: .center) {
5458
Text(configuration.language ?? "code")
55-
.foregroundStyle(.tertiary)
59+
.foregroundStyle(labelColor)
5660
.font(.callout)
5761
.padding(.leading, 8)
5862
.lineLimit(1)
@@ -63,12 +67,76 @@ extension View {
6367
}
6468
}
6569
}
70+
.overlay {
71+
RoundedRectangle(cornerRadius: 6).stroke(Color.primary.opacity(0.05), lineWidth: 1)
72+
}
6673
.markdownMargin(top: 4, bottom: 16)
6774
}
6875
}
6976

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+
70134
extension MarkdownUI.Theme {
71-
static func custom(fontSize: Double) -> MarkdownUI.Theme {
135+
static func custom(
136+
fontSize: Double,
137+
codeBlockBackgroundColor: Color,
138+
codeBlockLabelColor: Color
139+
) -> MarkdownUI.Theme {
72140
.gitHub.text {
73141
ForegroundColor(.primary)
74142
BackgroundColor(Color.clear)
@@ -80,14 +148,22 @@ extension MarkdownUI.Theme {
80148
if wrapCode {
81149
configuration.label
82150
.codeBlockLabelStyle()
83-
.codeBlockStyle(configuration)
151+
.codeBlockStyle(
152+
configuration,
153+
backgroundColor: codeBlockBackgroundColor,
154+
labelColor: codeBlockLabelColor
155+
)
84156
} else {
85157
ScrollView(.horizontal) {
86158
configuration.label
87159
.codeBlockLabelStyle()
88160
}
89161
.workaroundForVerticalScrollingBugInMacOS()
90-
.codeBlockStyle(configuration)
162+
.codeBlockStyle(
163+
configuration,
164+
backgroundColor: codeBlockBackgroundColor,
165+
labelColor: codeBlockLabelColor
166+
)
91167
}
92168
}
93169
}
@@ -109,14 +185,22 @@ extension MarkdownUI.Theme {
109185
if wrapCode {
110186
configuration.label
111187
.codeBlockLabelStyle()
112-
.codeBlockStyle(configuration)
188+
.codeBlockStyle(
189+
configuration,
190+
backgroundColor: Color(nsColor: .textBackgroundColor).opacity(0.7),
191+
labelColor: Color.secondary.opacity(0.7)
192+
)
113193
} else {
114194
ScrollView(.horizontal) {
115195
configuration.label
116196
.codeBlockLabelStyle()
117197
}
118198
.workaroundForVerticalScrollingBugInMacOS()
119-
.codeBlockStyle(configuration)
199+
.codeBlockStyle(
200+
configuration,
201+
backgroundColor: Color(nsColor: .textBackgroundColor).opacity(0.7),
202+
labelColor: Color.secondary.opacity(0.7)
203+
)
120204
}
121205
}
122206
.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 {

Core/Sources/HostApp/FeatureSettings/ChatSettingsView.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,12 @@ struct ChatSettingsView: View {
166166
Toggle(isOn: $settings.wrapCodeInCodeBlock) {
167167
Text("Wrap code in code block")
168168
}
169+
170+
#if canImport(ProHostApp)
171+
172+
CodeHighlightThemePicker(scenario: .chat)
173+
174+
#endif
169175
}
170176
}
171177

Core/Sources/HostApp/FeatureSettings/PromptToCodeSettingsView.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ struct PromptToCodeSettingsView: View {
103103

104104
Text("pt")
105105
}
106+
107+
#if canImport(ProHostApp)
108+
109+
CodeHighlightThemePicker(scenario: .promptToCode)
110+
111+
#endif
106112
}
107113

108114
ScopeForm()

Core/Sources/HostApp/FeatureSettings/SuggestionSettingsView.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ struct SuggestionSettingsView: View {
187187
Text("Accept Suggestion with Tab")
188188
}
189189
}
190-
190+
191191
Toggle(isOn: $settings.dismissSuggestionWithEsc) {
192192
Text("Dismiss Suggestion with ESC")
193193
}
@@ -259,6 +259,12 @@ struct SuggestionSettingsView: View {
259259

260260
Text("pt")
261261
}
262+
263+
#if canImport(ProHostApp)
264+
265+
CodeHighlightThemePicker(scenario: .suggestion)
266+
267+
#endif
262268
}
263269
}
264270
}

Core/Sources/SuggestionWidget/SuggestionPanelContent/CodeBlockSuggestionPanel.swift

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ struct CodeBlockSuggestionPanel: View {
88
@AppStorage(\.suggestionDisplayCompactMode) var suggestionDisplayCompactMode
99
@AppStorage(\.suggestionPresentationMode) var suggestionPresentationMode
1010
@AppStorage(\.hideCommonPrecedingSpacesInSuggestion) var hideCommonPrecedingSpaces
11+
@AppStorage(\.syncSuggestionHighlightTheme) var syncHighlightTheme
12+
@AppStorage(\.codeForegroundColorLight) var codeForegroundColorLight
13+
@AppStorage(\.codeForegroundColorDark) var codeForegroundColorDark
14+
@AppStorage(\.codeBackgroundColorLight) var codeBackgroundColorLight
15+
@AppStorage(\.codeBackgroundColorDark) var codeBackgroundColorDark
1116

1217
struct ToolBar: View {
1318
@ObservedObject var suggestion: CodeSuggestionProvider
@@ -38,7 +43,7 @@ struct CodeBlockSuggestionPanel: View {
3843
}) {
3944
Text("Dismiss").foregroundStyle(.tertiary).padding(.trailing, 4)
4045
}.buttonStyle(.plain)
41-
46+
4247
Button(action: {
4348
suggestion.rejectSuggestion()
4449
}) {
@@ -101,13 +106,37 @@ struct CodeBlockSuggestionPanel: View {
101106
code: suggestion.code,
102107
language: suggestion.language,
103108
startLineIndex: suggestion.startLineIndex,
109+
scenario: "suggestion",
104110
colorScheme: colorScheme,
105-
fontSize: fontSize,
106-
droppingLeadingSpaces: hideCommonPrecedingSpaces
111+
fontSize: fontSize,
112+
droppingLeadingSpaces: hideCommonPrecedingSpaces,
113+
proposedForegroundColor: {
114+
if syncHighlightTheme {
115+
if colorScheme == .light,
116+
let color = codeForegroundColorLight.value?.swiftUIColor
117+
{
118+
return color
119+
} else if let color = codeForegroundColorDark.value?.swiftUIColor {
120+
return color
121+
}
122+
}
123+
return nil
124+
}()
107125
)
108126
.frame(maxWidth: .infinity)
127+
.background({ () -> Color in
128+
if syncHighlightTheme {
129+
if colorScheme == .light,
130+
let color = codeBackgroundColorLight.value?.swiftUIColor
131+
{
132+
return color
133+
} else if let color = codeBackgroundColorDark.value?.swiftUIColor {
134+
return color
135+
}
136+
}
137+
return Color.contentBackground
138+
}())
109139
}
110-
.background(Color.contentBackground)
111140

112141
if suggestionDisplayCompactMode {
113142
CompactToolBar(suggestion: suggestion)

0 commit comments

Comments
 (0)