Branchwell

OpenTalon / app / native / Sources / OpenTalon / WorkspaceView.swift

1import SwiftUI
2
3struct WorkspaceView: View {
4 @Environment(WorkspaceModel.self) private var model
5 @Environment(\.colorScheme) private var systemScheme
6 private var theme: Theme { model.workspace?.theme ?? .system }
7 private var palette: Palette {
8 Palette(theme: theme == .system ? (systemScheme == .dark ? .graphite : .light) : theme)
9 }
10
11 private var canOfferConnectionIntroduction: Bool {
12 model.isWorkspaceWindowKey && !model.isolatedProfile && model.workspace != nil &&
13 model.startupError == nil && model.sheet == nil && model.notice == nil &&
14 !model.draftDirty && !model.isQuickAdding && !model.agentChat.busy &&
15 model.createdTask == nil && model.workspace?.agentRuns.contains(where: \.active) != true
16 }
17
18 var body: some View {
19 @Bindable var model = model
20 HStack(spacing: 0) {
21 if model.showsSidebar {
22 sidebar
23 Rectangle().fill(palette.line).frame(width: 1)
24 }
25 VStack(spacing: 0) {
26 if model.workspaceLocked {
27 ContentUnavailableView {
28 Label("Workspace already open", systemImage: "exclamationmark.triangle")
29 } description: {
30 Text("Quit the other copy, then click Retry.\nIf it’s running in the background, find it in\nActivity Monitor and choose Quit.")
31 } actions: {
32 Button("Retry") { model.retryWorkspace?() }
33 .glassPrimary()
34 }
35 } else if let error = model.startupError {
36 ContentUnavailableView("Could not open the workspace", systemImage: "exclamationmark.triangle",
37 description: Text(error))
38 } else if model.workspace == nil {
39 ProgressView("Opening OpenTalon…")
40 } else {
41 HStack {
42 if model.page == .about {
43 HStack(spacing: 9) {
44 Button("Workspace") { model.navigate(.dashboard) }
45 .buttonStyle(.plain)
46 .fontWeight(.semibold)
47 Image(systemName: "chevron.right").font(.system(size: 8))
48 .accessibilityHidden(true)
49 Text("About OpenTalon").foregroundStyle(palette.muted)
50 }
51 .font(.system(size: 12))
52 .padding(.leading, 12)
53 }
54 Spacer()
55 Button("Ask OpenTalon", systemImage: "sparkles") { model.navigate(.assistant) }
56 .accessibilityIdentifier("askOpenTalon")
57 if model.canUseAgents {
58 Button("Ask my agent", systemImage: "bubble.left.and.text.bubble.right") {
59 model.sheet = .agentChat
60 }
61 .keyboardShortcut("k", modifiers: [.command, .shift])
62 }
63 }
64 .padding(.horizontal, 24)
65 .padding(.vertical, 8)
66 .overlay(alignment: .bottom) { Rectangle().fill(palette.line).frame(height: 1) }
67 if let banner = model.banner { bannerView(banner) }
68 HStack(spacing: 0) {
69 switch model.page {
70 case .dashboard: DashboardView()
71 case .recent: RecentTasksView()
72 case .all, .board: KanbanView()
73 case .archive: ArchiveView()
74 case .trash: TrashView()
75 case .about: AboutView()
76 case .assistant: MacTaskAssistantView()
77 }
78 if let task = model.selected {
79 Rectangle().fill(palette.line).frame(width: 1)
80 TaskDetails(item: task).id(task.id).frame(width: 346)
81 }
82 }
83 }
84 }
85 .frame(maxWidth: .infinity, maxHeight: .infinity)
86 }
87 .frame(minWidth: 980, minHeight: 640)
88 .background(palette.canvas)
89 .foregroundStyle(palette.text)
90 .tint(palette.accent)
91 .overlay(alignment: .bottomTrailing) {
92 if let task = model.createdTask, model.sheet == nil {
93 TaskCreatedToast(task: task)
94 .id(task.id)
95 .padding(20)
96 }
97 }
98 .buttonStyle(GlassActionStyle())
99 .environment(\.palette, palette)
100 .environment(\.colorScheme, palette.dark ? .dark : .light)
101 .sheet(isPresented: Binding(get: { model.sheet != nil }, set: { if !$0 { model.sheet = nil; model.pendingSend = nil } })) {
102 SheetContent()
103 .environment(\.palette, palette)
104 .environment(\.colorScheme, palette.dark ? .dark : .light)
105 .environment(model)
106 }
107 .alert(item: $model.notice) { notice in
108 Alert(title: Text(notice.title), message: Text(notice.message), dismissButton: .default(Text("OK")))
109 }
110 .task(id: canOfferConnectionIntroduction) {
111 if canOfferConnectionIntroduction { model.offerConnectionIntroduction() }
112 }
113 .onExitCommand {
114 if model.selection != nil && model.canLeave() { model.selection = nil }
115 }
116 }
117
118 private var sidebar: some View {
119 VStack(alignment: .leading, spacing: 3) {
120 nav("Dashboard", "square.grid.2x2", .dashboard)
121 nav("All tasks", "list.bullet.rectangle", .all)
122 nav("Recent Tasks", "clock", .recent)
123 HStack {
124 Text("Projects").font(.system(size: 12)).foregroundStyle(palette.muted)
125 Spacer()
126 IconButton(symbol: "plus", label: "New project") { model.sheet = .project(nil) }
127 .disabled(model.workspace == nil)
128 }
129 .padding(.leading, 14)
130 .padding(.trailing, 5)
131 .padding(.top, 8)
132 ThemedScrollView(track: palette.sidebar) {
133 LazyVStack(spacing: 3) {
134 ForEach(model.boards) { board in
135 SidebarButton(title: board.name, symbol: "", selected: model.page == .board(board.id), project: board) {
136 model.navigate(.board(board.id))
137 }
138 .contextMenu { ProjectActions(board: board) }
139 }
140 SidebarButton(title: "New project", symbol: "plus", selected: false) {
141 model.sheet = .project(nil)
142 }
143 .disabled(model.workspace == nil)
144 }
145 }
146 Rectangle().fill(palette.line).frame(height: 1).padding(.vertical, 8)
147 nav("Archive", "archivebox", .archive)
148 nav("Trash", "trash", .trash)
149 nav("About OpenTalon", "info.circle", .about)
150 SidebarButton(title: "Settings", symbol: "gearshape", selected: false) {
151 model.sheet = .settings
152 }
153 .disabled(model.workspace == nil)
154 }
155 .padding(.horizontal, 14)
156 .padding(.top, 12)
157 .padding(.bottom, 20)
158 .frame(width: 234)
159 .frame(maxHeight: .infinity)
160 .background(palette.sidebar)
161 }
162
163 private func nav(_ title: String, _ icon: String, _ page: WorkspacePage) -> some View {
164 SidebarButton(title: title, symbol: icon, selected: model.page == page) { model.navigate(page) }
165 }
166
167 private func bannerView(_ title: String) -> some View {
168 HStack {
169 Text(title).font(.system(size: 12))
170 if let task = model.undoTrash {
171 Button("Undo") { model.restore(task) }.buttonStyle(.link)
172 }
173 Spacer()
174 IconButton(symbol: "xmark", label: "Dismiss message") {
175 model.banner = nil
176 model.undoTrash = nil
177 }
178 }
179 .padding(.horizontal, 18)
180 .padding(.vertical, 3)
181 .background(palette.active)
182 }
183}
184
185private struct TaskCreatedToast: View {
186 @Environment(WorkspaceModel.self) private var model
187 @Environment(\.palette) private var palette
188 @GestureState private var drag = CGFloat.zero
189 let task: WorkItem
190
191 var body: some View {
192 HStack(spacing: 10) {
193 Image(systemName: "checkmark.circle.fill")
194 .foregroundStyle(palette.flow(.done))
195 .accessibilityHidden(true)
196 VStack(alignment: .leading, spacing: 3) {
197 Text("Task created").font(.system(size: 13, weight: .semibold))
198 Text(task.title).font(.caption).foregroundStyle(palette.muted).lineLimit(1)
199 Text("\(model.projectName(task.boardId)) · \(task.priority.label)")
200 .font(.caption).foregroundStyle(palette.muted).lineLimit(1)
201 }
202 .frame(maxWidth: 250, alignment: .leading)
203 Button("Open task") { model.openCreatedTask(task.id) }
204 IconButton(symbol: "xmark", label: "Dismiss notification") { model.createdTask = nil }
205 }
206 .padding(12)
207 .surface(12, fill: palette.panel)
208 .shadow(color: .black.opacity(0.12), radius: 12, y: 4)
209 .offset(x: drag)
210 .simultaneousGesture(DragGesture(minimumDistance: 12)
211 .updating($drag) { value, drag, _ in drag = value.translation.width }
212 .onEnded { value in
213 if abs(value.translation.width) > 64, model.createdTask?.id == task.id {
214 model.createdTask = nil
215 }
216 })
217 }
218}
219
220private struct RecentTasksView: View {
221 @Environment(WorkspaceModel.self) private var model
222 @Environment(\.palette) private var palette
223
224 var body: some View {
225 VStack(alignment: .leading, spacing: 18) {
226 HStack {
227 Text("Recent Tasks").font(palette.display(35)).accessibilityAddTraits(.isHeader)
228 Spacer()
229 SearchField()
230 }
231 let tasks = model.recentTasks
232 if tasks.isEmpty {
233 ContentUnavailableView(model.search.isEmpty ? "No recent tasks" : "No matching tasks", systemImage: "clock")
234 .frame(maxWidth: .infinity, maxHeight: .infinity)
235 } else {
236 ThemedScrollView {
237 LazyVStack(spacing: 8) {
238 ForEach(tasks) { task in
239 Button { model.select(task) } label: {
240 HStack(spacing: 12) {
241 Image(systemName: task.status.symbol).foregroundStyle(palette.accent)
242 .accessibilityHidden(true)
243 VStack(alignment: .leading, spacing: 4) {
244 Text(task.title).font(.system(size: 13, weight: .semibold))
245 Text("\(model.projectName(task.boardId)) · \(task.status.rawValue)")
246 .font(.caption).foregroundStyle(palette.muted)
247 }
248 Spacer(minLength: 8)
249 if let date = task.creationDate {
250 Text(date.formatted(date: .abbreviated, time: .shortened))
251 .font(.caption).foregroundStyle(palette.muted)
252 }
253 }
254 .padding(13)
255 .frame(maxWidth: .infinity, alignment: .leading)
256 .contentShape(Rectangle())
257 }
258 .buttonStyle(.plain)
259 .surface()
260 }
261 }
262 }
263 }
264 }
265 .padding(28)
266 }
267}
268
269private struct SidebarButton: View {
270 @Environment(\.palette) private var palette
271 @State private var hovered = false
272 let title: String
273 let symbol: String
274 let selected: Bool
275 var project: Board? = nil
276 let action: () -> Void
277
278 var body: some View {
279 Button(action: action) {
280 HStack(spacing: 11) {
281 Group {
282 if let project { ProjectIcon(key: project.icon, size: 13) }
283 else { Image(systemName: symbol).accessibilityHidden(true) }
284 }
285 .frame(width: 16)
286 .foregroundStyle(selected ? palette.text : palette.muted)
287 Text(title).fontWeight(selected ? .semibold : .regular).lineLimit(1)
288 Spacer(minLength: 0)
289 }
290 .font(.system(size: 13))
291 .padding(.horizontal, 14)
292 .frame(height: 39)
293 .background(selected ? palette.active : hovered ? palette.hover : .clear, in: .rect(cornerRadius: 7))
294 .overlay(alignment: .leading) {
295 if selected { RoundedRectangle(cornerRadius: 2).fill(palette.accent).frame(width: 3, height: 23) }
296 }
297 .contentShape(Rectangle())
298 }
299 .buttonStyle(.plain)
300 .onHover { hovered = $0 }
301 .accessibilityAddTraits(selected ? .isSelected : [])
302 .help(title)
303 }
304}
305
306struct ProjectActions: View {
307 @Environment(WorkspaceModel.self) private var model
308 let board: Board
309 var body: some View {
310 Button("Rename project") { model.sheet = .project(board) }
311 Button("Change icon") { model.sheet = .icon(board) }
312 Button("Agent folder…") { model.sheet = .projectFolder(board) }
313 if model.boards.count > 1 {
314 Divider()
315 Button("Delete empty project", role: .destructive) {
316 model.perform {
317 try await model.mutate("delete_board", ["id": board.id, "expectedRevision": board.revision])
318 }
319 }
320 .disabled(model.tasks.contains { $0.boardId == board.id })
321 }
322 }
323}
324
325struct SearchField: View {
326 @Environment(WorkspaceModel.self) private var model
327 @Environment(\.palette) private var palette
328 @FocusState private var focused: Bool
329 var body: some View {
330 @Bindable var model = model
331 HStack(spacing: 8) {
332 Image(systemName: "magnifyingglass").foregroundStyle(palette.muted)
333 TextField("Search tasks", text: $model.search)
334 .textFieldStyle(.plain)
335 .focused($focused)
336 .accessibilityLabel("Search tasks")
337 if !model.search.isEmpty {
338 Button { model.search = "" } label: { Image(systemName: "xmark.circle.fill") }
339 .buttonStyle(.plain).help("Clear search").accessibilityLabel("Clear search")
340 }
341 }
342 .font(.system(size: 12))
343 .padding(.horizontal, 10)
344 .frame(height: 34)
345 .frame(width: 175)
346 .glassSearchSurface()
347 .onChange(of: model.searchSignal) { _, _ in focused = true }
348 .onAppear { if model.searchSignal > 0 { focused = true } }
349 }
350}
351
352private struct ArchiveView: View {
353 private struct BulkMove {
354 let ids: [String]
355 let revision: Int
356 }
357
358 @Environment(WorkspaceModel.self) private var model
359 @Environment(\.palette) private var palette
360 @State private var movePreview: BulkMove?
361 @State private var confirmMove = false
362
363 private var archived: [WorkItem] {
364 model.tasks.filter { !$0.deleted && $0.archivedAt != nil }
365 }
366
367 private var items: [WorkItem] {
368 archived.filter { model.matches($0, filterPriority: false) }
369 .sorted { ($0.archivedAt ?? "") > ($1.archivedAt ?? "") }
370 }
371
372 var body: some View {
373 VStack(alignment: .leading, spacing: 20) {
374 HStack {
375 VStack(alignment: .leading, spacing: 5) {
376 Text("Archive").font(palette.display(35))
377 Text("Tasks you archive appear here. Completed tasks also move here after seven days.")
378 .font(.system(size: 12)).foregroundStyle(palette.muted)
379 }
380 Spacer()
381 SearchField()
382 Menu("Archive actions") {
383 Button("Move All to Trash") {
384 guard model.canLeave(), let revision = model.workspace?.revision else { return }
385 movePreview = BulkMove(ids: archived.map(\.id), revision: revision)
386 confirmMove = true
387 }
388 Button("Delete All Permanently…", role: .destructive) {
389 guard model.canLeave() else { return }
390 let ids = archived.map(\.id)
391 model.perform { try await model.preview("purge_archived_tasks", taskIds: ids) }
392 }
393 }
394 .disabled(archived.isEmpty)
395 }
396 if items.isEmpty {
397 ContentUnavailableView("No archived tasks", systemImage: "archivebox",
398 description: model.search.isEmpty ? nil : Text("Try a different search."))
399 .frame(maxWidth: .infinity, maxHeight: .infinity)
400 } else {
401 ThemedScrollView {
402 LazyVStack(spacing: 8) {
403 ForEach(items) { item in
404 HStack {
405 Button { model.select(item) } label: {
406 VStack(alignment: .leading, spacing: 5) {
407 Text(item.title).fontWeight(.medium)
408 Text(model.projectName(item.boardId))
409 .font(.caption).foregroundStyle(palette.muted)
410 }
411 .frame(maxWidth: .infinity, alignment: .leading)
412 .contentShape(Rectangle())
413 }
414 .buttonStyle(.plain)
415 Button("Reopen") { model.move(item, to: .todo) }
416 Menu {
417 Button("Move to Trash", role: .destructive) { model.trash(item) }
418 Button("Delete permanently", role: .destructive) {
419 guard model.canLeave() else { return }
420 model.perform { try await model.preview("purge_archived_tasks", taskIds: [item.id]) }
421 }
422 } label: {
423 Image(systemName: "ellipsis")
424 }
425 .accessibilityLabel("More actions for \(item.title)")
426 }
427 .padding(13)
428 .surface()
429 }
430 }
431 }
432 }
433 }
434 .padding(35)
435 .alert("Move all archived tasks to Trash?", isPresented: $confirmMove, presenting: movePreview) { move in
436 Button("Cancel", role: .cancel) {}
437 Button("Move All to Trash") {
438 model.perform {
439 try await model.mutate("manage_archived_tasks", [
440 "taskIds": move.ids, "expectedRevision": move.revision, "permanently": false,
441 ])
442 model.banner = "Archived tasks moved to Trash"
443 }
444 }
445 } message: { move in
446 Text("You can restore these \(move.ids.count) archived items from Trash.")
447 }
448 }
449}
450
451struct TrashView: View {
452 @Environment(WorkspaceModel.self) private var model
453 @Environment(\.palette) private var palette
454 var items: [WorkItem] {
455 model.tasks.filter { $0.deleted && model.matches($0, filterPriority: false) }
456 .sorted { ($0.deletedAt ?? "") > ($1.deletedAt ?? "") }
457 }
458 var body: some View {
459 VStack(alignment: .leading, spacing: 20) {
460 HStack {
461 VStack(alignment: .leading, spacing: 5) {
462 Text("Trash").font(palette.display(35))
463 Text("Restore a task or permanently remove it.").font(.system(size: 12)).foregroundStyle(palette.muted)
464 }
465 Spacer()
466 SearchField()
467 Button("Empty Trash", role: .destructive) {
468 model.perform { try await model.preview("empty_trash") }
469 }
470 .disabled(!model.tasks.contains(where: \.deleted))
471 }
472 if items.isEmpty {
473 ContentUnavailableView("No tasks in Trash", systemImage: "trash",
474 description: Text(model.search.isEmpty ? "Deleted tasks appear here." : "Try a different search."))
475 .frame(maxWidth: .infinity, maxHeight: .infinity)
476 } else {
477 ThemedScrollView {
478 LazyVStack(spacing: 8) {
479 ForEach(items) { item in
480 HStack {
481 VStack(alignment: .leading, spacing: 5) {
482 Text(item.title).fontWeight(.medium)
483 Text(model.projectName(item.boardId)).font(.caption).foregroundStyle(palette.muted)
484 }
485 Spacer()
486 Button("Restore") { model.restore(item) }
487 IconButton(symbol: "trash", label: "Permanently delete \(item.title)") {
488 model.perform { try await model.preview("purge_tasks", taskIds: [item.id]) }
489 }.foregroundStyle(palette.danger)
490 }
491 .padding(13)
492 .surface()
493 }
494 }
495 }
496 }
497 }
498 .padding(35)
499 }
500}