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:
@@ -14,12 +14,16 @@ class MaterialSearchBody extends StatelessWidget {
|
||||
required this.query,
|
||||
required this.onOpen,
|
||||
required this.header,
|
||||
required this.selectedIndex,
|
||||
required this.rowKeys,
|
||||
});
|
||||
|
||||
final OmniSearchController controller;
|
||||
final String query;
|
||||
final ValueChanged<JsonMap> onOpen;
|
||||
final Widget header;
|
||||
final int selectedIndex;
|
||||
final Map<int, GlobalKey> rowKeys;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@@ -55,6 +59,8 @@ class MaterialSearchBody extends StatelessWidget {
|
||||
query: query,
|
||||
onOpen: onOpen,
|
||||
message: (text) => _SearchMessage(message: text),
|
||||
selectedIndex: selectedIndex,
|
||||
rowKeys: rowKeys,
|
||||
);
|
||||
case SearchBodyKind.empty:
|
||||
return [
|
||||
|
||||
@@ -28,6 +28,7 @@ class SearchBarControls extends StatelessWidget {
|
||||
required this.onKind,
|
||||
this.autofocus = true,
|
||||
this.focusNode,
|
||||
this.onKeyEvent,
|
||||
});
|
||||
|
||||
final TextEditingController text;
|
||||
@@ -38,6 +39,7 @@ class SearchBarControls extends StatelessWidget {
|
||||
final ValueChanged<SearchKind> onKind;
|
||||
final bool autofocus;
|
||||
final FocusNode? focusNode;
|
||||
final FocusOnKeyEventCallback? onKeyEvent;
|
||||
|
||||
@override
|
||||
// The pill grows with the bar, so the field and the results share both
|
||||
@@ -55,6 +57,7 @@ class SearchBarControls extends StatelessWidget {
|
||||
controller: text,
|
||||
autofocus: autofocus,
|
||||
focusNode: focusNode,
|
||||
onKeyEvent: onKeyEvent,
|
||||
inBar: true,
|
||||
placeholder: 'Search pipelines, runs, artifacts and chats',
|
||||
onChanged: onChanged,
|
||||
|
||||
@@ -20,19 +20,26 @@ class SearchResultRow extends StatelessWidget {
|
||||
required this.query,
|
||||
required this.onOpen,
|
||||
this.divider = true,
|
||||
this.selected = false,
|
||||
this.visibilityKey,
|
||||
this.rowKey,
|
||||
});
|
||||
|
||||
final JsonMap hit;
|
||||
final String query;
|
||||
final ValueChanged<JsonMap> onOpen;
|
||||
final bool divider;
|
||||
final bool selected;
|
||||
final GlobalKey? visibilityKey;
|
||||
final Key? rowKey;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final row = searchRowView(hit, query);
|
||||
final kind = '${hit['kind'] ?? ''}';
|
||||
return AppListRow(
|
||||
key: ValueKey('search.result.${hit['target']}'),
|
||||
final listRow = AppListRow(
|
||||
key: rowKey ?? ValueKey('search.result.${hit['target']}'),
|
||||
selected: selected,
|
||||
divider: divider,
|
||||
onTap: row.openable ? () => onOpen(hit) : null,
|
||||
leading:
|
||||
@@ -55,6 +62,9 @@ class SearchResultRow extends StatelessWidget {
|
||||
: null,
|
||||
menu: searchArtifactActions(context, hit),
|
||||
);
|
||||
return visibilityKey == null
|
||||
? listRow
|
||||
: KeyedSubtree(key: visibilityKey, child: listRow);
|
||||
}
|
||||
|
||||
TextSpan _line(SearchRowView row) => row.snippet == null
|
||||
|
||||
@@ -20,19 +20,39 @@ List<Widget> searchResultSlivers({
|
||||
required String query,
|
||||
required ValueChanged<JsonMap> onOpen,
|
||||
required Widget Function(String message) message,
|
||||
required int selectedIndex,
|
||||
required Map<int, GlobalKey> rowKeys,
|
||||
}) {
|
||||
final chats = controller.shownChats;
|
||||
final others = controller.shownResults;
|
||||
final allHits = [...chats, ...others];
|
||||
final targetCounts = <String, int>{};
|
||||
for (final hit in allHits) {
|
||||
final target = '${hit['target'] ?? ''}';
|
||||
targetCounts[target] = (targetCounts[target] ?? 0) + 1;
|
||||
}
|
||||
final grouped = controller.showsChatSection;
|
||||
final note = controller.chatsNote;
|
||||
Widget rows(List<JsonMap> hits) => SliverList.builder(
|
||||
Widget rows(List<JsonMap> hits, int offset) => SliverList.builder(
|
||||
itemCount: hits.length,
|
||||
itemBuilder: (context, index) => SearchResultRow(
|
||||
hit: hits[index],
|
||||
query: query,
|
||||
onOpen: onOpen,
|
||||
divider: index < hits.length - 1,
|
||||
),
|
||||
itemBuilder: (context, index) {
|
||||
final hit = hits[index];
|
||||
final target = '${hit['target'] ?? ''}';
|
||||
final indexInResults = offset + index;
|
||||
final key = rowKeys.putIfAbsent(indexInResults, GlobalKey.new);
|
||||
final rowKey = target.isEmpty || targetCounts[target]! > 1
|
||||
? ValueKey('search.result.$target.$indexInResults')
|
||||
: ValueKey('search.result.$target');
|
||||
return SearchResultRow(
|
||||
rowKey: rowKey,
|
||||
hit: hit,
|
||||
query: query,
|
||||
onOpen: onOpen,
|
||||
divider: index < hits.length - 1,
|
||||
selected: indexInResults == selectedIndex,
|
||||
visibilityKey: key,
|
||||
);
|
||||
},
|
||||
);
|
||||
final empty = controller.emptyMessage;
|
||||
return [
|
||||
@@ -44,7 +64,7 @@ List<Widget> searchResultSlivers({
|
||||
header: true,
|
||||
),
|
||||
),
|
||||
if (chats.isNotEmpty) rows(chats),
|
||||
if (chats.isNotEmpty) rows(chats, 0),
|
||||
if (note != null)
|
||||
SliverToBoxAdapter(
|
||||
child: Padding(
|
||||
@@ -60,7 +80,7 @@ List<Widget> searchResultSlivers({
|
||||
header: true,
|
||||
),
|
||||
),
|
||||
if (others.isNotEmpty) rows(others),
|
||||
if (others.isNotEmpty) rows(others, chats.length),
|
||||
if (others.isEmpty &&
|
||||
controller.parsed.kind != SearchKind.chat &&
|
||||
empty != null)
|
||||
|
||||
Reference in New Issue
Block a user