-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathNewTaskDialog.swift
More file actions
315 lines (285 loc) · 13.1 KB
/
Copy pathNewTaskDialog.swift
File metadata and controls
315 lines (285 loc) · 13.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
import SwiftUI
import KanbanCodeCore
struct NewTaskDialog: View {
@Binding var isPresented: Bool
var projects: [Project] = []
var defaultProjectPath: String?
var globalRemoteSettings: RemoteSettings?
var enabledAssistants: [CodingAssistant] = CodingAssistant.allCases
/// (prompt, projectPath, title, startImmediately, images) — creates task without an assistant set
var onCreate: (String, String?, String?, Bool, [ImageAttachment]) -> Void = { _, _, _, _, _ in }
/// (prompt, projectPath, title, createWorktree, worktreeBranch, runRemotely, skipPermissions, commandOverride, images, assistant) — creates and launches directly (skips LaunchConfirmation)
var onCreateAndLaunch: (String, String?, String?, Bool, String?, Bool, Bool, String?, [ImageAttachment], CodingAssistant) -> Void = { _, _, _, _, _, _, _, _, _, _ in }
@AppStorage("selectedAssistant") private var selectedAssistantRaw: String = CodingAssistant.claude.rawValue
private var selectedAssistant: CodingAssistant {
get { CodingAssistant(rawValue: selectedAssistantRaw) ?? .claude }
nonmutating set { selectedAssistantRaw = newValue.rawValue }
}
@State private var prompt = ""
@State private var images: [ImageAttachment] = []
@State private var title = ""
@State private var selectedProjectPath: String = ""
@State private var customPath = ""
@State private var command = ""
@State private var commandEdited = false
@State private var worktreeBranch = ""
@AppStorage("startTaskImmediately") private var startImmediately = true
@State private var createWorktree = true
@State private var runRemotely = true
@AppStorage("dangerouslySkipPermissions") private var dangerouslySkipPermissions = true
@AppStorage("lastSelectedProjectPath") private var lastSelectedProjectPath = ""
private static let customPathSentinel = "__custom__"
var body: some View {
VStack(alignment: .leading, spacing: 16) {
Text("New Task")
.font(.app(.title3))
.fontWeight(.semibold)
// Prompt
PromptSection(
text: $prompt,
images: $images,
placeholder: "Describe what you want \(selectedAssistant.displayName) to do...",
onSubmit: submitForm
)
// Title (optional)
TextField("Title (optional)", text: $title)
.textFieldStyle(.roundedBorder)
.font(.app(.callout))
// Project picker
if projects.isEmpty {
TextField("Project path (optional)", text: $customPath)
.textFieldStyle(.roundedBorder)
.font(.app(.caption))
} else {
Picker("Project", selection: $selectedProjectPath) {
ForEach(projects) { project in
Text(project.name).tag(project.path)
}
Divider()
Text("Custom path...").tag(Self.customPathSentinel)
}
if selectedProjectPath == Self.customPathSentinel {
TextField("Project path", text: $customPath)
.textFieldStyle(.roundedBorder)
.font(.app(.caption))
}
}
// Start immediately toggle
Toggle("Start immediately", isOn: $startImmediately)
.font(.app(.callout))
// Launch options (shown when "Start immediately" is checked)
if startImmediately {
VStack(alignment: .leading, spacing: 6) {
Toggle("Create worktree", isOn: (isGitRepo && selectedAssistant.supportsWorktree) ? $createWorktree : .constant(false))
.font(.app(.callout))
.disabled(!isGitRepo || !selectedAssistant.supportsWorktree)
if !isGitRepo {
Label("Not a git repository", systemImage: "info.circle")
.font(.app(.caption2))
.foregroundStyle(.secondary)
.padding(.leading, 20)
} else if !selectedAssistant.supportsWorktree {
Label("\(selectedAssistant.displayName) doesn't support worktrees", systemImage: "info.circle")
.font(.app(.caption2))
.foregroundStyle(.secondary)
.padding(.leading, 20)
}
if createWorktree && isGitRepo {
HStack {
Text("Branch name")
.font(.app(.callout))
.foregroundStyle(.secondary)
TextField("", text: $worktreeBranch, prompt: Text("Leave empty for a random name"))
.textFieldStyle(.roundedBorder)
.font(.app(.callout))
}
.padding(.leading, 20)
}
Toggle("Run remotely", isOn: hasRemoteConfig ? $runRemotely : .constant(false))
.font(.app(.callout))
.disabled(!hasRemoteConfig)
if !hasRemoteConfig {
Label(
globalRemoteSettings != nil
? "Project not under remote sync path"
: "Configure remote execution in Settings > Remote",
systemImage: "info.circle"
)
.font(.app(.caption2))
.foregroundStyle(.secondary)
.padding(.leading, 20)
}
Toggle("Dangerously skip permissions", isOn: $dangerouslySkipPermissions)
.font(.app(.callout))
}
// Editable command
VStack(alignment: .leading, spacing: 4) {
Text("Command")
.font(.app(.caption))
.foregroundStyle(.secondary)
TextEditor(text: $command)
.font(.app(.caption).monospaced())
.frame(minHeight: 36, maxHeight: 80)
.fixedSize(horizontal: false, vertical: true)
.padding(4)
.background(.quaternary.opacity(0.5), in: RoundedRectangle(cornerRadius: 6))
.onChange(of: command) {
if command != commandPreview {
commandEdited = true
}
}
}
}
// Buttons
HStack {
if startImmediately && enabledAssistants.count > 1 {
Picker(selection: $selectedAssistantRaw) {
ForEach(enabledAssistants, id: \.self) { assistant in
Text(assistant.displayName)
.tag(assistant.rawValue)
}
} label: {
EmptyView()
}
.fixedSize()
}
Spacer()
Button("Cancel") {
isPresented = false
}
.keyboardShortcut(.cancelAction)
Button(startImmediately ? "Create & Start" : "Create", action: submitForm)
.keyboardShortcut(.defaultAction)
.disabled(prompt.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty)
.buttonStyle(.borderedProminent)
}
}
.padding(20)
.frame(width: 450)
.onAppear {
if let defaultPath = defaultProjectPath,
projects.contains(where: { $0.path == defaultPath }) {
selectedProjectPath = defaultPath
} else if !lastSelectedProjectPath.isEmpty,
projects.contains(where: { $0.path == lastSelectedProjectPath }) {
selectedProjectPath = lastSelectedProjectPath
} else if let first = projects.first {
selectedProjectPath = first.path
}
// Ensure selected assistant is enabled; fall back to first enabled
if !enabledAssistants.contains(selectedAssistant),
let first = enabledAssistants.first {
selectedAssistant = first
}
if let path = resolvedProjectPath {
runRemotely = UserDefaults.standard.object(forKey: "runRemotely_\(path)") as? Bool ?? true
createWorktree = UserDefaults.standard.object(forKey: "createWorktree_\(path)") as? Bool ?? true
}
command = commandPreview
}
.onChange(of: prompt) {
if !commandEdited { command = commandPreview }
}
.onChange(of: createWorktree) {
if let path = resolvedProjectPath {
UserDefaults.standard.set(createWorktree, forKey: "createWorktree_\(path)")
}
if !commandEdited { command = commandPreview }
}
.onChange(of: worktreeBranch) {
if !commandEdited { command = commandPreview }
}
.onChange(of: runRemotely) {
if let path = resolvedProjectPath {
UserDefaults.standard.set(runRemotely, forKey: "runRemotely_\(path)")
}
if !commandEdited { command = commandPreview }
}
.onChange(of: selectedProjectPath) {
if let path = resolvedProjectPath {
runRemotely = UserDefaults.standard.object(forKey: "runRemotely_\(path)") as? Bool ?? true
createWorktree = UserDefaults.standard.object(forKey: "createWorktree_\(path)") as? Bool ?? true
}
if !commandEdited { command = commandPreview }
}
.onChange(of: dangerouslySkipPermissions) {
if !commandEdited { command = commandPreview }
}
.onChange(of: selectedAssistantRaw) {
if !commandEdited { command = commandPreview }
}
}
// MARK: - Actions
private func submitForm() {
guard !prompt.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { return }
let proj = resolvedProjectPath
let titleOrNil = title.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty ? nil : title.trimmingCharacters(in: .whitespacesAndNewlines)
if let proj { lastSelectedProjectPath = proj }
if startImmediately {
let branch = worktreeBranch.trimmingCharacters(in: .whitespacesAndNewlines)
onCreateAndLaunch(
prompt,
proj,
titleOrNil,
createWorktree && isGitRepo && selectedAssistant.supportsWorktree,
branch.isEmpty ? nil : branch,
runRemotely && hasRemoteConfig,
dangerouslySkipPermissions,
commandEdited ? command : nil,
images,
selectedAssistant
)
} else {
onCreate(prompt, proj, titleOrNil, false, images)
}
isPresented = false
}
// MARK: - Computed
private var resolvedProjectPath: String? {
if projects.isEmpty {
return customPath.isEmpty ? nil : customPath
}
if selectedProjectPath == Self.customPathSentinel {
return customPath.isEmpty ? nil : customPath
}
return selectedProjectPath.isEmpty ? nil : selectedProjectPath
}
private var selectedProject: Project? {
projects.first(where: { $0.path == resolvedProjectPath })
}
private var isGitRepo: Bool {
guard let path = resolvedProjectPath, !path.isEmpty else { return false }
return FileManager.default.fileExists(
atPath: (path as NSString).appendingPathComponent(".git")
)
}
private var hasRemoteConfig: Bool {
guard let remote = globalRemoteSettings else { return false }
guard let path = resolvedProjectPath else { return false }
return path.hasPrefix(remote.localPath)
}
private var remoteHost: String? {
globalRemoteSettings?.host
}
private var commandPreview: String {
var parts: [String] = []
if runRemotely && hasRemoteConfig {
parts.append("SHELL=~/.kanban-code/remote/zsh")
if selectedAssistant == .gemini {
parts.append("PATH=~/.kanban-code/remote:$PATH")
}
}
var cmd = selectedAssistant.cliCommand
if dangerouslySkipPermissions { cmd += " \(selectedAssistant.autoApproveFlag)" }
if createWorktree && isGitRepo && selectedAssistant.supportsWorktree {
let branch = worktreeBranch.trimmingCharacters(in: .whitespacesAndNewlines)
if branch.isEmpty {
cmd += " --worktree"
} else {
cmd += " --worktree \(branch)"
}
}
parts.append(cmd)
return parts.joined(separator: " \\\n ")
}
}