Steady read-aloud highlight and a calmer default pace

The spoken word is now painted behind unchanged text instead of
restyling the paragraph, so words never reshape, shift or rewrap as the
highlight moves. Revealing the word measures its own boxes against the
visible area, so a reversed transcript no longer jumps for a word already
in view. Sentence breaks the reading adds no longer map onto the next
item's first letter. 1.0x now reads at 0.8 of the platform's normal rate.
This commit is contained in:
2026-11-13 09:41:02 +00:00
parent b2736683e7
commit df0614e2f7
7 changed files with 310 additions and 78 deletions
@@ -54,8 +54,6 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
ModalRoute<dynamic>? _route;
var _sourceCursor = 0;
/// The source range of [ChatMarkdownView.text] being spoken, while building.
SpeechRange? _highlight;
SpeechRange? _revealed;
final _textKeys = <GlobalKey>[];
var _textCount = 0;
@@ -103,21 +101,33 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
final range = spokenSourceIn(speech, widget.messageId, widget.text);
if (range == null) {
_revealed = null;
return _rendered ?? _buildRendered(context, null);
}
if (range != _revealed) {
} else if (range != _revealed) {
_revealed = range;
WidgetsBinding.instance.addPostFrameCallback((_) => _reveal(range));
}
return _buildRendered(context, range);
// 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(
key: _paintKey,
painter: SpokenWordPainter(
anchor: _paintKey,
segments: _segments,
range: () => spokenSourceIn(speech, widget.messageId, widget.text),
color: Theme.of(context).colorScheme.primaryContainer,
repaint: Listenable.merge([speech, speech.word]),
),
child: rendered,
);
},
);
}
Widget _buildRendered(BuildContext context, SpeechRange? range) {
final _paintKey = GlobalKey();
Widget _buildRendered(BuildContext context) {
_sourceCursor = 0;
_textCount = 0;
_highlight = range;
_segments.clear();
final gap = chatParagraphGap(context);
final blocks = chatMarkdownCache.blocksFor(widget.text);
@@ -147,7 +157,7 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
),
),
);
if (range == null) _rendered = rendered;
_rendered = rendered;
return rendered;
}
@@ -394,19 +404,15 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
_segments.add(
MarkdownSpokenSegment(
source.key,
offsets.first,
offsets.last + 1,
offsets.reduce((a, b) => a < b ? a : b),
offsets.reduce((a, b) => a > b ? a : b) + 1,
renderedStart,
source.visibleOffset,
offsets,
),
);
}
return spokenTextSpan(
text,
offsets,
_highlight,
Theme.of(context).colorScheme.primaryContainer,
);
return TextSpan(text: text);
}
void _reveal(SpeechRange range) {