diff --git a/app/lib/features/chat/screen.dart b/app/lib/features/chat/screen.dart index e8095ff1..c200d948 100644 --- a/app/lib/features/chat/screen.dart +++ b/app/lib/features/chat/screen.dart @@ -349,6 +349,8 @@ class _ChatScreenState extends State /// On a phone the open chat is a real page above the main screen. final DetailPageHost _page = DetailPageHost(); + void _closeChat() => _page.open ? _page.close() : _back(); + /// The list rows the phone's chat page grows from and shrinks back into: /// the open chat's row, also after the page switched to another chat. final ContainerTransformSources _rowSources = ContainerTransformSources(); @@ -410,13 +412,15 @@ class _ChatScreenState extends State sessionId: selected, placement: ChatPanePlacement( syncToken: widget.syncToken, - onBack: _back, + // Back from a chat page pops that page itself (its completion runs + // [_back]); otherwise it leaves the open chat directly. The button + // never falls back to popping whichever route is nearest. + onBack: _closeChat, onNewSession: _startedNewSession, onSessionClosed: () => _closed(selected), onNextChat: _nextAfter(selected) != null ? () => _openNext(selected) : null, - useNativeBack: _page.open, pasteTarget: _workspaceVisible && pane.id == _workspace.activePaneId, showBackButton: _page.open || !showsListBesideDetail(context), paneCommands: _paneCommands, diff --git a/app/lib/shell/detail_page.dart b/app/lib/shell/detail_page.dart index 4bfae210..94f5e337 100644 --- a/app/lib/shell/detail_page.dart +++ b/app/lib/shell/detail_page.dart @@ -68,6 +68,21 @@ class DetailPageHost { } } + /// Closes the open page as its Back would: the page's own route is popped + /// explicitly, never whichever route happens to be nearest a button, and + /// its completion then clears the owner's selection. + void close() { + final route = _route; + if (route == null || !route.isActive) return; + final navigator = route.navigator; + if (navigator == null) return; + if (route.isCurrent) { + navigator.pop(); + } else { + navigator.removeRoute(route); + } + } + /// Removes the page with its owner. void dispose() { // The notifier is left to the page: a page still animating out keeps diff --git a/app/test/features/chat/chat_split_integration_test.dart b/app/test/features/chat/chat_split_integration_test.dart index c10ea315..a41dbf24 100644 --- a/app/test/features/chat/chat_split_integration_test.dart +++ b/app/test/features/chat/chat_split_integration_test.dart @@ -178,11 +178,13 @@ void main() { await tester.tap(find.text('Chat a').first); await tester.pumpAndSettle(); expect(find.byTooltip('Back').hitTestable(), findsOneWidget); + // The page's Back closes that page explicitly rather than popping + // whichever route is nearest the button. expect( tester .widget(find.byType(BackButton).hitTestable()) .onPressed, - isNull, + isNotNull, ); final stateA = tester.state(_thread('a')); await tester.enterText(_input('a'), 'Phone draft A'); diff --git a/app/test/shell/detail_page_close_test.dart b/app/test/shell/detail_page_close_test.dart new file mode 100644 index 00000000..9c48c26b --- /dev/null +++ b/app/test/shell/detail_page_close_test.dart @@ -0,0 +1,76 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/shell/destination.dart'; +import 'package:pipelines_app/shell/detail_page.dart'; + +/// A screen that opens one detail page whose content sits inside its own +/// inner navigator, as a chat pane can, with a Back that calls [onBack]. +class _Host extends StatefulWidget { + const _Host({required this.page, required this.useClose}); + + final DetailPageHost page; + final bool useClose; + + @override + State<_Host> createState() => _HostState(); +} + +class _HostState extends State<_Host> { + bool selected = true; + int closed = 0; + + @override + Widget build(BuildContext context) { + widget.page.sync( + context, + selected: selected, + destination: AppDestination.chat, + onClosed: () => setState(() { + selected = false; + closed++; + }), + builder: (_) => Navigator( + pages: [ + MaterialPage( + child: Scaffold( + body: Center( + child: BackButton( + key: const Key('inner-back'), + onPressed: widget.useClose ? widget.page.close : null, + ), + ), + ), + ), + ], + onDidRemovePage: (_) {}, + ), + ); + return Scaffold(body: Text('list (closed $closed)')); + } +} + +void main() { + testWidgets('close pops the page even from inside an inner navigator', ( + tester, + ) async { + final page = DetailPageHost(); + await tester.pumpWidget( + MaterialApp(home: _Host(page: page, useClose: true)), + ); + await tester.pumpAndSettle(); + expect(page.open, isTrue); + expect(find.byKey(const Key('inner-back')), findsOneWidget); + + await tester.tap(find.byKey(const Key('inner-back'))); + await tester.pumpAndSettle(); + expect(page.open, isFalse); + expect(find.byKey(const Key('inner-back')), findsNothing); + expect(find.text('list (closed 1)'), findsOneWidget); + }); + + testWidgets('close does nothing without an open page', (tester) async { + final page = DetailPageHost(); + page.close(); + expect(page.open, isFalse); + }); +}