forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditCustomCommandView.swift
More file actions
419 lines (381 loc) · 14.1 KB
/
EditCustomCommandView.swift
File metadata and controls
419 lines (381 loc) · 14.1 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import ComposableArchitecture
import MarkdownUI
import Preferences
import SwiftUI
@MainActor
struct EditCustomCommandView: View {
@Environment(\.toast) var toast
@Perception.Bindable var store: StoreOf<EditCustomCommand>
init(store: StoreOf<EditCustomCommand>) {
self.store = store
}
var body: some View {
ScrollView {
Form {
sharedForm
featureSpecificForm
}.padding()
}.safeAreaInset(edge: .bottom) {
bottomBar
}
}
@ViewBuilder var sharedForm: some View {
WithPerceptionTracking {
TextField("Name", text: $store.name)
Picker("Command Type", selection: $store.commandType) {
ForEach(
EditCustomCommand.CommandType.allCases,
id: \.rawValue
) { commandType in
Text({
switch commandType {
case .sendMessage:
return "Send Message"
case .promptToCode:
return "Modification"
case .customChat:
return "Custom Chat"
case .singleRoundDialog:
return "Single Round Dialog"
}
}() as String).tag(commandType)
}
}
}
}
@ViewBuilder var featureSpecificForm: some View {
WithPerceptionTracking {
switch store.commandType {
case .sendMessage:
EditSendMessageCommandView(
store: store.scope(
state: \.sendMessage,
action: \.sendMessage
),
attachmentStore: store.scope(
state: \.attachments,
action: \.attachments
)
)
case .promptToCode:
EditPromptToCodeCommandView(
store: store.scope(
state: \.promptToCode,
action: \.promptToCode
)
)
case .customChat:
EditCustomChatCommandView(
store: store.scope(
state: \.customChat,
action: \.customChat
),
attachmentStore: store.scope(
state: \.attachments,
action: \.attachments
)
)
case .singleRoundDialog:
EditSingleRoundDialogCommandView(
store: store.scope(
state: \.singleRoundDialog,
action: \.singleRoundDialog
)
)
}
}
}
@ViewBuilder var bottomBar: some View {
WithPerceptionTracking {
VStack {
Divider()
VStack(alignment: .trailing) {
Text(
"After renaming or adding a custom command, please restart Xcode to refresh the menu."
)
.foregroundStyle(.secondary)
HStack {
Spacer()
Button("Close") {
store.send(.close)
}
if store.isNewCommand {
Button("Add") {
store.send(.saveCommand)
}
} else {
Button("Save") {
store.send(.saveCommand)
}
}
}
}
.padding(.horizontal)
}
.padding(.bottom)
.background(.regularMaterial)
}
}
}
struct CustomCommandAttachmentPickerView: View {
@Perception.Bindable var store: StoreOf<EditCustomCommandAttachment>
@State private var isFileInputPresented = false
@State private var filePath = ""
#if canImport(ProHostApp)
var body: some View {
WithPerceptionTracking {
VStack(alignment: .leading) {
Text("Contexts")
HStack(alignment: .top) {
VStack(alignment: .leading, spacing: 8) {
if store.attachments.isEmpty {
Text("No context")
.foregroundStyle(.secondary)
} else {
ForEach(store.attachments, id: \.kind) { attachment in
HStack {
switch attachment.kind {
case let .file(path: path):
HStack {
Text("File:")
Text(path).foregroundStyle(.secondary)
}
default:
Text(attachment.kind.description)
}
Spacer()
Button {
store.attachments.removeAll { $0.kind == attachment.kind }
} label: {
Image(systemName: "trash")
}
.buttonStyle(.plain)
}
}
}
}
.frame(minWidth: 240)
.padding(.vertical, 8)
.padding(.horizontal, 8)
.background {
RoundedRectangle(cornerRadius: 8)
.strokeBorder(.separator, lineWidth: 1)
}
Form {
Menu {
ForEach(CustomCommand.Attachment.Kind.allCases.filter { kind in
!store.attachments.contains { $0.kind == kind }
}, id: \.self) { kind in
if kind == .file(path: "") {
Button {
isFileInputPresented = true
} label: {
Text("File...")
}
} else {
Button {
store.attachments.append(.init(kind: kind))
} label: {
Text(kind.description)
}
}
}
} label: {
Label("Add context", systemImage: "plus")
}
Toggle(
"Ignore existing contexts",
isOn: $store.ignoreExistingAttachments
)
}
}
}
.sheet(isPresented: $isFileInputPresented) {
VStack(alignment: .leading, spacing: 16) {
Text("Enter file path:")
.font(.headline)
Text(
"You can enter either an absolute path or a path relative to the project root."
)
.font(.caption)
.foregroundStyle(.secondary)
TextField("File path", text: $filePath)
.textFieldStyle(.roundedBorder)
HStack {
Spacer()
Button("Cancel") {
isFileInputPresented = false
filePath = ""
}
Button("Add") {
store.attachments.append(.init(kind: .file(path: filePath)))
isFileInputPresented = false
filePath = ""
}
.disabled(filePath.isEmpty)
}
}
.padding()
.frame(minWidth: 400)
}
}
}
#else
var body: some View { EmptyView() }
#endif
}
extension CustomCommand.Attachment.Kind {
public static var allCases: [CustomCommand.Attachment.Kind] {
[
.activeDocument,
.debugArea,
.clipboard,
.senseScope,
.projectScope,
.webScope,
.gitStatus,
.gitLog,
.file(path: ""),
]
}
var description: String {
switch self {
case .activeDocument: return "Active Document"
case .debugArea: return "Debug Area"
case .clipboard: return "Clipboard"
case .senseScope: return "Sense Scope"
case .projectScope: return "Project Scope"
case .webScope: return "Web Scope"
case .gitStatus: return "Git Status and Diff"
case .gitLog: return "Git Log"
case .file: return "File"
}
}
}
struct EditSendMessageCommandView: View {
@Perception.Bindable var store: StoreOf<EditSendMessageCommand>
var attachmentStore: StoreOf<EditCustomCommandAttachment>
var body: some View {
WithPerceptionTracking {
VStack(alignment: .leading, spacing: 4) {
Toggle("Extra Context", isOn: $store.useExtraSystemPrompt)
EditableText(text: $store.extraSystemPrompt)
}
.padding(.vertical, 4)
VStack(alignment: .leading, spacing: 4) {
Text("Send immediately")
EditableText(text: $store.prompt)
}
.padding(.vertical, 4)
CustomCommandAttachmentPickerView(store: attachmentStore)
.padding(.vertical, 4)
}
}
}
struct EditPromptToCodeCommandView: View {
@Perception.Bindable var store: StoreOf<EditPromptToCodeCommand>
var body: some View {
WithPerceptionTracking {
Toggle("Continuous Mode", isOn: $store.continuousMode)
VStack(alignment: .leading, spacing: 4) {
Text("Extra Context")
EditableText(text: $store.extraSystemPrompt)
}
.padding(.vertical, 4)
VStack(alignment: .leading, spacing: 4) {
Text("Instruction")
EditableText(text: $store.prompt)
}
.padding(.vertical, 4)
}
}
}
struct EditCustomChatCommandView: View {
@Perception.Bindable var store: StoreOf<EditCustomChatCommand>
var attachmentStore: StoreOf<EditCustomCommandAttachment>
var body: some View {
WithPerceptionTracking {
VStack(alignment: .leading, spacing: 4) {
Text("Topic")
EditableText(text: $store.systemPrompt)
}
.padding(.vertical, 4)
VStack(alignment: .leading, spacing: 4) {
Text("Send immediately")
EditableText(text: $store.prompt)
}
.padding(.vertical, 4)
CustomCommandAttachmentPickerView(store: attachmentStore)
.padding(.vertical, 4)
}
}
}
struct EditSingleRoundDialogCommandView: View {
@Perception.Bindable var store: StoreOf<EditSingleRoundDialogCommand>
var body: some View {
WithPerceptionTracking {
VStack(alignment: .leading, spacing: 4) {
Text("System Prompt")
EditableText(text: $store.systemPrompt)
}
.padding(.vertical, 4)
Picker(selection: $store.overwriteSystemPrompt) {
Text("Append to default system prompt").tag(false)
Text("Overwrite default system prompt").tag(true)
} label: {
Text("Mode")
}
.pickerStyle(.radioGroup)
VStack(alignment: .leading, spacing: 4) {
Text("Prompt")
EditableText(text: $store.prompt)
}
.padding(.vertical, 4)
Toggle("Receive response in notification", isOn: $store.receiveReplyInNotification)
Text(
"You will be prompted to grant the app permission to send notifications for the first time."
)
.font(.footnote)
.foregroundColor(.secondary)
}
}
}
// MARK: - Preview
struct EditCustomCommandView_Preview: PreviewProvider {
static var previews: some View {
EditCustomCommandView(
store: .init(
initialState: .init(.init(
commandId: "4",
name: "Explain Code",
feature: .promptToCode(
extraSystemPrompt: nil,
prompt: "Hello",
continuousMode: false,
generateDescription: true
),
ignoreExistingAttachments: false,
attachments: [] as [CustomCommand.Attachment]
)),
reducer: {
EditCustomCommand(
settings: .init(customCommands: .init(
wrappedValue: [],
"CustomCommandView_Preview"
))
)
}
)
)
.frame(width: 800)
}
}
struct EditSingleRoundDialogCommandView_Preview: PreviewProvider {
static var previews: some View {
EditSingleRoundDialogCommandView(store: .init(
initialState: .init(),
reducer: { EditSingleRoundDialogCommand() }
))
.frame(width: 800, height: 600)
}
}