Files
pipelines/app/lib/features/search/ui/search_result_row.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

114 lines
3.6 KiB
Dart

import 'package:flutter/material.dart';
import '../../../core/api_gateway.dart';
import '../../../ui/layout/app_list_avatar.dart';
import '../../../ui/layout/app_list_row.dart';
import '../domain/search_row.dart';
import 'search_result_view.dart';
/// One search hit in the app's shared row: the kind's icon (or an image
/// artifact's picture), the title with its recency, and one muted line that
/// names the kind and then shows the snippet (its first query occurrence
/// emphasised by weight, never by parsing server text as markup) or, without
/// one, the kind's own facts. A chat hit with a matching message shows its
/// harness and directory, then the snippet on a second line. An artifact
/// hit's open choices are its menu.
class SearchResultRow extends StatelessWidget {
const SearchResultRow({
super.key,
required this.hit,
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'] ?? ''}';
final listRow = AppListRow(
key: rowKey ?? ValueKey('search.result.${hit['target']}'),
selected: selected,
divider: divider,
onTap: row.openable ? () => onOpen(hit) : null,
leading:
searchArtifactThumbnail(context, hit) ??
AppListAvatar(child: Icon(searchMaterialIcon(kind))),
title: Text(row.title),
time: row.recency,
subtitle: kind == 'chat' && row.snippet != null
? Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Text.rich(_facts(row)),
Text.rich(TextSpan(children: _snippet(row))),
],
)
: Text.rich(_line(row)),
compactSubtitle: kind == 'chat' && row.snippet != null
? Text.rich(TextSpan(children: _snippet(row)))
: null,
menu: searchArtifactActions(context, hit),
);
return visibilityKey == null
? listRow
: KeyedSubtree(key: visibilityKey, child: listRow);
}
TextSpan _line(SearchRowView row) => row.snippet == null
? _facts(row)
: TextSpan(children: [_kind(row), ..._snippet(row)]);
/// The kind, then the kind's own facts (a chat's harness and directory).
TextSpan _facts(SearchRowView row) => TextSpan(
children: [
_kind(row),
TextSpan(text: row.secondary),
],
);
TextSpan _kind(SearchRowView row) => TextSpan(
children: [
TextSpan(
text: row.kindLabel,
style: const TextStyle(fontWeight: FontWeight.w600),
),
const TextSpan(text: ' · '),
],
);
/// The snippet with its first query occurrence emphasised by weight.
List<TextSpan> _snippet(SearchRowView row) => [
for (final span in row.snippetSpans)
TextSpan(
text: span.text,
style: span.matched
? const TextStyle(fontWeight: FontWeight.bold)
: null,
),
];
}
IconData searchMaterialIcon(String kind) => switch (kind) {
'pipeline' => Icons.account_tree_outlined,
'version' => Icons.layers_outlined,
'run' => Icons.play_circle_outline,
'step' => Icons.arrow_forward,
'field' => Icons.notes,
'artifact' => Icons.attach_file,
'chat' => Icons.chat_bubble_outline,
_ => Icons.help_outline,
};