Files
pipelines/app/lib/features/graph/widgets/graph_edge_hover.dart
T
alan 7872e57d40 Run card mini-map and stage strip; graph edge hover highlight
The show_run card carries the run's graph and per-stage progress (bounded by
the graph, not the run length) and draws a still mini-map with the run graph
plus a stage strip with status, visits and time. Hovering or tapping an edge
in the pipeline and run graphs highlights it, its label and endpoints, with a
tooltip naming from -> to, the outcome and, for runs, how often it was taken.

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

283 lines
9.0 KiB
Dart

import 'package:flutter/gestures.dart' show PointerDeviceKind;
import 'package:flutter/material.dart';
import '../domain/graph_protocol.dart';
import '../layout/layout_models.dart';
import '../painting/edge_highlight_layer.dart';
import '../painting/edge_hit_test.dart';
import '../routing/route_models.dart';
/// Builds a graph canvas with [highlighted] (an edge id, or null) emphasized.
/// [select] highlights an edge without a pointer, e.g. from semantics.
typedef GraphEdgeHoverBuilder =
Widget Function(
BuildContext context,
int? highlighted,
ValueChanged<int> select,
);
/// How far from a route, in screen pixels, a pointer still counts as on it.
const double graphEdgeHitTolerance = 8;
/// Highlights the edge under the pointer of a graph canvas, and shows what it
/// is in a tooltip: hovering with a mouse, or tapping with touch (tap again
/// elsewhere to clear).
///
/// Wraps the canvas in graph space, inside any pan/zoom transform, so hits are
/// tested against the routed polylines and label boxes with a tolerance that
/// stays [graphEdgeHitTolerance] screen pixels at every zoom. Nodes keep their
/// own taps; a point over a node is never an edge.
class GraphEdgeHover extends StatefulWidget {
const GraphEdgeHover({
super.key,
required this.layout,
required this.routing,
required this.describe,
required this.builder,
});
final GraphLayout layout;
final GraphRouting routing;
/// The tooltip text for one edge.
final String Function(EdgeRoute route) describe;
final GraphEdgeHoverBuilder builder;
@override
State<GraphEdgeHover> createState() => _GraphEdgeHoverState();
}
class _GraphEdgeHoverState extends State<GraphEdgeHover> {
final OverlayPortalController _tooltip = OverlayPortalController();
int? _hovered;
int? _pinned;
Offset? _pointer;
int? get _highlighted => _hovered ?? _pinned;
/// Graph-space pixels per screen pixel under the current transform.
double _tolerance() {
final box = context.findRenderObject();
if (box is! RenderBox || !box.attached) return graphEdgeHitTolerance;
final scale =
(box.localToGlobal(const Offset(1, 0)) - box.localToGlobal(Offset.zero))
.distance;
return scale <= 0 ? graphEdgeHitTolerance : graphEdgeHitTolerance / scale;
}
int? _hit(Offset local) => hitTestEdge(
routes: widget.routing.routes,
labels: widget.routing.labels,
nodes: widget.layout.nodes,
point: local,
tolerance: _tolerance(),
);
void _update({int? hovered, int? pinned, Offset? pointer, bool pin = false}) {
final nextPinned = pin ? pinned : _pinned;
if (hovered == _hovered && nextPinned == _pinned && pointer == _pointer) {
return;
}
setState(() {
_hovered = hovered;
_pinned = nextPinned;
_pointer = pointer;
});
_syncTooltip();
}
void _syncTooltip() {
if (_highlighted != null && _pointer != null) {
_tooltip.show();
} else {
_tooltip.hide();
}
}
@override
void didUpdateWidget(covariant GraphEdgeHover oldWidget) {
super.didUpdateWidget(oldWidget);
// A re-laid graph may have dropped the highlighted edge.
bool known(int? id) => id != null && widget.routing.routeFor(id) != null;
if (!known(_hovered)) _hovered = null;
if (!known(_pinned)) _pinned = null;
if (_highlighted == null) _pointer = null;
WidgetsBinding.instance.addPostFrameCallback((_) {
if (mounted) _syncTooltip();
});
}
@override
Widget build(BuildContext context) {
final canvas = MouseRegion(
onHover: (event) {
final id = _hit(event.localPosition);
_update(hovered: id, pointer: id == null ? null : event.position);
},
onExit: (_) =>
_update(hovered: null, pointer: _pinned == null ? null : _pointer),
child: GestureDetector(
behavior: HitTestBehavior.translucent,
// A tap pins the edge under it (touch has no hover); tapping it again
// or anywhere off an edge clears it.
onTapUp: (details) {
final id = _hit(details.localPosition);
final hovered = details.kind == PointerDeviceKind.mouse
? _hovered
: null;
final pinned = id == _pinned ? null : id;
_update(
hovered: hovered,
pinned: pinned,
pin: true,
pointer: (hovered ?? pinned) == null
? null
: details.globalPosition,
);
},
child: widget.builder(context, _highlighted, (id) {
_update(hovered: _hovered, pinned: id, pin: true, pointer: _pointer);
}),
),
);
if (Overlay.maybeOf(context) == null) return canvas;
return OverlayPortal(
controller: _tooltip,
overlayChildBuilder: _buildTooltip,
child: canvas,
);
}
Widget _buildTooltip(BuildContext context) {
final id = _highlighted;
final pointer = _pointer;
final route = id == null ? null : widget.routing.routeFor(id);
final overlay = Overlay.of(context).context.findRenderObject();
if (route == null || pointer == null || overlay is! RenderBox) {
return const SizedBox.shrink();
}
final local = overlay.globalToLocal(pointer);
final size = overlay.size;
final theme = Theme.of(context);
final colors = theme.colorScheme;
const gap = 14.0;
final flipX = local.dx > size.width * 0.6;
final flipY = local.dy > size.height * 0.75;
return Positioned(
left: flipX ? null : local.dx + gap,
right: flipX ? size.width - local.dx + gap : null,
top: flipY ? null : local.dy + gap,
bottom: flipY ? size.height - local.dy + gap : null,
child: IgnorePointer(
child: ConstrainedBox(
constraints: BoxConstraints(maxWidth: size.width * 0.6),
child: Material(
key: const Key('graph-edge-tooltip'),
color: colors.inverseSurface,
elevation: 2,
borderRadius: BorderRadius.circular(6),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 6),
child: Text(
widget.describe(route),
style: theme.textTheme.bodySmall?.copyWith(
color: colors.onInverseSurface,
),
),
),
),
),
),
);
}
}
/// The highlight layer a canvas places above its edges and below its nodes,
/// or nothing when no edge is highlighted.
Widget graphEdgeHighlight({
required BuildContext context,
required GraphLayout layout,
required GraphRouting routing,
required int? highlighted,
required double edgeWidth,
}) {
final route = highlighted == null ? null : routing.routeFor(highlighted);
if (route == null) return const SizedBox.shrink();
final endpoints = [
?layout.nodeFor(route.source),
if (route.target != route.source) ?layout.nodeFor(route.target),
];
return IgnorePointer(
child: CustomPaint(
key: ValueKey('graph-edge-highlight:${route.id}'),
size: Size(routing.width, routing.height),
painter: EdgeHighlightPainter(
route: route,
endpoints: endpoints,
color: Theme.of(context).colorScheme.primary,
strokeWidth: edgeWidth * 2.5,
),
),
);
}
/// What an edge is, for its tooltip and screen readers: `from → to`, the
/// outcome, and for a run how many times it was taken ([taken] null for a
/// definition).
String describeGraphEdge(EdgeRoute route, {int? taken}) {
final lines = [
'${nodeDisplayLabel(route.source)} → ${nodeDisplayLabel(route.target)}',
'Outcome: ${route.label}',
if (taken != null)
switch (taken) {
0 => 'Not taken',
1 => 'Taken once',
_ => 'Taken $taken times',
},
];
return lines.join('\n');
}
/// The label style of a highlighted edge: [base] on the emphasis container.
TextStyle graphEdgeLabelStyle(
BuildContext context,
TextStyle base, {
required bool highlighted,
}) => highlighted
? base.copyWith(color: Theme.of(context).colorScheme.onPrimaryContainer)
: base;
/// An outcome label drawn emphasized while its edge is highlighted. The
/// emphasis is a backdrop only, so the measured label box does not change.
/// [description] names the edge for screen readers; activating it highlights
/// the edge through [onSelect].
Widget graphEdgeLabel({
required BuildContext context,
required Widget child,
required bool highlighted,
required String description,
required VoidCallback onSelect,
}) {
final colors = Theme.of(context).colorScheme;
return Semantics(
container: true,
label: description,
selected: highlighted,
onTap: onSelect,
child: ExcludeSemantics(
child: !highlighted
? child
: DecoratedBox(
decoration: BoxDecoration(
color: colors.primaryContainer,
borderRadius: BorderRadius.circular(4),
boxShadow: [
BoxShadow(color: colors.primaryContainer, spreadRadius: 2),
],
),
child: child,
),
),
);
}