import 'package:flutter/material.dart'; import '../../../core/api_gateway.dart'; import '../domain/graph_definition.dart'; import '../domain/stage_summary.dart'; import '../layout/layout_models.dart'; import '../painting/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_node.dart'; import 'graph_stage_popover.dart'; import 'stage_popover_card.dart'; /// The measured definition canvas at any viewport size and selection. /// /// Used by both the inline [GraphViewport] and the expanded presentation: the /// passive edge layer paints behind the native nodes and outcome labels. /// Tapping a node opens its stage popover (the declaration at a glance); the /// popover's "Open definition" reports the node through [onSelected]. class DefinitionGraphCanvas extends StatelessWidget { const DefinitionGraphCanvas({ super.key, required this.graph, required this.nodes, required this.layout, required this.routing, required this.style, required this.selectedStage, required this.onSelected, }); /// The definition the nodes came from, for each stage's declaration. final JsonMap graph; final List nodes; final GraphLayout layout; final GraphRouting routing; final GraphCanvasStyle style; final String? selectedStage; final ValueChanged onSelected; @override Widget build(BuildContext context) { final nodesByKey = { for (final node in nodes) node.key: node, }; final size = Size(routing.width, routing.height); return GraphStagePopover( nodeRects: { for (final box in layout.nodes) box.key: Rect.fromLTWH(box.x, box.y, box.width, box.height), }, cardBuilder: (context, stage, close) => StagePopoverCard( label: nodesByKey[stage]?.label ?? stage, summary: stageSummaryFor(graph, stage), onClose: close, onOpenDefinition: () { close(); onSelected(stage); }, ), builder: (context, open, toggle) => GraphEdgeHover( layout: layout, routing: routing, describe: describeGraphEdge, builder: (context, highlighted, select) => _canvas( context, nodesByKey, size, highlighted, select, open, toggle, ), ), ); } Widget _canvas( BuildContext context, Map nodesByKey, Size size, int? highlighted, ValueChanged select, String? open, ValueChanged toggle, ) { return Stack( clipBehavior: Clip.none, children: [ // The passive layer comes first so every edge paints behind the native // nodes and labels without taking their input. Positioned.fill( child: GraphEdgeLayer( routes: routing.routes, canvasSize: size, color: 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, ), ), for (final box in layout.nodes) Positioned( left: box.x, top: box.y, width: box.width, height: box.height, child: GraphStageNode( node: nodesByKey[box.key]!, selected: box.key == selectedStage || box.key == open, onSelected: toggle, ), ), for (final label in routing.labels) Positioned( left: label.x, top: label.y, child: graphEdgeLabel( context: context, highlighted: label.edgeId == highlighted, description: describeGraphEdge(routing.routeFor(label.edgeId)!), onSelect: () => select(label.edgeId), child: Text( routing.routeFor(label.edgeId)!.label, style: graphEdgeLabelStyle( context, style.labelStyle, highlighted: label.edgeId == highlighted, ), ), ), ), ], ); } }