forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSuggestionPanelView.swift
More file actions
234 lines (213 loc) · 7.97 KB
/
SuggestionPanelView.swift
File metadata and controls
234 lines (213 loc) · 7.97 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import Environment
import SwiftUI
@MainActor
final class SuggestionPanelViewModel: ObservableObject {
@Published var startLineIndex: Int
@Published var suggestion: [String]
@Published var isPanelDisplayed: Bool
@Published var suggestionCount: Int
@Published var currentSuggestionIndex: Int
var onAcceptButtonTapped: (() -> Void)?
var onRejectButtonTapped: (() -> Void)?
var onPreviousButtonTapped: (() -> Void)?
var onNextButtonTapped: (() -> Void)?
public init(
startLineIndex: Int = 0,
suggestion: [String] = [],
isPanelDisplayed: Bool = false,
suggestionCount: Int = 0,
currentSuggestionIndex: Int = 0,
onAcceptButtonTapped: (() -> Void)? = nil,
onRejectButtonTapped: (() -> Void)? = nil,
onPreviousButtonTapped: (() -> Void)? = nil,
onNextButtonTapped: (() -> Void)? = nil
) {
self.startLineIndex = startLineIndex
self.suggestion = suggestion
self.isPanelDisplayed = isPanelDisplayed
self.suggestionCount = suggestionCount
self.currentSuggestionIndex = currentSuggestionIndex
self.onAcceptButtonTapped = onAcceptButtonTapped
self.onRejectButtonTapped = onRejectButtonTapped
self.onPreviousButtonTapped = onPreviousButtonTapped
self.onNextButtonTapped = onNextButtonTapped
}
}
struct SuggestionPanelView: View {
@ObservedObject var viewModel: SuggestionPanelViewModel
@State var isHovering: Bool = false
@State var codeHeight: Double = 0
var body: some View {
VStack {
Spacer()
.frame(minHeight: 0, maxHeight: .infinity)
.allowsHitTesting(false)
ZStack(alignment: .topLeading) {
VStack(spacing: 0) {
ScrollView {
CodeBlock(viewModel: viewModel)
}
.background(Color(red: 31 / 255, green: 31 / 255, blue: 36 / 255))
ToolBar(viewModel: viewModel)
}
}
.frame(maxWidth: .infinity, maxHeight: Style.panelHeight)
.fixedSize(horizontal: false, vertical: true)
.clipShape(RoundedRectangle(cornerRadius: 8, style: .continuous))
.overlay(
RoundedRectangle(cornerRadius: 8, style: .continuous)
.stroke(Color.black.opacity(0.3), style: .init(lineWidth: 1))
)
.overlay(
RoundedRectangle(cornerRadius: 7, style: .continuous)
.stroke(Color.white.opacity(0.2), style: .init(lineWidth: 1))
.padding(2)
)
.onHover { yes in
withAnimation(.easeInOut(duration: 0.2)) {
isHovering = yes
}
}
.allowsHitTesting(viewModel.isPanelDisplayed && !viewModel.suggestion.isEmpty)
.preferredColorScheme(.dark)
}
.opacity({
guard viewModel.isPanelDisplayed else { return 0 }
guard !viewModel.suggestion.isEmpty else { return 0 }
return 1
}())
}
}
struct CodeBlock: View {
struct SizePreferenceKey: PreferenceKey {
public static var defaultValue: CGSize = .zero
public static func reduce(value: inout CGSize, nextValue: () -> CGSize) {
value = value.width + value.height > nextValue().width + nextValue()
.height ? value : nextValue()
}
}
@ObservedObject var viewModel: SuggestionPanelViewModel
var body: some View {
LazyVGrid(columns: [
GridItem(.fixed(30), alignment: .top),
GridItem(.flexible()),
], spacing: 4) {
ForEach(0..<viewModel.suggestion.count, id: \.self) { index in
Text("\(index + viewModel.startLineIndex + 1)")
.foregroundColor(Color.white.opacity(0.6))
Text(viewModel.suggestion[index])
.frame(maxWidth: .infinity, alignment: .leading)
.multilineTextAlignment(.leading)
.lineSpacing(4)
}
}
.foregroundColor(.white)
.font(.system(size: 12, design: .monospaced))
.padding()
.background(GeometryReader(content: { proxy in
Color.clear.preference(key: SizePreferenceKey.self, value: proxy.size)
}))
}
}
struct ToolBar: View {
@ObservedObject var viewModel: SuggestionPanelViewModel
var body: some View {
HStack {
Button(action: {
Task {
if let block = viewModel.onPreviousButtonTapped {
block()
return
}
try await Environment.triggerAction("Previous Suggestion")
}
}) {
Image(systemName: "chevron.left")
}.buttonStyle(.plain)
Text("\(viewModel.currentSuggestionIndex + 1) / \(viewModel.suggestionCount)")
.monospacedDigit()
Button(action: {
Task {
if let block = viewModel.onNextButtonTapped {
block()
return
}
try await Environment.triggerAction("Next Suggestion")
}
}) {
Image(systemName: "chevron.right")
}.buttonStyle(.plain)
Spacer()
Button(action: {
Task {
if let block = viewModel.onRejectButtonTapped {
block()
return
}
try await Environment.triggerAction("Reject Suggestion")
}
}) {
Text("Reject")
}.buttonStyle(CommandButtonStyle(color: .gray))
Button(action: {
Task {
if let block = viewModel.onAcceptButtonTapped {
block()
return
}
try await Environment.triggerAction("Accept Suggestion")
}
}) {
Text("Accept")
}.buttonStyle(CommandButtonStyle(color: .indigo))
}
.padding()
.foregroundColor(.secondary)
.background(.thickMaterial)
}
}
struct CommandButtonStyle: ButtonStyle {
let color: Color
func makeBody(configuration: Configuration) -> some View {
configuration.label
.padding(.vertical, 4)
.padding(.horizontal, 8)
.foregroundColor(.white)
.background(
RoundedRectangle(cornerRadius: 4, style: .continuous)
.fill(color.opacity(configuration.isPressed ? 0.8 : 1))
.animation(.easeOut(duration: 0.1), value: configuration.isPressed)
)
.overlay {
RoundedRectangle(cornerRadius: 4, style: .continuous)
.stroke(Color.white.opacity(0.2), style: .init(lineWidth: 1))
}
}
}
struct SuggestionPanelView_Preview: PreviewProvider {
static var previews: some View {
SuggestionPanelView(viewModel: .init(
startLineIndex: 8,
suggestion:
"""
LazyVGrid(columns: [GridItem(.fixed(30)), GridItem(.flexible())]) {
ForEach(0..<viewModel.suggestion.count, id: \\.self) { index in // lkjaskldjalksjdlkasjdlkajslkdjas
Text(viewModel.suggestion[index])
.frame(maxWidth: .infinity, alignment: .leading)
.multilineTextAlignment(.leading)
}
Spacer()
}
""".split(separator: "\n").map(String.init),
isPanelDisplayed: true
))
.frame(width: 450, height: 400)
.background {
HStack {
Color.red
Color.green
Color.blue
}
}
}
}