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
163 lines (147 loc) · 5.23 KB
/
ThemedMarkdownText.swift
File metadata and controls
163 lines (147 loc) · 5.23 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import Foundation
import MarkdownUI
import SwiftUI
import ChatService
import ComposableArchitecture
import SuggestionBasic
import ChatTab
public struct MarkdownActionProvider {
let supportInsert: Bool
let onInsert: ((String) -> Void)?
public init(supportInsert: Bool = true, onInsert: ((String) -> Void)? = nil) {
self.supportInsert = supportInsert
self.onInsert = onInsert
}
}
public 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 context: MarkdownActionProvider
public init(text: String, context: MarkdownActionProvider) {
self.text = text
self.context = context
}
init(text: String, chat: StoreOf<Chat>) {
self.text = text
self.context = .init(onInsert: { content in
chat.send(.insertCode(content))
})
}
public 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)
}(),
context: context
))
}
}
// MARK: - Theme
extension MarkdownUI.Theme {
static func custom(
fontSize: Double,
codeFont: NSFont,
codeBlockBackgroundColor: Color,
codeBlockLabelColor: Color,
context: MarkdownActionProvider
) -> MarkdownUI.Theme {
.gitHub.text {
ForegroundColor(.primary)
BackgroundColor(Color.clear)
FontSize(fontSize)
}
.codeBlock { configuration in
MarkdownCodeBlockView(
codeBlockConfiguration: configuration,
codeFont: codeFont,
codeBlockBackgroundColor: codeBlockBackgroundColor,
codeBlockLabelColor: codeBlockLabelColor,
context: context
)
}
}
}
struct MarkdownCodeBlockView: View {
let codeBlockConfiguration: CodeBlockConfiguration
let codeFont: NSFont
let codeBlockBackgroundColor: Color
let codeBlockLabelColor: Color
let context: MarkdownActionProvider
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,
context: context
)
} else {
ScrollView(.horizontal) {
AsyncCodeBlockView(
fenceInfo: codeBlockConfiguration.language,
content: codeBlockConfiguration.content,
font: codeFont
)
.codeBlockLabelStyle()
}
.workaroundForVerticalScrollingBugInMacOS()
.codeBlockStyle(
codeBlockConfiguration,
backgroundColor: codeBlockBackgroundColor,
labelColor: codeBlockLabelColor,
context: context
)
}
}
}
struct ThemedMarkdownText_Previews: PreviewProvider {
static var previews: some View {
let chatTabInfo = ChatTabInfo(id: "id", workspacePath: "path", username: "name")
ThemedMarkdownText(
text:"""
```swift
let sumClosure: (Int, Int) -> Int = { (a: Int, b: Int) in
return a + b
}
```
""",
context: .init(onInsert: {_ in print("Inserted") }))
}
}