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>
This commit is contained in:
@@ -0,0 +1,278 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/rendering.dart';
|
||||
|
||||
import '../../conversation/chat_find_controller.dart';
|
||||
import '../../shell/chat_shell_terminal_theme.dart';
|
||||
|
||||
/// The boxes, in [ancestor]'s coordinates, of a character range of a
|
||||
/// rendered text; empty when it is not laid out.
|
||||
typedef ChatFindRects =
|
||||
List<Rect> Function(int start, int end, RenderBox ancestor);
|
||||
|
||||
/// Where find's matches are drawn: the open find (null while it is closed)
|
||||
/// and the rendered texts and rows the transcript has built, so the current
|
||||
/// match can be scrolled into view.
|
||||
class ChatFindTargets {
|
||||
final rows = <String, BuildContext>{};
|
||||
final texts = <String, Map<String, ChatFindRects>>{};
|
||||
|
||||
ChatFindRects? textFor(String rowKey, String text) => texts[rowKey]?[text];
|
||||
}
|
||||
|
||||
class ChatFindScope extends InheritedWidget {
|
||||
const ChatFindScope({
|
||||
super.key,
|
||||
required this.controller,
|
||||
required this.targets,
|
||||
required super.child,
|
||||
});
|
||||
|
||||
final ChatFindController? controller;
|
||||
final ChatFindTargets targets;
|
||||
|
||||
static ChatFindScope? maybeOf(BuildContext context) =>
|
||||
context.dependOnInheritedWidgetOfExactType<ChatFindScope>();
|
||||
|
||||
@override
|
||||
bool updateShouldNotify(ChatFindScope oldWidget) =>
|
||||
controller != oldWidget.controller || targets != oldWidget.targets;
|
||||
}
|
||||
|
||||
/// Marks one transcript row for find: the texts in it highlight their
|
||||
/// matches under [rowKey], and the row registers itself while built.
|
||||
class ChatFindRow extends StatefulWidget {
|
||||
const ChatFindRow({super.key, required this.rowKey, required this.child});
|
||||
|
||||
final String rowKey;
|
||||
final Widget child;
|
||||
|
||||
static String? keyOf(BuildContext context) =>
|
||||
context.dependOnInheritedWidgetOfExactType<_ChatFindRowKey>()?.rowKey;
|
||||
|
||||
@override
|
||||
State<ChatFindRow> createState() => _ChatFindRowState();
|
||||
}
|
||||
|
||||
class _ChatFindRowState extends State<ChatFindRow> {
|
||||
ChatFindTargets? _targets;
|
||||
String? _registered;
|
||||
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
super.didChangeDependencies();
|
||||
_register();
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(ChatFindRow oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
_register();
|
||||
}
|
||||
|
||||
void _register() {
|
||||
_unregister();
|
||||
final targets = ChatFindScope.maybeOf(context)?.targets;
|
||||
if (targets == null) return;
|
||||
targets.rows[widget.rowKey] = context;
|
||||
_targets = targets;
|
||||
_registered = widget.rowKey;
|
||||
}
|
||||
|
||||
void _unregister() {
|
||||
final targets = _targets;
|
||||
final key = _registered;
|
||||
if (targets != null && key != null && targets.rows[key] == context) {
|
||||
targets.rows.remove(key);
|
||||
}
|
||||
_targets = null;
|
||||
_registered = null;
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_unregister();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) =>
|
||||
_ChatFindRowKey(rowKey: widget.rowKey, child: widget.child);
|
||||
}
|
||||
|
||||
class _ChatFindRowKey extends InheritedWidget {
|
||||
const _ChatFindRowKey({required this.rowKey, required super.child});
|
||||
|
||||
final String rowKey;
|
||||
|
||||
@override
|
||||
bool updateShouldNotify(_ChatFindRowKey oldWidget) =>
|
||||
rowKey != oldWidget.rowKey;
|
||||
}
|
||||
|
||||
/// Paints find's matches in one rendered [text], the current one stronger,
|
||||
/// like the terminal's find. The text itself is never restyled, so nothing
|
||||
/// shifts or rewraps as the current match moves. The translucent marks go
|
||||
/// over the text, since a code block's own fill would hide them behind it.
|
||||
class ChatFindHighlight extends StatefulWidget {
|
||||
const ChatFindHighlight({
|
||||
super.key,
|
||||
required this.text,
|
||||
required this.rects,
|
||||
required this.child,
|
||||
});
|
||||
|
||||
final String text;
|
||||
final ChatFindRects rects;
|
||||
final Widget child;
|
||||
|
||||
@override
|
||||
State<ChatFindHighlight> createState() => _ChatFindHighlightState();
|
||||
}
|
||||
|
||||
class _ChatFindHighlightState extends State<ChatFindHighlight> {
|
||||
final _anchor = GlobalKey();
|
||||
ChatFindTargets? _targets;
|
||||
(String, String)? _registered;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_unregister();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _register(ChatFindTargets? targets, String? rowKey) {
|
||||
final next = rowKey == null ? null : (rowKey, widget.text);
|
||||
if (identical(targets, _targets) && next == _registered) {
|
||||
if (next != null) targets!.texts[next.$1]![next.$2] = widget.rects;
|
||||
return;
|
||||
}
|
||||
_unregister();
|
||||
if (targets == null || next == null) return;
|
||||
targets.texts.putIfAbsent(next.$1, () => {})[next.$2] = widget.rects;
|
||||
_targets = targets;
|
||||
_registered = next;
|
||||
}
|
||||
|
||||
void _unregister() {
|
||||
final targets = _targets;
|
||||
final key = _registered;
|
||||
if (targets != null && key != null) {
|
||||
final texts = targets.texts[key.$1];
|
||||
texts?.remove(key.$2);
|
||||
if (texts != null && texts.isEmpty) targets.texts.remove(key.$1);
|
||||
}
|
||||
_targets = null;
|
||||
_registered = null;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final scope = ChatFindScope.maybeOf(context);
|
||||
final rowKey = ChatFindRow.keyOf(context);
|
||||
_register(scope?.targets, rowKey);
|
||||
final controller = scope?.controller;
|
||||
if (controller == null || rowKey == null) {
|
||||
return KeyedSubtree(key: _anchor, child: widget.child);
|
||||
}
|
||||
final colours = chatShellTerminalMatchColours(context);
|
||||
return CustomPaint(
|
||||
key: _anchor,
|
||||
foregroundPainter: _ChatFindPainter(
|
||||
controller: controller,
|
||||
rowKey: rowKey,
|
||||
text: widget.text,
|
||||
rects: widget.rects,
|
||||
anchor: _anchor,
|
||||
hit: colours.hit,
|
||||
current: colours.current,
|
||||
),
|
||||
child: widget.child,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _ChatFindPainter extends CustomPainter {
|
||||
_ChatFindPainter({
|
||||
required this.controller,
|
||||
required this.rowKey,
|
||||
required this.text,
|
||||
required this.rects,
|
||||
required this.anchor,
|
||||
required this.hit,
|
||||
required this.current,
|
||||
}) : super(repaint: controller);
|
||||
|
||||
final ChatFindController controller;
|
||||
final String rowKey;
|
||||
final String text;
|
||||
final ChatFindRects rects;
|
||||
final GlobalKey anchor;
|
||||
final Color hit;
|
||||
final Color current;
|
||||
|
||||
@override
|
||||
void paint(Canvas canvas, Size size) {
|
||||
final highlights = controller.highlightsFor(rowKey, text);
|
||||
if (highlights.isEmpty) return;
|
||||
final box = anchor.currentContext?.findRenderObject();
|
||||
if (box is! RenderBox || !box.attached) return;
|
||||
for (final highlight in highlights) {
|
||||
final fill = Paint()..color = highlight.current ? current : hit;
|
||||
for (final rect in rects(highlight.start, highlight.end, box)) {
|
||||
canvas.drawRRect(
|
||||
RRect.fromRectAndRadius(rect.inflate(1), const Radius.circular(3)),
|
||||
fill,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
bool shouldRepaint(_ChatFindPainter oldDelegate) =>
|
||||
oldDelegate.controller != controller ||
|
||||
oldDelegate.rowKey != rowKey ||
|
||||
oldDelegate.text != text ||
|
||||
oldDelegate.hit != hit ||
|
||||
oldDelegate.current != current;
|
||||
}
|
||||
|
||||
/// The boxes of characters [start]..[end] of the first paragraph (or
|
||||
/// editable) under [context], for a text rendered verbatim.
|
||||
List<Rect> chatFindPlainRects(
|
||||
BuildContext? context,
|
||||
int start,
|
||||
int end,
|
||||
RenderBox ancestor,
|
||||
) {
|
||||
final paragraph = chatFindTextRenderer(context);
|
||||
if (paragraph == null || !paragraph.attached || !paragraph.hasSize) {
|
||||
return const [];
|
||||
}
|
||||
final selection = TextSelection(baseOffset: start, extentOffset: end);
|
||||
final boxes = switch (paragraph) {
|
||||
RenderParagraph() => paragraph.getBoxesForSelection(selection),
|
||||
RenderEditable() => paragraph.getBoxesForSelection(selection),
|
||||
_ => const <TextBox>[],
|
||||
};
|
||||
final transform = paragraph.getTransformTo(ancestor);
|
||||
return [
|
||||
for (final box in boxes) MatrixUtils.transformRect(transform, box.toRect()),
|
||||
];
|
||||
}
|
||||
|
||||
/// The first [RenderParagraph] or [RenderEditable] under [context].
|
||||
RenderBox? chatFindTextRenderer(BuildContext? context) {
|
||||
RenderBox? found;
|
||||
void visit(RenderObject object) {
|
||||
if (found != null) return;
|
||||
if (object is RenderParagraph || object is RenderEditable) {
|
||||
found = object as RenderBox;
|
||||
} else {
|
||||
object.visitChildren(visit);
|
||||
}
|
||||
}
|
||||
|
||||
final root = context?.findRenderObject();
|
||||
if (root != null) visit(root);
|
||||
return found;
|
||||
}
|
||||
@@ -2,6 +2,7 @@ import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'chat_find_highlight.dart';
|
||||
import 'chat_link_launcher.dart';
|
||||
import 'chat_link_spans.dart';
|
||||
import 'chat_links.dart';
|
||||
@@ -38,8 +39,19 @@ class _ChatLinkTextState extends State<ChatLinkText> {
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
final _textKey = GlobalKey();
|
||||
|
||||
/// The text with find's matches painted behind it; it renders verbatim,
|
||||
/// so a match's offsets are the rendered characters'.
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
Widget build(BuildContext context) => ChatFindHighlight(
|
||||
text: widget.text,
|
||||
rects: (start, end, box) =>
|
||||
chatFindPlainRects(_textKey.currentContext, start, end, box),
|
||||
child: KeyedSubtree(key: _textKey, child: _text(context)),
|
||||
);
|
||||
|
||||
Widget _text(BuildContext context) {
|
||||
final text = widget.text;
|
||||
if (_found.isEmpty) {
|
||||
return ChatTranscriptSelectionScope.maybeOf(context) == null
|
||||
|
||||
@@ -5,6 +5,7 @@ import 'package:flutter/rendering.dart';
|
||||
|
||||
import 'chat_transcript_selection.dart';
|
||||
import 'chat_code_hover.dart';
|
||||
import 'chat_find_highlight.dart';
|
||||
import '../../../../ui/text/monospace.dart';
|
||||
import 'chat_link_launcher.dart';
|
||||
import 'chat_link_spans.dart';
|
||||
@@ -110,7 +111,7 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
|
||||
// The text is built once and never restyled while speaking; the
|
||||
// spoken word is painted behind it, so nothing shifts or rewraps.
|
||||
final rendered = _rendered ??= _buildRendered(context);
|
||||
return CustomPaint(
|
||||
final spoken = CustomPaint(
|
||||
key: _paintKey,
|
||||
painter: SpokenWordPainter(
|
||||
anchor: _paintKey,
|
||||
@@ -121,6 +122,13 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
|
||||
),
|
||||
child: rendered,
|
||||
);
|
||||
// Find's matches are painted the same way, behind the text.
|
||||
return ChatFindHighlight(
|
||||
text: widget.text,
|
||||
rects: (start, end, box) =>
|
||||
spokenWordRects(_segments, (start: start, end: end), box),
|
||||
child: spoken,
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -252,6 +260,7 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
|
||||
],
|
||||
);
|
||||
case MarkdownCode(language: final language, code: final code):
|
||||
final codeKey = _codeSegment(code);
|
||||
_skipCodeBlock(code);
|
||||
final labelStyle = _roleStyle(
|
||||
context,
|
||||
@@ -267,6 +276,7 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
|
||||
child: ChatTranscriptHorizontalScrollView(
|
||||
label: 'code block',
|
||||
child: Text.rich(
|
||||
key: codeKey,
|
||||
TextSpan(
|
||||
children: _linked(
|
||||
context,
|
||||
@@ -283,6 +293,27 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Records where a code block's text renders, character for character
|
||||
/// from its place in the source, so find can highlight matches in it.
|
||||
GlobalKey _codeSegment(String code) {
|
||||
if (_textCount == _textKeys.length) _textKeys.add(GlobalKey());
|
||||
final key = _textKeys[_textCount++];
|
||||
final start = code.isEmpty ? -1 : widget.text.indexOf(code, _sourceCursor);
|
||||
if (start >= 0) {
|
||||
_segments.add(
|
||||
MarkdownSpokenSegment(
|
||||
key,
|
||||
start,
|
||||
start + code.length,
|
||||
0,
|
||||
code.length,
|
||||
List<int>.generate(code.length, (index) => start + index),
|
||||
),
|
||||
);
|
||||
}
|
||||
return key;
|
||||
}
|
||||
|
||||
void _skipCodeBlock(String code) {
|
||||
_sourceCursor = fencedCodeSpan(
|
||||
widget.text,
|
||||
|
||||
Reference in New Issue
Block a user