import 'dart:async'; import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; import '../../speech/speech.dart'; /// Space kept between a revealed word and the transcript's edge. const double _revealMargin = 24; /// The range of [text] (one part of reply [messageId]) that [speech] is /// speaking now, or null. SpeechRange? spokenSourceIn( SpeechController speech, String? messageId, String text, ) { final document = speech.document; final word = speech.word.value; if (messageId == null || speech.messageId != messageId) return null; if (document == null || word == null) return null; final part = document.partOf(text); if (part < 0) return null; final range = document.sourceRange(part, word); if (range == null) return null; // Document parts are trimmed; the view's offsets include the lead. final lead = text.length - text.trimLeft().length; return (start: range.start + lead, end: range.end + lead); } /// Paints the spoken word's highlight behind the rendered text. /// /// The text itself is never restyled while speaking: splitting a paragraph /// into differently styled runs changes how it is shaped, so words would /// shift and lines rewrap as the highlight moves. Instead this painter, under /// the text, looks up the spoken characters' boxes in the laid-out paragraphs /// ([segments]) and fills them; each new word only repaints. class SpokenWordPainter extends CustomPainter { SpokenWordPainter({ required this.anchor, required this.segments, required this.range, required this.color, super.repaint, }); /// The key of the [CustomPaint] this painter draws in. final GlobalKey anchor; final List segments; final SpeechRange? Function() range; final Color color; @override void paint(Canvas canvas, Size size) { final spoken = range(); final box = anchor.currentContext?.findRenderObject(); if (spoken == null || box is! RenderBox || !box.attached) return; final fill = Paint()..color = color; for (final rect in spokenWordRects(segments, spoken, box)) { canvas.drawRRect( RRect.fromRectAndRadius(rect.inflate(1.5), const Radius.circular(4)), fill, ); } } @override bool shouldRepaint(SpokenWordPainter oldDelegate) => oldDelegate.color != color || !identical(oldDelegate.segments, segments); } /// The boxes, in [ancestor]'s coordinates, of the rendered characters whose /// source offsets fall inside [range]. List spokenWordRects( List segments, SpeechRange range, RenderBox ancestor, ) { final rects = []; for (final segment in segments) { if (segment.sourceStart >= range.end || segment.sourceEnd <= range.start) { continue; } int? first; var last = 0; for (var index = 0; index < segment.offsets.length; index += 1) { final offset = segment.offsets[index]; if (offset >= range.start && offset < range.end) { first ??= index; last = index + 1; } } if (first == null) continue; final paragraph = _paragraphIn(segment.key.currentContext); if (paragraph == null || !paragraph.attached || !paragraph.hasSize) { continue; } final transform = paragraph.getTransformTo(ancestor); for (final box in paragraph.getBoxesForSelection( TextSelection( baseOffset: segment.renderedStart + first, extentOffset: segment.renderedStart + last, ), )) { rects.add(MatrixUtils.transformRect(transform, box.toRect())); } } return rects; } /// Scrolls [scroll] just enough to show the spoken [range] when it has left /// the viewport; a word already in view never moves the transcript. /// /// The word's own boxes are measured against the visible area directly, in /// the viewport's coordinates, so a reversed or multi-sliver transcript /// scrolls only by the distance the word is out of view. void revealSpokenWord( ScrollPosition scroll, List segments, SpeechRange range, { required bool reduceMotion, }) { if (!scroll.hasContentDimensions) return; final segment = segments .where( (item) => item.sourceStart < range.end && item.sourceEnd > range.start, ) .firstOrNull; final paragraph = _paragraphIn(segment?.key.currentContext); if (paragraph == null || !paragraph.attached) return; final viewport = RenderAbstractViewport.maybeOf(paragraph); if (viewport is! RenderBox || !(viewport as RenderBox).hasSize) return; final box = viewport as RenderBox; final rects = spokenWordRects(segments, range, box); if (rects.isEmpty) return; final word = rects.reduce((a, b) => a.expandToInclude(b)); final vertical = scroll.axis == Axis.vertical; final extent = vertical ? box.size.height : box.size.width; final start = vertical ? word.top : word.left; final end = vertical ? word.bottom : word.right; // Positive when the word must move toward the start of the view. final double delta; if (start < _revealMargin) { delta = start - _revealMargin; } else if (end > extent - _revealMargin) { delta = end - (extent - _revealMargin); } else { return; } final reversed = scroll.axisDirection == AxisDirection.up || scroll.axisDirection == AxisDirection.left; final target = (scroll.pixels + (reversed ? -delta : delta)).clamp( scroll.minScrollExtent, scroll.maxScrollExtent, ); if ((target - scroll.pixels).abs() < 1) return; if (reduceMotion) { scroll.jumpTo(target); return; } unawaited( scroll.animateTo( target, duration: const Duration(milliseconds: 240), curve: Curves.easeOut, ), ); } /// The paragraph a keyed [Text] draws; selectable text wraps it in a mouse /// region, so the keyed element's own render object is not it. RenderParagraph? _paragraphIn(BuildContext? context) { RenderParagraph? found; void visit(RenderObject object) { if (found != null) return; if (object is RenderParagraph) { found = object; } else { object.visitChildren(visit); } } final root = context?.findRenderObject(); if (root != null) visit(root); return found; } /// Resolves visible inline leaves in order against the original block source. /// Searching from the previous match skips emphasis/link/code delimiters and /// keeps repeated words tied to their own occurrence. class MarkdownSourceCursor { MarkdownSourceCursor(this.source, int textStart, this.key) : _cursor = textStart; final String source; /// The paragraph these leaves render into. final GlobalKey key; var _cursor = 0; var visibleOffset = 0; List consume(String text) { final offsets = []; for (var index = 0; index < text.length; index += 1) { _skipLinkDestination(); final character = text.substring(index, index + 1); final searchStart = _cursor.clamp(0, source.length); final found = searchStart >= source.length ? -1 : source.indexOf(character, searchStart); final offset = found < 0 ? searchStart : found; offsets.add(offset); _cursor = found < 0 ? searchStart + 1 : found + 1; } return offsets; } void _skipLinkDestination() { if (_cursor >= source.length || source[_cursor] != ']') return; if (_cursor + 1 >= source.length || source[_cursor + 1] != '(') return; var depth = 0; for (var index = _cursor + 1; index < source.length; index += 1) { if (source[index] == '(') depth += 1; if (source[index] == ')' && --depth == 0) { _cursor = index + 1; return; } } } } /// One inline leaf: its source range and its character range in [key]'s /// rendered paragraph. class MarkdownSpokenSegment { const MarkdownSpokenSegment( this.key, this.sourceStart, this.sourceEnd, this.renderedStart, this.renderedEnd, [ this.offsets = const [], ]); final GlobalKey key; final int sourceStart; final int sourceEnd; final int renderedStart; final int renderedEnd; /// Each rendered character's offset in the source, in order. final List offsets; }