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'; import 'run_detail_visit.dart'; import 'run_detail_visit_facts.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 that visit's pane opens. 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, required this.configuredEndpoint, }); 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; /// The configured server origin used to resolve a recorded session export /// through the landed safe-link helper. Blank leaves relative urls unresolved. final String configuredEndpoint; @override State createState() => _RunDetailTabsState(); } class _RunDetailTabsState extends State { 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 index = latestDetailVisitIndex(widget.run['steps'], stageKey); if (index == null) return; setState(() { _selectedStage = stageKey; _selectedVisit = index; _tab = RunDetailTab.timeline; }); } /// Opens one recorded timeline row. The row index is also the index into the /// detail's own steps, so the pane reads exactly the visit it came from. void _openVisit(int index) { if (detailVisitStepAt(widget.run, index) == null) return; setState(() => _selectedVisit = index); } void _closeVisit() => setState(() => _selectedVisit = null); @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: final visits = detailTimeline(widget.run['steps']); final index = _selectedVisit; if (index != null && index >= 0 && index < visits.length) { final step = detailVisitStepAt(widget.run, index); if (step != null) { return KeyedSubtree( key: const Key('run-detail-tab-body-timeline'), child: RunVisitPane( run: widget.run, visit: visits[index], visitIndex: index, step: step, runSteps: detailRunSteps(widget.run), configuredEndpoint: widget.configuredEndpoint, cupertino: widget.cupertino, onClose: _closeVisit, ), ); } } return RunTimelineList( visits: visits, selectedSequence: index != null && index >= 0 && index < visits.length ? visits[index].sequence : null, cupertino: widget.cupertino, onSelected: _openVisit, ); } } /// 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); } }