Files
pipelines/app/lib/features/pipelines/widgets/pipeline_graph_tab.dart
T
alan d1b05217c9 Remove Cupertino pipeline branches
Keep the pipeline feature on its existing Material path because the app runs under MaterialApp and never selects Cupertino hosts. Preserve public cupertino parameters and required Cupertino icon values for later cleanup and shared controls.
2026-11-11 04:53:12 +00:00

216 lines
7.9 KiB
Dart

import 'dart:math' as math;
import 'package:flutter/material.dart';
import '../../graph/widgets/definition_graph_view.dart';
import '../pipeline_models.dart';
import 'pipeline_detail_styles.dart';
/// The Graph tab's guidance note. Gestures stay the host's own: wheel or pinch
/// zoom, drag pan, and an ordinary tap/click selects a node.
const String pipelineGraphNote =
'Scroll or pinch to zoom, drag to pan, select a stage for details. '
'Edges are labelled with their outcome; error* is the implicit fallback.';
/// A reply that carried no definition graph: nothing is reconstructed from the
/// source and no empty graph stands in for the missing definition.
const String pipelineGraphMissing =
'This reply did not include the pipeline definition graph, so it cannot be '
'drawn. No graph has been reconstructed from the source.';
/// The most of the tab's slot the guidance note may take.
///
/// The graph is the tab's content and the note is guidance, so the note starts
/// from one third. When the whole note does not fit it keeps whole lines only
/// and scrolls behind a native, visible scrollbar instead of ending on a
/// half-drawn row. A share shorter than two lines grows to two whole lines (one
/// line for very large text), and never past half the tab, so the canvas always
/// keeps at least half of it.
const double pipelineGraphNoteShare = 1 / 3;
/// The detail Graph tab: the pinned definition as a bounded native canvas.
///
/// The graph comes from [PipelineDetail.graph] exactly as the reply pinned it
/// for the selected version. Selecting any node, including a terminal, reports
/// its key so the host can open the landed full-screen stage-definition page
/// for that same pipeline id, name and resolved version. No run evidence is
/// read, so nodes and terminals can never look visited.
class PipelineGraphTab extends StatelessWidget {
const PipelineGraphTab({
super.key,
required this.detail,
required this.selectedStage,
required this.onSelected,
});
final PipelineDetail detail;
/// The selected node key, kept by the detail panel so it survives a tab
/// switch. Null means nothing is selected.
final String? selectedStage;
/// Reports the exact node key activated, including `#success`/`#failure`.
final ValueChanged<String> onSelected;
@override
Widget build(BuildContext context) {
final graph = detail.graph;
if (graph == null) {
return ListView(
key: const Key('pipeline-graph-missing'),
padding: const EdgeInsets.all(16),
children: [
Text(pipelineGraphMissing, style: pipelineMutedStyle(context)),
],
);
}
return LayoutBuilder(
builder: (context, constraints) {
// The panel gives this tab the remaining bounded height. Cap the note
// against that same slot so a short window or scaled text can only
// scroll the note, never overflow the column or leave the canvas at
// zero height.
final noteLimit = constraints.hasBoundedHeight
? constraints.maxHeight * pipelineGraphNoteShare
: double.infinity;
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
_GraphGuidanceNote(
maxHeight: noteLimit,
slotHeight: constraints.hasBoundedHeight
? constraints.maxHeight
: double.infinity,
),
Expanded(
child: DefinitionGraphView(
key: const Key('pipeline-definition-graph'),
graph: graph,
selectedStage: selectedStage,
onSelected: onSelected,
),
),
],
);
},
);
}
}
/// The guidance note, kept to a readable share of the tab without ever cutting
/// a text line.
///
/// An earlier cap let the note end on a partially drawn row with no hint that
/// more guidance existed. The note is measured with the same style, width and
/// text scale its [Text] will use, so the visible part is always a whole number
/// of lines, and the hidden rest scrolls behind the host's own scrollbar. That
/// keeps a visible affordance while the graph below keeps at least half the
/// slot.
class _GraphGuidanceNote extends StatefulWidget {
const _GraphGuidanceNote({required this.maxHeight, required this.slotHeight});
/// The note's starting share of the tab (see [pipelineGraphNoteShare]).
final double maxHeight;
/// The whole tab slot, so two whole lines may exceed [maxHeight] while the
/// canvas still keeps at least half of it.
final double slotHeight;
@override
State<_GraphGuidanceNote> createState() => _GraphGuidanceNoteState();
}
class _GraphGuidanceNoteState extends State<_GraphGuidanceNote> {
final ScrollController _scrollController = ScrollController();
@override
void dispose() {
_scrollController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
final style = DefaultTextStyle.of(
context,
).style.merge(pipelineMutedStyle(context));
return LayoutBuilder(
builder: (context, constraints) {
final painter = TextPainter(
text: TextSpan(text: pipelineGraphNote, style: style),
textDirection: Directionality.of(context),
textScaler: MediaQuery.textScalerOf(context),
)..layout(maxWidth: math.max(0, constraints.maxWidth - 32));
final totalHeight = painter.height;
final limit = _noteLimit(painter);
final truncated = totalHeight > limit + 0.5;
final visibleHeight = truncated
? _wholeLineHeight(painter, limit)
: totalHeight;
final text = Padding(
padding: EdgeInsets.fromLTRB(16, 0, 16, truncated ? 0 : 8),
child: Text(pipelineGraphNote, style: style),
);
return SizedBox(
key: const Key('pipeline-graph-note'),
height: truncated ? visibleHeight : totalHeight + 8,
child: truncated
? _scrollbar(
SingleChildScrollView(
key: const Key('pipeline-graph-note-scroll'),
controller: _scrollController,
primary: false,
padding: const EdgeInsets.only(bottom: 8),
child: text,
),
)
: text,
);
},
);
}
/// The note's slot: its [pipelineGraphNoteShare] of the tab, or two whole
/// lines when that share is shorter, and never more than half the tab so the
/// canvas always keeps the other half.
double _noteLimit(TextPainter painter) {
if (!widget.slotHeight.isFinite) return widget.maxHeight;
final lines = painter.computeLineMetrics();
final lineHeight = lines.isEmpty
? painter.preferredLineHeight
: lines.first.height;
return math.max(
widget.maxHeight,
math.min(lineHeight * 2, widget.slotHeight * 0.5),
);
}
/// The largest whole-line height that fits in [limit].
///
/// Whole lines keep the clipped edge from cutting through a glyph row. If
/// even the first line is taller than the limit the row is clipped anyway:
/// that only happens when the whole tab is barely two lines tall.
double _wholeLineHeight(TextPainter painter, double limit) {
final lines = painter.computeLineMetrics();
var consumed = 0.0;
for (final line in lines) {
final next = consumed + line.height;
if (next > limit + 0.5) break;
consumed = next;
}
if (consumed > 0 || lines.isEmpty) return consumed;
return math.min(lines.first.height, limit);
}
/// The host's own scrollbar: visible whenever the note is clipped, and
/// draggable with the pointer, so the rest is discoverable without a wheel.
Widget _scrollbar(Widget child) {
return Scrollbar(
key: const Key('pipeline-graph-note-scrollbar'),
controller: _scrollController,
thumbVisibility: true,
child: child,
);
}
}