Files
pipelines/app/lib/features/graph/widgets/run_graph_canvas.dart
T

402 lines
13 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart' show listEquals;
import 'package:flutter/material.dart';
import '../../../ui/adaptive/host.dart';
import '../domain/graph_definition.dart';
import '../domain/run_graph_state.dart';
import '../layout/layout_models.dart';
import '../painting/graph_edge_layer.dart';
import '../routing/route_models.dart';
import 'graph_canvas_style.dart';
import 'graph_stage_node.dart';
import 'measured_child.dart';
/// The run graph's canvas primitives: split route painting and measurement of
/// the exact status-bearing nodes the run renderer places.
///
/// This keeps the landed `GraphEdgePainter` geometry (paths and arrowheads) and
/// adds only what run evidence needs: two route classes on one passive layer.
/// No node is invented, no route is re-routed and no style is assumed here.
/// A passive canvas that draws taken routes as solid, arrowed strokes and
/// untaken routes as dashed, arrowless strokes behind them.
///
/// The caller supplies the already-routed [executedRoutes] and
/// [untakenRoutes], the graph-space [canvasSize] and its native theme colours.
/// The layer never takes pointer input, so nodes and labels keep their gestures.
class RunGraphEdgeLayer extends StatelessWidget {
const RunGraphEdgeLayer({
super.key,
required this.executedRoutes,
required this.untakenRoutes,
required this.canvasSize,
required this.executedColor,
required this.untakenColor,
required this.strokeWidth,
});
final List<EdgeRoute> executedRoutes;
final List<EdgeRoute> untakenRoutes;
final Size canvasSize;
final Color executedColor;
final Color untakenColor;
final double strokeWidth;
@override
Widget build(BuildContext context) => CustomPaint(
size: canvasSize,
painter: RunGraphEdgePainter(
executedRoutes: executedRoutes,
untakenRoutes: untakenRoutes,
executedColor: executedColor,
untakenColor: untakenColor,
strokeWidth: strokeWidth,
),
);
}
/// Paints a [RunGraphEdgeLayer].
///
/// Executed routes reuse the landed [GraphEdgePainter] path and arrow geometry;
/// untaken routes are the same polyline broken into native-sized dashes and
/// never receive an arrowhead. Public so the compositor and its tests share one
/// decision about which class of route gets an arrow.
class RunGraphEdgePainter extends CustomPainter {
RunGraphEdgePainter({
required this.executedRoutes,
required this.untakenRoutes,
required this.executedColor,
required this.untakenColor,
required this.strokeWidth,
}) : assert(strokeWidth > 0),
_executed = GraphEdgePainter(
routes: executedRoutes,
color: executedColor,
strokeWidth: strokeWidth,
),
_executedLine = Paint()
..color = executedColor
..strokeWidth = strokeWidth
..style = PaintingStyle.stroke
..strokeJoin = StrokeJoin.round,
_untakenLine = Paint()
..color = untakenColor
..strokeWidth = strokeWidth
..style = PaintingStyle.stroke
..strokeJoin = StrokeJoin.round,
_arrow = Paint()
..color = executedColor
..style = PaintingStyle.fill;
final List<EdgeRoute> executedRoutes;
final List<EdgeRoute> untakenRoutes;
final Color executedColor;
final Color untakenColor;
final double strokeWidth;
final GraphEdgePainter _executed;
final Paint _executedLine;
final Paint _untakenLine;
final Paint _arrow;
/// Dash and gap lengths in graph space; a dash never becomes a route style.
static const double dashLength = 12;
static const double dashGap = 8;
@override
void paint(Canvas canvas, Size size) {
if (executedRoutes.isEmpty && untakenRoutes.isEmpty) return;
canvas.save();
canvas.clipRect(Offset.zero & size);
for (final route in untakenRoutes) {
if (route.points.length < 2) continue;
canvas.drawPath(dashedPath(route), _untakenLine);
}
for (final route in executedRoutes) {
if (route.points.length < 2) continue;
canvas.drawPath(_executed.linePath(route), _executedLine);
}
for (final route in executedRoutes) {
if (route.points.length < 2) continue;
canvas.drawPath(_executed.arrowPath(route), _arrow);
}
canvas.restore();
}
/// The route's polyline split into [dashLength] segments separated by
/// [dashGap], following every orthogonal bend without smoothing.
Path dashedPath(EdgeRoute route) {
final source = _executed.linePath(route);
final dashed = Path();
for (final metric in source.computeMetrics()) {
var start = 0.0;
while (start < metric.length) {
final end = start + dashLength < metric.length
? start + dashLength
: metric.length;
dashed.addPath(metric.extractPath(start, end), Offset.zero);
start = end + dashGap;
}
}
return dashed;
}
/// Passive like the landed edge layer: every point misses.
@override
bool hitTest(Offset position) => false;
@override
bool shouldRepaint(RunGraphEdgePainter oldDelegate) =>
oldDelegate.executedColor != executedColor ||
oldDelegate.untakenColor != untakenColor ||
oldDelegate.strokeWidth != strokeWidth ||
!listEquals(oldDelegate.executedRoutes, executedRoutes) ||
!listEquals(oldDelegate.untakenRoutes, untakenRoutes);
}
/// 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.
class RunGraphCanvas extends StatelessWidget {
const RunGraphCanvas({
super.key,
required this.nodes,
required this.layout,
required this.routing,
required this.state,
required this.style,
required this.recordedStatuses,
required this.onStageSelected,
this.selectedStage,
});
final List<GraphNode> nodes;
final GraphLayout layout;
final GraphRouting routing;
final RunGraphState state;
final GraphCanvasStyle style;
final Map<String, String?> recordedStatuses;
final ValueChanged<String> onStageSelected;
final String? selectedStage;
@override
Widget build(BuildContext context) {
final nodesByKey = <String, GraphNode>{
for (final node in nodes) node.key: node,
};
final executed = <EdgeRoute>[];
final untaken = <EdgeRoute>[];
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 cupertino = usesCupertinoHost(context);
final executedColor = cupertino
? CupertinoTheme.of(context).primaryColor
: Theme.of(context).colorScheme.primary;
return Stack(
clipBehavior: Clip.none,
children: [
Positioned.fill(
child: RunGraphEdgeLayer(
executedRoutes: executed,
untakenRoutes: untaken,
canvasSize: size,
executedColor: executedColor,
untakenColor: style.edgeColor,
strokeWidth: style.edgeWidth,
),
),
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,
active: box.key == activeStage,
opensVisit:
!nodesByKey[box.key]!.terminal &&
state.isStageVisited(box.key),
onSelected: onStageSelected,
),
),
// A run labels only the routes that actually happened.
for (final label in routing.labels)
if (executedIds.contains(label.edgeId))
Positioned(
left: label.x,
top: label.y,
child: _takenLabel(
routing.routeFor(label.edgeId)!,
executedColor,
),
),
],
);
}
}
/// A taken route's label led by its place in the run (`2`, or `2, 5` for a
/// loop), so the path reads in order.
extension on RunGraphCanvas {
Widget _takenLabel(EdgeRoute route, Color color) {
final order = state.routeOrder(route.source, route.outcome, route.target);
final text = Text(route.label, style: style.labelStyle);
if (order.isEmpty) return text;
return Row(
mainAxisSize: MainAxisSize.min,
children: [
Container(
key: ValueKey('run-route-order:${route.id}'),
padding: const EdgeInsets.symmetric(horizontal: 5, vertical: 1),
decoration: BoxDecoration(
color: color,
borderRadius: BorderRadius.circular(8),
),
child: Text(
order.join(', '),
style: const TextStyle(
color: Color(0xFFFFFFFF),
fontSize: 10,
fontWeight: FontWeight.w700,
),
),
),
const SizedBox(width: 4),
text,
],
);
}
}
/// One placed run node. A visited stage is the native control with its recorded
/// status; an unvisited node and a terminal are labels with no tap target or
/// button semantics, because neither owns a recorded visit to open. [active]
/// marks the run's recorded current stage while it executes; it only adds the
/// live halo, never a visit or a control.
class RunGraphNode extends StatelessWidget {
const RunGraphNode({
super.key,
required this.node,
required this.recordedStatus,
required this.opensVisit,
required this.onSelected,
this.selected = false,
this.active = false,
});
final GraphNode node;
final String? recordedStatus;
final bool opensVisit;
final bool selected;
final bool active;
final ValueChanged<String> onSelected;
@override
Widget build(BuildContext context) {
final status = recordedStatus;
if (opensVisit) {
return GraphStageNode(
node: node,
recordedStatus: status,
selected: selected,
active: active,
onSelected: onSelected,
);
}
return Semantics(
container: true,
label: status == null ? node.label : '${node.label}\n$status',
child: ExcludeSemantics(
child: IgnorePointer(
child: GraphStageNode(
node: node,
recordedStatus: status,
active: active,
onSelected: (_) {},
),
),
),
);
}
}
/// Measures the status-bearing run nodes and every route label offstage.
///
/// A visited node is taller than the same node without its recorded status, so
/// the measurement must use the exact widget the canvas places. Sizes are
/// reported after layout and callers schedule the placement frame.
class RunGraphMeasurements extends StatelessWidget {
const RunGraphMeasurements({
super.key,
required this.nodes,
required this.edges,
required this.maxNodeWidth,
required this.labelStyle,
required this.recordedStatuses,
required this.onNodeSize,
required this.onLabelSize,
});
final List<GraphNode> nodes;
final List<GraphEdge> edges;
final double maxNodeWidth;
final TextStyle labelStyle;
final Map<String, String?> recordedStatuses;
final void Function(String, Size) onNodeSize;
final void Function(String, Size) onLabelSize;
@override
Widget build(BuildContext context) => Offstage(
child: OverflowBox(
alignment: Alignment.topLeft,
minWidth: 0,
maxWidth: double.infinity,
minHeight: 0,
maxHeight: double.infinity,
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
for (final node in nodes)
MeasuredChild(
key: ValueKey(
'run-node:${node.key}:${node.label}:'
'${recordedStatuses[node.key] ?? ''}',
),
onSize: (size) => onNodeSize(node.key, size),
child: ConstrainedBox(
constraints: BoxConstraints(maxWidth: maxNodeWidth),
child: GraphStageNode(
node: node,
recordedStatus: recordedStatuses[node.key],
onSelected: (_) {},
),
),
),
for (final edge in edges)
MeasuredChild(
key: ValueKey('run-label:${edge.id}'),
onSize: (size) => onLabelSize(edge.label, size),
child: Text(edge.label, style: labelStyle),
),
],
),
),
);
}