Merge branch 'worktree-agent-a3cb3553b5c91a345'
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
import '../../../core/api_gateway.dart';
|
||||
import '../../pipelines/pipeline_format.dart' show formatDuration;
|
||||
import '../../runs/domain/run_events.dart' show isTerminalRunStatus;
|
||||
import 'chat_shown_run_map.dart';
|
||||
|
||||
export 'chat_shown_run_map.dart';
|
||||
|
||||
/// A `show_run` item's run, as the server records it
|
||||
/// (`internal/protocol/chat_shown.go` ChatShownRunBody). The server pushes the
|
||||
@@ -15,6 +18,7 @@ class ChatShownRun {
|
||||
required this.currentStage,
|
||||
required this.startedAt,
|
||||
required this.finishedAt,
|
||||
this.map,
|
||||
});
|
||||
|
||||
/// The run of a `run` item, or null for any other item or a malformed one.
|
||||
@@ -32,6 +36,7 @@ class ChatShownRun {
|
||||
currentStage: _text(body['current_stage']),
|
||||
startedAt: _millis(body['started_at']),
|
||||
finishedAt: _millis(body['finished_at']),
|
||||
map: ChatShownRunMap.of(body['map']),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -44,6 +49,9 @@ class ChatShownRun {
|
||||
final int? startedAt;
|
||||
final int? finishedAt;
|
||||
|
||||
/// The graph and stage progress for the card's mini-map, when sent.
|
||||
final ChatShownRunMap? map;
|
||||
|
||||
/// Whether the run has ended (succeeded, failed, canceled, interrupted).
|
||||
bool get finished => isTerminalRunStatus(status);
|
||||
|
||||
|
||||
@@ -0,0 +1,119 @@
|
||||
import '../../../core/api_gateway.dart';
|
||||
import '../../pipelines/pipeline_format.dart' show formatDuration;
|
||||
|
||||
/// One declared stage of a `show_run` card's map with its progress in the run
|
||||
/// (`internal/protocol/chat_shown.go` ChatShownRunStage).
|
||||
class ChatShownRunStage {
|
||||
const ChatShownRunStage({
|
||||
required this.key,
|
||||
required this.type,
|
||||
required this.visits,
|
||||
required this.status,
|
||||
required this.seconds,
|
||||
required this.runningSince,
|
||||
});
|
||||
|
||||
final String key;
|
||||
final String type;
|
||||
|
||||
/// How many times the run entered the stage; 0 when it never did.
|
||||
final int visits;
|
||||
|
||||
/// The latest visit's recorded status, empty when never visited.
|
||||
final String status;
|
||||
|
||||
/// Seconds spent in the stage's finished visits.
|
||||
final int seconds;
|
||||
|
||||
/// The running visit's start (epoch milliseconds), or null.
|
||||
final int? runningSince;
|
||||
|
||||
bool get visited => visits > 0;
|
||||
|
||||
/// Total time in the stage at [now] (epoch milliseconds), counting a running
|
||||
/// visit; null for a stage that never ran.
|
||||
String? duration(int now) {
|
||||
if (!visited) return null;
|
||||
final since = runningSince;
|
||||
final live = since == null || now < since ? 0 : (now - since) ~/ 1000;
|
||||
return formatDuration(seconds + live);
|
||||
}
|
||||
}
|
||||
|
||||
/// A `show_run` card's map: the run's graph as the graph feature reads it and
|
||||
/// the compact route evidence as run-detail steps, plus the stage list.
|
||||
///
|
||||
/// The server bounds it by the graph's size (ChatShownRunMapStages), not by
|
||||
/// the run's length.
|
||||
class ChatShownRunMap {
|
||||
const ChatShownRunMap({
|
||||
required this.graph,
|
||||
required this.steps,
|
||||
required this.stages,
|
||||
});
|
||||
|
||||
/// The map of a run body, or null when the server sent none.
|
||||
static ChatShownRunMap? of(Object? raw) {
|
||||
if (raw is! Map) return null;
|
||||
final rawStages = raw['stages'];
|
||||
if (rawStages is! List) return null;
|
||||
final stages = <ChatShownRunStage>[];
|
||||
final definitions = <JsonMap>[];
|
||||
for (final stage in rawStages.whereType<Map>()) {
|
||||
final key = '${stage['key'] ?? ''}'.trim();
|
||||
if (key.isEmpty) continue;
|
||||
final edges = stage['edges'];
|
||||
definitions.add({
|
||||
'key': key,
|
||||
'type': stage['type'],
|
||||
'edges': edges is Map ? JsonMap.from(edges) : const <String, dynamic>{},
|
||||
'fallback_error': stage['fallback_error'] == true,
|
||||
});
|
||||
stages.add(
|
||||
ChatShownRunStage(
|
||||
key: key,
|
||||
type: '${stage['type'] ?? ''}',
|
||||
visits: _int(stage['visits']) ?? 0,
|
||||
status: '${stage['status'] ?? ''}'.trim(),
|
||||
seconds: _int(stage['seconds']) ?? 0,
|
||||
runningSince: _positive(stage['running_since']),
|
||||
),
|
||||
);
|
||||
}
|
||||
if (stages.isEmpty) return null;
|
||||
final rawVisits = raw['visits'];
|
||||
final steps = <JsonMap>[
|
||||
if (rawVisits is List)
|
||||
for (final visit in rawVisits.whereType<Map>())
|
||||
{
|
||||
'sequence': visit['sequence'],
|
||||
'stage': '${visit['stage'] ?? ''}',
|
||||
'status': '${visit['status'] ?? ''}',
|
||||
'outcome': '${visit['outcome'] ?? ''}',
|
||||
'target': '${visit['target'] ?? ''}',
|
||||
},
|
||||
];
|
||||
return ChatShownRunMap(
|
||||
graph: {'stages': definitions},
|
||||
steps: steps,
|
||||
stages: stages,
|
||||
);
|
||||
}
|
||||
|
||||
/// The graph definition shape (`stages` with `key`, `edges`).
|
||||
final JsonMap graph;
|
||||
|
||||
/// The route evidence in the run-detail step shape, in sequence order.
|
||||
final List<JsonMap> steps;
|
||||
|
||||
final List<ChatShownRunStage> stages;
|
||||
|
||||
/// The run-detail map the graph feature reads its evidence from.
|
||||
JsonMap runDetail({required String currentStage, required bool executing}) =>
|
||||
{'steps': steps, 'current_stage': currentStage, 'executing': executing};
|
||||
}
|
||||
|
||||
int? _int(Object? value) => value is num ? value.toInt() : null;
|
||||
|
||||
int? _positive(Object? value) =>
|
||||
value is num && value > 0 ? value.toInt() : null;
|
||||
@@ -5,6 +5,7 @@ import 'package:flutter/material.dart';
|
||||
import '../../../../../core/api_gateway.dart';
|
||||
import '../../../chat_run_opener.dart';
|
||||
import '../../../domain/chat_shown_run.dart';
|
||||
import 'chat_shown_run_map_view.dart';
|
||||
import 'chat_shown_surface.dart';
|
||||
|
||||
/// The glyph of a run status.
|
||||
@@ -134,6 +135,10 @@ class _ChatShownRunCardState extends State<ChatShownRunCard> {
|
||||
),
|
||||
],
|
||||
),
|
||||
if (run.map case final map?) ...[
|
||||
const SizedBox(height: 10),
|
||||
ChatShownRunMapView(run: run, map: map, now: _now),
|
||||
],
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
@@ -0,0 +1,175 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../../graph/widgets/run_graph_view.dart';
|
||||
import '../../../domain/chat_shown_run.dart';
|
||||
|
||||
/// The mini-map's height: enough for one row of stages and the ends below it
|
||||
/// at a glance, small enough to keep the card compact.
|
||||
const double chatShownRunMapHeight = 160;
|
||||
|
||||
/// A `show_run` card's graph: a still mini-map of the run's pipeline drawn by
|
||||
/// the run graph itself (visited stages with their status, the live stage's
|
||||
/// halo, the taken path solid and untaken routes dashed), then a strip of every
|
||||
/// stage with its status, visits and time.
|
||||
class ChatShownRunMapView extends StatelessWidget {
|
||||
const ChatShownRunMapView({
|
||||
super.key,
|
||||
required this.run,
|
||||
required this.map,
|
||||
required this.now,
|
||||
});
|
||||
|
||||
final ChatShownRun run;
|
||||
final ChatShownRunMap map;
|
||||
|
||||
/// The current time in epoch milliseconds, for running stages' durations.
|
||||
final int now;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final colors = Theme.of(context).colorScheme;
|
||||
final current = run.finished ? '' : run.currentStage;
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
DecoratedBox(
|
||||
decoration: BoxDecoration(
|
||||
color: colors.surfaceContainerLow,
|
||||
border: Border.all(color: colors.outlineVariant),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: ClipRRect(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
child: SizedBox(
|
||||
key: const Key('chat-shown-run-map'),
|
||||
height: chatShownRunMapHeight,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(6),
|
||||
child: RunGraphView(
|
||||
graph: map.graph,
|
||||
run: map.runDetail(
|
||||
currentStage: run.currentStage,
|
||||
executing: !run.finished,
|
||||
),
|
||||
onStageSelected: (_) {},
|
||||
showHeader: false,
|
||||
expandable: false,
|
||||
still: true,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Wrap(
|
||||
key: const Key('chat-shown-run-stages'),
|
||||
spacing: 6,
|
||||
runSpacing: 6,
|
||||
children: [
|
||||
for (final stage in map.stages)
|
||||
_StageEntry(
|
||||
stage: stage,
|
||||
current: stage.key == current,
|
||||
duration: stage.duration(now),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// The glyph of a recorded stage status; a stage never entered is a hollow
|
||||
/// circle.
|
||||
IconData chatShownStageIcon(String status, {required bool visited}) {
|
||||
if (!visited) return Icons.radio_button_unchecked;
|
||||
return switch (status) {
|
||||
'completed' || 'succeeded' => Icons.check_circle,
|
||||
'failed' => Icons.error,
|
||||
'running' => Icons.play_circle,
|
||||
'waiting' => Icons.front_hand,
|
||||
'interrupted' => Icons.pause_circle,
|
||||
'canceled' => Icons.cancel,
|
||||
_ => Icons.circle,
|
||||
};
|
||||
}
|
||||
|
||||
Color _stageColor(ColorScheme colors, ChatShownRunStage stage) {
|
||||
if (!stage.visited) return colors.outline;
|
||||
return switch (stage.status) {
|
||||
'completed' || 'succeeded' => colors.primary,
|
||||
'failed' || 'interrupted' => colors.error,
|
||||
'running' || 'waiting' => colors.tertiary,
|
||||
_ => colors.onSurfaceVariant,
|
||||
};
|
||||
}
|
||||
|
||||
/// One stage of the strip: status glyph, name, `×visits` when it looped and
|
||||
/// the time spent. The run's current stage is filled and bold.
|
||||
class _StageEntry extends StatelessWidget {
|
||||
const _StageEntry({
|
||||
required this.stage,
|
||||
required this.current,
|
||||
required this.duration,
|
||||
});
|
||||
|
||||
final ChatShownRunStage stage;
|
||||
final bool current;
|
||||
final String? duration;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
final colors = theme.colorScheme;
|
||||
final base = theme.textTheme.labelMedium ?? const TextStyle();
|
||||
final name = base.copyWith(
|
||||
color: stage.visited ? colors.onSurface : colors.onSurfaceVariant,
|
||||
fontWeight: current ? FontWeight.w700 : null,
|
||||
);
|
||||
final meta = base.copyWith(color: colors.onSurfaceVariant);
|
||||
final status = stage.visited ? stage.status : 'not run';
|
||||
final facts = [if (stage.visits > 1) '×${stage.visits}', ?duration];
|
||||
return Semantics(
|
||||
container: true,
|
||||
label: [
|
||||
stage.key,
|
||||
status,
|
||||
if (stage.visits > 1) '${stage.visits} visits',
|
||||
?duration,
|
||||
if (current) 'current stage',
|
||||
].join(', '),
|
||||
child: ExcludeSemantics(
|
||||
child: DecoratedBox(
|
||||
key: ValueKey('chat-shown-run-stage:${stage.key}'),
|
||||
decoration: BoxDecoration(
|
||||
color: current ? colors.secondaryContainer : null,
|
||||
border: Border.all(
|
||||
color: current ? colors.secondary : colors.outlineVariant,
|
||||
),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Padding(
|
||||
padding: const EdgeInsetsDirectional.fromSTEB(6, 3, 8, 3),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(
|
||||
chatShownStageIcon(stage.status, visited: stage.visited),
|
||||
size: MediaQuery.textScalerOf(context).scale(14),
|
||||
color: _stageColor(colors, stage),
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
Text(stage.key, style: name),
|
||||
if (facts.isNotEmpty) ...[
|
||||
const SizedBox(width: 6),
|
||||
Text(facts.join(' · '), style: meta),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../layout/layout_models.dart';
|
||||
import '../routing/route_models.dart';
|
||||
import 'graph_edge_layer.dart';
|
||||
|
||||
/// Paints one highlighted edge over the other routes and behind the nodes: a
|
||||
/// soft glow under the two endpoint stages, then the route in [color] at a
|
||||
/// heavier stroke with its arrow, on a translucent halo.
|
||||
///
|
||||
/// Passive like the other edge layers: it never takes a hit.
|
||||
class EdgeHighlightPainter extends CustomPainter {
|
||||
EdgeHighlightPainter({
|
||||
required this.route,
|
||||
required this.endpoints,
|
||||
required this.color,
|
||||
required this.strokeWidth,
|
||||
});
|
||||
|
||||
final EdgeRoute route;
|
||||
|
||||
/// The source and target node boxes (one for a self loop).
|
||||
final List<GraphNodeBox> endpoints;
|
||||
final Color color;
|
||||
|
||||
/// The highlighted stroke; the halo is wider.
|
||||
final double strokeWidth;
|
||||
|
||||
@override
|
||||
void paint(Canvas canvas, Size size) {
|
||||
final glow = Paint()..color = color.withValues(alpha: 0.18);
|
||||
for (final box in endpoints) {
|
||||
final rect = Rect.fromLTWH(box.x, box.y, box.width, box.height);
|
||||
canvas.drawRRect(
|
||||
RRect.fromRectAndRadius(rect.inflate(5), const Radius.circular(14)),
|
||||
glow,
|
||||
);
|
||||
}
|
||||
if (route.points.length < 2) return;
|
||||
final geometry = GraphEdgePainter(
|
||||
routes: [route],
|
||||
color: color,
|
||||
strokeWidth: strokeWidth,
|
||||
);
|
||||
final line = geometry.linePath(route);
|
||||
canvas.drawPath(
|
||||
line,
|
||||
Paint()
|
||||
..color = color.withValues(alpha: 0.22)
|
||||
..strokeWidth = strokeWidth * 3
|
||||
..style = PaintingStyle.stroke
|
||||
..strokeJoin = StrokeJoin.round
|
||||
..strokeCap = StrokeCap.round,
|
||||
);
|
||||
canvas.drawPath(
|
||||
line,
|
||||
Paint()
|
||||
..color = color
|
||||
..strokeWidth = strokeWidth
|
||||
..style = PaintingStyle.stroke
|
||||
..strokeJoin = StrokeJoin.round,
|
||||
);
|
||||
canvas.drawPath(
|
||||
GraphEdgePainter(
|
||||
routes: [route],
|
||||
color: color,
|
||||
// The arrow scales with the stroke; keep it near the plain arrow.
|
||||
strokeWidth: strokeWidth / 1.5,
|
||||
).arrowPath(route),
|
||||
Paint()..color = color,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
bool hitTest(Offset position) => false;
|
||||
|
||||
@override
|
||||
bool shouldRepaint(EdgeHighlightPainter oldDelegate) =>
|
||||
oldDelegate.route != route ||
|
||||
oldDelegate.color != color ||
|
||||
oldDelegate.strokeWidth != strokeWidth ||
|
||||
oldDelegate.endpoints.length != endpoints.length ||
|
||||
!_sameBoxes(oldDelegate.endpoints, endpoints);
|
||||
|
||||
static bool _sameBoxes(List<GraphNodeBox> a, List<GraphNodeBox> b) {
|
||||
for (var index = 0; index < a.length; index += 1) {
|
||||
if (a[index] != b[index]) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
import 'dart:math' as math;
|
||||
import 'dart:ui' show Offset, Rect;
|
||||
|
||||
import '../layout/layout_models.dart';
|
||||
import '../routing/route_models.dart';
|
||||
|
||||
/// Which routed edge a pointer at [point] (graph space) is on, or null.
|
||||
///
|
||||
/// A point inside a node box belongs to the node, never to an edge. A point on
|
||||
/// an edge's label box is that edge. Otherwise the nearest route polyline
|
||||
/// within [tolerance] wins, so overlapping corridors pick the closest lane and
|
||||
/// self loops and back edges are hit along every bend they take.
|
||||
int? hitTestEdge({
|
||||
required List<EdgeRoute> routes,
|
||||
required List<EdgeLabelBox> labels,
|
||||
required List<GraphNodeBox> nodes,
|
||||
required Offset point,
|
||||
required double tolerance,
|
||||
}) {
|
||||
for (final node in nodes) {
|
||||
if (Rect.fromLTWH(
|
||||
node.x,
|
||||
node.y,
|
||||
node.width,
|
||||
node.height,
|
||||
).contains(point)) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
for (final label in labels) {
|
||||
final box = Rect.fromLTWH(label.x, label.y, label.width, label.height);
|
||||
if (box.inflate(2).contains(point)) return label.edgeId;
|
||||
}
|
||||
int? best;
|
||||
var bestDistance = tolerance;
|
||||
for (final route in routes) {
|
||||
final distance = distanceToRoute(route, point);
|
||||
if (distance <= bestDistance) {
|
||||
best = route.id;
|
||||
bestDistance = distance;
|
||||
}
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
/// The shortest distance from [point] to [route]'s polyline.
|
||||
double distanceToRoute(EdgeRoute route, Offset point) {
|
||||
final points = route.points;
|
||||
if (points.isEmpty) return double.infinity;
|
||||
var best = double.infinity;
|
||||
for (var index = 0; index < points.length; index += 1) {
|
||||
final a = Offset(points[index].x, points[index].y);
|
||||
final b = index + 1 < points.length
|
||||
? Offset(points[index + 1].x, points[index + 1].y)
|
||||
: a;
|
||||
best = math.min(best, _distanceToSegment(point, a, b));
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
double _distanceToSegment(Offset p, Offset a, Offset b) {
|
||||
final ab = b - a;
|
||||
final lengthSquared = ab.distanceSquared;
|
||||
if (lengthSquared == 0) return (p - a).distance;
|
||||
final t = (((p.dx - a.dx) * ab.dx + (p.dy - a.dy) * ab.dy) / lengthSquared)
|
||||
.clamp(0.0, 1.0);
|
||||
return (p - (a + ab * t)).distance;
|
||||
}
|
||||
@@ -1,10 +1,11 @@
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../domain/graph_definition.dart';
|
||||
import '../layout/layout_models.dart';
|
||||
import '../painting/graph_edge_layer.dart';
|
||||
import '../routing/route_models.dart';
|
||||
import 'graph_canvas_style.dart';
|
||||
import 'graph_edge_hover.dart';
|
||||
import 'graph_stage_node.dart';
|
||||
|
||||
/// The measured definition canvas at any viewport size and selection.
|
||||
@@ -36,6 +37,22 @@ class DefinitionGraphCanvas extends StatelessWidget {
|
||||
for (final node in nodes) node.key: node,
|
||||
};
|
||||
final size = Size(routing.width, routing.height);
|
||||
return GraphEdgeHover(
|
||||
layout: layout,
|
||||
routing: routing,
|
||||
describe: describeGraphEdge,
|
||||
builder: (context, highlighted, select) =>
|
||||
_canvas(context, nodesByKey, size, highlighted, select),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _canvas(
|
||||
BuildContext context,
|
||||
Map<String, GraphNode> nodesByKey,
|
||||
Size size,
|
||||
int? highlighted,
|
||||
ValueChanged<int> select,
|
||||
) {
|
||||
return Stack(
|
||||
clipBehavior: Clip.none,
|
||||
children: [
|
||||
@@ -49,6 +66,15 @@ class DefinitionGraphCanvas extends StatelessWidget {
|
||||
strokeWidth: style.edgeWidth,
|
||||
),
|
||||
),
|
||||
Positioned.fill(
|
||||
child: graphEdgeHighlight(
|
||||
context: context,
|
||||
layout: layout,
|
||||
routing: routing,
|
||||
highlighted: highlighted,
|
||||
edgeWidth: style.edgeWidth,
|
||||
),
|
||||
),
|
||||
for (final box in layout.nodes)
|
||||
Positioned(
|
||||
left: box.x,
|
||||
@@ -65,9 +91,19 @@ class DefinitionGraphCanvas extends StatelessWidget {
|
||||
Positioned(
|
||||
left: label.x,
|
||||
top: label.y,
|
||||
child: Text(
|
||||
routing.routeFor(label.edgeId)!.label,
|
||||
style: style.labelStyle,
|
||||
child: graphEdgeLabel(
|
||||
context: context,
|
||||
highlighted: label.edgeId == highlighted,
|
||||
description: describeGraphEdge(routing.routeFor(label.edgeId)!),
|
||||
onSelect: () => select(label.edgeId),
|
||||
child: Text(
|
||||
routing.routeFor(label.edgeId)!.label,
|
||||
style: graphEdgeLabelStyle(
|
||||
context,
|
||||
style.labelStyle,
|
||||
highlighted: label.edgeId == highlighted,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
|
||||
@@ -0,0 +1,282 @@
|
||||
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,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import '../layout/layout_models.dart';
|
||||
import '../painting/graph_edge_layer.dart';
|
||||
import '../routing/route_models.dart';
|
||||
import 'graph_canvas_style.dart';
|
||||
import 'graph_edge_hover.dart';
|
||||
import 'graph_stage_node.dart';
|
||||
import 'measured_child.dart';
|
||||
|
||||
@@ -199,56 +200,115 @@ class RunGraphCanvas extends StatelessWidget {
|
||||
final size = Size(routing.width, routing.height);
|
||||
final activeStage = state.activeStage;
|
||||
final executedColor = 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,
|
||||
String describe(EdgeRoute route) => describeGraphEdge(
|
||||
route,
|
||||
taken: state.routeOrder(route.source, route.outcome, route.target).length,
|
||||
);
|
||||
return GraphEdgeHover(
|
||||
layout: layout,
|
||||
routing: routing,
|
||||
describe: describe,
|
||||
builder: (context, highlighted, select) => Stack(
|
||||
clipBehavior: Clip.none,
|
||||
children: [
|
||||
Positioned.fill(
|
||||
child: RunGraphEdgeLayer(
|
||||
executedRoutes: executed,
|
||||
untakenRoutes: untaken,
|
||||
canvasSize: size,
|
||||
executedColor: executedColor,
|
||||
untakenColor: style.edgeColor,
|
||||
strokeWidth: style.edgeWidth,
|
||||
),
|
||||
),
|
||||
// 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,
|
||||
),
|
||||
Positioned.fill(
|
||||
child: graphEdgeHighlight(
|
||||
context: context,
|
||||
layout: layout,
|
||||
routing: routing,
|
||||
highlighted: highlighted,
|
||||
edgeWidth: style.edgeWidth,
|
||||
),
|
||||
],
|
||||
),
|
||||
..._placed(
|
||||
context,
|
||||
nodesByKey,
|
||||
executedIds,
|
||||
executedColor,
|
||||
activeStage,
|
||||
highlighted,
|
||||
select,
|
||||
describe,
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _takenLabel(EdgeRoute route, Color color) => RunRouteLabel(
|
||||
List<Widget> _placed(
|
||||
BuildContext context,
|
||||
Map<String, GraphNode> nodesByKey,
|
||||
Set<int> executedIds,
|
||||
Color executedColor,
|
||||
String? activeStage,
|
||||
int? highlighted,
|
||||
ValueChanged<int> select,
|
||||
String Function(EdgeRoute) describe,
|
||||
) {
|
||||
return [
|
||||
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, and the route
|
||||
// being highlighted so its outcome reads where it is drawn.
|
||||
for (final label in routing.labels)
|
||||
if (executedIds.contains(label.edgeId) || label.edgeId == highlighted)
|
||||
Positioned(
|
||||
left: label.x,
|
||||
top: label.y,
|
||||
child: graphEdgeLabel(
|
||||
context: context,
|
||||
highlighted: label.edgeId == highlighted,
|
||||
description: describe(routing.routeFor(label.edgeId)!),
|
||||
onSelect: () => select(label.edgeId),
|
||||
child: _takenLabel(
|
||||
context,
|
||||
routing.routeFor(label.edgeId)!,
|
||||
executedColor,
|
||||
highlighted: label.edgeId == highlighted,
|
||||
),
|
||||
),
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
Widget _takenLabel(
|
||||
BuildContext context,
|
||||
EdgeRoute route,
|
||||
Color color, {
|
||||
required bool highlighted,
|
||||
}) => RunRouteLabel(
|
||||
edgeId: route.id,
|
||||
label: route.label,
|
||||
order: state.routeOrder(route.source, route.outcome, route.target),
|
||||
style: style.labelStyle,
|
||||
style: graphEdgeLabelStyle(
|
||||
context,
|
||||
style.labelStyle,
|
||||
highlighted: highlighted,
|
||||
),
|
||||
color: color,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -48,8 +48,13 @@ class RunGraphView extends StatefulWidget {
|
||||
this.now,
|
||||
this.showHeader = true,
|
||||
this.expandable = true,
|
||||
this.still = false,
|
||||
});
|
||||
|
||||
/// True for a mini-map: the whole graph scaled to fit the box, with no
|
||||
/// pan/zoom viewport, controls, input or semantics of its own.
|
||||
final bool still;
|
||||
|
||||
/// False when this graph is already the full-screen view: no maximise.
|
||||
final bool expandable;
|
||||
|
||||
@@ -207,7 +212,9 @@ class _RunGraphViewState extends State<RunGraphView> {
|
||||
child: Stack(
|
||||
fit: StackFit.expand,
|
||||
children: [
|
||||
if (geometry != null)
|
||||
if (geometry != null && widget.still)
|
||||
_stillCanvas(nodes, geometry, state, style, statuses)
|
||||
else if (geometry != null)
|
||||
GraphViewport(
|
||||
controller: _viewport,
|
||||
contentSize: Size(geometry.$2.width, geometry.$2.height),
|
||||
@@ -273,6 +280,40 @@ class _RunGraphViewState extends State<RunGraphView> {
|
||||
),
|
||||
);
|
||||
|
||||
/// The whole measured canvas scaled down (never up) to fit the box: a
|
||||
/// picture with no input or semantics, for a mini-map.
|
||||
Widget _stillCanvas(
|
||||
List<GraphNode> nodes,
|
||||
(GraphLayout, GraphRouting) geometry,
|
||||
RunGraphState state,
|
||||
GraphCanvasStyle style,
|
||||
Map<String, String?> statuses,
|
||||
) {
|
||||
final routing = geometry.$2;
|
||||
return ExcludeSemantics(
|
||||
child: IgnorePointer(
|
||||
child: FittedBox(
|
||||
key: const Key('run-graph-still'),
|
||||
fit: BoxFit.scaleDown,
|
||||
child: SizedBox(
|
||||
width: routing.width,
|
||||
height: routing.height,
|
||||
child: _runCanvas(
|
||||
nodes: nodes,
|
||||
layout: geometry.$1,
|
||||
routing: routing,
|
||||
state: state,
|
||||
style: style,
|
||||
statuses: statuses,
|
||||
selectedStage: widget.selectedStage,
|
||||
onStageSelected: widget.onStageSelected,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// The same measured run canvas at any viewport size and selection, used by
|
||||
/// the inline viewport and the expanded presentation.
|
||||
Widget _runCanvas({
|
||||
|
||||
Reference in New Issue
Block a user