forked from github/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThemedMarkdownText.swift
More file actions
145 lines (132 loc) · 4.63 KB
/
ThemedMarkdownText.swift
File metadata and controls
145 lines (132 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import Foundation
import MarkdownUI
import SwiftUI
import ChatService
import ComposableArchitecture
import SuggestionBasic
struct ThemedMarkdownText: View {
@AppStorage(\.syncChatCodeHighlightTheme) var syncCodeHighlightTheme
@AppStorage(\.codeForegroundColorLight) var codeForegroundColorLight
@AppStorage(\.codeBackgroundColorLight) var codeBackgroundColorLight
@AppStorage(\.codeForegroundColorDark) var codeForegroundColorDark
@AppStorage(\.codeBackgroundColorDark) var codeBackgroundColorDark
@AppStorage(\.chatFontSize) var chatFontSize
@AppStorage(\.chatCodeFont) var chatCodeFont
@Environment(\.colorScheme) var colorScheme
let text: String
let chat: StoreOf<Chat>
init(text: String, chat: StoreOf<Chat>) {
self.text = text
self.chat = chat
}
var body: some View {
Markdown(text)
.textSelection(.enabled)
.markdownTheme(.custom(
fontSize: chatFontSize,
codeFont: chatCodeFont.value.nsFont,
codeBlockBackgroundColor: {
if syncCodeHighlightTheme {
if colorScheme == .light, let color = codeBackgroundColorLight.value {
return color.swiftUIColor
} else if let color = codeBackgroundColorDark.value {
return color.swiftUIColor
}
}
return Color(nsColor: .textBackgroundColor).opacity(0.7)
}(),
codeBlockLabelColor: {
if syncCodeHighlightTheme {
if colorScheme == .light,
let color = codeForegroundColorLight.value
{
return color.swiftUIColor.opacity(0.5)
} else if let color = codeForegroundColorDark.value {
return color.swiftUIColor.opacity(0.5)
}
}
return Color.secondary.opacity(0.7)
}(),
chat: chat
))
}
}
// MARK: - Theme
extension MarkdownUI.Theme {
static func custom(
fontSize: Double,
codeFont: NSFont,
codeBlockBackgroundColor: Color,
codeBlockLabelColor: Color,
chat: StoreOf<Chat>
) -> MarkdownUI.Theme {
.gitHub.text {
ForegroundColor(.primary)
BackgroundColor(Color.clear)
FontSize(fontSize)
}
.codeBlock { configuration in
MarkdownCodeBlockView(
codeBlockConfiguration: configuration,
codeFont: codeFont,
codeBlockBackgroundColor: codeBlockBackgroundColor,
codeBlockLabelColor: codeBlockLabelColor,
chat: chat
)
}
}
}
struct MarkdownCodeBlockView: View {
let codeBlockConfiguration: CodeBlockConfiguration
let codeFont: NSFont
let codeBlockBackgroundColor: Color
let codeBlockLabelColor: Color
let chat: StoreOf<Chat>
func insertCode() {
chat.send(.insertCode(codeBlockConfiguration.content))
}
var body: some View {
let wrapCode = UserDefaults.shared.value(for: \.wrapCodeInChatCodeBlock)
if wrapCode {
AsyncCodeBlockView(
fenceInfo: codeBlockConfiguration.language,
content: codeBlockConfiguration.content,
font: codeFont
)
.codeBlockLabelStyle()
.codeBlockStyle(
codeBlockConfiguration,
backgroundColor: codeBlockBackgroundColor,
labelColor: codeBlockLabelColor,
insertAction: insertCode
)
} else {
ScrollView(.horizontal) {
AsyncCodeBlockView(
fenceInfo: codeBlockConfiguration.language,
content: codeBlockConfiguration.content,
font: codeFont
)
.codeBlockLabelStyle()
}
.workaroundForVerticalScrollingBugInMacOS()
.codeBlockStyle(
codeBlockConfiguration,
backgroundColor: codeBlockBackgroundColor,
labelColor: codeBlockLabelColor,
insertAction: insertCode
)
}
}
}
#Preview("Themed Markdown Text") {
ThemedMarkdownText(
text:"""
```swift
let sumClosure: (Int, Int) -> Int = { (a: Int, b: Int) in
return a + b
}
```
""",
chat: .init(initialState: .init(), reducer: { Chat(service: ChatService.service()) }))
}