Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Features/Quant/LabJobsView.swift3// Description: The list of Quant Lab computations: status, progress of running jobs, duration, engine version; open, compare, run again, cancel, delete. Rows select by their own action (macOS 27: `List(selection:)` does not).4import SwiftUI5import VQuantCore6import VQuantKit7import VQuantUI89private typealias P = VQTokens.Palette10private typealias S = VQTokens.Space11private typealias T = VQTokens.Typography1213struct LabJobList: View {14 let app: AppEnvironment15 @Bindable var model: WindowModel16 @Bindable var state: LabWindowState17 /// Only the jobs of this engine (nil = all).18 var toolFilter: String?1920 private var lab: LabStore { app.lab }21 private var jobs: [VQQuantJob] { toolFilter.map { t in lab.jobs.filter { $0.tool == t } } ?? lab.jobs }2223 var body: some View {24 VStack(spacing: 0) {25 if lab.fromCache { banner("icloud.slash", "Offline — the list this Mac last received. Finished results opened before stay readable.") }26 else if let e = lab.error { banner("exclamationmark.triangle", e) }27 if jobs.isEmpty {28 if lab.jobsLoaded {29 Color.clear.frame(maxWidth: .infinity, maxHeight: .infinity).overlay { VQEmptyState(systemImage: "function", title: "No computation yet", message: "Results are kept on the server: reopen them here from any of your devices.") }30 } else { VQSkeletonRows(rows: 5, columns: 2).padding(S.s4).frame(maxHeight: .infinity, alignment: .top) }31 } else {32 ScrollView {33 LazyVStack(spacing: 0) { ForEach(jobs) { j in row(j) } }.padding(.vertical, S.s1)34 }35 }36 }37 }3839 private func banner(_ symbol: String, _ text: String) -> some View {40 Label(text, systemImage: symbol).vqFont(T.caption).foregroundStyle(P.warn).fixedSize(horizontal: false, vertical: true)41 .padding(.horizontal, S.s4).padding(.vertical, S.s2).frame(maxWidth: .infinity, alignment: .leading)42 .overlay(alignment: .bottom) { Rectangle().fill(P.line).frame(height: 1) }43 }4445 private func row(_ j: VQQuantJob) -> some View {46 let selected = model.quantJobId == j.id47 let comparing = state.compareWith == j.id48 return Button { model.quantJobId = j.id } label: {49 HStack(alignment: .top, spacing: S.s2) {50 LabStatusGlyph(job: j, app: app).padding(.top, 2)51 VStack(alignment: .leading, spacing: 2) {52 HStack(alignment: .firstTextBaseline, spacing: S.s1) {53 Text([lab.title(j.tool), j.subject].filter { !$0.isEmpty }.joined(separator: " · ")).vqFont(T.bodySm).fontWeight(selected ? .semibold : .regular).foregroundStyle(P.text1).lineLimit(1)54 Spacer(minLength: S.s1)55 if comparing { Text("B").font(.system(size: 9.5, weight: .bold, design: .monospaced)).foregroundStyle(P.info).padding(.horizontal, 4).overlay { RoundedRectangle(cornerRadius: 3).strokeBorder(P.info, lineWidth: 1) }.help("Compared with the open result") }56 TimelineView(.periodic(from: .now, by: 1)) { t in57 Text(LabFormat.duration(j.duration(now: t.date))).vqFont(T.numSm).foregroundStyle(j.isRunning ? P.accentText : P.text3)58 }59 }60 Text(caption(j)).vqFont(T.caption).foregroundStyle(P.text3).lineLimit(1)61 if j.isRunning, let p = j.progress {62 LabProgressBar(value: p, height: 3).padding(.top, 3)63 }64 }65 }66 .padding(.horizontal, S.s3).padding(.vertical, 7)67 .frame(maxWidth: .infinity, alignment: .leading)68 .background(RoundedRectangle(cornerRadius: 7, style: .continuous).fill(selected ? P.ink3 : Color.clear))69 .overlay(alignment: .leading) { if selected { RoundedRectangle(cornerRadius: 1).fill(P.accent).frame(width: 2).padding(.vertical, 8) } }70 .contentShape(Rectangle())71 }72 .buttonStyle(.plain)73 .padding(.horizontal, S.s1)74 .accessibilityIdentifier("quantjob.\(j.id)")75 .accessibilityLabel("\(lab.title(j.tool)) \(j.subject), \(LabStatusGlyph.word(j))")76 .accessibilityAddTraits(selected ? [.isSelected, .isButton] : .isButton)77 .contextMenu {78 Button("Open") { model.quantJobId = j.id }79 if let open = model.quantJobId, open != j.id, let a = lab.job(open), a.tool == j.tool, a.status == .done, j.status == .done {80 Button("Compare With the Open Result") { state.compareWith = j.id }81 }82 Button("Edit These Parameters and Run Again") { LabJobActions.reuse(j, app: app, model: model, state: state) }.disabled(lab.tool(j.tool) == nil)83 Divider()84 if j.isRunning { Button("Cancel") { Task { await lab.cancel(j.id) } } }85 Button("Delete", role: .destructive) { Task { await lab.delete(j.id); if model.quantJobId == j.id { model.quantJobId = nil; if model.quantTool == nil { model.quantTool = j.tool } } } }.disabled(j.isRunning)86 }87 }8889 private func caption(_ j: VQQuantJob) -> String {90 var parts: [String] = []91 if let v = j.variant { parts.append(v) }92 if j.status != .done { parts.append(LabStatusGlyph.word(j)) }93 parts.append(LabFormat.started(j.startedAt))94 if let v = j.engineVersion { parts.append("v\(v)") }95 return parts.joined(separator: " · ")96 }97}9899/// Small formatters shared by the Quant Lab views.100enum LabFormat {101 static func duration(_ s: TimeInterval?) -> String {102 guard let s else { return "" }103 if s < 10 { return String(format: "%.1f s", s) }104 if s < 60 { return "\(Int(s.rounded())) s" }105 return "\(Int(s) / 60) min \(String(format: "%02d", Int(s) % 60)) s"106 }107 /// "14:02" today, "Nov 19, 14:02" otherwise.108 static func started(_ d: Date?) -> String {109 guard let d else { return "—" }110 return Calendar.current.isDateInToday(d) ? d.formatted(date: .omitted, time: .shortened) : d.formatted(.dateTime.month(.abbreviated).day().hour().minute())111 }112}113114@MainActor enum LabJobActions {115 /// Puts a job's recorded parameters back in its tool's form.116 static func reuse(_ j: VQQuantJob, app: AppEnvironment, model: WindowModel, state: LabWindowState) {117 guard let tool = app.lab.tool(j.tool) else { return }118 var values: VQJSONObject = [:]119 for p in tool.params { if let v = j.params[p.name] { values[p.name] = p.type == .json ? .string(v.text(pretty: true)) : v } }120 app.lab.forms[tool.id] = VQLabForm(tool: tool, values: values)121 model.quantTool = tool.id122 state.leftTab = .parameters123 }124}125