Files
pipelines/app/lib/features/chat/ui/transcript/chat_copy_gutter.dart
T
alan 6367cce68a 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.
2026-11-13 01:29:04 +00:00

117 lines
3.6 KiB
Dart

import 'package:flutter/material.dart';
import '../../../../ui/adaptive/platform.dart';
/// The width of the strip left of the reading column that an answer row
/// draws its Copy button in (`chatCopyGutter`); 0 when there is no room, and
/// Copy stays in the answer's footer.
class ChatCopyGutter extends InheritedWidget {
const ChatCopyGutter({super.key, required this.width, required super.child});
final double width;
static double of(BuildContext context) =>
context.dependOnInheritedWidgetOfExactType<ChatCopyGutter>()?.width ?? 0;
@override
bool updateShouldNotify(ChatCopyGutter oldWidget) => width != oldWidget.width;
}
/// The transcript's [children] when the list starts [gutter] left of the
/// reading column: every child except the transcript [rows] (which inset
/// themselves) is inset past the gutter, so it keeps to the column.
List<Widget> chatCopyGutterInset(
double gutter,
List<Widget> rows,
List<Widget> children,
) {
if (gutter <= 0) return children;
final own = Set<Widget>.identity()..addAll(rows);
return [
for (final child in children)
if (own.contains(child))
child
else
Padding(
key: child.key,
padding: EdgeInsets.only(left: gutter),
child: child,
),
];
}
/// An answer with its Copy button in the gutter to its left, bottom-aligned
/// with the answer's footer row. On a desktop the button shows while the
/// pointer is over the answer or the button has focus; it stays in the
/// focus order and the semantics tree when hidden. Touch platforms always
/// show it.
class ChatCopyGutterRow extends StatefulWidget {
const ChatCopyGutterRow({
super.key,
required this.width,
required this.copy,
required this.child,
this.pinned = false,
});
final double width;
/// The Copy button, or null when the answer has nothing to copy yet.
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();
}
class _ChatCopyGutterRowState extends State<ChatCopyGutterRow> {
bool _hovered = false;
bool _focused = false;
@override
Widget build(BuildContext context) {
final hover = prefersCompactHitTargets(context);
final copy = widget.copy;
final row = Row(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.end,
children: [
SizedBox(
key: const Key('chat-copy-gutter'),
width: widget.width,
child: copy == null
? null
: Align(
alignment: Alignment.bottomRight,
child: Focus(
canRequestFocus: false,
skipTraversal: true,
onFocusChange: (focused) =>
setState(() => _focused = focused),
child: AnimatedOpacity(
key: const Key('chat-copy-gutter-visibility'),
opacity: !hover || widget.pinned || _hovered || _focused
? 1
: 0,
duration: const Duration(milliseconds: 120),
alwaysIncludeSemantics: true,
child: copy,
),
),
),
),
Flexible(child: widget.child),
],
);
if (!hover || copy == null) return row;
return MouseRegion(
onEnter: (_) => setState(() => _hovered = true),
onExit: (_) => setState(() => _hovered = false),
child: row,
);
}
}