diff --git a/app/lib/features/chat/screen.dart b/app/lib/features/chat/screen.dart index c8049662..559b4b4f 100644 --- a/app/lib/features/chat/screen.dart +++ b/app/lib/features/chat/screen.dart @@ -256,6 +256,7 @@ class _ChatScreenState extends State { if (!beside) return list; return ListDetailPanes( list: list, + detailIdentity: selected, detail: selected == null ? const EmptyDetailPane( destination: AppDestination.chat, diff --git a/app/lib/features/pipelines/screen.dart b/app/lib/features/pipelines/screen.dart index 174920af..cfc8d093 100644 --- a/app/lib/features/pipelines/screen.dart +++ b/app/lib/features/pipelines/screen.dart @@ -255,6 +255,7 @@ class _PipelinesScreenState extends State { list: ContentWidth( child: PipelineListPanel(controller: _controller), ), + detailIdentity: _controller.selectedId, detail: _controller.hasSelection ? DetailPane( key: const ValueKey('pipeline-detail-pane'), diff --git a/app/lib/features/runs/screen.dart b/app/lib/features/runs/screen.dart index 805044a3..d0784ea7 100644 --- a/app/lib/features/runs/screen.dart +++ b/app/lib/features/runs/screen.dart @@ -277,6 +277,7 @@ class _RunsScreenState extends State { if (narrow) return list; return ListDetailPanes( list: list, + detailIdentity: controller.selectedRunId, detail: selected ? DetailPane( key: const ValueKey('run-detail-pane'), diff --git a/app/lib/shell/detail_pane_navigator.dart b/app/lib/shell/detail_pane_navigator.dart index 62e87203..ede2e21f 100644 --- a/app/lib/shell/detail_pane_navigator.dart +++ b/app/lib/shell/detail_pane_navigator.dart @@ -12,12 +12,17 @@ import '../ui/adaptive/host.dart'; /// /// The item is this navigator's first page under a fixed key, so a rebuilt /// item updates in place and is never remounted. The system Back pops a page -/// pushed here before it leaves the item. +/// pushed here before it leaves the item. Opening a different item +/// ([identity] changes) closes any page the previous item had pushed, so the +/// pane shows the newly selected item. class DetailPaneNavigator extends StatefulWidget { - const DetailPaneNavigator({super.key, required this.child}); + const DetailPaneNavigator({super.key, required this.child, this.identity}); final Widget child; + /// Which item the pane shows (a run, chat or pipeline id). + final Object? identity; + @override State createState() => _DetailPaneNavigatorState(); } @@ -26,6 +31,14 @@ class _DetailPaneNavigatorState extends State { final GlobalKey _navigator = GlobalKey(); static const _rootKey = ValueKey('list-detail-root'); + @override + void didUpdateWidget(covariant DetailPaneNavigator oldWidget) { + super.didUpdateWidget(oldWidget); + if (oldWidget.identity != widget.identity) { + _navigator.currentState?.popUntil((route) => route.isFirst); + } + } + @override Widget build(BuildContext context) { final Page root = usesCupertinoHost(context) diff --git a/app/lib/shell/list_detail.dart b/app/lib/shell/list_detail.dart index 863a30c1..b3ab2f80 100644 --- a/app/lib/shell/list_detail.dart +++ b/app/lib/shell/list_detail.dart @@ -110,11 +110,20 @@ class ListPaneWidth extends StatelessWidget { /// composer) float above the shell's bar, never on it, and its content clears /// both. Pages the item opens stay in its pane ([DetailPaneNavigator]). class ListDetailPanes extends StatelessWidget { - const ListDetailPanes({super.key, required this.list, required this.detail}); + const ListDetailPanes({ + super.key, + required this.list, + required this.detail, + this.detailIdentity, + }); final Widget list; final Widget detail; + /// Which item [detail] shows; selecting another item closes any page the + /// previous one opened in its pane. + final Object? detailIdentity; + @override Widget build(BuildContext context) { final divider = usesCupertinoHost(context) @@ -151,7 +160,10 @@ class ListDetailPanes extends StatelessWidget { ), child: PageTabsEdge( edge: FloatingBarEdge.bottom, - child: DetailPaneNavigator(child: body), + child: DetailPaneNavigator( + identity: detailIdentity, + child: body, + ), ), ); // The tab bar sits over the detail pane, never the list (the diff --git a/app/test/shell/detail_pane_reset_test.dart b/app/test/shell/detail_pane_reset_test.dart new file mode 100644 index 00000000..becdb31f --- /dev/null +++ b/app/test/shell/detail_pane_reset_test.dart @@ -0,0 +1,74 @@ +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/shell/adaptive_shell.dart'; +import 'package:pipelines_app/shell/destination.dart'; +import 'package:pipelines_app/shell/list_detail.dart'; + +/// On a wide split window a page an item opened in its pane (a run's stage +/// page) closes when another item is selected, so the pane shows the newly +/// selected item instead of the old item's page. +void main() { + for (final cupertino in [false, true]) { + testWidgets('selecting another item closes the previous item\'s page ' + '(${cupertino ? 'cupertino' : 'material'})', (tester) async { + tester.view.physicalSize = const Size(1200, 800); + tester.view.devicePixelRatio = 1; + addTearDown(tester.view.reset); + final selected = ValueNotifier(1); + addTearDown(selected.dispose); + + Widget detail(int id) => Builder( + builder: (context) => Center( + child: GestureDetector( + key: const Key('open-page'), + onTap: () => Navigator.of(context).push( + PageRouteBuilder( + pageBuilder: (_, _, _) => Text('page of item $id'), + ), + ), + child: Text('item $id'), + ), + ), + ); + + final shell = AdaptiveShell( + cupertino: cupertino, + destination: AppDestination.runs, + destinationBodies: { + AppDestination.runs: (_) => ValueListenableBuilder( + valueListenable: selected, + builder: (_, id, _) => ListDetailPanes( + list: const Text('list'), + detailIdentity: id, + detail: detail(id), + ), + ), + }, + ); + await tester.pumpWidget( + cupertino + ? CupertinoApp(home: shell) + : MaterialApp( + theme: ThemeData(platform: TargetPlatform.android), + home: shell, + ), + ); + await tester.pumpAndSettle(); + + await tester.tap(find.byKey(const Key('open-page'))); + await tester.pumpAndSettle(); + expect(find.text('page of item 1'), findsOneWidget); + + // The same item rebuilt keeps its page open. + selected.value = 1; + await tester.pumpAndSettle(); + expect(find.text('page of item 1'), findsOneWidget); + + selected.value = 2; + await tester.pumpAndSettle(); + expect(find.text('page of item 1'), findsNothing); + expect(find.text('item 2'), findsOneWidget); + }); + } +}