oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1import EquipoKit
2import Observation
3import SwiftUI
4
5/// One focused window's navigation and task selection. Commands never use a
6/// global selected task, so moving focus between windows changes authority.
7@MainActor @Observable final class MacWorkspace {
8 let session: EquipoSession
9 let app: EquipoAppModel
10 let standalone: Bool
11 let history = MacNavigationHistory()
12 var pane = Destination.Pane.conversation
13 var projectID: String?
14 var focusCard: String?
15 var inspector = false
16 var palette = false
17 struct QueuedAction { let action: MacTaskAction; let context: MacTaskContext; let navigation: Int }
18 private var navigationRevision = 0
19 var pendingAction: QueuedAction?
20 let composerDraft = MacComposerDraft()
21 var composerFocus = 0
22 var context: MacTaskContext?
23 var mainRequest = 0
24 init(app: EquipoAppModel, session: EquipoSession, taskID: String? = nil) {
25 self.app = app; self.session = session; standalone = taskID != nil
26 if let taskID { context = MacTaskContext(session: session, taskID: taskID) }
27 }
28 private struct IndexSource: Equatable {
29 let tasks: [ThinTask]; let needs: [ThinTask]; let projects: [ThinProject]
30 }
31 @ObservationIgnored private var indexSource: IndexSource?
32 @ObservationIgnored private var cachedIndex = MacJumpIndex(tasks: [], needs: [], projects: [])
33 @ObservationIgnored private(set) var indexGeneration = 0
34 var index: MacJumpIndex {
35 let source = IndexSource(tasks: session.tasks, needs: session.needsYou ?? [], projects: session.projectCatalog.items)
36 if source != indexSource {
37 cachedIndex = MacJumpIndex(tasks: source.tasks, needs: source.needs, projects: source.projects)
38 indexSource = source; indexGeneration += 1
39 }
40 return cachedIndex
41 }
42 func open(_ destination: Destination) {
43 go(.pane(destination.pane))
44 focusCard = destination.cardID
45 if destination.focusComposer { composerFocus += 1 }
46 if let project = destination.projectID { go(.project(project)) }
47 if let task = destination.taskID { go(.task(task)) }
48 if let action = destination.macAction, let context {
49 let navigation = navigationRevision
50 Task { await performRoutedAction(action, context: context, navigation: navigation) }
51 }
52 }
53 func performMenuAction(_ action: MacTaskAction) async {
54 guard let context, !palette, context.actions.contains(action) else { return }
55 inspector = true
56 await context.perform(action)
57 }
58 func go(_ destination: MacDestination, record: Bool = true) {
59 navigationRevision += 1
60 palette = false; pendingAction = nil
61 if standalone {
62 switch destination {
63 case .pane(let pane): app.destination = Destination(pane: pane)
64 case .task(let id): app.destination = Destination(pane: .tasks, taskID: id)
65 case .project(let id): app.destination = Destination(pane: .projects, projectID: id)
66 }
67 mainRequest += 1
68 return
69 }
70 if record { history.go(destination) }
71 switch destination {
72 case .pane(let value): pane = value
73 case .project(let id): pane = .projects; projectID = id
74 case .task(let id):
75 pane = history.pane
76 if context?.taskID != id { context = MacTaskContext(session: session, taskID: id) }
77 inspector = true
78 }
79 }
80 func back() { if let destination = history.back() { go(destination, record: false) } }
81 func forward() { if let destination = history.forward() { go(destination, record: false) } }
82 func newMessage() {
83 go(.pane(.conversation)); composerFocus += 1
84 if standalone { app.destination = Destination(pane: .conversation, focusComposer: true) }
85 }
86 static func newMessage(app: EquipoAppModel, focused: MacWorkspace?) {
87 if let focused { focused.newMessage() }
88 else { app.destination = Destination(pane: .conversation, focusComposer: true) }
89 }
90 /// A task window keeps its identity. Actions for another highlighted task
91 /// are presented in the main window, bound to that task and a fresh read.
92 func queuePaletteAction(_ action: MacTaskAction, context: MacTaskContext) -> Bool {
93 guard context.actions.contains(action) else { return false }
94 if standalone && self.context?.taskID != context.taskID {
95 app.destination = Destination(pane: .tasks, taskID: context.taskID, macAction: action)
96 return true
97 }
98 self.context = context; inspector = true
99 pendingAction = QueuedAction(action: action, context: context, navigation: navigationRevision)
100 return false
101 }
102 func performRoutedAction(_ action: MacTaskAction, context: MacTaskContext, navigation: Int? = nil) async {
103 let expected = navigation ?? navigationRevision
104 await context.load()
105 guard self.context === context, expected == navigationRevision else { return }
106 await context.perform(action)
107 }
108 func refresh() async { if let context { await context.refresh() } else { await session.refresh() } }
109}
110
111struct MacTaskWindowID: Codable, Hashable {
112 let taskID: String
113 let origin: String
114 let account: String
115 @MainActor init(taskID: String, session: EquipoSession) {
116 self.taskID = taskID; origin = session.origin.absoluteString
117 // Local lifetime identity is deliberately not a credential. Restored
118 // windows after a restart close; they cannot follow an account switch.
119 account = session.presentationID.uuidString
120 }
121 @MainActor func matches(_ session: EquipoSession) -> Bool {
122 origin == session.origin.absoluteString && account == session.presentationID.uuidString && session.lifetime.isActive
123 }
124}
125