Keep graph edge labels clear of nodes and other labels

The linear layout used by the definition and run graphs placed each
outcome label at the middle of its widest run with no collision check,
and the run graph measured only the label text while rendering an order
badge in front of it, so taken labels ran into the next node.

Labels now try positions along their own route (horizontal runs above
and below, sliding along, then vertical runs) and take the first whose
box stays 4px clear of every node and earlier label, preferring spots
off other wires. If a label finds no spot, the layout widens its column
gaps and lanes and runs again; a last-resort shelf below the drawing
never overlaps. The run graph measures the exact badge+label widget it
renders, keyed by edge id.
This commit is contained in:
2026-10-31 16:36:48 +00:00
parent 4235cd858f
commit fb1935a62d
6 changed files with 794 additions and 111 deletions
@@ -248,20 +248,46 @@ class RunGraphCanvas extends StatelessWidget {
],
);
}
Widget _takenLabel(EdgeRoute route, Color color) => RunRouteLabel(
edgeId: route.id,
label: route.label,
order: state.routeOrder(route.source, route.outcome, route.target),
style: style.labelStyle,
color: color,
);
}
/// 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);
///
/// The canvas places this widget and [RunGraphMeasurements] measures the same
/// one, so the routed label box covers the order badge as well as the text.
class RunRouteLabel extends StatelessWidget {
const RunRouteLabel({
super.key,
required this.edgeId,
required this.label,
required this.order,
required this.style,
required this.color,
});
final int edgeId;
final String label;
final List<int> order;
final TextStyle style;
final Color color;
@override
Widget build(BuildContext context) {
final text = Text(label, style: style);
if (order.isEmpty) return text;
return Row(
mainAxisSize: MainAxisSize.min,
children: [
Container(
key: ValueKey('run-route-order:${route.id}'),
key: ValueKey('run-route-order:$edgeId'),
padding: const EdgeInsets.symmetric(horizontal: 5, vertical: 1),
decoration: BoxDecoration(
color: color,
@@ -348,6 +374,7 @@ class RunGraphMeasurements extends StatelessWidget {
required this.maxNodeWidth,
required this.labelStyle,
required this.recordedStatuses,
required this.state,
required this.onNodeSize,
required this.onLabelSize,
});
@@ -357,8 +384,14 @@ class RunGraphMeasurements extends StatelessWidget {
final double maxNodeWidth;
final TextStyle labelStyle;
final Map<String, String?> recordedStatuses;
/// The run whose route order badges lead the taken labels.
final RunGraphState state;
final void Function(String, Size) onNodeSize;
final void Function(String, Size) onLabelSize;
/// Receives each edge's label size keyed by its edge id, because two edges
/// with the same outcome can carry different order badges.
final void Function(int, Size) onLabelSize;
@override
Widget build(BuildContext context) => Offstage(
@@ -391,8 +424,14 @@ class RunGraphMeasurements extends StatelessWidget {
for (final edge in edges)
MeasuredChild(
key: ValueKey('run-label:${edge.id}'),
onSize: (size) => onLabelSize(edge.label, size),
child: Text(edge.label, style: labelStyle),
onSize: (size) => onLabelSize(edge.id, size),
child: RunRouteLabel(
edgeId: edge.id,
label: edge.label,
order: state.routeOrder(edge.source, edge.outcome, edge.target),
style: labelStyle,
color: const Color(0x00000000),
),
),
],
),
@@ -76,7 +76,7 @@ class RunGraphView extends StatefulWidget {
class _RunGraphViewState extends State<RunGraphView> {
final Map<String, Size> _nodeSizes = {};
final Map<String, Size> _labelSizes = {};
final Map<int, Size> _labelSizes = {};
final Map<String, String?> _measuredStatuses = {};
final GraphViewportController _viewport = GraphViewportController();
TextScaler? _scaler;
@@ -99,7 +99,7 @@ class _RunGraphViewState extends State<RunGraphView> {
}
}
void _record(Map<String, Size> sizes, String key, Size size) {
void _record<K>(Map<K, Size> sizes, K key, Size size) {
if (sizes[key] == size) return;
sizes[key] = size;
if (_rebuildScheduled) return;
@@ -122,7 +122,7 @@ class _RunGraphViewState extends State<RunGraphView> {
}
final labelExtents = <int, LabelExtent>{};
for (final edge in edges) {
final size = _labelSizes[edge.label];
final size = _labelSizes[edge.id];
if (size == null) return null;
labelExtents[edge.id] = LabelExtent(
width: size.width,
@@ -239,6 +239,7 @@ class _RunGraphViewState extends State<RunGraphView> {
maxNodeWidth: constraints.maxWidth,
labelStyle: style.labelStyle,
recordedStatuses: statuses,
state: state,
onNodeSize: (key, size) => _record(_nodeSizes, key, size),
onLabelSize: (key, size) => _record(_labelSizes, key, size),
),