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

372 lines
11 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.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/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.
if (cupertino) {
return ListView(
key: const Key('run-detail-tab-body-summary'),
children: [
?graphPreview,
_cupertinoIdentity(context, identity),
if (rows.any((row) => !row.block))
CupertinoListSection.insetGrouped(
children: [
_summaryFacts(context, [
for (final row in rows)
if (!row.block) row,
], cupertino: true),
],
),
for (final row in rows.where((row) => row.block))
CupertinoListSection.insetGrouped(
children: [_cupertinoBlock(context, row)],
),
_cupertinoFieldSection(
context,
key: 'run-detail-summary-input',
title: 'Input',
value: run['input'],
empty: visitEmptyInput,
controller: inputController,
),
_cupertinoFieldSection(
context,
key: 'run-detail-summary-output',
title: 'Output',
value: run['output'],
empty: visitEmptyOutput,
controller: outputController,
),
],
);
}
final theme = Theme.of(context);
return ListView(
key: const Key('run-detail-tab-body-summary'),
padding: const EdgeInsets.only(bottom: 16),
children: [
?graphPreview,
Padding(
padding: const EdgeInsets.fromLTRB(16, 12, 16, 0),
child: Text(
identity,
style: theme.textTheme.bodySmall?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
),
),
),
if (rows.any((row) => !row.block))
_summaryFacts(context, [
for (final row in rows)
if (!row.block) row,
], cupertino: false),
for (final row in rows)
if (row.block) _materialBlock(context, row),
_materialFieldSection(
context,
key: 'run-detail-summary-input',
title: 'Input',
value: run['input'],
empty: visitEmptyInput,
controller: inputController,
),
_materialFieldSection(
context,
key: 'run-detail-summary-output',
title: 'Output',
value: run['output'],
empty: visitEmptyOutput,
controller: outputController,
),
],
);
}
/// 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(
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<DetailSummaryRow> 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 ExpansionTile(
key: ValueKey('run-detail-summary-${_slug(row.label)}'),
title: Text(row.label),
expandedCrossAxisAlignment: CrossAxisAlignment.start,
childrenPadding: const EdgeInsetsDirectional.fromSTEB(16, 0, 16, 16),
children: [
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,
),
),
),
],
);
}
Widget _materialFieldSection(
BuildContext context, {
required String key,
required String title,
required dynamic value,
required String empty,
ExpansibleController? controller,
}) {
return ExpansionTile(
key: ValueKey(key),
controller: controller,
title: Text(title),
expandedCrossAxisAlignment: CrossAxisAlignment.start,
childrenPadding: const EdgeInsetsDirectional.fromSTEB(16, 0, 16, 16),
children: [RunFieldBody(value: value, empty: empty, cupertino: false)],
);
}
// --- 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 CupertinoExpansionTile(
key: ValueKey('run-detail-summary-${_slug(row.label)}'),
title: Text(row.label),
child: Padding(
padding: const EdgeInsetsDirectional.fromSTEB(16, 0, 16, 16),
child: 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,
),
),
),
),
);
}
Widget _cupertinoFieldSection(
BuildContext context, {
required String key,
required String title,
required dynamic value,
required String empty,
ExpansibleController? controller,
}) {
return CupertinoListSection.insetGrouped(
key: ValueKey(key),
children: [
CupertinoExpansionTile(
title: Text(title),
controller: controller,
child: Padding(
padding: const EdgeInsetsDirectional.fromSTEB(16, 0, 16, 16),
child: Align(
alignment: AlignmentDirectional.centerStart,
child: RunFieldBody(
value: value,
empty: empty,
cupertino: true,
textBuilder: _CupertinoSelectableText.new,
),
),
),
),
],
);
}
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,
);
}