Files
pipelines/app/lib/features/chat/conversation/chat_find_bar.dart
T
alan 8bd219e636 Chat: find in chat (Ctrl+F / Cmd+F)
A find bar over the transcript: query, Match case, n of N (counted from
the newest), Enter for an older match and Shift+Enter for a newer one.
Matches are painted over the rendered text, code blocks included, without
restyling it; the current match is scrolled into view. Matches in history
not loaded yet come from the server's chat:<id>-scoped search and are
reached by loading older pages. Esc closes and returns to the composer.
The key is the pane's, so a focused terminal keeps Ctrl+F. Fixture answers
scoped searches for captures; cheatsheet lists the key. The scoped search
matches the query as typed, spaces included, like the client.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 00:41:39 +00:00

141 lines
4.8 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'chat_find_controller.dart';
/// The chat's find bar, like the terminal's: the query field, the match
/// count, Match case, the older/newer and close buttons. Matches count from
/// the newest; Enter moves to an older match and Shift+Enter to a newer one;
/// Esc closes. [onStep] moves and reveals; [onClose] closes the bar and
/// returns the keyboard to the composer.
class ChatFindBar extends StatefulWidget {
const ChatFindBar({
super.key,
required this.find,
required this.focus,
required this.onStep,
required this.onClose,
});
final ChatFindController find;
final FocusNode focus;
final ValueChanged<int> onStep;
final VoidCallback onClose;
@override
State<ChatFindBar> createState() => _ChatFindBarState();
}
class _ChatFindBarState extends State<ChatFindBar> {
late final TextEditingController _text = TextEditingController(
text: widget.find.query,
);
@override
void dispose() {
_text.dispose();
super.dispose();
}
KeyEventResult _key(FocusNode node, KeyEvent event) {
if (event is KeyUpEvent) return KeyEventResult.ignored;
if (event.logicalKey == LogicalKeyboardKey.escape) {
widget.onClose();
return KeyEventResult.handled;
}
if (event.logicalKey == LogicalKeyboardKey.enter ||
event.logicalKey == LogicalKeyboardKey.numpadEnter) {
widget.onStep(HardwareKeyboard.instance.isShiftPressed ? -1 : 1);
return KeyEventResult.handled;
}
return KeyEventResult.ignored;
}
String _count(ChatFindController find) {
if (find.query.isEmpty) return '';
if (find.count == 0) return find.capped ? 'Searching…' : 'No results';
final total = find.capped ? '${find.count}+' : '${find.count}';
return '${find.index + 1} of $total';
}
@override
Widget build(BuildContext context) {
final find = widget.find;
final scheme = Theme.of(context).colorScheme;
// The buttons count as part of the field, so clicking one keeps the
// keyboard in it (Enter steps on from there).
return TextFieldTapRegion(
child: Material(
elevation: 3,
color: scheme.surfaceContainerHigh,
borderRadius: BorderRadius.circular(8),
child: Padding(
padding: const EdgeInsetsDirectional.only(start: 12, end: 4),
child: ListenableBuilder(
listenable: find,
builder: (context, _) => Row(
mainAxisSize: MainAxisSize.min,
children: [
Flexible(
child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 240),
child: Focus(
onKeyEvent: _key,
skipTraversal: true,
child: TextField(
key: const ValueKey('chat-find-field'),
controller: _text,
focusNode: widget.focus,
autofocus: true,
decoration: const InputDecoration(
hintText: 'Find in chat',
border: InputBorder.none,
isDense: true,
),
onChanged: (value) => find.query = value,
),
),
),
),
Semantics(
liveRegion: true,
child: Text(
_count(find),
key: const ValueKey('chat-find-count'),
style: Theme.of(context).textTheme.bodySmall,
),
),
IconButton(
key: const ValueKey('chat-find-case'),
tooltip: 'Match case',
isSelected: find.caseSensitive,
onPressed: find.toggleCase,
icon: const Text('Aa'),
),
IconButton(
key: const ValueKey('chat-find-older'),
tooltip: 'Older match (Enter)',
onPressed: find.count == 0 ? null : () => widget.onStep(1),
icon: const Icon(Icons.keyboard_arrow_up),
),
IconButton(
key: const ValueKey('chat-find-newer'),
tooltip: 'Newer match (Shift+Enter)',
onPressed: find.count == 0 ? null : () => widget.onStep(-1),
icon: const Icon(Icons.keyboard_arrow_down),
),
IconButton(
key: const ValueKey('chat-find-close'),
tooltip: 'Close find (Esc)',
onPressed: widget.onClose,
icon: const Icon(Icons.close),
),
],
),
),
),
),
);
}
}