Show a chat turn's changed files as a chip that opens its diff

The answer footer shows '3 files changed +9 -2'; tapping opens the turn's
files in the sheet (wide) or a full-screen page (narrow), and a file opens
the run Changes diff viewer. Patches load through the chat's own gateway.
This commit is contained in:
2026-11-09 16:08:35 +00:00
parent 0696ab849a
commit 3374bea2ef
15 changed files with 1155 additions and 36 deletions
@@ -13,16 +13,22 @@ class ChatTurnChip extends StatelessWidget {
required this.label,
required this.color,
required this.semanticsLabel,
required this.sheet,
this.sheet,
this.open,
this.ellipsizeLabel = false,
});
}) : assert(sheet != null || open != null);
final Key chipKey;
final Widget leading;
final String label;
final Color color;
final String semanticsLabel;
final Widget sheet;
/// The native sheet the chip opens, unless [open] presents it instead.
final Widget? sheet;
/// A custom presentation (a full-screen route on a narrow window).
final Future<void> Function(BuildContext context, bool cupertino)? open;
/// Shrink a long label to one ellipsized line. Only for a chip whose parent
/// bounds its width (a turn footer's Wrap, an aligned row).
@@ -89,18 +95,29 @@ class ChatTurnChip extends StatelessWidget {
);
Future<void> _open(BuildContext context, bool cupertino) {
if (cupertino) {
return showCupertinoModalPopup<void>(
context: context,
builder: (_) => CupertinoPopupSurface(child: SafeArea(child: sheet)),
);
}
return showModalBottomSheet<void>(
context: context,
isScrollControlled: true,
showDragHandle: true,
useSafeArea: true,
builder: (_) => sheet,
);
if (open case final open?) return open(context, cupertino);
return showChatTurnSheet(context, cupertino, sheet!);
}
}
/// The native sheet a turn chip opens: a Cupertino popup surface or a
/// Material modal bottom sheet.
Future<void> showChatTurnSheet(
BuildContext context,
bool cupertino,
Widget sheet,
) {
if (cupertino) {
return showCupertinoModalPopup<void>(
context: context,
builder: (_) => CupertinoPopupSurface(child: SafeArea(child: sheet)),
);
}
return showModalBottomSheet<void>(
context: context,
isScrollControlled: true,
showDragHandle: true,
useSafeArea: true,
builder: (_) => sheet,
);
}
@@ -0,0 +1,76 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../../../core/api_gateway.dart';
import '../../../../shell/list_detail_policy.dart';
import '../../domain/chat_turn_diffs.dart';
import '../sessions/chat_adaptive.dart';
import 'chat_turn_chip.dart';
import 'chat_turn_diff_view.dart';
/// A turn's workspace changes below its answer, in the tool chip's family:
/// `3 files changed +42 −7`. A wide window opens the files in the turn sheet;
/// a narrow one opens them as a full-screen page. No diff text is inline.
class ChatTurnDiffChip extends StatelessWidget {
const ChatTurnDiffChip({
super.key,
required this.summary,
required this.load,
});
final JsonMap summary;
/// Fetches the turn's `chat_turn_diff` body through the chat's gateway.
final Future<JsonMap> Function() load;
@override
Widget build(BuildContext context) {
final cupertino =
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
final color = cupertino
? CupertinoColors.secondaryLabel.resolveFrom(context)
: Theme.of(context).colorScheme.onSurfaceVariant;
return ChatTurnChip(
chipKey: const Key('chat-turn-diff-chip'),
leading: Icon(
cupertino ? CupertinoIcons.doc_text : Icons.difference_outlined,
size: 14,
color: color,
),
label: chatTurnDiffLabel(summary),
color: color,
semanticsLabel: chatTurnDiffSemantics(summary),
ellipsizeLabel: true,
open: _open,
);
}
Future<void> _open(BuildContext context, bool cupertino) {
if (MediaQuery.sizeOf(context).width >= kListDetailMinWidth) {
return showChatTurnSheet(
context,
cupertino,
ChatTurnDiffView(summary: summary, load: load),
);
}
final view = ChatTurnDiffView(summary: summary, load: load, inSheet: false);
const title = Text('Turn changes');
return Navigator.of(context).push<void>(
cupertino
? CupertinoPageRoute<void>(
fullscreenDialog: true,
builder: (_) => CupertinoPageScaffold(
navigationBar: const CupertinoNavigationBar(middle: title),
child: SafeArea(child: view),
),
)
: MaterialPageRoute<void>(
fullscreenDialog: true,
builder: (_) => Scaffold(
appBar: AppBar(title: title),
body: SafeArea(child: view),
),
),
);
}
}
@@ -0,0 +1,154 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../../../core/api_gateway.dart';
import '../../../changes/changes.dart';
import '../../domain/chat_turn_diffs.dart';
import '../sessions/chat_adaptive.dart';
/// One chat turn's recorded file changes: the counts, an honest note when the
/// capture was partial or truncated, and one row per file that opens the
/// app's file diff viewer. It reads the changes through [load] (the chat's own
/// gateway) when it opens; the transcript never holds the patches.
class ChatTurnDiffView extends StatefulWidget {
const ChatTurnDiffView({
super.key,
required this.summary,
required this.load,
this.inSheet = true,
});
final JsonMap summary;
final Future<JsonMap> Function() load;
/// In a sheet the list shrinks to its content under a height bound; on a
/// full-screen page it fills the body.
final bool inSheet;
@override
State<ChatTurnDiffView> createState() => _ChatTurnDiffViewState();
}
class _ChatTurnDiffViewState extends State<ChatTurnDiffView> {
late Future<JsonMap> _reply = widget.load();
void _retry() {
final next = widget.load();
setState(() {
_reply = next;
});
}
@override
Widget build(BuildContext context) {
final cupertino =
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
final style = cupertino
? CupertinoTheme.of(context).textTheme.textStyle
: Theme.of(context).textTheme.bodyMedium;
final list = FutureBuilder<JsonMap>(
future: _reply,
builder: (context, snapshot) {
final children = <Widget>[
Padding(
padding: const EdgeInsets.only(bottom: 4),
// The run Changes stat form (+N -M), so it is coloured alike.
child: ChangeStatText(
chatTurnDiffHeading(widget.summary),
style: const TextStyle(fontWeight: FontWeight.w600),
),
),
..._body(context, snapshot, cupertino),
];
return ListView(
key: const Key('chat-turn-diff-list'),
shrinkWrap: widget.inSheet,
padding: const EdgeInsets.fromLTRB(16, 8, 16, 16),
children: children,
);
},
);
// File rows may be Material list tiles inside a Cupertino surface.
final content = Material(
type: MaterialType.transparency,
child: DefaultTextStyle.merge(style: style, child: list),
);
if (!widget.inSheet) return content;
return ConstrainedBox(
constraints: BoxConstraints(
maxHeight: MediaQuery.sizeOf(context).height * 0.8,
),
child: content,
);
}
List<Widget> _body(
BuildContext context,
AsyncSnapshot<JsonMap> snapshot,
bool cupertino,
) {
if (snapshot.connectionState != ConnectionState.done) {
return [
Padding(
padding: const EdgeInsets.all(16),
child: Center(
child: cupertino
? const CupertinoActivityIndicator()
: const CircularProgressIndicator(),
),
),
];
}
if (snapshot.hasError) {
return [
Text('Could not load the changes: ${snapshot.error}'),
Align(
alignment: AlignmentDirectional.centerStart,
child: cupertino
? CupertinoButton(onPressed: _retry, child: const Text('Retry'))
: TextButton(onPressed: _retry, child: const Text('Retry')),
),
];
}
final changes = snapshot.data!['changes'];
final capture = ChangeCapture.fromJson(
Map<String, dynamic>.from(changes as Map),
);
final truncated =
chatTurnDiffTruncated(widget.summary) ||
!capture.complete ||
capture.omitted > 0;
return [
if (truncated)
Padding(
key: const Key('chat-turn-diff-truncated'),
padding: const EdgeInsets.only(bottom: 8),
child: Text(
chatTurnDiffTruncationNote(capture.diagnostic),
style: TextStyle(
fontSize: 13,
color: cupertino
? CupertinoColors.secondaryLabel.resolveFrom(context)
: Theme.of(context).colorScheme.onSurfaceVariant,
),
),
),
for (final file in capture.files)
ChangeFileRow(
key: ValueKey<String>('chat-turn-diff-file-${file.path}'),
file: file,
onOpen: (file) =>
showFileDiffViewer(context, file, files: capture.files),
),
if (capture.omitted > 0)
Padding(
padding: const EdgeInsets.only(top: 8),
child: Text(
'${capture.omitted} more '
'${capture.omitted == 1 ? 'file was' : 'files were'} '
'omitted by the size limit.',
),
),
];
}
}