330 lines
11 KiB
Dart
330 lines
11 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
import '../../../core/api_gateway.dart';
|
|
import '../domain/graph_definition.dart';
|
|
import '../domain/run_graph_state.dart';
|
|
import '../domain/stage_visits.dart';
|
|
import '../layout/layout_models.dart';
|
|
import '../layout/linear_graph_layout.dart';
|
|
import '../routing/route_models.dart';
|
|
import '../viewport/graph_expanded_view.dart';
|
|
import '../viewport/graph_viewport.dart';
|
|
import '../viewport/graph_viewport_controller.dart';
|
|
import 'graph_canvas_style.dart';
|
|
import 'run_graph_canvas.dart';
|
|
import 'run_graph_header.dart';
|
|
|
|
export 'run_graph_header.dart' show runGraphExecuted, runGraphNotExecuted;
|
|
|
|
/// The run tab's exact missing-graph wording (web `RUN_GRAPH_MISSING`).
|
|
const String runGraphMissing = 'This server did not include the run graph.';
|
|
|
|
/// The expanded run presentation's title.
|
|
const String runGraphExpandedTitle = 'Run graph';
|
|
|
|
/// One run's definition drawn as evidence: visited stages carry their recorded
|
|
/// status, only taken routes are solid and arrowed, and every other route stays
|
|
/// dashed and muted.
|
|
///
|
|
/// [run] is the run detail map whose recorded steps are the only source of
|
|
/// visits, statuses and routes. A node with no recorded visit is not a control:
|
|
/// tapping it does nothing, and no visit, session or status is manufactured for
|
|
/// it. A visited stage reports its exact key through [onStageSelected] so the
|
|
/// caller can open that stage's latest recorded visit. Visited terminals are
|
|
/// shown with the outcome that resolved there but have no recorded visit to
|
|
/// open, so they stay non-interactive.
|
|
///
|
|
/// The view fills its bounded constraints with a pan/zoom [GraphViewport]; the
|
|
/// elapsed line appears only while the run is executing and is static (no
|
|
/// invented progress or timer). The same canvas can be opened full screen with
|
|
/// its selection preserved and the route legend still explaining its marks.
|
|
class RunGraphView extends StatefulWidget {
|
|
const RunGraphView({
|
|
super.key,
|
|
required this.graph,
|
|
required this.run,
|
|
required this.onStageSelected,
|
|
this.selectedStage,
|
|
this.now,
|
|
this.showHeader = true,
|
|
this.expandable = true,
|
|
});
|
|
|
|
/// False when this graph is already the full-screen view: no maximise.
|
|
final bool expandable;
|
|
|
|
/// The run's definition graph, or null when the server omitted it.
|
|
final JsonMap? graph;
|
|
|
|
/// The run detail whose recorded steps supply all execution evidence.
|
|
final JsonMap run;
|
|
|
|
/// Reports one visited stage's exact key; never called for an unvisited node.
|
|
final ValueChanged<String> onStageSelected;
|
|
|
|
final String? selectedStage;
|
|
|
|
/// Injectable clock for deterministic elapsed labels; defaults to now.
|
|
final DateTime Function()? now;
|
|
|
|
/// False for a still preview, which has no room for the status legend.
|
|
final bool showHeader;
|
|
|
|
@override
|
|
State<RunGraphView> createState() => _RunGraphViewState();
|
|
}
|
|
|
|
class _RunGraphViewState extends State<RunGraphView> {
|
|
final Map<String, Size> _nodeSizes = {};
|
|
final Map<String, Size> _labelSizes = {};
|
|
final Map<String, String?> _measuredStatuses = {};
|
|
final GraphViewportController _viewport = GraphViewportController();
|
|
TextScaler? _scaler;
|
|
bool _rebuildScheduled = false;
|
|
|
|
@override
|
|
void dispose() {
|
|
_viewport.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
void didChangeDependencies() {
|
|
super.didChangeDependencies();
|
|
final scaler = MediaQuery.textScalerOf(context);
|
|
if (scaler != _scaler) {
|
|
_scaler = scaler;
|
|
_nodeSizes.clear();
|
|
_labelSizes.clear();
|
|
}
|
|
}
|
|
|
|
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(() {});
|
|
});
|
|
}
|
|
|
|
(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,
|
|
);
|
|
}
|
|
// The web's linear drawing: stages in one row, the ends below it.
|
|
final (layout, routing) = layoutLinearGraph(
|
|
nodes: nodes,
|
|
edges: edges,
|
|
extents: extents,
|
|
labelExtents: labelExtents,
|
|
);
|
|
return (layout, routing);
|
|
}
|
|
|
|
/// Executed evidence shown by the native node's recorded-status affordance.
|
|
///
|
|
/// A stage shows its latest recorded status (sequence order) verbatim. A
|
|
/// terminal has no step; it shows the recorded route outcome that resolved
|
|
/// there. Null means no recorded execution, so an unvisited node cannot be
|
|
/// mistaken for a visit.
|
|
String? _recordedStatus(
|
|
GraphNode node,
|
|
RunGraphState state,
|
|
Map<String, List<JsonMap>> visits,
|
|
) {
|
|
if (!state.isStageVisited(node.key)) return null;
|
|
if (!node.terminal) {
|
|
final stageVisits = visits[node.key];
|
|
if (stageVisits == null || stageVisits.isEmpty) return null;
|
|
final status = '${stageVisits.last['status'] ?? ''}'.trim();
|
|
return status.isEmpty ? null : status;
|
|
}
|
|
for (final route in state.takenRoutes) {
|
|
final parts = route.split('\u0000');
|
|
if (parts.length == 3 && parts[2] == node.key) return parts[1];
|
|
}
|
|
return null;
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final graph = widget.graph;
|
|
final nodes = graph == null ? const <GraphNode>[] : graphNodes(graph);
|
|
final edges = graph == null ? const <GraphEdge>[] : graphEdges(graph);
|
|
final style = GraphCanvasStyle.of(context);
|
|
final state = runGraphState(widget.run);
|
|
final rawSteps = widget.run['steps'];
|
|
final steps = rawSteps is List
|
|
? rawSteps.whereType<Map>().map(JsonMap.from).toList()
|
|
: const <JsonMap>[];
|
|
final visits = visitsByStage(steps);
|
|
final statuses = <String, String?>{
|
|
for (final node in nodes) node.key: _recordedStatus(node, state, visits),
|
|
};
|
|
// A status line changes the node's measured height. Drop only the stale
|
|
// extents so this frame never places a taller node in its old box; the
|
|
// offstage measurement reports the new extent and schedules the placement.
|
|
for (final entry in statuses.entries) {
|
|
if (_measuredStatuses[entry.key] != entry.value) {
|
|
_nodeSizes.remove(entry.key);
|
|
_measuredStatuses[entry.key] = entry.value;
|
|
}
|
|
}
|
|
_measuredStatuses.removeWhere((key, _) => !statuses.containsKey(key));
|
|
return LayoutBuilder(
|
|
builder: (context, constraints) {
|
|
assert(
|
|
constraints.hasBoundedWidth && constraints.hasBoundedHeight,
|
|
'RunGraphView needs a bounded viewport; constrain it, for example '
|
|
'with a SizedBox or an Expanded slot.',
|
|
);
|
|
if (nodes.isEmpty) return _message(runGraphMissing, style);
|
|
final geometry = _geometry(nodes, edges);
|
|
return Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
if (widget.showHeader)
|
|
RunGraphHeader(state: state, style: style, now: widget.now),
|
|
Expanded(
|
|
child: Stack(
|
|
fit: StackFit.expand,
|
|
children: [
|
|
if (geometry != null)
|
|
GraphViewport(
|
|
controller: _viewport,
|
|
contentSize: Size(geometry.$2.width, geometry.$2.height),
|
|
onExpand: !widget.expandable
|
|
? null
|
|
: () => _openExpanded(
|
|
nodes,
|
|
geometry.$1,
|
|
geometry.$2,
|
|
state,
|
|
statuses,
|
|
),
|
|
child: _runCanvas(
|
|
nodes: nodes,
|
|
layout: geometry.$1,
|
|
routing: geometry.$2,
|
|
state: state,
|
|
style: style,
|
|
statuses: statuses,
|
|
selectedStage: widget.selectedStage,
|
|
onStageSelected: widget.onStageSelected,
|
|
),
|
|
)
|
|
else
|
|
const SizedBox.shrink(),
|
|
RunGraphMeasurements(
|
|
nodes: nodes,
|
|
edges: edges,
|
|
maxNodeWidth: constraints.maxWidth,
|
|
labelStyle: style.labelStyle,
|
|
recordedStatuses: statuses,
|
|
onNodeSize: (key, size) => _record(_nodeSizes, key, size),
|
|
onLabelSize: (key, size) => _record(_labelSizes, key, size),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
if (geometry != null && geometry.$2.skipped.isNotEmpty)
|
|
Flexible(
|
|
child: SingleChildScrollView(
|
|
child: _message(
|
|
[
|
|
for (final diagnostic in geometry.$2.skipped)
|
|
'${diagnostic.source} → ${diagnostic.outcome}: '
|
|
'${diagnostic.message}',
|
|
].join('\n'),
|
|
style,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
Widget _message(String message, GraphCanvasStyle style) => Padding(
|
|
padding: const EdgeInsets.all(24),
|
|
child: Align(
|
|
alignment: AlignmentDirectional.topStart,
|
|
child: Text(message, style: style.messageStyle),
|
|
),
|
|
);
|
|
|
|
/// The same measured run canvas at any viewport size and selection, used by
|
|
/// the inline viewport and the expanded presentation.
|
|
Widget _runCanvas({
|
|
required List<GraphNode> nodes,
|
|
required GraphLayout layout,
|
|
required GraphRouting routing,
|
|
required RunGraphState state,
|
|
required GraphCanvasStyle style,
|
|
required Map<String, String?> statuses,
|
|
required String? selectedStage,
|
|
required ValueChanged<String> onStageSelected,
|
|
}) => RunGraphCanvas(
|
|
nodes: nodes,
|
|
layout: layout,
|
|
routing: routing,
|
|
state: state,
|
|
style: style,
|
|
recordedStatuses: statuses,
|
|
selectedStage: selectedStage,
|
|
onStageSelected: onStageSelected,
|
|
);
|
|
|
|
void _openExpanded(
|
|
List<GraphNode> nodes,
|
|
GraphLayout layout,
|
|
GraphRouting routing,
|
|
RunGraphState state,
|
|
Map<String, String?> statuses,
|
|
) {
|
|
openGraphExpanded(
|
|
context,
|
|
contentSize: Size(routing.width, routing.height),
|
|
title: runGraphExpandedTitle,
|
|
selectedStage: widget.selectedStage,
|
|
onSelected: widget.onStageSelected,
|
|
// The full-screen canvas draws solid and dashed routes; the legend that
|
|
// explains them must travel with it, as it does on the web.
|
|
header: RunGraphHeader(
|
|
state: state,
|
|
style: GraphCanvasStyle.of(context),
|
|
now: widget.now,
|
|
),
|
|
canvasBuilder: (context, selected, onSelected) => _runCanvas(
|
|
nodes: nodes,
|
|
layout: layout,
|
|
routing: routing,
|
|
state: state,
|
|
style: GraphCanvasStyle.of(context),
|
|
statuses: statuses,
|
|
selectedStage: selected,
|
|
onStageSelected: onSelected,
|
|
),
|
|
);
|
|
}
|
|
}
|