import '../../../ui/theme/app_theme.dart'; import 'package:flutter/cupertino.dart'; 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/adaptive/host.dart'; import '../../../ui/layout/sticky_section.dart'; import '../../../ui/text/monospace.dart'; /// The Summary tab: the run's identity, the recorded facts as key/value rows, /// the stored original prompt with its copy control and the collapsible Input /// and Output sections. /// /// The web run detail has no usage block, so no metrics are drawn here. 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 cupertino, 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. final facts = [ for (final row in rows) if (!row.block) row, ]; return CustomScrollView( key: const Key('run-detail-tab-body-summary'), slivers: [ if (graphPreview != null) SliverToBoxAdapter(child: graphPreview), SliverToBoxAdapter( child: cupertino ? _cupertinoIdentity(context, identity) : 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, ), ), ), ), if (facts.isNotEmpty) SliverToBoxAdapter( child: cupertino ? CupertinoListSection.insetGrouped( backgroundColor: AppColors.groupedBackground, children: [_summaryFacts(context, facts, cupertino: true)], ) : _summaryFacts(context, facts, cupertino: false), ), // 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: cupertino ? _cupertinoBlock(context, row) : _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, cupertino: cupertino, textBuilder: cupertino ? _CupertinoSelectableText.new : null, ), ), _StickyField( key: const ValueKey('run-detail-summary-output'), title: 'Output', controller: outputController, copyText: runFieldCopyText(run['output']), body: RunFieldBody( value: run['output'], empty: visitEmptyOutput, cupertino: cupertino, textBuilder: cupertino ? _CupertinoSelectableText.new : null, ), ), const SliverToBoxAdapter(child: SizedBox(height: 16)), ], ); } /// A summary section that starts closed, follows its [controller] (so 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, required this.body, this.controller, this.copyText, }); final String title; final Widget body; 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 cupertino = usesCupertinoHost(context); final copyText = widget.copyText; return ListenableBuilder( listenable: _controller, builder: (context, _) => StickySection( title: widget.title, open: _controller.isExpanded, onToggle: () => _controller.isExpanded ? _controller.collapse() : _controller.expand(), trailing: [ if (copyText != null && copyText.isNotEmpty) cupertino ? CupertinoButton( padding: const EdgeInsets.symmetric(horizontal: 8), onPressed: _copy, child: Icon( _copied ? CupertinoIcons.checkmark_alt : CupertinoIcons.doc_on_doc, semanticLabel: 'Copy ${widget.title}', ), ) : IconButton( tooltip: 'Copy ${widget.title}', onPressed: _copy, icon: Icon(_copied ? Icons.check : Icons.copy), ), ], body: Padding( padding: const EdgeInsets.fromLTRB(16, 0, 16, 16), child: widget.body, ), ), ); } } /// 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, required bool cupertino, }) { // The recorded fields by name, as values, never raw JSON. final body = RunFieldBody( value: value, empty: empty, cupertino: cupertino, textBuilder: cupertino ? _CupertinoSelectableText.new : null, ); if (cupertino) { return ListView( key: Key('run-detail-tab-body-$tabKey'), children: [ CupertinoListSection.insetGrouped( backgroundColor: AppColors.groupedBackground, children: [Padding(padding: const EdgeInsets.all(16), child: body)], ), ], ); } return ListView( key: Key('run-detail-tab-body-$tabKey'), padding: const EdgeInsets.all(16), children: [body], ); } // --- Material -------------------------------------------------------------- /// The recorded facts as a compact grid: two short facts per line on a phone, /// three when wider; a long or monospace value (a path, a hash) takes a whole /// line so it is never squeezed. Widget _summaryFacts( BuildContext context, List rows, { required bool cupertino, }) { final labelStyle = cupertino ? CupertinoTheme.of(context).textTheme.tabLabelTextStyle.copyWith( color: CupertinoColors.secondaryLabel.resolveFrom(context), ) : Theme.of(context).textTheme.bodySmall?.copyWith( color: Theme.of(context).colorScheme.onSurfaceVariant, ); final TextStyle valueStyle = (cupertino ? CupertinoTheme.of(context).textTheme.textStyle : Theme.of(context).textTheme.bodyMedium) ?? const TextStyle(); return Padding( key: const Key('run-detail-summary-facts'), padding: const EdgeInsets.fromLTRB(16, 12, 16, 12), child: LayoutBuilder( builder: (context, constraints) { const gap = 16.0; final columns = constraints.maxWidth >= 600 ? 3 : 2; final cell = (constraints.maxWidth - gap * (columns - 1)) / columns; return Wrap( spacing: gap, runSpacing: 12, children: [ for (final row in rows) SizedBox( key: ValueKey('run-detail-summary-${_slug(row.label)}'), width: row.monospace || row.value.length > 28 ? constraints.maxWidth : cell, child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text(row.label, style: labelStyle), const SizedBox(height: 2), cupertino ? Text( row.value, style: row.monospace ? valueStyle.copyWith( fontFamily: appMonospaceFamily, ) : valueStyle, ) : SelectableText( row.value, style: row.monospace ? valueStyle.copyWith( fontFamily: appMonospaceFamily, ) : valueStyle, ), ], ), ), ], ); }, ), ); } /// 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, ), ), ); } // --- Cupertino ------------------------------------------------------------- Widget _cupertinoIdentity(BuildContext context, String identity) { final theme = CupertinoTheme.of(context); return Padding( padding: const EdgeInsetsDirectional.fromSTEB(16, 12, 16, 8), child: Text( identity, style: theme.textTheme.tabLabelTextStyle.copyWith( color: CupertinoColors.secondaryLabel.resolveFrom(context), ), ), ); } /// A long recorded block (the Diagnostic) starts collapsed under its label. Widget _cupertinoBlock(BuildContext context, DetailSummaryRow row) { final theme = CupertinoTheme.of(context); return Container( width: double.infinity, padding: const EdgeInsets.all(12), decoration: BoxDecoration( color: CupertinoColors.tertiarySystemFill.resolveFrom(context), borderRadius: BorderRadius.circular(8), ), child: Text( row.value, style: theme.textTheme.textStyle.copyWith(fontFamily: appMonospaceFamily), ), ); } String _slug(String label) => label.toLowerCase().replaceAll(' ', '-'); /// A read-only Cupertino text control: plain, selectable text without a /// Material dependency, while keeping native selection and copy affordances. class _CupertinoSelectableText extends StatefulWidget { const _CupertinoSelectableText(this.text); final String text; @override State<_CupertinoSelectableText> createState() => _CupertinoSelectableTextState(); } class _CupertinoSelectableTextState extends State<_CupertinoSelectableText> { late final TextEditingController _controller = TextEditingController( text: widget.text, ); @override void didUpdateWidget(_CupertinoSelectableText oldWidget) { super.didUpdateWidget(oldWidget); if (widget.text != oldWidget.text) _controller.text = widget.text; } @override void dispose() { _controller.dispose(); super.dispose(); } @override Widget build(BuildContext context) => CupertinoTextField( controller: _controller, readOnly: true, maxLines: null, padding: EdgeInsets.zero, decoration: const BoxDecoration(), style: CupertinoTheme.of(context).textTheme.textStyle, ); }