Files
pipelines/app/lib/features/chat/ui/transcript/chat_find_highlight.dart
T

283 lines
8.1 KiB
Dart

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) {
// Another text with the same row and string may have unregistered
// the shared entry; put it back.
if (next != null) {
targets!.texts.putIfAbsent(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;
}