From 4b1e74cf611ad585c3f24fb1ea1bbad60192c296 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Fri, 23 Oct 2026 17:09:57 +0100 Subject: [PATCH] feat(app): a file diff opens as its own page with previous and next file chevrons --- .../changes/widgets/changes_viewer.dart | 11 +- .../changes/widgets/file_diff_viewer.dart | 155 ++++++++++++++---- .../features/changes/changes_viewer_test.dart | 4 +- .../changes/diff_presentation_test.dart | 83 ++++++---- 4 files changed, 190 insertions(+), 63 deletions(-) diff --git a/app/lib/features/changes/widgets/changes_viewer.dart b/app/lib/features/changes/widgets/changes_viewer.dart index 83bf2f41..0059fc28 100644 --- a/app/lib/features/changes/widgets/changes_viewer.dart +++ b/app/lib/features/changes/widgets/changes_viewer.dart @@ -4,6 +4,7 @@ import 'package:flutter/material.dart'; import '../../../core/api_gateway.dart'; import '../domain/change_capture.dart'; import '../domain/change_wording.dart'; +import '../domain/file_change.dart'; import '../domain/run_changes.dart'; import 'capture_section.dart'; import 'change_style.dart'; @@ -169,7 +170,15 @@ class _ChangesViewerState extends State { StageCaptureSection( stage: visit, 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 [], + _query, + ), + ), ), ], ), diff --git a/app/lib/features/changes/widgets/file_diff_viewer.dart b/app/lib/features/changes/widgets/file_diff_viewer.dart index 2c1c186d..2cd35d6a 100644 --- a/app/lib/features/changes/widgets/file_diff_viewer.dart +++ b/app/lib/features/changes/widgets/file_diff_viewer.dart @@ -230,42 +230,137 @@ class _FileDiffViewerState extends State { Widget _scrollsWhenTooWide(Widget control) => SingleChildScrollView(scrollDirection: Axis.horizontal, child: control); -/// Open [file] in the platform's standard modal: a Material dialog on -/// Android/desktop/web, a Cupertino dialog with a popup surface on iOS/macOS. -Future showFileDiffViewer(BuildContext context, FileChange file) { - final cupertino = usesCupertino(context); - Widget buildDialog(BuildContext dialogContext) { +/// Open [file] on its own page, as the browser's file view. [files] is the +/// list it was picked from (the visit's shown files); the page steps through +/// them with previous/next chevrons at its bottom right. +Future showFileDiffViewer( + BuildContext context, + FileChange file, { + List? files, +}) { + final list = files == null || !files.contains(file) ? [file] : files; + final page = FileDiffPage(files: list, initialIndex: list.indexOf(file)); + return Navigator.of(context).push( + usesCupertino(context) + ? CupertinoPageRoute(builder: (_) => page) + : MaterialPageRoute(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 files; + final int initialIndex; + + @override + State createState() => _FileDiffPageState(); +} + +class _FileDiffPageState extends State { + 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( + key: ValueKey('file-diff-${file.path}'), 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) { - // A popup surface spans the window unless it is inset; on a phone the - // screen edge would clip its native rounded corners. - final width = MediaQuery.sizeOf(dialogContext).width; - return Theme( - // 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 CupertinoPageScaffold( + navigationBar: CupertinoNavigationBar(middle: Text(name)), + child: SafeArea( + child: Theme(data: hostTextTheme(context), child: body), ), ); } - 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(context: context, builder: buildDialog); - } - return showDialog(context: context, builder: buildDialog); } diff --git a/app/test/features/changes/changes_viewer_test.dart b/app/test/features/changes/changes_viewer_test.dart index 34689da7..c30a4446 100644 --- a/app/test/features/changes/changes_viewer_test.dart +++ b/app/test/features/changes/changes_viewer_test.dart @@ -294,7 +294,9 @@ void main() { await tester.tap(find.byKey(const ValueKey('change-1-lib/feature.dart'))); 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('Raw patch'), findsOneWidget); final rendered = tester diff --git a/app/test/features/changes/diff_presentation_test.dart b/app/test/features/changes/diff_presentation_test.dart index a522ec68..cf131ce0 100644 --- a/app/test/features/changes/diff_presentation_test.dart +++ b/app/test/features/changes/diff_presentation_test.dart @@ -273,38 +273,59 @@ void main() { expect(tester.takeException(), isNull); }); - testWidgets('a Cupertino phone dialog is inset from the screen edge', ( - tester, - ) async { - tester.view.physicalSize = const Size(390, 844); - tester.view.devicePixelRatio = 1; - addTearDown(tester.view.reset); - await tester.pumpWidget( - cupertinoHost( - ChangesViewer( - steps: [ - stepFixture( - fileChanges: captureFixture( - files: [ - fileChangeFixture( - path: 'lib/feature.dart', - patch: multiHunkPatch, - ), - ], - ), + for (final cupertino in [false, true]) { + testWidgets('a file opens as a page that steps to the previous and next ' + 'file (${cupertino ? 'cupertino' : 'material'})', (tester) async { + tester.view.physicalSize = const Size(390, 844); + tester.view.devicePixelRatio = 1; + addTearDown(tester.view.reset); + final viewer = ChangesViewer( + steps: [ + stepFixture( + fileChanges: captureFixture( + files: [ + fileChangeFixture( + path: 'lib/first.dart', + patch: multiHunkPatch, + ), + fileChangeFixture( + path: 'lib/second.dart', + patch: multiHunkPatch, + ), + ], ), - ], - ), - ), - ); - await tester.tap(find.byKey(const ValueKey('change-1-lib/feature.dart'))); - await tester.pumpAndSettle(); - final rect = tester.getRect(find.byType(CupertinoPopupSurface)); - expect(rect.width, lessThanOrEqualTo(390 - 24)); - expect(rect.left, greaterThan(8)); - expect(rect.right, lessThan(382)); - expect(tester.takeException(), isNull); - }); + ), + ], + ); + await tester.pumpWidget( + cupertino ? cupertinoHost(viewer) : materialHost(viewer), + ); + await tester.tap(find.byKey(const ValueKey('change-1-lib/first.dart'))); + await tester.pumpAndSettle(); + + Finder control(String label) => + 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( path: 'lib/feature.dart',