Skip to content

Commit 33d0081

Browse files
committed
Use user-defined font size in UI
1 parent 59b9090 commit 33d0081

File tree

7 files changed

+64
-33
lines changed

7 files changed

+64
-33
lines changed

Core/Sources/SuggestionWidget/Styles.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ extension View {
5656
}
5757

5858
extension MarkdownUI.Theme {
59-
static var custom: MarkdownUI.Theme {
59+
static func custom(fontSize: Double) -> MarkdownUI.Theme {
6060
.gitHub.text {
6161
BackgroundColor(Color.clear)
62+
FontSize(fontSize)
6263
}
6364
.codeBlock { configuration in
6465
configuration.label

Core/Sources/SuggestionWidget/SuggestionPanelContent/ChatPanel.swift

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ struct ChatPanelMessages: View {
6262
@ObservedObject var chat: ChatProvider
6363
@AppStorage(\.disableLazyVStack) var disableLazyVStack
6464
@State var height: Double = 0
65-
65+
6666
struct HeightPreferenceKey: PreferenceKey {
6767
static var defaultValue: Double = 0
6868
static func reduce(value: inout Double, nextValue: () -> Double) {
6969
value = nextValue() + value
7070
}
7171
}
72-
72+
7373
struct UpdateHeightModifier: ViewModifier {
7474
func body(content: Content) -> some View {
7575
content
@@ -81,7 +81,7 @@ struct ChatPanelMessages: View {
8181
}
8282
}
8383
}
84-
84+
8585
var body: some View {
8686
List {
8787
Group {
@@ -169,13 +169,18 @@ private struct UserMessage: View {
169169
let text: String
170170
let chat: ChatProvider
171171
@Environment(\.colorScheme) var colorScheme
172+
@AppStorage(\.chatFontSize) var chatFontSize
173+
@AppStorage(\.chatCodeFontSize) var chatCodeFontSize
172174

173175
var body: some View {
174176
Markdown(text)
175177
.textSelection(.enabled)
176-
.markdownTheme(.custom)
178+
.markdownTheme(.custom(fontSize: chatFontSize))
177179
.markdownCodeSyntaxHighlighter(
178-
ChatCodeSyntaxHighlighter(brightMode: colorScheme != .dark)
180+
ChatCodeSyntaxHighlighter(
181+
brightMode: colorScheme != .dark,
182+
fontSize: chatCodeFontSize
183+
)
179184
)
180185
.frame(alignment: .leading)
181186
.padding()
@@ -197,13 +202,13 @@ private struct UserMessage: View {
197202
NSPasteboard.general.clearContents()
198203
NSPasteboard.general.setString(text, forType: .string)
199204
}
200-
205+
201206
Button("Send Again") {
202207
chat.resendMessage(id: id)
203208
}
204-
209+
205210
Divider()
206-
211+
207212
Button("Delete") {
208213
chat.deleteMessage(id: id)
209214
}
@@ -216,6 +221,8 @@ private struct BotMessage: View {
216221
let text: String
217222
let chat: ChatProvider
218223
@Environment(\.colorScheme) var colorScheme
224+
@AppStorage(\.chatFontSize) var chatFontSize
225+
@AppStorage(\.chatCodeFontSize) var chatCodeFontSize
219226

220227
var body: some View {
221228
HStack(alignment: .bottom, spacing: 2) {
@@ -227,9 +234,12 @@ private struct BotMessage: View {
227234

228235
Markdown(text)
229236
.textSelection(.enabled)
230-
.markdownTheme(.custom)
237+
.markdownTheme(.custom(fontSize: chatFontSize))
231238
.markdownCodeSyntaxHighlighter(
232-
ChatCodeSyntaxHighlighter(brightMode: colorScheme != .dark)
239+
ChatCodeSyntaxHighlighter(
240+
brightMode: colorScheme != .dark,
241+
fontSize: chatCodeFontSize
242+
)
233243
)
234244
.frame(alignment: .trailing)
235245
.padding()
@@ -249,9 +259,9 @@ private struct BotMessage: View {
249259
NSPasteboard.general.clearContents()
250260
NSPasteboard.general.setString(text, forType: .string)
251261
}
252-
262+
253263
Divider()
254-
264+
255265
Button("Delete") {
256266
chat.deleteMessage(id: id)
257267
}
@@ -499,17 +509,19 @@ struct ChatPanel_EmptyChat_Preview: PreviewProvider {
499509

500510
struct ChatCodeSyntaxHighlighter: CodeSyntaxHighlighter {
501511
let brightMode: Bool
512+
let fontSize: Double
502513

503-
init(brightMode: Bool) {
514+
init(brightMode: Bool, fontSize: Double) {
504515
self.brightMode = brightMode
516+
self.fontSize = fontSize
505517
}
506518

507519
func highlightCode(_ content: String, language: String?) -> Text {
508520
let content = highlightedCodeBlock(
509521
code: content,
510522
language: language ?? "",
511523
brightMode: brightMode,
512-
fontSize: 12
524+
fontSize: fontSize
513525
)
514526
return Text(AttributedString(content))
515527
}

Core/Sources/SuggestionWidget/SuggestionPanelContent/CodeBlock.swift

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,37 @@ struct CodeBlock: View {
88
let commonPrecedingSpaceCount: Int
99
let highlightedCode: [NSAttributedString]
1010
let firstLinePrecedingSpaceCount: Int
11-
11+
let fontSize: Double
12+
1213
@AppStorage(\.disableLazyVStack) var disableLazyVStack
1314

1415
init(
1516
code: String,
1617
language: String,
1718
startLineIndex: Int,
1819
colorScheme: ColorScheme,
19-
firstLinePrecedingSpaceCount: Int = 0
20+
firstLinePrecedingSpaceCount: Int = 0,
21+
fontSize: Double
2022
) {
2123
self.code = code
2224
self.language = language
2325
self.startLineIndex = startLineIndex
2426
self.colorScheme = colorScheme
2527
self.firstLinePrecedingSpaceCount = firstLinePrecedingSpaceCount
28+
self.fontSize = fontSize
2629
let padding = firstLinePrecedingSpaceCount > 0
2730
? String(repeating: " ", count: firstLinePrecedingSpaceCount)
2831
: ""
2932
let result = Self.highlight(
3033
code: padding + code,
3134
language: language,
32-
colorScheme: colorScheme
35+
colorScheme: colorScheme,
36+
fontSize: fontSize
3337
)
3438
commonPrecedingSpaceCount = result.commonLeadingSpaceCount
3539
highlightedCode = result.code
3640
}
37-
41+
3842
@ViewBuilder
3943
func vstack(@ViewBuilder content: () -> some View) -> some View {
4044
if disableLazyVStack {
@@ -82,14 +86,16 @@ struct CodeBlock: View {
8286
static func highlight(
8387
code: String,
8488
language: String,
85-
colorScheme: ColorScheme
89+
colorScheme: ColorScheme,
90+
fontSize: Double
8691
) -> (code: [NSAttributedString], commonLeadingSpaceCount: Int) {
8792
return highlighted(
8893
code: code,
8994
language: language,
9095
brightMode: colorScheme != .dark,
9196
droppingLeadingSpaces: UserDefaults.shared
92-
.value(for: \.hideCommonPrecedingSpacesInSuggestion)
97+
.value(for: \.hideCommonPrecedingSpacesInSuggestion),
98+
fontSize: fontSize
9399
)
94100
}
95101
}

Core/Sources/SuggestionWidget/SuggestionPanelContent/CodeBlockSuggestionPanel.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import SwiftUI
33
struct CodeBlockSuggestionPanel: View {
44
@ObservedObject var suggestion: SuggestionProvider
55
@Environment(\.colorScheme) var colorScheme
6+
@AppStorage(\.suggestionCodeFontSize) var fontSize
67

78
struct ToolBar: View {
89
@ObservedObject var suggestion: SuggestionProvider
@@ -53,7 +54,8 @@ struct CodeBlockSuggestionPanel: View {
5354
code: suggestion.code,
5455
language: suggestion.language,
5556
startLineIndex: suggestion.startLineIndex,
56-
colorScheme: colorScheme
57+
colorScheme: colorScheme,
58+
fontSize: fontSize
5759
)
5860
.frame(maxWidth: .infinity)
5961
}

Core/Sources/SuggestionWidget/SuggestionPanelContent/PromptToCodePanel.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ struct PromptToCodePanel: View {
9595
struct PromptToCodePanelContent: View {
9696
@ObservedObject var provider: PromptToCodeProvider
9797
@Environment(\.colorScheme) var colorScheme
98+
@AppStorage(\.suggestionCodeFontSize) var fontSize
9899

99100
var body: some View {
100101
CustomScrollView {
@@ -142,7 +143,8 @@ struct PromptToCodePanelContent: View {
142143
language: provider.language,
143144
startLineIndex: provider.startLineIndex,
144145
colorScheme: colorScheme,
145-
firstLinePrecedingSpaceCount: provider.startLineColumn
146+
firstLinePrecedingSpaceCount: provider.startLineColumn,
147+
fontSize: fontSize
146148
)
147149
.frame(maxWidth: .infinity)
148150
.scaleEffect(x: -1, y: -1, anchor: .center)

Core/Sources/SuggestionWidget/SyntaxHighlighting.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func highlightedCodeBlock(
1919
let highlighter =
2020
SyntaxHighlighter(
2121
format: AttributedStringOutputFormat(theme: .init(
22-
font: .init(size: 14),
22+
font: .init(size: fontSize),
2323
plainTextColor: plainTextColor,
2424
tokenColors: brightMode
2525
? [
@@ -108,13 +108,14 @@ func highlighted(
108108
code: String,
109109
language: String,
110110
brightMode: Bool,
111-
droppingLeadingSpaces: Bool
111+
droppingLeadingSpaces: Bool,
112+
fontSize: Double
112113
) -> (code: [NSAttributedString], commonLeadingSpaceCount: Int) {
113114
let formatted = highlightedCodeBlock(
114115
code: code,
115116
language: language,
116117
brightMode: brightMode,
117-
fontSize: 13
118+
fontSize: fontSize
118119
)
119120
let middleDotColor = brightMode
120121
? NSColor.black.withAlphaComponent(0.1)

Core/Tests/SuggestionWidgetTests/ConvertToCodeLinesTests.swift

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ final class ConvertToCodeLinesTests: XCTestCase {
1212
code: code,
1313
language: "swift",
1414
brightMode: true,
15-
droppingLeadingSpaces: false
15+
droppingLeadingSpaces: false,
16+
fontSize: 14
1617
)
1718

1819
XCTAssertEqual(spaceCount, 0)
@@ -32,7 +33,8 @@ final class ConvertToCodeLinesTests: XCTestCase {
3233
code: code,
3334
language: "md",
3435
brightMode: true,
35-
droppingLeadingSpaces: true
36+
droppingLeadingSpaces: true,
37+
fontSize: 14
3638
)
3739

3840
XCTAssertEqual(spaceCount, 0)
@@ -51,7 +53,8 @@ final class ConvertToCodeLinesTests: XCTestCase {
5153
code: code,
5254
language: "md",
5355
brightMode: true,
54-
droppingLeadingSpaces: true
56+
droppingLeadingSpaces: true,
57+
fontSize: 14
5558
)
5659

5760
XCTAssertEqual(spaceCount, 4)
@@ -70,7 +73,8 @@ final class ConvertToCodeLinesTests: XCTestCase {
7073
code: code,
7174
language: "md",
7275
brightMode: true,
73-
droppingLeadingSpaces: true
76+
droppingLeadingSpaces: true,
77+
fontSize: 14
7478
)
7579

7680
XCTAssertEqual(spaceCount, 8)
@@ -90,7 +94,8 @@ final class ConvertToCodeLinesTests: XCTestCase {
9094
code: code,
9195
language: "md",
9296
brightMode: true,
93-
droppingLeadingSpaces: true
97+
droppingLeadingSpaces: true,
98+
fontSize: 14
9499
)
95100

96101
XCTAssertEqual(spaceCount, 4)
@@ -111,7 +116,8 @@ final class ConvertToCodeLinesTests: XCTestCase {
111116
code: code,
112117
language: "md",
113118
brightMode: true,
114-
droppingLeadingSpaces: true
119+
droppingLeadingSpaces: true,
120+
fontSize: 14
115121
)
116122

117123
XCTAssertEqual(spaceCount, 0)
@@ -132,7 +138,8 @@ final class ConvertToCodeLinesTests: XCTestCase {
132138
code: code,
133139
language: "md",
134140
brightMode: true,
135-
droppingLeadingSpaces: true
141+
droppingLeadingSpaces: true,
142+
fontSize: 14
136143
)
137144

138145
XCTAssertEqual(spaceCount, 4)

0 commit comments

Comments
 (0)