import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import '../../../core/api_gateway.dart'; import 'run_detail_summary_fields.dart'; import 'run_detail_visit_facts.dart'; import 'run_field_body.dart'; import '../../../ui/layout/facts_grid.dart'; import '../../../ui/layout/warnings_section.dart'; import '../../../ui/layout/sticky_section.dart'; import '../../../ui/text/live_elapsed_builder.dart'; import '../../../ui/text/monospace.dart'; import '../../../ui/adaptive/floating_bar_layout.dart'; /// The Summary tab: the run's identity, the recorded facts as one grid, /// the stored original prompt with its copy control and the collapsible Input /// and Output sections. /// /// The run's size and reported token usage are facts like the others. A fact /// the server did not record is omitted rather than shown as an invented or /// `Unavailable` row; only the Input/Output empty sentences remain, and only /// when that section recorded nothing. Widget runDetailSummaryTab({ required BuildContext context, required JsonMap run, required bool copying, required String? copyStatus, required VoidCallback onCopy, ExpansibleController? inputController, ExpansibleController? outputController, Widget? graphPreview, }) { final rows = detailSummaryRows(run); final identity = detailSummaryIdentity(run); // No separate "Original prompt": the run's Input carries it, as data. // The top bar's subtitle already names the run's pipeline (and its title // opens it), so a Pipeline fact would only repeat it. final pipelineInBar = '${run['name'] ?? ''}'.trim().isNotEmpty; List facts(DateTime now) => [ for (final row in detailSummaryRows(run, now: now)) if (!row.block && !(pipelineInBar && row.label == 'Pipeline')) row, ]; // An executing run's Duration ticks; only the facts grid rebuilds with it. final runningSince = detailRunningSince(run); return CustomScrollView( key: const Key('run-detail-tab-body-summary'), slivers: [ // Below the pane's top tabs at rest (nothing on a phone page); the // sticky sections pin under them. const SliverTopBarClearance(), if (graphPreview != null) SliverToBoxAdapter(child: graphPreview), SliverToBoxAdapter( child: Padding( padding: const EdgeInsets.fromLTRB(16, 12, 16, 0), child: Text( identity, style: Theme.of(context).textTheme.bodySmall?.copyWith( color: Theme.of(context).colorScheme.onSurfaceVariant, ), ), ), ), SliverToBoxAdapter( child: WarningsSection(messages: warningMessages(run['warnings'])), ), // The facts sit on the page as one grid, the same on both hosts: no // grouped card, so a single fact starts at the leading edge. SliverToBoxAdapter( child: LiveElapsedBuilder( since: runningSince, builder: (context, now) { final shown = facts(now); if (shown.isEmpty) return const SizedBox.shrink(); return FactsGrid( key: const Key('run-detail-summary-facts'), items: [ for (final row in shown) FactsGridItem( key: ValueKey('run-detail-summary-${_slug(row.label)}'), label: row.label, value: row.value, monospace: row.monospace, ), ], ); }, ), ), // Long blocks (the Diagnostic), Input and Output are sticky // collapsibles: each title stays pinned while its content scrolls. for (final row in rows) if (row.block) _StickyField( key: ValueKey('run-detail-summary-${_slug(row.label)}'), title: row.label, body: _materialBlock(context, row), ), _StickyField( key: const ValueKey('run-detail-summary-input'), title: 'Input', controller: inputController, copyText: runFieldCopyText(run['input']), body: RunFieldBody(value: run['input'], empty: visitEmptyInput), ), _StickyField( key: const ValueKey('run-detail-summary-output'), title: 'Output', controller: outputController, copyText: runFieldCopyText(run['output']), // Output starts open, so it is a sliver: a long one builds only the // fields on screen. sliverBody: RunFieldSliver( value: run['output'], empty: visitEmptyOutput, ), ), const SliverToBoxAdapter(child: SizedBox(height: 16)), const SliverFloatingBarClearance(), ], ); } /// A summary section that follows its [controller] (its open state starts from /// the controller's; a search hit can open it), and offers a copy of its /// recorded text. class _StickyField extends StatefulWidget { const _StickyField({ super.key, required this.title, this.body, this.sliverBody, this.controller, this.copyText, }) : assert((body == null) != (sliverBody == null)); final String title; /// The content as a box, or as [sliverBody] for a long one (see /// [StickySection.sliverBody]); exactly one is given. final Widget? body; final Widget? sliverBody; final ExpansibleController? controller; final String? copyText; @override State<_StickyField> createState() => _StickyFieldState(); } class _StickyFieldState extends State<_StickyField> { ExpansibleController? _own; bool _copied = false; ExpansibleController get _controller => widget.controller ?? (_own ??= ExpansibleController()); @override void dispose() { _own?.dispose(); super.dispose(); } Future _copy() async { await Clipboard.setData(ClipboardData(text: widget.copyText ?? '')); if (mounted) setState(() => _copied = true); } @override Widget build(BuildContext context) { final copyText = widget.copyText; return ListenableBuilder( listenable: _controller, builder: (context, _) { // The body's own padding, the same for the box and the sliver form. const padding = EdgeInsets.fromLTRB(16, 0, 16, 16); final box = widget.body; final sliver = widget.sliverBody; return StickySection( title: widget.title, open: _controller.isExpanded, onToggle: () => _controller.isExpanded ? _controller.collapse() : _controller.expand(), trailing: [ if (copyText != null && copyText.isNotEmpty) IconButton( tooltip: 'Copy ${widget.title}', onPressed: _copy, icon: Icon(_copied ? Icons.check : Icons.copy), ), ], body: box == null ? null : Padding(padding: padding, child: box), sliverBody: sliver == null ? null : SliverPadding(padding: padding, sliver: sliver), ); }, ); } } /// One recorded Input or Output field as its own tab, used by a visit pane. Widget runDetailFieldTab({ required String tabKey, required dynamic value, required String empty, }) { // The recorded fields by name, as values, never raw JSON. final body = RunFieldBody(value: value, empty: empty); // Flat on the page on both hosts, like the Summary. return Builder( builder: (context) => ListView( key: Key('run-detail-tab-body-$tabKey'), padding: withBarClearance(context, const EdgeInsets.all(16)), children: [body], ), ); } // --- Material -------------------------------------------------------------- /// A long recorded block (the Diagnostic) starts collapsed under its label. Widget _materialBlock(BuildContext context, DetailSummaryRow row) { final theme = Theme.of(context); return Container( width: double.infinity, padding: const EdgeInsets.all(12), decoration: BoxDecoration( color: theme.colorScheme.surfaceContainerHighest, borderRadius: BorderRadius.circular(8), ), child: SelectableText( row.value, style: theme.textTheme.bodySmall?.copyWith( fontFamily: appMonospaceFamily, ), ), ); } String _slug(String label) => label.toLowerCase().replaceAll(' ', '-');