merge(app): land diff page

This commit is contained in:
2026-10-23 17:10:59 +01:00
4 changed files with 190 additions and 63 deletions
@@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
import '../../../core/api_gateway.dart'; import '../../../core/api_gateway.dart';
import '../domain/change_capture.dart'; import '../domain/change_capture.dart';
import '../domain/change_wording.dart'; import '../domain/change_wording.dart';
import '../domain/file_change.dart';
import '../domain/run_changes.dart'; import '../domain/run_changes.dart';
import 'capture_section.dart'; import 'capture_section.dart';
import 'change_style.dart'; import 'change_style.dart';
@@ -169,7 +170,15 @@ class _ChangesViewerState extends State<ChangesViewer> {
StageCaptureSection( StageCaptureSection(
stage: visit, stage: visit,
query: _query, query: _query,
onOpen: (file) => showFileDiffViewer(context, file), // The page steps through the files this visit shows.
onOpen: (file) => showFileDiffViewer(
context,
file,
files: filterFileChanges(
visit.capture?.files ?? const <FileChange>[],
_query,
),
),
), ),
], ],
), ),
@@ -230,42 +230,137 @@ class _FileDiffViewerState extends State<FileDiffViewer> {
Widget _scrollsWhenTooWide(Widget control) => Widget _scrollsWhenTooWide(Widget control) =>
SingleChildScrollView(scrollDirection: Axis.horizontal, child: control); SingleChildScrollView(scrollDirection: Axis.horizontal, child: control);
/// Open [file] in the platform's standard modal: a Material dialog on /// Open [file] on its own page, as the browser's file view. [files] is the
/// Android/desktop/web, a Cupertino dialog with a popup surface on iOS/macOS. /// list it was picked from (the visit's shown files); the page steps through
Future<void> showFileDiffViewer(BuildContext context, FileChange file) { /// them with previous/next chevrons at its bottom right.
final cupertino = usesCupertino(context); Future<void> showFileDiffViewer(
Widget buildDialog(BuildContext dialogContext) { BuildContext context,
FileChange file, {
List<FileChange>? files,
}) {
final list = files == null || !files.contains(file) ? [file] : files;
final page = FileDiffPage(files: list, initialIndex: list.indexOf(file));
return Navigator.of(context).push<void>(
usesCupertino(context)
? CupertinoPageRoute<void>(builder: (_) => page)
: MaterialPageRoute<void>(builder: (_) => page),
);
}
/// One recorded file's diff as a page, with the previous and next file of the
/// list it came from.
class FileDiffPage extends StatefulWidget {
const FileDiffPage({super.key, required this.files, this.initialIndex = 0});
final List<FileChange> files;
final int initialIndex;
@override
State<FileDiffPage> createState() => _FileDiffPageState();
}
class _FileDiffPageState extends State<FileDiffPage> {
late int _index = widget.initialIndex.clamp(0, widget.files.length - 1);
void _step(int delta) => setState(() => _index += delta);
@override
Widget build(BuildContext context) {
final cupertino = usesCupertino(context);
final file = widget.files[_index];
final name = file.path.split('/').last;
final count = widget.files.length;
final viewer = FileDiffViewer( final viewer = FileDiffViewer(
key: ValueKey<String>('file-diff-${file.path}'),
file: file, file: file,
onClose: () => Navigator.of(dialogContext).pop(), );
final pager = count < 2
? null
: FileDiffPager(
index: _index,
count: count,
onPrevious: _index > 0 ? () => _step(-1) : null,
onNext: _index < count - 1 ? () => _step(1) : null,
);
final body = Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expanded(child: viewer),
?pager,
],
); );
if (cupertino) { if (cupertino) {
// A popup surface spans the window unless it is inset; on a phone the return CupertinoPageScaffold(
// screen edge would clip its native rounded corners. navigationBar: CupertinoNavigationBar(middle: Text(name)),
final width = MediaQuery.sizeOf(dialogContext).width; child: SafeArea(
return Theme( child: Theme(data: hostTextTheme(context), child: body),
// Dialogs open on the app Navigator, above the Changes page's own
// brightness-aware theme, so the popup needs its own.
data: hostTextTheme(dialogContext),
child: Padding(
padding: EdgeInsets.symmetric(
horizontal: width < 600 ? 16 : 40,
vertical: 24,
),
child: Center(
child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 900),
child: CupertinoPopupSurface(child: SafeArea(child: viewer)),
),
),
), ),
); );
} }
return Dialog(child: SafeArea(child: viewer)); return Scaffold(
appBar: AppBar(title: Text(name)),
body: SafeArea(top: false, child: body),
);
}
}
/// Previous and next as chevrons in one row at the bottom right, with the
/// position between them read as `2 of 5`.
class FileDiffPager extends StatelessWidget {
const FileDiffPager({
super.key,
required this.index,
required this.count,
this.onPrevious,
this.onNext,
});
final int index;
final int count;
final VoidCallback? onPrevious;
final VoidCallback? onNext;
@override
Widget build(BuildContext context) {
final cupertino = usesCupertino(context);
Widget button(
IconData material,
IconData ios,
String tip,
VoidCallback? on,
) => cupertino
? Semantics(
button: true,
label: tip,
child: CupertinoButton(
padding: const EdgeInsets.all(8),
onPressed: on,
child: Icon(ios),
),
)
: IconButton(onPressed: on, tooltip: tip, icon: Icon(material));
return Padding(
key: const ValueKey('file-diff-pager'),
padding: const EdgeInsets.fromLTRB(16, 4, 8, 8),
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Text('${index + 1} of $count'),
const SizedBox(width: 4),
button(
Icons.chevron_left,
CupertinoIcons.chevron_left,
'Previous file',
onPrevious,
),
button(
Icons.chevron_right,
CupertinoIcons.chevron_right,
'Next file',
onNext,
),
],
),
);
} }
if (cupertino) {
return showCupertinoDialog<void>(context: context, builder: buildDialog);
}
return showDialog<void>(context: context, builder: buildDialog);
} }
@@ -294,7 +294,9 @@ void main() {
await tester.tap(find.byKey(const ValueKey('change-1-lib/feature.dart'))); await tester.tap(find.byKey(const ValueKey('change-1-lib/feature.dart')));
await tester.pumpAndSettle(); await tester.pumpAndSettle();
expect(find.byType(Dialog), findsOneWidget); // A file opens on its own page, not a dialog.
expect(find.byType(FileDiffPage), findsOneWidget);
expect(find.byType(Dialog), findsNothing);
expect(find.text('Rendered'), findsOneWidget); expect(find.text('Rendered'), findsOneWidget);
expect(find.text('Raw patch'), findsOneWidget); expect(find.text('Raw patch'), findsOneWidget);
final rendered = tester final rendered = tester
@@ -273,38 +273,59 @@ void main() {
expect(tester.takeException(), isNull); expect(tester.takeException(), isNull);
}); });
testWidgets('a Cupertino phone dialog is inset from the screen edge', ( for (final cupertino in [false, true]) {
tester, testWidgets('a file opens as a page that steps to the previous and next '
) async { 'file (${cupertino ? 'cupertino' : 'material'})', (tester) async {
tester.view.physicalSize = const Size(390, 844); tester.view.physicalSize = const Size(390, 844);
tester.view.devicePixelRatio = 1; tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset); addTearDown(tester.view.reset);
await tester.pumpWidget( final viewer = ChangesViewer(
cupertinoHost( steps: [
ChangesViewer( stepFixture(
steps: [ fileChanges: captureFixture(
stepFixture( files: [
fileChanges: captureFixture( fileChangeFixture(
files: [ path: 'lib/first.dart',
fileChangeFixture( patch: multiHunkPatch,
path: 'lib/feature.dart', ),
patch: multiHunkPatch, fileChangeFixture(
), path: 'lib/second.dart',
], patch: multiHunkPatch,
), ),
],
), ),
], ),
), ],
), );
); await tester.pumpWidget(
await tester.tap(find.byKey(const ValueKey('change-1-lib/feature.dart'))); cupertino ? cupertinoHost(viewer) : materialHost(viewer),
await tester.pumpAndSettle(); );
final rect = tester.getRect(find.byType(CupertinoPopupSurface)); await tester.tap(find.byKey(const ValueKey('change-1-lib/first.dart')));
expect(rect.width, lessThanOrEqualTo(390 - 24)); await tester.pumpAndSettle();
expect(rect.left, greaterThan(8));
expect(rect.right, lessThan(382)); Finder control(String label) =>
expect(tester.takeException(), isNull); cupertino ? find.bySemanticsLabel(label) : find.byTooltip(label);
}); expect(find.byType(FileDiffPage), findsOneWidget);
expect(find.text('first.dart'), findsOneWidget);
expect(find.text('1 of 2'), findsOneWidget);
// The pager sits at the bottom right.
final pager = tester.getRect(
find.byKey(const ValueKey('file-diff-pager')),
);
expect(pager.bottom, greaterThan(844 - 120));
final next = tester.getRect(control('Next file'));
expect(next.right, greaterThan(390 - 80));
await tester.tap(control('Next file'));
await tester.pumpAndSettle();
expect(find.text('second.dart'), findsOneWidget);
expect(find.text('2 of 2'), findsOneWidget);
await tester.tap(control('Previous file'));
await tester.pumpAndSettle();
expect(find.text('first.dart'), findsOneWidget);
expect(tester.takeException(), isNull);
});
}
final diffFile = FileChange( final diffFile = FileChange(
path: 'lib/feature.dart', path: 'lib/feature.dart',