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:
@@ -1,29 +1,45 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:flutter/rendering.dart';
|
||||
import 'package:pipelines_app/features/chat/ui/transcript/chat_markdown_speech.dart';
|
||||
import 'package:pipelines_app/features/chat/ui/transcript/chat_markdown_view.dart';
|
||||
|
||||
import '../../speech/fake_speech_engine.dart';
|
||||
|
||||
/// The text of every span drawn with the spoken-word highlight.
|
||||
/// The rendered text the spoken-word highlight is painted behind, per view.
|
||||
List<String> _highlighted(WidgetTester tester, [Finder? within]) {
|
||||
final fill = Theme.of(
|
||||
tester.element(find.byType(ChatMarkdownView).first),
|
||||
).colorScheme.primaryContainer;
|
||||
final spans = <String>[];
|
||||
final texts = within == null
|
||||
? find.byType(RichText)
|
||||
: find.descendant(of: within, matching: find.byType(RichText));
|
||||
for (final rich in tester.widgetList<RichText>(texts)) {
|
||||
rich.text.visitChildren((span) {
|
||||
if (span is TextSpan &&
|
||||
span.text != null &&
|
||||
span.style?.backgroundColor == fill) {
|
||||
spans.add(span.text!);
|
||||
final paints = within == null
|
||||
? find.byType(CustomPaint)
|
||||
: find.descendant(of: within, matching: find.byType(CustomPaint));
|
||||
final words = <String>[];
|
||||
for (final paint in tester.widgetList<CustomPaint>(paints)) {
|
||||
final painter = paint.painter;
|
||||
if (painter is! SpokenWordPainter) continue;
|
||||
final range = painter.range();
|
||||
if (range == null) continue;
|
||||
final box = painter.anchor.currentContext!.findRenderObject()! as RenderBox;
|
||||
if (spokenWordRects(painter.segments, range, box).isEmpty) continue;
|
||||
for (final segment in painter.segments) {
|
||||
final chars = StringBuffer();
|
||||
final paragraph = tester
|
||||
.renderObjectList<RenderParagraph>(
|
||||
find.descendant(
|
||||
of: find.byKey(segment.key),
|
||||
matching: find.byType(RichText),
|
||||
),
|
||||
)
|
||||
.first;
|
||||
final plain = paragraph.text.toPlainText();
|
||||
for (var index = 0; index < segment.offsets.length; index += 1) {
|
||||
final offset = segment.offsets[index];
|
||||
if (offset >= range.start && offset < range.end) {
|
||||
chars.write(plain[segment.renderedStart + index]);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
});
|
||||
if (chars.isNotEmpty) words.add(chars.toString());
|
||||
}
|
||||
}
|
||||
return spans;
|
||||
return words;
|
||||
}
|
||||
|
||||
void main() {
|
||||
@@ -73,6 +89,51 @@ void main() {
|
||||
expect(_highlighted(tester), isEmpty);
|
||||
});
|
||||
|
||||
testWidgets('highlighting never restyles or moves the text', (tester) async {
|
||||
final engine = FakeSpeechEngine();
|
||||
final speech = installFakeSpeech(engine);
|
||||
const text =
|
||||
'Alpha beta gamma delta epsilon zeta eta theta iota kappa lambda mu '
|
||||
'nu xi omicron pi rho sigma tau upsilon phi chi psi omega.';
|
||||
await tester.pumpWidget(
|
||||
const MaterialApp(
|
||||
home: Scaffold(
|
||||
body: SizedBox(
|
||||
width: 300,
|
||||
child: ChatMarkdownView(text: text, messageId: 'm'),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
final paragraph = tester.renderObject<RenderParagraph>(
|
||||
find.byType(RichText).first,
|
||||
);
|
||||
final span = paragraph.text;
|
||||
final size = paragraph.size;
|
||||
Rect wordRect(String word) {
|
||||
final start = text.indexOf(word);
|
||||
return paragraph
|
||||
.getBoxesForSelection(
|
||||
TextSelection(baseOffset: start, extentOffset: start + word.length),
|
||||
)
|
||||
.first
|
||||
.toRect();
|
||||
}
|
||||
|
||||
final omega = wordRect('omega');
|
||||
await speech.read('m', [text]);
|
||||
engine.onStart!();
|
||||
for (final word in ['beta', 'kappa', 'sigma', 'omega']) {
|
||||
engine.speakWord(word);
|
||||
await tester.pump();
|
||||
expect(_highlighted(tester), [word]);
|
||||
// Same spans, same size, same word positions: nothing shifts.
|
||||
expect(identical(paragraph.text, span), isTrue);
|
||||
expect(paragraph.size, size);
|
||||
expect(wordRect('omega'), omega);
|
||||
}
|
||||
});
|
||||
|
||||
testWidgets('the transcript scrolls only when the word leaves the view', (
|
||||
tester,
|
||||
) async {
|
||||
@@ -117,4 +178,48 @@ void main() {
|
||||
await tester.pumpAndSettle();
|
||||
expect(scroll.offset, followed);
|
||||
});
|
||||
|
||||
testWidgets('a reversed transcript never jumps for a word already in view', (
|
||||
tester,
|
||||
) async {
|
||||
final engine = FakeSpeechEngine();
|
||||
final speech = installFakeSpeech(engine);
|
||||
final text = [
|
||||
for (var index = 0; index < 30; index += 1) 'Line $index words here.',
|
||||
].join('\n\n');
|
||||
final scroll = ScrollController();
|
||||
addTearDown(scroll.dispose);
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(
|
||||
body: SizedBox(
|
||||
height: 400,
|
||||
child: ListView(
|
||||
controller: scroll,
|
||||
reverse: true,
|
||||
children: [
|
||||
const SizedBox(height: 50),
|
||||
ChatMarkdownView(text: text, messageId: 'm'),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
await speech.read('m', [text]);
|
||||
engine.onStart!();
|
||||
// The newest lines sit at the bottom of a reversed transcript, in view.
|
||||
for (final word in ['Line 28 ', 'Line 29 ', 'here.']) {
|
||||
engine.speakWord(word, from: engine.spoken.last.indexOf('Line 28'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(scroll.offset, 0, reason: '"$word" is in view');
|
||||
}
|
||||
// An early line above the view scrolls just into view, and only then.
|
||||
engine.speakWord('Line 2 ');
|
||||
await tester.pumpAndSettle();
|
||||
expect(scroll.offset, greaterThan(0));
|
||||
final viewport = tester.getRect(find.byType(ListView));
|
||||
final word = find.textContaining('Line 2 ', findRichText: true);
|
||||
expect(viewport.overlaps(tester.getRect(word)), isTrue);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user