Make Search Escape restore input then previous tab

This commit is contained in:
2026-11-06 19:10:47 +00:00
parent 1106adbce2
commit 1ee7960925
5 changed files with 279 additions and 4 deletions
+5 -2
View File
@@ -120,8 +120,11 @@ class _SearchScreenState extends State<SearchScreen> {
}
@override
Widget build(BuildContext context) =>
ShellSearchShortcut(onFocus: _focusSearch, child: _build(context));
Widget build(BuildContext context) => ShellSearchShortcut(
onFocus: _focusSearch,
hasFocus: () => _focus.hasPrimaryFocus,
child: _build(context),
);
Widget _build(BuildContext context) {
// Follow the actual host app class: Cupertino only under an explicit
+31
View File
@@ -282,6 +282,16 @@ class _AdaptiveShellState extends State<AdaptiveShell> {
/// have no global keyboard chord.
bool _handleDestinationKeyEvent(KeyEvent event) {
final keyboard = HardwareKeyboard.instance;
if (_selected == AppDestination.search &&
event is KeyDownEvent &&
event.logicalKey == LogicalKeyboardKey.escape &&
!keyboard.isControlPressed &&
!keyboard.isMetaPressed &&
!keyboard.isShiftPressed &&
!keyboard.isAltPressed &&
_searchEscape()) {
return true;
}
if (shellSearchShortcut(event, keyboard)) {
_select(AppDestination.search);
WidgetsBinding.instance.addPostFrameCallback((_) {
@@ -297,6 +307,27 @@ class _AdaptiveShellState extends State<AdaptiveShell> {
return _bar.lists.open(AppDestination.chat, listIndex);
}
/// A popup or nested viewer owns Escape before the underlying Search tab.
bool _searchEscape() {
final route = ModalRoute.of(context);
if (route != null && !route.isCurrent) return false;
final focusedContext = FocusManager.instance.primaryFocus?.context;
final focusedRoute = focusedContext == null
? null
: ModalRoute.of(focusedContext);
if (focusedRoute != null && route != null && focusedRoute != route) {
return false;
}
if (!_bar.searchFocus.available) return false;
if (!_bar.searchFocus.focused) {
_bar.searchFocus.request();
return true;
}
if (_history.isEmpty) return false;
_back(page: false);
return true;
}
List<FloatingDestinationItem> _destinationItems() =>
shellTabDestinations.map((destination) {
final item = destinationDetails(destination);
+13 -2
View File
@@ -8,17 +8,22 @@ import 'shell_bar.dart';
class ShellSearchFocus {
Object? _owner;
VoidCallback? _focus;
bool Function()? _hasFocus;
bool get available => _focus != null;
bool get focused => _hasFocus?.call() ?? false;
void request() => _focus?.call();
void _set(Object owner, VoidCallback focus) {
void _set(Object owner, VoidCallback focus, bool Function() hasFocus) {
_owner = owner;
_focus = focus;
_hasFocus = hasFocus;
}
void _clear(Object owner) {
if (_owner != owner) return;
_owner = null;
_focus = null;
_hasFocus = null;
}
}
@@ -26,9 +31,11 @@ class ShellSearchShortcut extends StatefulWidget {
const ShellSearchShortcut({
super.key,
required this.onFocus,
required this.hasFocus,
required this.child,
});
final VoidCallback onFocus;
final bool Function() hasFocus;
final Widget child;
@override
@@ -47,7 +54,11 @@ class _ShellSearchShortcutState extends State<ShellSearchShortcut> {
_scope?.registry.searchFocus._clear(_owner);
_scope = scope;
if (scope?.destination == AppDestination.search) {
scope!.registry.searchFocus._set(_owner, () => widget.onFocus());
scope!.registry.searchFocus._set(
_owner,
() => widget.onFocus(),
() => widget.hasFocus(),
);
}
}
+32
View File
@@ -0,0 +1,32 @@
# Search Escape navigation
Unmodified Escape in Search first returns focus to the native search field when
focus is elsewhere. With the field already focused, it returns to the actual
previous destination through the shell's existing tab history. The query survives
both actions and subsequent visits. Search opened without a previous destination
stays open. Hidden Search, modified Escape and key repeats do not consume history.
An open dialog or popup retains priority over the Search shortcut.
The focus registry reports the real editable field's focus while retaining the
existing native input handoff used by Cupertino web. No chat workspace behavior
or deployment is included here.
Verification:
- Shell/Search integration: 197 passed, peak 1816 MB;
`/private/tmp/search-escape-integration.log`.
- Final focused suite: 9 passed, peak 916 MB; includes Chat/Runs/Pipelines history,
Cupertino focus, retained query, dialog/menu dismissal, modifiers, hidden Search,
missing history and held-key repeat.
`/private/tmp/search-escape-final-focused.log`.
- Bounded analysis of all changed Dart files: clean. Fixture web build passed.
- Real Chrome fixture interaction passed for Material/light and Cupertino/dark.
Assertions checked the native active input value after typing, Tab and Escape,
second-Escape navigation to Chat, retained query, dialog dismissal, and returning
to Runs rather than Chat. All four PNGs were inspected.
Reports and captures are in
`/private/tmp/search-escape-browser-material/` and
`/private/tmp/search-escape-browser-cupertino/`, each containing `report.json`,
`01-field-refocused.png` and `02-previous-runs.png`.
No physical-device verification or deployment was performed.
+198
View File
@@ -0,0 +1,198 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pipelines_app/core/api_gateway.dart';
import 'package:pipelines_app/features/search/search_screen.dart';
import 'package:pipelines_app/features/search/ui/search_bar_controls.dart';
import 'package:pipelines_app/shell/adaptive_shell.dart';
import 'package:pipelines_app/shell/destination.dart';
class _Gateway implements ApiGateway {
@override
Stream<JsonMap> get frames => const Stream.empty();
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async => {
'search_results': {'results': [], 'truncated': false},
};
}
EditableText _field(WidgetTester tester) => tester.widget<EditableText>(
find.descendant(
of: find.byKey(searchFieldKey),
matching: find.byType(EditableText),
),
);
Future<void> _press(WidgetTester tester, LogicalKeyboardKey key) async {
await tester.sendKeyEvent(key);
await tester.pumpAndSettle();
}
Future<void> _search(WidgetTester tester) async {
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
await tester.sendKeyEvent(LogicalKeyboardKey.slash);
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
await tester.pumpAndSettle();
}
Future<void> _pump(
WidgetTester tester,
AppDestination from, {
bool cupertino = false,
}) async {
tester.view.physicalSize = const Size(1200, 900);
tester.view.devicePixelRatio = 1;
addTearDown(tester.view.reset);
final shell = AdaptiveShell(
cupertino: cupertino,
destination: from,
destinationBodies: {
for (final destination in AppDestination.values)
destination: (_) => Text('Body ${destination.name}'),
AppDestination.search: (_) =>
SearchScreen(gateway: _Gateway(), onOpen: (_) {}, embedded: true),
},
);
await tester.pumpWidget(
cupertino ? CupertinoApp(home: shell) : MaterialApp(home: shell),
);
await tester.pumpAndSettle();
}
void main() {
for (final from in [
AppDestination.chat,
AppDestination.runs,
AppDestination.pipelines,
]) {
testWidgets(
'Escape focuses Search, then returns to ${from.name} without clearing query',
(tester) async {
await _pump(tester, from);
await _search(tester);
await tester.enterText(find.byKey(searchFieldKey), 'release');
await tester.pumpAndSettle();
await _press(tester, LogicalKeyboardKey.tab);
expect(_field(tester).focusNode.hasPrimaryFocus, isFalse);
await _press(tester, LogicalKeyboardKey.escape);
expect(_field(tester).focusNode.hasPrimaryFocus, isTrue);
expect(_field(tester).controller.text, 'release');
await _press(tester, LogicalKeyboardKey.escape);
expect(find.text('Body ${from.name}'), findsOneWidget);
await _search(tester);
expect(_field(tester).controller.text, 'release');
expect(_field(tester).focusNode.hasPrimaryFocus, isTrue);
await tester.pumpWidget(const SizedBox.shrink());
},
);
}
testWidgets(
'Cupertino Escape preserves native Search focus handoff and previous tab',
(tester) async {
await _pump(tester, AppDestination.runs, cupertino: true);
await _search(tester);
await tester.enterText(find.byKey(searchFieldKey), 'query');
await tester.pumpAndSettle();
_field(tester).focusNode.unfocus();
await tester.pumpAndSettle();
await _press(tester, LogicalKeyboardKey.escape);
expect(_field(tester).focusNode.hasPrimaryFocus, isTrue);
expect(_field(tester).controller.text, 'query');
await _press(tester, LogicalKeyboardKey.escape);
expect(find.text('Body runs'), findsOneWidget);
await tester.pumpWidget(const SizedBox.shrink());
},
);
testWidgets('Escape dismisses a Search dialog before changing tabs', (
tester,
) async {
await _pump(tester, AppDestination.chat);
await _search(tester);
await tester.tap(find.byKey(searchHelpKey));
await tester.pumpAndSettle();
expect(find.byType(AlertDialog), findsOneWidget);
await _press(tester, LogicalKeyboardKey.escape);
expect(find.byType(AlertDialog), findsNothing);
expect(find.byKey(searchFieldKey), findsOneWidget);
expect(find.text('Body chat'), findsNothing);
await tester.pumpWidget(const SizedBox.shrink());
});
testWidgets('Escape dismisses a Search menu before changing tabs', (
tester,
) async {
await _pump(tester, AppDestination.chat);
await _search(tester);
await tester.tap(find.byKey(searchKindKey));
await tester.pumpAndSettle();
expect(
find.byWidgetPredicate((widget) => widget is PopupMenuEntry),
findsWidgets,
);
await _press(tester, LogicalKeyboardKey.escape);
expect(
find.byWidgetPredicate((widget) => widget is PopupMenuEntry),
findsNothing,
);
expect(find.byKey(searchFieldKey), findsOneWidget);
expect(find.text('Body chat'), findsNothing);
await tester.pumpWidget(const SizedBox.shrink());
});
testWidgets('modified Escape and hidden Search never consume tab history', (
tester,
) async {
await _pump(tester, AppDestination.pipelines);
await _search(tester);
for (final modifier in [
LogicalKeyboardKey.controlLeft,
LogicalKeyboardKey.shiftLeft,
LogicalKeyboardKey.altLeft,
LogicalKeyboardKey.metaLeft,
]) {
await tester.sendKeyDownEvent(modifier);
await tester.sendKeyEvent(LogicalKeyboardKey.escape);
await tester.sendKeyUpEvent(modifier);
await tester.pumpAndSettle();
expect(find.byKey(searchFieldKey), findsOneWidget);
}
await _search(tester);
await _press(tester, LogicalKeyboardKey.escape);
expect(find.text('Body pipelines'), findsOneWidget);
await _press(tester, LogicalKeyboardKey.escape);
expect(find.text('Body pipelines'), findsOneWidget);
await tester.pumpWidget(const SizedBox.shrink());
});
testWidgets('initial Search with no previous tab stays open on Escape', (
tester,
) async {
await _pump(tester, AppDestination.search);
await _search(tester);
await tester.enterText(find.byKey(searchFieldKey), 'keep');
await tester.pumpAndSettle();
await _press(tester, LogicalKeyboardKey.escape);
expect(find.byKey(searchFieldKey), findsOneWidget);
expect(_field(tester).controller.text, 'keep');
await tester.pumpWidget(const SizedBox.shrink());
});
testWidgets('holding Escape does not perform the second back action', (
tester,
) async {
await _pump(tester, AppDestination.chat);
await _search(tester);
_field(tester).focusNode.unfocus();
await tester.pumpAndSettle();
await tester.sendKeyDownEvent(LogicalKeyboardKey.escape);
await tester.pumpAndSettle();
expect(_field(tester).focusNode.hasPrimaryFocus, isTrue);
await tester.sendKeyRepeatEvent(LogicalKeyboardKey.escape);
await tester.pumpAndSettle();
expect(find.byKey(searchFieldKey), findsOneWidget);
expect(find.text('Body chat'), findsNothing);
await tester.sendKeyUpEvent(LogicalKeyboardKey.escape);
await tester.pumpWidget(const SizedBox.shrink());
});
}