Files
pipelines/app/lib/features/graph/widgets/definition_graph_view.dart
T
alan 413c923e77 Graph: stage popover with the prompt, tools, configs and run facts
Tapping or pressing Enter on a stage in the pipeline or run graph (inline
and full screen) opens a card anchored to the node: type, config chips,
tools, collapsed permissions, the prompt clamped with Show all (commands,
gate or operator instructions by type), limits and routes, and for runs
the status, visits, time, verdict, handoff and failure. Open definition
keeps the old tap target; Open visit the old run tap. It follows pan and
zoom, closes on outside tap, Escape or the same node, and opens as a
bottom sheet on a narrow viewport.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 23:19:18 +00:00

230 lines
7.2 KiB
Dart

import 'package:flutter/material.dart';
import '../../../core/api_gateway.dart';
import '../domain/graph_definition.dart';
import '../layout/layout_models.dart';
import '../layout/linear_graph_layout.dart';
import '../routing/route_models.dart';
import '../viewport/graph_expanded_view.dart';
import '../viewport/graph_viewport.dart';
import '../viewport/graph_viewport_controller.dart';
import 'definition_graph_canvas.dart';
import 'graph_canvas_style.dart';
import 'graph_measurements.dart';
/// The definition's own empty-state wording (web `PIPELINE_GRAPH_EMPTY`).
const String definitionGraphEmpty = 'This pipeline has no stages.';
/// The expanded definition presentation's title.
const String definitionGraphExpandedTitle = 'Pipeline graph';
/// One pipeline definition as a readable native graph: measured stage nodes,
/// routed passive edges behind them and native outcome labels on their boxes.
///
/// [selectedStage] is a node key (including `#success`/`#failure`) and
/// [onSelected] reports the exact key activated. Nothing reads run evidence:
/// nodes show no status and terminals stay plain, so no visit is implied. The
/// view fills bounded caller constraints with a pan/zoom [GraphViewport] and can
/// present the same canvas full screen without losing the selection.
class DefinitionGraphView extends StatefulWidget {
const DefinitionGraphView({
super.key,
required this.graph,
required this.onSelected,
this.selectedStage,
});
final JsonMap graph;
final String? selectedStage;
final ValueChanged<String> onSelected;
@override
State<DefinitionGraphView> createState() => _DefinitionGraphViewState();
}
class _DefinitionGraphViewState extends State<DefinitionGraphView> {
final Map<String, Size> _nodeSizes = {};
final Map<String, Size> _labelSizes = {};
final GraphViewportController _viewport = GraphViewportController();
TextScaler? _scaler;
bool _rebuildScheduled = false;
@override
void dispose() {
_viewport.dispose();
super.dispose();
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
// A different text scale changes every measured extent; drop the old
// extents so a stale 1x layout is never rendered around 2x text.
final scaler = MediaQuery.textScalerOf(context);
if (scaler != _scaler) {
_scaler = scaler;
_nodeSizes.clear();
_labelSizes.clear();
}
}
/// Records one measured extent and schedules the frame that uses it.
void _record(Map<String, Size> sizes, String key, Size size) {
if (sizes[key] == size) return;
sizes[key] = size;
if (_rebuildScheduled) return;
_rebuildScheduled = true;
WidgetsBinding.instance.addPostFrameCallback((_) {
_rebuildScheduled = false;
if (mounted) setState(() {});
});
}
/// The landed layout and routing for the measured extents, or null until
/// every node and every edge label has a real measurement.
(GraphLayout, GraphRouting)? _geometry(
List<GraphNode> nodes,
List<GraphEdge> edges,
) {
final extents = <String, NodeExtent>{};
for (final node in nodes) {
final size = _nodeSizes[node.key];
if (size == null) return null;
extents[node.key] = NodeExtent(width: size.width, height: size.height);
}
final labelExtents = <int, LabelExtent>{};
for (final edge in edges) {
final size = _labelSizes[edge.label];
if (size == null) return null;
labelExtents[edge.id] = LabelExtent(
width: size.width,
height: size.height,
);
}
// The web's linear drawing: stages in one row, the ends below it.
final (layout, routing) = layoutLinearGraph(
nodes: nodes,
edges: edges,
extents: extents,
labelExtents: labelExtents,
);
return (layout, routing);
}
@override
Widget build(BuildContext context) {
final nodes = graphNodes(widget.graph);
final edges = graphEdges(widget.graph);
final style = GraphCanvasStyle.of(context);
return LayoutBuilder(
builder: (context, constraints) {
assert(
constraints.hasBoundedWidth && constraints.hasBoundedHeight,
'DefinitionGraphView needs a bounded viewport; constrain it, for '
'example with a SizedBox or an Expanded slot.',
);
final geometry = nodes.isEmpty ? null : _geometry(nodes, edges);
return Stack(
fit: StackFit.expand,
children: [
if (nodes.isEmpty)
_message(definitionGraphEmpty, style)
else if (geometry != null)
_canvas(nodes, geometry.$1, geometry.$2, style)
else
const SizedBox.shrink(),
if (nodes.isNotEmpty)
GraphMeasurements(
nodes: nodes,
edges: edges,
maxNodeWidth: constraints.maxWidth,
labelStyle: style.labelStyle,
selectedStage: widget.selectedStage,
onNodeSize: (key, size) => _record(_nodeSizes, key, size),
onLabelSize: (key, size) => _record(_labelSizes, key, size),
),
],
);
},
);
}
Widget _message(String message, GraphCanvasStyle style) {
return Padding(
padding: const EdgeInsets.all(24),
child: Align(
alignment: AlignmentDirectional.topStart,
child: Text(message, style: style.messageStyle),
),
);
}
Widget _canvas(
List<GraphNode> nodes,
GraphLayout layout,
GraphRouting routing,
GraphCanvasStyle style,
) {
final size = Size(routing.width, routing.height);
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expanded(
child: GraphViewport(
controller: _viewport,
contentSize: size,
onExpand: () => _openExpanded(nodes, layout, routing, size),
child: DefinitionGraphCanvas(
graph: widget.graph,
nodes: nodes,
layout: layout,
routing: routing,
style: style,
selectedStage: widget.selectedStage,
onSelected: widget.onSelected,
),
),
),
if (routing.skipped.isNotEmpty)
Flexible(
child: SingleChildScrollView(
child: _message(
[
for (final diagnostic in routing.skipped)
'${diagnostic.source} → ${diagnostic.outcome}: '
'${diagnostic.message}',
].join('\n'),
style,
),
),
),
],
);
}
/// Opens the same measured canvas full screen, keeping the selection.
void _openExpanded(
List<GraphNode> nodes,
GraphLayout layout,
GraphRouting routing,
Size size,
) {
openGraphExpanded(
context,
contentSize: size,
title: definitionGraphExpandedTitle,
selectedStage: widget.selectedStage,
onSelected: widget.onSelected,
canvasBuilder: (context, selected, onSelected) => DefinitionGraphCanvas(
graph: widget.graph,
nodes: nodes,
layout: layout,
routing: routing,
style: GraphCanvasStyle.of(context),
selectedStage: selected,
onSelected: onSelected,
),
);
}
}