Files
pipelines/app/lib/features/search/ui/search_bar_controls.dart
T
alan ccc80a9a8d 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.
2026-11-13 15:51:00 +00:00

144 lines
4.3 KiB
Dart

import 'package:flutter/material.dart';
import '../../../ui/input/app_search_field.dart';
import '../../../ui/navigation/bar_icon_button.dart';
import '../domain/search_models.dart';
const searchFieldKey = ValueKey<String>('search.field');
const searchKindKey = ValueKey<String>('search.kind');
const searchHelpKey = ValueKey<String>('search.help');
const searchBarControlsKey = ValueKey<String>('search.bar');
const searchTokenHint =
'Filter with t:pipeline or type:artifact (pipeline, run, step, field, '
'artifact, chat, version). Last type wins; choosing Kind replaces it. Quote '
'tokens to search literally.';
/// Search's controls as the top bar's main pill (in place of a title): the
/// search field filling the pill, a compact Kind menu and the query-syntax
/// help behind an info button.
class SearchBarControls extends StatelessWidget {
const SearchBarControls({
super.key,
required this.text,
required this.kind,
required this.onChanged,
required this.onSubmitted,
required this.onCleared,
required this.onKind,
this.autofocus = true,
this.focusNode,
this.onKeyEvent,
});
final TextEditingController text;
final SearchKind kind;
final ValueChanged<String> onChanged;
final VoidCallback onSubmitted;
final VoidCallback onCleared;
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
// edges at every width.
Widget build(BuildContext context) => KeyedSubtree(
key: searchBarControlsKey,
child: Row(
children: [
Expanded(
child: Padding(
padding: const EdgeInsetsDirectional.only(start: 4),
child: AppSearchField(
fieldKey: searchFieldKey,
clearKey: const ValueKey('search.clear'),
controller: text,
autofocus: autofocus,
focusNode: focusNode,
onKeyEvent: onKeyEvent,
inBar: true,
placeholder: 'Search pipelines, runs, artifacts and chats',
onChanged: onChanged,
onSubmitted: (_) => onSubmitted(),
onCleared: onCleared,
),
),
),
SearchKindButton(kind: kind, onSelected: onKind),
BarIconButton(
key: searchHelpKey,
icon: Icons.info_outline,
label: 'Search syntax',
onPressed: () => showSearchHelp(context),
),
],
),
);
}
/// Kind as a compact menu: a filter icon while every kind is searched, the
/// chosen kind's name once narrowed.
class SearchKindButton extends StatelessWidget {
const SearchKindButton({
super.key,
required this.kind,
required this.onSelected,
});
final SearchKind kind;
final ValueChanged<SearchKind> onSelected;
String get _label => 'Kind: ${kind.label}';
@override
Widget build(BuildContext context) {
final narrowed = kind != SearchKind.all;
return PopupMenuButton<SearchKind>(
key: searchKindKey,
tooltip: _label,
initialValue: kind,
onSelected: onSelected,
itemBuilder: (_) => [
for (final value in SearchKind.values)
CheckedPopupMenuItem(
value: value,
checked: value == kind,
child: Text(value.label),
),
],
child: narrowed
? Padding(
padding: const EdgeInsets.symmetric(horizontal: 4),
child: Chip(
avatar: const Icon(Icons.filter_list),
label: Text(kind.label),
visualDensity: VisualDensity.compact,
),
)
: const SizedBox.square(
dimension: kMinInteractiveDimension,
child: Icon(Icons.filter_list),
),
);
}
}
/// The query syntax, on demand instead of a paragraph above the results.
Future<void> showSearchHelp(BuildContext context) {
return showDialog<void>(
context: context,
builder: (context) => AlertDialog(
title: const Text('Search syntax'),
content: const Text(searchTokenHint),
actions: [
TextButton(
onPressed: () => Navigator.pop(context),
child: const Text('OK'),
),
],
),
);
}