Add keyboard navigation to search results

Keep the search field focused while selecting results with Control or arrow keys, and open the selected result with Enter. Document the field-scoped shortcuts and cover navigation, reset, submission, and typing behavior with regression tests.
This commit is contained in:
2026-11-13 15:51:00 +00:00
parent eeb07e5645
commit ccc80a9a8d
9 changed files with 372 additions and 44 deletions
@@ -1,7 +1,9 @@
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/ui/layout/app_list_row.dart';
class ImmediateGateway implements ApiGateway {
ImmediateGateway(this.reply);
@@ -161,6 +163,200 @@ void main() {
expect(opened, isNull);
});
testWidgets('field keys select results, retain focus, and Enter opens', (
tester,
) async {
useNarrowSurface(tester);
final second = hit(
kind: 'field',
target: 'run:11:4:output',
payload: {
'matched_field': 'output',
'field': {
'run_id': 11,
'sequence': 4,
'field': 'output',
'snippet': 'the release is ready',
},
},
);
final gateway = ImmediateGateway((_, _) => response([pipeline, second]));
JsonMap? opened;
await tester.pumpWidget(
MaterialApp(
home: SearchScreen(gateway: gateway, onOpen: (value) => opened = value),
),
);
await search(tester, 'release');
final fieldFinder = find.byKey(const ValueKey('search.field'));
expect(
tester
.widget<AppListRow>(
find.byKey(const ValueKey('search.result.pipeline:7')),
)
.selected,
isTrue,
);
await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown);
await tester.pump();
expect(
tester
.widget<AppListRow>(
find.byKey(const ValueKey('search.result.run:11:4:output')),
)
.selected,
isTrue,
);
expect(FocusManager.instance.primaryFocus?.debugLabel, 'omni-search');
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
await tester.sendKeyEvent(LogicalKeyboardKey.keyP);
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
await tester.pump();
expect(
tester
.widget<AppListRow>(
find.byKey(const ValueKey('search.result.pipeline:7')),
)
.selected,
isTrue,
);
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
await tester.sendKeyEvent(LogicalKeyboardKey.keyN);
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
await tester.pump();
expect(
tester
.widget<AppListRow>(
find.byKey(const ValueKey('search.result.run:11:4:output')),
)
.selected,
isTrue,
);
await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown);
await tester.pump();
expect(
tester
.widget<AppListRow>(
find.byKey(const ValueKey('search.result.run:11:4:output')),
)
.selected,
isTrue,
);
await tester.sendKeyEvent(LogicalKeyboardKey.arrowUp);
await tester.pump();
expect(
tester
.widget<AppListRow>(
find.byKey(const ValueKey('search.result.pipeline:7')),
)
.selected,
isTrue,
);
await tester.tap(fieldFinder);
await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown);
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
expect(opened?['target'], 'run:11:4:output');
});
testWidgets(
'selection resets when result data changes and typing remains active',
(tester) async {
useNarrowSurface(tester);
final changed = hit(
kind: 'pipeline',
target: 'pipeline:7',
payload: {
'pipeline': {
'id': 7,
'name': 'Updated release',
'version': 4,
'stages': 5,
},
},
);
final gateway = ImmediateGateway(
(query, _) =>
response(query == 'updated' ? [changed, field] : [pipeline, field]),
);
await tester.pumpWidget(
MaterialApp(
home: SearchScreen(gateway: gateway, onOpen: (_) {}),
),
);
await search(tester, 'release');
await tester.sendKeyEvent(LogicalKeyboardKey.arrowDown);
await tester.pump();
expect(
tester
.widget<AppListRow>(
find.byKey(const ValueKey('search.result.run:11:4:output')),
)
.selected,
isTrue,
);
await tester.enterText(
find.byKey(const ValueKey('search.field')),
'updated',
);
await tester.pump(const Duration(milliseconds: 300));
await tester.pumpAndSettle();
expect(
tester
.widget<AppListRow>(
find.byKey(const ValueKey('search.result.pipeline:7')),
)
.selected,
isTrue,
);
expect(
tester
.widget<TextField>(find.byKey(const ValueKey('search.field')))
.controller!
.text,
'updated',
);
},
);
testWidgets(
'Enter submits with no results and targetless rows have unique keys',
(tester) async {
useNarrowSurface(tester);
tester.view.physicalSize = const Size(390, 2000);
final gateway = ImmediateGateway(
(query, _) => query == 'empty'
? response([])
: response([
hit(kind: 'future_kind', target: '', payload: const {}),
hit(kind: 'future_kind', target: '', payload: const {}),
]),
);
await tester.pumpWidget(
MaterialApp(
home: SearchScreen(gateway: gateway, onOpen: (_) {}),
),
);
await search(tester, 'empty');
final callsBeforeSubmit = gateway.calls.length;
await tester.tap(find.byKey(const ValueKey('search.field')));
await tester.testTextInput.receiveAction(TextInputAction.search);
await tester.pumpAndSettle();
expect(gateway.calls.length, callsBeforeSubmit + 1);
await search(tester, 'rows');
expect(find.byKey(const ValueKey('search.result..0')), findsOneWidget);
expect(find.byKey(const ValueKey('search.result..1')), findsOneWidget);
expect(tester.takeException(), isNull);
},
);
testWidgets('an unsupported server shows its sentence and retry recovers', (
tester,
) async {
@@ -30,6 +30,7 @@ const _implemented = [
'Resize the terminal panel',
'Open a chat list item',
'Resize a list pane',
'Move through Search results',
'Previous or next artifact or changed file',
];
@@ -130,6 +131,18 @@ void main() {
expect(actions, isNot(contains('Move between terminal tabs')));
});
test('Search documents field-scoped Control navigation', () {
final registry = KeyboardShortcutRegistry(
platform: TargetPlatform.macOS,
web: true,
);
final entry = _entry(registry, 'Move through Search results');
expect(entry.group, KeyboardShortcutGroup.search);
expect(entry.keys, 'Ctrl+N, Ctrl+P, Up, Down');
expect(entry.caveat, contains('Search field'));
expect(entry.caveat, contains('Control rather than Cmd'));
});
testWidgets('groups stack in one column with their keys right-aligned', (
tester,
) async {