Read chat replies aloud with word highlighting
An assistant reply gets Read aloud beside Copy; while it reads it becomes Pause/Resume and Stop, one reply at a time app-wide. The spoken word is highlighted in the rendered reply (across multi-part replies) and the transcript scrolls only when that word leaves the viewport. Reading stops when its chat closes or is swapped, another reply starts, or the app goes to the background. Markdown is spoken as sentences; code blocks are announced as "Code block.". Speech goes through a SpeechEngine seam with a flutter_tts implementation (Android, macOS, web; hidden on Linux) and a fake for tests. Settings has a Read aloud section: a voice picker grouped by language (device language first, a sample per voice), a rate slider and reset, saved per device. flutter_tts has no Swift package, so macOS/iOS gain the CocoaPods integration (Podfiles with the Runner deployment targets); Android declares the TTS_SERVICE query. The capture fixture gains ?speech=fake.
This commit is contained in:
@@ -51,6 +51,7 @@ class ChatCopyGutterRow extends StatefulWidget {
|
||||
required this.width,
|
||||
required this.copy,
|
||||
required this.child,
|
||||
this.pinned = false,
|
||||
});
|
||||
|
||||
final double width;
|
||||
@@ -59,6 +60,9 @@ class ChatCopyGutterRow extends StatefulWidget {
|
||||
final Widget? copy;
|
||||
final Widget child;
|
||||
|
||||
/// Keeps the buttons shown without hover (while the reply reads aloud).
|
||||
final bool pinned;
|
||||
|
||||
@override
|
||||
State<ChatCopyGutterRow> createState() => _ChatCopyGutterRowState();
|
||||
}
|
||||
@@ -89,7 +93,9 @@ class _ChatCopyGutterRowState extends State<ChatCopyGutterRow> {
|
||||
setState(() => _focused = focused),
|
||||
child: AnimatedOpacity(
|
||||
key: const Key('chat-copy-gutter-visibility'),
|
||||
opacity: !hover || _hovered || _focused ? 1 : 0,
|
||||
opacity: !hover || widget.pinned || _hovered || _focused
|
||||
? 1
|
||||
: 0,
|
||||
duration: const Duration(milliseconds: 120),
|
||||
alwaysIncludeSemantics: true,
|
||||
child: copy,
|
||||
|
||||
@@ -0,0 +1,181 @@
|
||||
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);
|
||||
}
|
||||
|
||||
/// [text] as spans whose characters with source [offsets] inside [range] are
|
||||
/// filled with [fill]; plain text when nothing in it is spoken.
|
||||
TextSpan spokenTextSpan(
|
||||
String text,
|
||||
List<int> offsets,
|
||||
SpeechRange? range,
|
||||
Color fill,
|
||||
) {
|
||||
if (range == null || offsets.isEmpty) return TextSpan(text: text);
|
||||
bool spoken(int index) =>
|
||||
offsets[index] >= range.start && offsets[index] < range.end;
|
||||
final children = <InlineSpan>[];
|
||||
var runStart = 0;
|
||||
for (var index = 1; index <= text.length; index += 1) {
|
||||
if (index < text.length && spoken(index) == spoken(runStart)) continue;
|
||||
children.add(
|
||||
TextSpan(
|
||||
text: text.substring(runStart, index),
|
||||
style: spoken(runStart) ? TextStyle(backgroundColor: fill) : null,
|
||||
),
|
||||
);
|
||||
runStart = index;
|
||||
}
|
||||
return TextSpan(children: children);
|
||||
}
|
||||
|
||||
/// Scrolls [scroll] just enough to show the spoken [range] when it has left
|
||||
/// the viewport; a word already in view never moves the transcript.
|
||||
void revealSpokenWord(
|
||||
ScrollPosition scroll,
|
||||
List<MarkdownSpokenSegment> 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 (segment == null || paragraph == null) return;
|
||||
final boxes = paragraph.getBoxesForSelection(
|
||||
TextSelection(
|
||||
baseOffset: segment.renderedStart,
|
||||
extentOffset: segment.renderedEnd,
|
||||
),
|
||||
);
|
||||
final viewport = RenderAbstractViewport.maybeOf(paragraph);
|
||||
if (boxes.isEmpty || viewport == null) return;
|
||||
final rect = boxes.first.toRect().inflate(_revealMargin);
|
||||
final leading = viewport.getOffsetToReveal(paragraph, 0, rect: rect).offset;
|
||||
final trailing = viewport.getOffsetToReveal(paragraph, 1, rect: rect).offset;
|
||||
final low = leading < trailing ? leading : trailing;
|
||||
final high = leading < trailing ? trailing : leading;
|
||||
final target = scroll.pixels
|
||||
.clamp(low, high)
|
||||
.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<int> consume(String text) {
|
||||
final offsets = <int>[];
|
||||
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,
|
||||
);
|
||||
|
||||
final GlobalKey key;
|
||||
final int sourceStart;
|
||||
final int sourceEnd;
|
||||
final int renderedStart;
|
||||
final int renderedEnd;
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/semantics.dart';
|
||||
import 'package:flutter/rendering.dart';
|
||||
|
||||
import 'chat_transcript_selection.dart';
|
||||
import 'chat_code_hover.dart';
|
||||
@@ -10,7 +10,9 @@ import 'chat_link_launcher.dart';
|
||||
import 'chat_link_spans.dart';
|
||||
import 'chat_markdown.dart';
|
||||
import 'chat_markdown_cache.dart';
|
||||
import 'chat_markdown_speech.dart';
|
||||
import 'chat_message_bubble.dart';
|
||||
import '../../speech/speech.dart';
|
||||
|
||||
/// Renders one assistant text part with native text widgets and the host's own
|
||||
/// theme roles. Links (`http`, `https`, `mailto` and `www.` hosts only) open
|
||||
@@ -19,9 +21,15 @@ import 'chat_message_bubble.dart';
|
||||
/// dimension is overridden; code keeps its exact whitespace and scrolls
|
||||
/// sideways instead of widening the transcript.
|
||||
class ChatMarkdownView extends StatefulWidget {
|
||||
const ChatMarkdownView({super.key, required this.text, this.onOpenLink});
|
||||
const ChatMarkdownView({
|
||||
super.key,
|
||||
required this.text,
|
||||
this.messageId,
|
||||
this.onOpenLink,
|
||||
});
|
||||
|
||||
final String text;
|
||||
final String? messageId;
|
||||
final ValueChanged<String>? onOpenLink;
|
||||
|
||||
@override
|
||||
@@ -44,6 +52,14 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
|
||||
final _codeHover = ChatCodeHover();
|
||||
ScrollPosition? _scroll;
|
||||
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;
|
||||
final _segments = <MarkdownSpokenSegment>[];
|
||||
|
||||
@override
|
||||
void didUpdateWidget(ChatMarkdownView oldWidget) {
|
||||
@@ -77,12 +93,35 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
// A new token or usage update elsewhere in the turn must not reparse and
|
||||
// rebuild every earlier answer part, or disturb its text selection.
|
||||
if (_rendered != null) return _rendered!;
|
||||
// Keep the state subscribed to inherited styling even when the cached
|
||||
// subtree is returned without rebuilding its Markdown spans.
|
||||
Theme.of(context);
|
||||
final speech = speechController;
|
||||
return ListenableBuilder(
|
||||
listenable: Listenable.merge([speech, speech.word]),
|
||||
builder: (context, _) {
|
||||
final range = spokenSourceIn(speech, widget.messageId, widget.text);
|
||||
if (range == null) {
|
||||
_revealed = null;
|
||||
return _rendered ?? _buildRendered(context, null);
|
||||
}
|
||||
if (range != _revealed) {
|
||||
_revealed = range;
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) => _reveal(range));
|
||||
}
|
||||
return _buildRendered(context, range);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildRendered(BuildContext context, SpeechRange? range) {
|
||||
_sourceCursor = 0;
|
||||
_textCount = 0;
|
||||
_highlight = range;
|
||||
_segments.clear();
|
||||
final gap = chatParagraphGap(context);
|
||||
final blocks = chatMarkdownCache.blocksFor(widget.text);
|
||||
return _rendered = Listener(
|
||||
final rendered = Listener(
|
||||
onPointerDown: (_) => _codeHover.hide(),
|
||||
onPointerSignal: (_) => _codeHover.hide(),
|
||||
child: NotificationListener<ScrollNotification>(
|
||||
@@ -108,6 +147,8 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
|
||||
),
|
||||
),
|
||||
);
|
||||
if (range == null) _rendered = rendered;
|
||||
return rendered;
|
||||
}
|
||||
|
||||
/// Blocks are separated by an empty line of body text, like paragraphs;
|
||||
@@ -196,6 +237,7 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
|
||||
),
|
||||
);
|
||||
case MarkdownCode(language: final language, code: final code):
|
||||
_skipCodeBlock(code);
|
||||
final labelStyle = _roleStyle(
|
||||
context,
|
||||
Theme.of(context).textTheme.labelSmall,
|
||||
@@ -220,6 +262,14 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
|
||||
}
|
||||
}
|
||||
|
||||
void _skipCodeBlock(String code) {
|
||||
_sourceCursor = fencedCodeSpan(
|
||||
widget.text,
|
||||
_sourceCursor,
|
||||
code,
|
||||
).end.clamp(0, widget.text.length);
|
||||
}
|
||||
|
||||
Widget _cell(BuildContext context, String text, {bool header = false}) {
|
||||
final style = header
|
||||
? _roleStyle(context, Theme.of(context).textTheme.labelLarge)
|
||||
@@ -234,43 +284,77 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
|
||||
}
|
||||
|
||||
Widget _text(BuildContext context, String text, {TextStyle? style}) {
|
||||
if (text.isEmpty) return const SizedBox.shrink();
|
||||
final sourceStart = widget.text.indexOf(text, _sourceCursor);
|
||||
if (sourceStart >= 0) _sourceCursor = sourceStart + text.length;
|
||||
// Keys follow render order, which is stable for the same text, so the
|
||||
// revealed word's paragraph can be found after the frame.
|
||||
if (_textCount == _textKeys.length) _textKeys.add(GlobalKey());
|
||||
final key = _textKeys[_textCount++];
|
||||
final source = MarkdownSourceCursor(
|
||||
widget.text,
|
||||
sourceStart < 0 ? _sourceCursor : sourceStart,
|
||||
key,
|
||||
);
|
||||
return Builder(
|
||||
builder: (textContext) => Text.rich(
|
||||
TextSpan(children: _spans(textContext, parseInline(text))),
|
||||
key: key,
|
||||
TextSpan(children: _spans(textContext, parseInline(text), source)),
|
||||
style: style,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
List<InlineSpan> _spans(BuildContext context, List<MarkdownInline> spans) => [
|
||||
List<InlineSpan> _spans(
|
||||
BuildContext context,
|
||||
List<MarkdownInline> spans,
|
||||
MarkdownSourceCursor source,
|
||||
) => [
|
||||
for (final span in spans)
|
||||
switch (span) {
|
||||
MarkdownPlain(text: final text) => TextSpan(text: text),
|
||||
MarkdownInlineCode(text: final text) => _inlineCode(context, text),
|
||||
MarkdownPlain(text: final text) => _leaf(context, text, source),
|
||||
MarkdownInlineCode(text: final text) => _inlineCode(
|
||||
context,
|
||||
text,
|
||||
source,
|
||||
),
|
||||
MarkdownStrong(spans: final inner) => TextSpan(
|
||||
style: const TextStyle(fontWeight: FontWeight.bold),
|
||||
children: _spans(context, inner),
|
||||
children: _spans(context, inner, source),
|
||||
),
|
||||
MarkdownEmphasis(spans: final inner) => TextSpan(
|
||||
style: const TextStyle(fontStyle: FontStyle.italic),
|
||||
children: _spans(context, inner),
|
||||
children: _spans(context, inner, source),
|
||||
),
|
||||
MarkdownLink(url: final url, spans: final inner) => _link(
|
||||
context,
|
||||
url,
|
||||
inner,
|
||||
source,
|
||||
),
|
||||
},
|
||||
];
|
||||
|
||||
TextSpan _inlineCode(BuildContext context, String text) {
|
||||
TextSpan _leaf(
|
||||
BuildContext context,
|
||||
String text,
|
||||
MarkdownSourceCursor source,
|
||||
) => _highlightedText(context, text, source);
|
||||
|
||||
TextSpan _inlineCode(
|
||||
BuildContext context,
|
||||
String text,
|
||||
MarkdownSourceCursor source,
|
||||
) {
|
||||
final token = text;
|
||||
final highlighted = _highlightedText(context, text, source);
|
||||
return TextSpan(
|
||||
text: text,
|
||||
style: TextStyle(
|
||||
fontFamily: appMonospaceFamily,
|
||||
backgroundColor: _codeFill(context),
|
||||
),
|
||||
text: highlighted.text,
|
||||
children: highlighted.children,
|
||||
onEnter: (event) {
|
||||
if (_route?.isCurrent != false) {
|
||||
_codeHover.show(context, token, text, event.position);
|
||||
@@ -284,10 +368,11 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
|
||||
BuildContext context,
|
||||
String url,
|
||||
List<MarkdownInline> spans,
|
||||
MarkdownSourceCursor source,
|
||||
) => _links.link(
|
||||
context,
|
||||
url,
|
||||
_spans(context, spans),
|
||||
_spans(context, spans, source),
|
||||
onOpen: (uri) {
|
||||
final onOpenLink = widget.onOpenLink;
|
||||
if (onOpenLink != null) return onOpenLink(uri.toString());
|
||||
@@ -295,6 +380,46 @@ class _ChatMarkdownViewState extends State<ChatMarkdownView> {
|
||||
},
|
||||
);
|
||||
|
||||
/// A leaf's spans, recording where it renders so a spoken word in it can
|
||||
/// be revealed, with the spoken characters highlighted.
|
||||
TextSpan _highlightedText(
|
||||
BuildContext context,
|
||||
String text,
|
||||
MarkdownSourceCursor source,
|
||||
) {
|
||||
final offsets = source.consume(text);
|
||||
final renderedStart = source.visibleOffset;
|
||||
source.visibleOffset += text.length;
|
||||
if (offsets.isNotEmpty) {
|
||||
_segments.add(
|
||||
MarkdownSpokenSegment(
|
||||
source.key,
|
||||
offsets.first,
|
||||
offsets.last + 1,
|
||||
renderedStart,
|
||||
source.visibleOffset,
|
||||
),
|
||||
);
|
||||
}
|
||||
return spokenTextSpan(
|
||||
text,
|
||||
offsets,
|
||||
_highlight,
|
||||
Theme.of(context).colorScheme.primaryContainer,
|
||||
);
|
||||
}
|
||||
|
||||
void _reveal(SpeechRange range) {
|
||||
final scroll = _scroll;
|
||||
if (!mounted || range != _revealed || scroll == null) return;
|
||||
revealSpokenWord(
|
||||
scroll,
|
||||
_segments,
|
||||
range,
|
||||
reduceMotion: MediaQuery.maybeDisableAnimationsOf(context) ?? false,
|
||||
);
|
||||
}
|
||||
|
||||
/// Heading roles are a non-increasing sequence from the host's own text
|
||||
/// theme. H3–H6 share one title role, matching the web renderer's flat
|
||||
/// lower-heading treatment, and no level falls back to a body role, so a
|
||||
|
||||
@@ -14,6 +14,8 @@ import 'chat_message_bubble.dart';
|
||||
import 'chat_turn_activity_view.dart';
|
||||
import 'native_disclosure.dart';
|
||||
import 'chat_message_timestamp.dart';
|
||||
import 'chat_read_aloud_button.dart';
|
||||
import '../../speech/speech_controller.dart';
|
||||
|
||||
/// Builds an assistant text part; the default keeps the server's text exactly.
|
||||
typedef ChatMessageTextBuilder = Widget Function(BuildContext, String);
|
||||
@@ -27,9 +29,14 @@ class ChatMessageView extends StatelessWidget {
|
||||
this.textBuilder,
|
||||
this.footer = const [],
|
||||
this.images,
|
||||
this.speechScope = 'chat',
|
||||
});
|
||||
|
||||
final JsonMap row;
|
||||
|
||||
/// Names the conversation in the reader's message ids, so leaving it can
|
||||
/// stop only its own reply (see [speechMessageId]).
|
||||
final String speechScope;
|
||||
final ChatMessageTextBuilder? textBuilder;
|
||||
|
||||
/// Loads attached images for the operator's turn; without one, only images
|
||||
@@ -179,13 +186,41 @@ class ChatMessageView extends StatelessWidget {
|
||||
Widget answer,
|
||||
double gutter,
|
||||
) {
|
||||
final copyText = row['streaming'] == true ? '' : _answerText(_partsOf(row));
|
||||
final copy = copyText.isEmpty ? null : ChatCopyButton(text: copyText);
|
||||
if (gutter <= 0) return _agentTurn(context, content, answer, copy);
|
||||
return ChatCopyGutterRow(
|
||||
width: gutter,
|
||||
copy: copy,
|
||||
child: _agentTurn(context, content, answer, null),
|
||||
final answerParts = row['streaming'] == true
|
||||
? const <String>[]
|
||||
: _answerParts(_partsOf(row));
|
||||
final copyText = answerParts.join('\n\n');
|
||||
final id = speechMessageId(speechScope, '${row['key'] ?? row['id'] ?? ''}');
|
||||
Widget? actions(Axis direction) => copyText.isEmpty
|
||||
? null
|
||||
: Flex(
|
||||
direction: direction,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
verticalDirection: VerticalDirection.up,
|
||||
children: [
|
||||
ChatCopyButton(text: copyText),
|
||||
ChatReadAloudButton(
|
||||
messageId: id,
|
||||
parts: answerParts,
|
||||
direction: direction,
|
||||
),
|
||||
],
|
||||
);
|
||||
if (gutter <= 0) {
|
||||
return _agentTurn(context, content, answer, actions(Axis.horizontal));
|
||||
}
|
||||
// The gutter is one button wide: Read aloud stacks above Copy, and stays
|
||||
// shown on a desktop while this reply reads.
|
||||
final turn = _agentTurn(context, content, answer, null);
|
||||
return ListenableBuilder(
|
||||
listenable: speechController,
|
||||
child: turn,
|
||||
builder: (context, child) => ChatCopyGutterRow(
|
||||
width: gutter,
|
||||
copy: actions(Axis.vertical),
|
||||
pinned: speechController.messageId == id,
|
||||
child: child!,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -267,13 +302,13 @@ class ChatMessageView extends StatelessWidget {
|
||||
|
||||
/// The answer as the operator reads it: the turn's text parts in order,
|
||||
/// without reasoning, tools or attachments.
|
||||
String _answerText(List<JsonMap> parts) => [
|
||||
List<String> _answerParts(List<JsonMap> parts) => [
|
||||
for (final part in parts)
|
||||
if ((part['type'] == null || part['type'] == 'text') &&
|
||||
part['text'] is String &&
|
||||
(part['text'] as String).trim().isNotEmpty)
|
||||
(part['text'] as String).trim(),
|
||||
].join('\n\n');
|
||||
];
|
||||
|
||||
List<Widget> _agentParts(BuildContext context) {
|
||||
final widgets = <Widget>[];
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../speech/speech.dart';
|
||||
|
||||
const chatReadAloudLabel = 'Read aloud';
|
||||
const chatPauseReadingLabel = 'Pause reading';
|
||||
const chatResumeReadingLabel = 'Resume reading';
|
||||
const chatStopReadingLabel = 'Stop reading';
|
||||
|
||||
/// Read aloud for one reply, styled like its Copy button. While this reply
|
||||
/// reads it becomes Pause (or Resume) and Stop, laid out along [direction]
|
||||
/// (a column in the narrow copy gutter). Absent where speech is unsupported.
|
||||
class ChatReadAloudButton extends StatelessWidget {
|
||||
const ChatReadAloudButton({
|
||||
super.key,
|
||||
required this.messageId,
|
||||
required this.parts,
|
||||
this.direction = Axis.horizontal,
|
||||
});
|
||||
|
||||
final String messageId;
|
||||
|
||||
/// The reply's markdown text parts, in order.
|
||||
final List<String> parts;
|
||||
final Axis direction;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final speech = speechController;
|
||||
if (!speech.supported) return const SizedBox.shrink();
|
||||
return ListenableBuilder(
|
||||
listenable: speech,
|
||||
builder: (context, _) {
|
||||
if (speech.messageId != messageId) {
|
||||
return IconButton(
|
||||
key: const Key('chat-message-read-aloud'),
|
||||
tooltip: chatReadAloudLabel,
|
||||
onPressed: () => unawaited(speech.read(messageId, parts)),
|
||||
icon: const Icon(Icons.volume_up_outlined),
|
||||
);
|
||||
}
|
||||
final paused = speech.state == SpeechState.paused;
|
||||
return Flex(
|
||||
direction: direction,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
IconButton(
|
||||
key: const Key('chat-message-read-aloud-pause'),
|
||||
tooltip: paused ? chatResumeReadingLabel : chatPauseReadingLabel,
|
||||
onPressed: () =>
|
||||
unawaited(paused ? speech.resume() : speech.pause()),
|
||||
icon: Icon(
|
||||
paused ? Icons.play_arrow_outlined : Icons.pause_outlined,
|
||||
),
|
||||
),
|
||||
IconButton(
|
||||
key: const Key('chat-message-read-aloud-stop'),
|
||||
tooltip: chatStopReadingLabel,
|
||||
onPressed: () => unawaited(speech.stop()),
|
||||
icon: const Icon(Icons.stop_outlined),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user