Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: App/Next/Commands.swift3// Description: The hooks between the menu bar and the feature views: focused values a Research or Quant Lab view publishes so the Research and Lab menus act on the frontmost window, the notifications posted when no view has published one, and the menus themselves.4import SwiftUI5import VQuantCore67// MARK: hooks a feature view publishes89/// What the frontmost Research view lets the menu bar do. A view publishes it with10/// `.focusedSceneValue(\.researchActions, ResearchActions(newStudy: …, attachDataset: …, stop: …))`;11/// a closure left nil disables its menu item (`stop` nil = nothing is running in this window).12struct ResearchActions {13 /// Research ▸ New Research Study (⇧⌘R): a new conversation in the autonomous research mode.14 var newStudy: (() -> Void)?15 /// Research ▸ Attach Dataset… (⇧⌘A): the file picker of the composer.16 var attachDataset: (() -> Void)?17 /// Research ▸ Stop Analysis (⌘.): stops what this window is running.18 var stop: (() -> Void)?19 /// Research ▸ Show/Hide Inspector (⌥⌘I): the cockpit's inspector of this window.20 var toggleInspector: (() -> Void)?21}2223/// What the frontmost Quant Lab view lets the menu bar do (`.focusedSceneValue(\.labActions, …)`).24struct LabActions {25 /// Lab ▸ Open Catalogue (⇧⌘L): back to the list of engines.26 var openCatalogue: (() -> Void)?27 /// Lab ▸ Run Again (⌥⌘R): resubmits the computation shown, with the same parameters. nil = nothing to run again.28 var runAgain: (() -> Void)?29}3031struct ResearchActionsKey: FocusedValueKey { typealias Value = ResearchActions }32struct LabActionsKey: FocusedValueKey { typealias Value = LabActions }33extension FocusedValues {34 var researchActions: ResearchActions? { get { self[ResearchActionsKey.self] } set { self[ResearchActionsKey.self] = newValue } }35 var labActions: LabActions? { get { self[LabActionsKey.self] } set { self[LabActionsKey.self] = newValue } }36}3738/// Posted (on the main thread, `object` = the frontmost window's `WindowModel`) when a menu command is chosen and the39/// frontmost view has NOT published the matching focused action — after the menu's own fallback has navigated to the40/// section. A view that prefers notifications observes these with `.onReceive` and checks `object === model`.41extension Notification.Name {42 static let vqNewResearchStudy = Notification.Name("ai.spboucher.vquant.research.newStudy")43 static let vqAttachDataset = Notification.Name("ai.spboucher.vquant.research.attachDataset")44 static let vqStopAnalysis = Notification.Name("ai.spboucher.vquant.research.stop")45 static let vqLabOpenCatalogue = Notification.Name("ai.spboucher.vquant.lab.openCatalogue")46 static let vqLabRunAgain = Notification.Name("ai.spboucher.vquant.lab.runAgain")47}4849extension View {50 /// The shortcut of the table (`AppShortcuts`), so menus and the collision test read the same source.51 func keyboardShortcut(_ s: AppShortcut) -> some View {52 var m: EventModifiers = []53 if s.modifiers.contains(.command) { m.insert(.command) }54 if s.modifiers.contains(.shift) { m.insert(.shift) }55 if s.modifiers.contains(.option) { m.insert(.option) }56 if s.modifiers.contains(.control) { m.insert(.control) }57 let key: KeyEquivalent = s.key == "return" ? .return : KeyEquivalent(Character(s.key))58 return keyboardShortcut(key, modifiers: m)59 }60}6162// MARK: Research and Lab menus6364struct ResearchLabCommands: Commands {65 let app: AppEnvironment66 @FocusedValue(\.windowModel) private var model67 @FocusedValue(\.researchActions) private var research68 @FocusedValue(\.labActions) private var lab6970 /// The analysis this window follows, when the view has not published its own `stop`.71 private var activeRun: RunController? { model.flatMap { app.research.run(for: $0.sessionId) ?? app.research.pendingRun($0.draftKey) }.flatMap { $0.isActive ? $0 : nil } }7273 var body: some Commands {74 CommandMenu("Research") {75 Button("New Conversation") { model?.openConversation(nil) }.keyboardShortcut(AppShortcuts.newConversation).disabled(model == nil)76 Button("New Research Study") {77 if let a = research?.newStudy { a() }78 else if let model {79 // no view has claimed the command: open a new conversation proposed in the autonomous research mode80 model.ask(AskDraft(text: "", mode: .research))81 NotificationCenter.default.post(name: .vqNewResearchStudy, object: model)82 }83 }84 .keyboardShortcut(AppShortcuts.newStudy).disabled(model == nil)85 Divider()86 Button("Attach Dataset…") {87 if let a = research?.attachDataset { a() }88 else if let model { model.show(.research); NotificationCenter.default.post(name: .vqAttachDataset, object: model) }89 }90 .keyboardShortcut(AppShortcuts.attachDataset).disabled(model == nil)91 Button("Stop Analysis") {92 if let a = research?.stop { a() }93 else if let run = activeRun { Task { await app.research.stop(run) }; NotificationCenter.default.post(name: .vqStopAnalysis, object: model) }94 }95 .keyboardShortcut(AppShortcuts.stopAnalysis).disabled(research?.stop == nil && activeRun == nil)96 }97 CommandMenu("Lab") {98 Button("Open Catalogue") {99 if let a = lab?.openCatalogue { a() }100 else if let model {101 model.quantJobId = nil; model.quantTool = nil; model.show(.quant)102 NotificationCenter.default.post(name: .vqLabOpenCatalogue, object: model)103 }104 }105 .keyboardShortcut(AppShortcuts.labCatalogue).disabled(model == nil)106 Button("Run Again") {107 if let a = lab?.runAgain { a() } else { NotificationCenter.default.post(name: .vqLabRunAgain, object: model) }108 }109 // without a published action the command is offered only where a computation is on screen110 .keyboardShortcut(AppShortcuts.labRunAgain).disabled(lab?.runAgain == nil && !(model?.section == .quant && model?.quantJobId != nil))111 }112 }113}114