Files
pipelines/app/lib/features/runs/ui/run_detail_tabs.dart
T

175 lines
5.8 KiB
Dart

import 'dart:math' as math;
import 'package:flutter/material.dart';
import '../../../core/api_gateway.dart';
import '../../graph/widgets/run_graph_view.dart';
import 'run_detail_content.dart';
import 'run_detail_tab.dart';
import 'run_detail_tab_bodies.dart';
import 'run_detail_tab_strip.dart';
import 'run_detail_timeline.dart';
/// The least height the selected tab keeps for its own content.
///
/// A header and tab strip that would leave the tab less than this may scroll
/// instead, so scaled title text or a short window cannot squeeze the Graph
/// canvas away. The Graph view's own legend is one wrapped row pair; this
/// reserve keeps its bounded canvas positive at 320px with 2x text.
const double kRunDetailMinContentHeight = 240;
/// The run detail's tabbed body: the host-built header and platform tab strip
/// above the bounded selected tab.
///
/// The Graph tab fills its slot with [RunGraphView] from the run's own pinned
/// graph and recorded steps. A visited stage click selects that stage's latest
/// recorded visit and switches to Timeline, where the row is highlighted. A
/// node with no recorded visit reports nothing, so no visit is invented.
class RunDetailTabs extends StatefulWidget {
const RunDetailTabs({
super.key,
required this.run,
required this.header,
required this.cupertino,
required this.copying,
required this.copyStatus,
required this.onCopy,
});
final JsonMap run;
/// The host's own header: identity, status and the run actions. It stays
/// outside the tab body, so switching tabs never drops an action.
final Widget header;
final bool cupertino;
final bool copying;
final String? copyStatus;
final VoidCallback onCopy;
@override
State<RunDetailTabs> createState() => _RunDetailTabsState();
}
class _RunDetailTabsState extends State<RunDetailTabs> {
RunDetailTab _tab = RunDetailTab.summary;
String? _selectedStage;
int? _selectedVisit;
@override
void didUpdateWidget(covariant RunDetailTabs oldWidget) {
super.didUpdateWidget(oldWidget);
// A different run is a different detail: its visits and graph do not carry
// the previous run's selection.
if ('${widget.run['id']}' != '${oldWidget.run['id']}') {
_tab = RunDetailTab.summary;
_selectedStage = null;
_selectedVisit = null;
}
}
/// Selects the stage's latest recorded visit. An unvisited stage has none,
/// so the detail stays where it is rather than fabricating a visit.
void _openStage(String stageKey) {
final visit = latestDetailVisit(widget.run['steps'], stageKey);
if (visit == null) return;
setState(() {
_selectedStage = stageKey;
_selectedVisit = visit.sequence;
_tab = RunDetailTab.timeline;
});
}
@override
Widget build(BuildContext context) {
return LayoutBuilder(
builder: (context, constraints) {
final headerLimit = _headerLimit(constraints);
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
ConstrainedBox(
constraints: BoxConstraints(maxHeight: headerLimit),
child: SingleChildScrollView(
key: ValueKey('run-detail-header-${widget.run['id']}'),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
widget.header,
RunDetailTabStrip(
selected: _tab,
onSelected: (tab) => setState(() => _tab = tab),
),
],
),
),
),
// Every tab gets the bounded remaining height. The scrolling tabs
// own their content; the Graph tab fills the slot so its canvas is
// never asked for an unbounded extent.
Expanded(
key: ValueKey('run-detail-body-${widget.run['id']}'),
child: _body(),
),
],
);
},
);
}
Widget _body() {
switch (_tab) {
case RunDetailTab.summary:
return runDetailSummaryTab(
context: context,
run: widget.run,
cupertino: widget.cupertino,
copying: widget.copying,
copyStatus: widget.copyStatus,
onCopy: widget.onCopy,
);
case RunDetailTab.graph:
return RunGraphView(
key: const Key('run-detail-graph'),
graph: detailGraph(widget.run),
run: widget.run,
selectedStage: _selectedStage,
onStageSelected: _openStage,
);
case RunDetailTab.input:
return runDetailFieldTab(
tabKey: 'input',
value: widget.run['input'],
empty: 'No input recorded.',
cupertino: widget.cupertino,
);
case RunDetailTab.output:
return runDetailFieldTab(
tabKey: 'output',
value: widget.run['output'],
empty: 'No output recorded.',
cupertino: widget.cupertino,
);
case RunDetailTab.timeline:
return RunTimelineList(
visits: detailTimeline(widget.run['steps']),
selectedSequence: _selectedVisit,
cupertino: widget.cupertino,
);
}
}
/// The header keeps its natural height while at least
/// [kRunDetailMinContentHeight] remains for the tab, and never more than the
/// detail itself; past that it scrolls.
double _headerLimit(BoxConstraints constraints) {
if (!constraints.hasBoundedHeight) return double.infinity;
final height = constraints.maxHeight;
// Below twice the reserve the two shares meet at 40%, so the header stays
// reachable even when the detail is shorter than the reserve itself.
final floorShare = height * 0.4;
final reserve = height - kRunDetailMinContentHeight;
return math.max(floorShare, reserve).clamp(0.0, height);
}
}