import 'package:flutter/material.dart'; import '../../../core/api_gateway.dart'; import '../domain/graph_definition.dart'; import '../domain/run_graph_state.dart'; import '../domain/stage_run_facts.dart'; import '../domain/stage_summary.dart'; import '../layout/layout_models.dart'; import '../painting/run_graph_edge_layer.dart'; import '../painting/stage_edges_highlight.dart'; import '../routing/route_models.dart'; import 'graph_canvas_style.dart'; import 'graph_edge_hover.dart'; import 'graph_stage_popover.dart'; import 'run_graph_parts.dart'; import 'stage_popover_card.dart'; export '../painting/run_graph_edge_layer.dart'; export 'run_graph_parts.dart'; /// The run canvas: split route classes behind status-bearing native nodes, with /// outcome labels only for routes the run actually took. /// /// [layout] and [routing] are the caller's measured geometry. The canvas never /// changes them and never assumes a node size; the caller supplies the pan/zoom /// viewport around this content. /// /// Tapping a stage opens its popover: the declaration with what the run /// recorded for it. "Open visit" reports a visited stage through /// [onStageSelected]; "Open definition" reports it through [onOpenDefinition]. class RunGraphCanvas extends StatelessWidget { const RunGraphCanvas({ super.key, required this.graph, required this.run, required this.onOpenDefinition, required this.nodes, required this.layout, required this.routing, required this.state, required this.style, required this.recordedStatuses, required this.onStageSelected, this.selectedStage, }); /// The run's pinned definition and the run detail, for the popover. final JsonMap graph; final JsonMap run; final ValueChanged onOpenDefinition; final List nodes; final GraphLayout layout; final GraphRouting routing; final RunGraphState state; final GraphCanvasStyle style; final Map recordedStatuses; final ValueChanged onStageSelected; final String? selectedStage; @override Widget build(BuildContext context) { final nodesByKey = { for (final node in nodes) node.key: node, }; final executed = []; final untaken = []; for (final route in routing.routes) { if (state.isRouteTaken(route.source, route.outcome, route.target)) { executed.add(route); } else { untaken.add(route); } } final executedIds = {for (final route in executed) route.id}; final size = Size(routing.width, routing.height); final activeStage = state.activeStage; final executedColor = Theme.of(context).colorScheme.primary; String describe(EdgeRoute route) => describeGraphEdge( route, taken: state.routeOrder(route.source, route.outcome, route.target).length, ); return GraphStagePopover( nodeRects: { for (final box in layout.nodes) if (!(nodesByKey[box.key]?.terminal ?? true)) box.key: Rect.fromLTWH(box.x, box.y, box.width, box.height), }, cardBuilder: (context, stage, close) => _card(nodesByKey[stage]?.label ?? stage, stage, close), builder: (context, open, toggle) => _hover( context, nodesByKey, executed, untaken, executedIds, size, activeStage, executedColor, describe, open, toggle, ), ); } Widget _card(String label, String stage, VoidCallback close) { final visited = state.isStageVisited(stage); return StagePopoverCard( label: label, summary: stageSummaryFor(graph, stage), facts: stageRunFacts(run, stage), onClose: close, onOpenDefinition: () { close(); onOpenDefinition(stage); }, onOpenVisit: !visited ? null : () { close(); onStageSelected(stage); }, ); } Widget _hover( BuildContext context, Map nodesByKey, List executed, List untaken, Set executedIds, Size size, String? activeStage, Color executedColor, String Function(EdgeRoute) describe, String? open, ValueChanged toggle, ) { return GraphEdgeHover( layout: layout, routing: routing, describe: describe, builder: (context, highlighted, select) => Stack( clipBehavior: Clip.none, children: [ Positioned.fill( child: RunGraphEdgeLayer( executedRoutes: executed, untakenRoutes: untaken, canvasSize: size, executedColor: executedColor, untakenColor: style.edgeColor, strokeWidth: style.edgeWidth, ), ), Positioned.fill( child: graphStageEdgesHighlight( context: context, routing: routing, stage: open, edgeWidth: style.edgeWidth, ), ), Positioned.fill( child: graphEdgeHighlight( context: context, layout: layout, routing: routing, highlighted: highlighted, edgeWidth: style.edgeWidth, ), ), ..._placed( context, nodesByKey, executedIds, executedColor, activeStage, highlighted, select, describe, open, toggle, ), ], ), ); } List _placed( BuildContext context, Map nodesByKey, Set executedIds, Color executedColor, String? activeStage, int? highlighted, ValueChanged select, String Function(EdgeRoute) describe, String? open, ValueChanged toggle, ) { return [ for (final box in layout.nodes) Positioned( left: box.x, top: box.y, width: box.width, height: box.height, child: RunGraphNode( node: nodesByKey[box.key]!, recordedStatus: recordedStatuses[box.key], selected: box.key == selectedStage || box.key == open, active: box.key == activeStage, interactive: !nodesByKey[box.key]!.terminal, onSelected: toggle, ), ), // A run labels only the routes that actually happened, and the route // being highlighted so its outcome reads where it is drawn. for (final label in routing.labels) if (executedIds.contains(label.edgeId) || label.edgeId == highlighted) Positioned( left: label.x, top: label.y, child: graphEdgeLabel( context: context, highlighted: label.edgeId == highlighted, description: describe(routing.routeFor(label.edgeId)!), onSelect: () => select(label.edgeId), child: _takenLabel( context, routing.routeFor(label.edgeId)!, executedColor, highlighted: label.edgeId == highlighted, ), ), ), ]; } Widget _takenLabel( BuildContext context, EdgeRoute route, Color color, { required bool highlighted, }) => RunRouteLabel( edgeId: route.id, label: route.label, order: state.routeOrder(route.source, route.outcome, route.target), style: graphEdgeLabelStyle( context, style.labelStyle, highlighted: highlighted, ), color: color, ); }