330 lines
11 KiB
Dart
330 lines
11 KiB
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/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 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.
|
|
// 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<DetailSummaryRow> 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: 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,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
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: 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)),
|
|
const SliverFloatingBarClearance(),
|
|
],
|
|
);
|
|
}
|
|
|
|
/// 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<void> _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,
|
|
);
|
|
// 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,
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
// --- 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,
|
|
);
|
|
}
|