feat(app): finish measured native graph canvas and clear labels from wires

This commit is contained in:
2026-10-21 21:06:57 +01:00
parent 7be4456fbf
commit bf378c94ca
11 changed files with 1123 additions and 2 deletions
@@ -16,6 +16,7 @@ EdgeLabelBox placeEdgeLabel({
required LabelExtent extent,
required List<GraphNodeBox> nodes,
required List<EdgeLabelBox> placed,
required List<EdgeRoute> routes,
required RoutingConfig config,
required double floorY,
}) {
@@ -29,7 +30,7 @@ EdgeLabelBox placeEdgeLabel({
width: width,
height: height,
);
if (_fits(box, nodes, placed, config)) return box;
if (_fits(box, nodes, placed, routes, config)) return box;
}
// The fallback shelf is below every route and every existing label, so it
@@ -50,7 +51,7 @@ EdgeLabelBox placeEdgeLabel({
width: width,
height: height,
);
if (_fits(box, nodes, placed, config)) return box;
if (_fits(box, nodes, placed, routes, config)) return box;
top += step;
}
// A zero-sized label cannot collide; this is unreachable for real extents.
@@ -67,9 +68,35 @@ bool _fits(
EdgeLabelBox box,
List<GraphNodeBox> nodes,
List<EdgeLabelBox> placed,
List<EdgeRoute> routes,
RoutingConfig config,
) {
if (box.x < 0 || box.y < 0) return false;
// A label clear of nodes can still sit across its self-loop's vertical leg
// or another edge. Keep its measured text rectangle clear of every wire.
final left = box.x - config.labelGap;
final right = box.right + config.labelGap;
final top = box.y - config.labelGap;
final bottom = box.bottom + config.labelGap;
for (final route in routes) {
for (var index = 1; index < route.points.length; index++) {
final a = route.points[index - 1], b = route.points[index];
if (a.x == b.x &&
a.x > left &&
a.x < right &&
math.max(a.y, b.y) > top &&
math.min(a.y, b.y) < bottom) {
return false;
}
if (a.y == b.y &&
a.y > top &&
a.y < bottom &&
math.max(a.x, b.x) > left &&
math.min(a.x, b.x) < right) {
return false;
}
}
}
for (final node in nodes) {
if (boxIntersectsNode(
x: box.x,
@@ -106,6 +106,7 @@ GraphRouting routeGraphEdges({
extent: labelExtents[routes[index].id]!,
nodes: layout.nodes,
placed: labels,
routes: routes,
config: config,
floorY: floorY,
),
@@ -0,0 +1,231 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../../core/api_gateway.dart';
import '../domain/graph_definition.dart';
import '../layout/graph_node_layout.dart';
import '../layout/layout_models.dart';
import '../painting/graph_edge_layer.dart';
import '../routing/graph_edge_routing.dart';
import '../routing/route_models.dart';
import 'graph_canvas_style.dart';
import 'graph_stage_node.dart';
import 'graph_measurements.dart';
/// The definition's own empty-state wording (web `PIPELINE_GRAPH_EMPTY`).
const String definitionGraphEmpty = 'This pipeline has no stages.';
/// 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 stock [InteractiveViewer].
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 = {};
TextScaler? _scaler;
bool _rebuildScheduled = false;
@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,
);
}
final layout = layoutGraphNodes(
nodes: nodes,
edges: edges,
extents: extents,
config: graphCanvasLayoutConfig(labelExtents.values),
);
final routing = routeGraphEdges(
layout: layout,
edges: edges,
labelExtents: labelExtents,
config: graphCanvasRoutingConfig(),
);
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 nodesByKey = <String, GraphNode>{
for (final node in nodes) node.key: node,
};
final size = Size(routing.width, routing.height);
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expanded(
child: InteractiveViewer(
constrained: false,
minScale: 0.3,
maxScale: 3,
child: SizedBox.fromSize(
size: size,
child: 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,
),
),
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 == widget.selectedStage,
onSelected: widget.onSelected,
),
),
for (final label in routing.labels)
Positioned(
left: label.x,
top: label.y,
child: Text(
routing.routeFor(label.edgeId)!.label,
style: style.labelStyle,
),
),
],
),
),
),
),
if (routing.skipped.isNotEmpty)
Flexible(
child: SingleChildScrollView(
child: _message(
[
for (final diagnostic in routing.skipped)
'${diagnostic.source} → ${diagnostic.outcome}: '
'${diagnostic.message}',
].join('\n'),
style,
),
),
),
],
);
}
}
@@ -0,0 +1,86 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../../ui/adaptive/host.dart';
import '../layout/layout_models.dart';
import '../routing/route_models.dart';
/// Centralized graph spacing, not component dimensions. Rank separation follows
/// actual native label measurements at any text scale.
GraphLayoutConfig graphCanvasLayoutConfig(Iterable<LabelExtent> labels) {
final widest = labels.fold<double>(
0,
(width, label) => label.width > width ? label.width : width,
);
final routing = graphCanvasRoutingConfig();
return GraphLayoutConfig(
columnGap: widest + 2 * (routing.nodeClearance + routing.labelGap) + 24,
rowGap: 24,
padding: 24,
);
}
RoutingConfig graphCanvasRoutingConfig() => RoutingConfig(
nodeClearance: 12,
laneSpacing: 28,
corridorSpacing: 20,
portSpacing: 12,
labelGap: 6,
boundsPadding: 12,
);
/// The native theme roles one definition graph canvas needs.
///
/// No palette, font family or size is defined here: every value comes from the
/// ambient Material or Cupertino theme, so light/dark and text scaling follow
/// the host exactly. Edge labels and diagnostics are metadata, so they use the
/// host's secondary-label role rather than the node's own label style.
class GraphCanvasStyle {
const GraphCanvasStyle({
required this.labelStyle,
required this.messageStyle,
required this.edgeColor,
required this.edgeWidth,
});
factory GraphCanvasStyle.of(BuildContext context) {
if (usesCupertinoHost(context)) {
final theme = CupertinoTheme.of(context);
final secondary = CupertinoColors.secondaryLabel.resolveFrom(context);
return GraphCanvasStyle(
labelStyle: theme.textTheme.tabLabelTextStyle.copyWith(
color: secondary,
),
messageStyle: theme.textTheme.textStyle.copyWith(color: secondary),
edgeColor: CupertinoColors.separator.resolveFrom(context),
// Cupertino has no divider-thickness token; its separators are one
// logical pixel hairlines, so the stroke keeps that native default.
edgeWidth: 1,
);
}
final theme = Theme.of(context);
final secondary = theme.colorScheme.onSurfaceVariant;
return GraphCanvasStyle(
labelStyle: (theme.textTheme.labelMedium ?? const TextStyle()).copyWith(
color: secondary,
),
messageStyle: (theme.textTheme.bodySmall ?? const TextStyle()).copyWith(
color: secondary,
),
edgeColor: theme.colorScheme.outline,
edgeWidth: theme.dividerTheme.thickness ?? 1,
);
}
/// Style of one routed outcome label.
final TextStyle labelStyle;
/// Style of the empty-state and skipped-edge messages.
final TextStyle messageStyle;
/// Native outline/separator color for the routed edges.
final Color edgeColor;
/// Native hairline thickness for the routed edges and their arrows.
final double edgeWidth;
}
@@ -0,0 +1,64 @@
import 'package:flutter/widgets.dart';
import '../domain/graph_definition.dart';
import 'graph_stage_node.dart';
import 'measured_child.dart';
/// Measures the same native controls that the graph places, at a bound derived
/// from its actual viewport rather than a fixed node dimension.
class GraphMeasurements extends StatelessWidget {
const GraphMeasurements({
super.key,
required this.nodes,
required this.edges,
required this.maxNodeWidth,
required this.labelStyle,
required this.onNodeSize,
required this.onLabelSize,
this.selectedStage,
});
final List<GraphNode> nodes;
final List<GraphEdge> edges;
final double maxNodeWidth;
final TextStyle labelStyle;
final String? selectedStage;
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('node:${node.key}'),
onSize: (size) => onNodeSize(node.key, size),
child: ConstrainedBox(
constraints: BoxConstraints(maxWidth: maxNodeWidth),
child: GraphStageNode(
node: node,
selected: node.key == selectedStage,
onSelected: (_) {},
),
),
),
for (final edge in edges)
MeasuredChild(
key: ValueKey('label:${edge.id}'),
onSize: (size) => onLabelSize(edge.label, size),
child: Text(edge.label, style: labelStyle),
),
],
),
),
);
}
@@ -0,0 +1,47 @@
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
/// Reports the laid-out size of its real child.
///
/// The definition graph measures the very node and label widgets it renders
/// instead of assuming a height, so a wrapped key, a larger text scale or a
/// platform host all produce the honest extent. The child is laid out with the
/// constraints its caller gives, and [onSize] runs during layout.
///
/// Because it runs inside layout, a caller must not rebuild synchronously:
/// record the size and schedule the rebuild for after the frame. The size is
/// reported again only when it actually changes, so a caller that keeps its
/// reported sizes stable does not loop.
class MeasuredChild extends SingleChildRenderObjectWidget {
const MeasuredChild({super.key, required this.onSize, required super.child});
/// Receives the child's size, in logical pixels, after every size change.
final ValueChanged<Size> onSize;
@override
MeasuredBox createRenderObject(BuildContext context) =>
MeasuredBox(onSize: onSize);
@override
void updateRenderObject(BuildContext context, MeasuredBox renderObject) {
renderObject.onSize = onSize;
}
}
/// The proxy render object behind [MeasuredChild].
class MeasuredBox extends RenderProxyBox {
MeasuredBox({required this.onSize});
ValueChanged<Size> onSize;
Size? _reported;
@override
void performLayout() {
super.performLayout();
final measured = size;
if (measured == _reported) return;
_reported = measured;
onSize(measured);
}
}