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:
@@ -12,6 +12,13 @@ extension _SettingsPageSections on _SettingsScreenState {
|
||||
keywords: 'text size scale zoom large small font',
|
||||
builder: (_) => const SettingsScaleSection(),
|
||||
),
|
||||
if (speechController.supported)
|
||||
const SettingsSection(
|
||||
id: 'read-aloud',
|
||||
title: 'Read aloud',
|
||||
keywords: 'speech voice audio speaking rate read aloud',
|
||||
builder: _readAloudBuilder,
|
||||
),
|
||||
SettingsSection(
|
||||
id: 'animations',
|
||||
title: 'Animations',
|
||||
@@ -216,6 +223,9 @@ extension _SettingsPageSections on _SettingsScreenState {
|
||||
];
|
||||
}
|
||||
|
||||
static Widget _readAloudBuilder(BuildContext _) =>
|
||||
const SettingsReadAloudSection();
|
||||
|
||||
/// A group holding one free-form block (a form, a status with buttons).
|
||||
Widget _single(String title, Widget child) => SettingsGroup(
|
||||
title: title,
|
||||
|
||||
@@ -0,0 +1,134 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../chat/speech/speech.dart';
|
||||
import 'settings_group.dart';
|
||||
import 'settings_row.dart';
|
||||
import 'settings_theme.dart';
|
||||
import 'settings_voice_picker.dart';
|
||||
|
||||
/// Read aloud: the voice and speaking rate replies are read with, saved on
|
||||
/// this device, with a sample and a reset to the system voice at normal speed.
|
||||
class SettingsReadAloudSection extends StatefulWidget {
|
||||
const SettingsReadAloudSection({super.key, this.controller});
|
||||
|
||||
/// The reader; the app's own by default.
|
||||
final SpeechController? controller;
|
||||
|
||||
@override
|
||||
State<SettingsReadAloudSection> createState() =>
|
||||
_SettingsReadAloudSectionState();
|
||||
}
|
||||
|
||||
class _SettingsReadAloudSectionState extends State<SettingsReadAloudSection> {
|
||||
SpeechController get _speech => widget.controller ?? speechController;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
unawaited(_speech.restore());
|
||||
}
|
||||
|
||||
Future<void> _pickVoice() async {
|
||||
final choice = await showSpeechVoicePicker(context, _speech);
|
||||
if (choice != null) await _speech.setVoice(choice.voice);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => ListenableBuilder(
|
||||
listenable: _speech,
|
||||
builder: (context, _) {
|
||||
final rate = _speech.rate;
|
||||
final label = '${rate.toStringAsFixed(1)}×';
|
||||
final voice = _speech.voice;
|
||||
final sampling = _speech.messageId == speechSampleId;
|
||||
return SettingsGroup(
|
||||
title: 'Read aloud',
|
||||
footer: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
const Text(
|
||||
'Saved on this device. Replies are read with this voice and rate.',
|
||||
),
|
||||
if (_speech.saveFailed)
|
||||
Text(
|
||||
'Could not save; applies until reload.',
|
||||
style: TextStyle(color: settingsErrorColor(context)),
|
||||
),
|
||||
],
|
||||
),
|
||||
children: [
|
||||
SettingsRow(
|
||||
key: const Key('settings-read-aloud-voice'),
|
||||
leading: const Icon(Icons.record_voice_over_outlined),
|
||||
title: const Text('Voice'),
|
||||
subtitle: Text(
|
||||
voice == null
|
||||
? 'System default'
|
||||
: '${voice.name} · ${voice.locale}',
|
||||
),
|
||||
chevron: true,
|
||||
onTap: () => unawaited(_pickVoice()),
|
||||
),
|
||||
SettingsGroupItem(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
Text(
|
||||
'Speaking rate $label',
|
||||
style: settingsTextStyle(context, title: true),
|
||||
),
|
||||
Semantics(
|
||||
label: 'Speaking rate',
|
||||
child: Slider(
|
||||
key: const Key('settings-read-aloud-rate'),
|
||||
value: rate,
|
||||
min: minSpeechRate,
|
||||
max: maxSpeechRate,
|
||||
divisions: 15,
|
||||
label: label,
|
||||
semanticFormatterCallback: (value) =>
|
||||
'${value.toStringAsFixed(1)} times',
|
||||
onChanged: (value) =>
|
||||
unawaited(_speech.setRate(value, save: false)),
|
||||
onChangeEnd: (value) => unawaited(_speech.setRate(value)),
|
||||
),
|
||||
),
|
||||
const Wrap(
|
||||
alignment: WrapAlignment.spaceBetween,
|
||||
children: [Text('0.5× · Slower'), Text('2.0× · Faster')],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
SettingsGroupItem(
|
||||
child: Wrap(
|
||||
spacing: 8,
|
||||
runSpacing: 8,
|
||||
crossAxisAlignment: WrapCrossAlignment.center,
|
||||
children: [
|
||||
FilledButton.tonalIcon(
|
||||
key: const Key('settings-read-aloud-sample'),
|
||||
onPressed: () =>
|
||||
unawaited(sampling ? _speech.stop() : _speech.sample()),
|
||||
icon: Icon(
|
||||
sampling ? Icons.stop_outlined : Icons.play_arrow_outlined,
|
||||
),
|
||||
label: Text(sampling ? 'Stop sample' : 'Play sample'),
|
||||
),
|
||||
TextButton(
|
||||
key: const Key('settings-read-aloud-reset'),
|
||||
onPressed: _speech.isDefault
|
||||
? null
|
||||
: () => unawaited(_speech.reset()),
|
||||
child: const Text('Reset to defaults'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -40,6 +40,8 @@ import 'managed_connection_section.dart';
|
||||
import 'settings_keyboard_shortcuts_section.dart';
|
||||
import 'settings_keys.dart';
|
||||
import 'settings_row.dart';
|
||||
import 'settings_read_aloud_section.dart';
|
||||
import '../chat/speech/speech_controller.dart';
|
||||
|
||||
part 'settings_page_sections.dart';
|
||||
|
||||
|
||||
@@ -0,0 +1,203 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../chat/speech/speech.dart';
|
||||
|
||||
/// The picker's answer: a voice, or null for the system default.
|
||||
typedef SpeechVoiceChoice = ({SpeechVoice? voice});
|
||||
|
||||
/// Lists the platform's voices grouped by language, the device's language
|
||||
/// first, each with a sample. Resolves to null when dismissed.
|
||||
Future<SpeechVoiceChoice?> showSpeechVoicePicker(
|
||||
BuildContext context,
|
||||
SpeechController speech,
|
||||
) async {
|
||||
final choice = await showDialog<SpeechVoiceChoice>(
|
||||
context: context,
|
||||
builder: (_) => _VoicePickerDialog(speech: speech),
|
||||
);
|
||||
if (speech.messageId == speechSampleId) unawaited(speech.stop());
|
||||
return choice;
|
||||
}
|
||||
|
||||
/// Voices grouped by language subtag, the [device] language first and the
|
||||
/// rest by name; voices within a group by name.
|
||||
List<({String language, List<SpeechVoice> voices})> groupSpeechVoices(
|
||||
List<SpeechVoice> voices,
|
||||
String device,
|
||||
) {
|
||||
final groups = <String, List<SpeechVoice>>{};
|
||||
for (final voice in voices) {
|
||||
(groups[voice.language] ??= []).add(voice);
|
||||
}
|
||||
final languages = groups.keys.toList()
|
||||
..sort((a, b) {
|
||||
if (a == device.toLowerCase()) return -1;
|
||||
if (b == device.toLowerCase()) return 1;
|
||||
return speechLanguageName(a).compareTo(speechLanguageName(b));
|
||||
});
|
||||
return [
|
||||
for (final language in languages)
|
||||
(
|
||||
language: language,
|
||||
voices: groups[language]!
|
||||
..sort((a, b) {
|
||||
final name = a.name.compareTo(b.name);
|
||||
return name != 0 ? name : a.locale.compareTo(b.locale);
|
||||
}),
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
/// An English name for common language subtags; others show their code.
|
||||
String speechLanguageName(String language) =>
|
||||
_languageNames[language] ?? language.toUpperCase();
|
||||
|
||||
const _languageNames = {
|
||||
'ar': 'Arabic',
|
||||
'bg': 'Bulgarian',
|
||||
'ca': 'Catalan',
|
||||
'cs': 'Czech',
|
||||
'da': 'Danish',
|
||||
'de': 'German',
|
||||
'el': 'Greek',
|
||||
'en': 'English',
|
||||
'es': 'Spanish',
|
||||
'fi': 'Finnish',
|
||||
'fr': 'French',
|
||||
'he': 'Hebrew',
|
||||
'hi': 'Hindi',
|
||||
'hr': 'Croatian',
|
||||
'hu': 'Hungarian',
|
||||
'id': 'Indonesian',
|
||||
'it': 'Italian',
|
||||
'ja': 'Japanese',
|
||||
'ko': 'Korean',
|
||||
'ms': 'Malay',
|
||||
'nb': 'Norwegian',
|
||||
'nl': 'Dutch',
|
||||
'pl': 'Polish',
|
||||
'pt': 'Portuguese',
|
||||
'ro': 'Romanian',
|
||||
'ru': 'Russian',
|
||||
'sk': 'Slovak',
|
||||
'sv': 'Swedish',
|
||||
'th': 'Thai',
|
||||
'tr': 'Turkish',
|
||||
'uk': 'Ukrainian',
|
||||
'vi': 'Vietnamese',
|
||||
'zh': 'Chinese',
|
||||
};
|
||||
|
||||
class _VoicePickerDialog extends StatefulWidget {
|
||||
const _VoicePickerDialog({required this.speech});
|
||||
|
||||
final SpeechController speech;
|
||||
|
||||
@override
|
||||
State<_VoicePickerDialog> createState() => _VoicePickerDialogState();
|
||||
}
|
||||
|
||||
class _VoicePickerDialogState extends State<_VoicePickerDialog> {
|
||||
late final Future<List<SpeechVoice>> _voices = widget.speech.voices();
|
||||
|
||||
String _keyOf(SpeechVoice? voice) =>
|
||||
voice == null ? '' : '${voice.name}\n${voice.locale}';
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final device = View.of(context).platformDispatcher.locale.languageCode;
|
||||
return AlertDialog(
|
||||
title: const Text('Voice'),
|
||||
contentPadding: const EdgeInsets.symmetric(vertical: 16),
|
||||
// A list needs a definite width; keep it to a readable dialog measure.
|
||||
content: ConstrainedBox(
|
||||
constraints: const BoxConstraints(maxWidth: 560),
|
||||
child: SizedBox(
|
||||
width: double.maxFinite,
|
||||
child: FutureBuilder<List<SpeechVoice>>(
|
||||
future: _voices,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState != ConnectionState.done) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
final voices = snapshot.data ?? const <SpeechVoice>[];
|
||||
return _list(context, groupSpeechVoices(voices, device));
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(context).pop(),
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _list(
|
||||
BuildContext context,
|
||||
List<({String language, List<SpeechVoice> voices})> groups,
|
||||
) {
|
||||
final theme = Theme.of(context);
|
||||
final header = theme.textTheme.titleSmall?.copyWith(
|
||||
color: theme.colorScheme.primary,
|
||||
);
|
||||
return RadioGroup<String>(
|
||||
groupValue: _keyOf(widget.speech.voice),
|
||||
onChanged: (key) {
|
||||
final voice = [
|
||||
for (final group in groups) ...group.voices,
|
||||
].where((voice) => _keyOf(voice) == key).firstOrNull;
|
||||
Navigator.of(context).pop((voice: voice));
|
||||
},
|
||||
child: ListView(
|
||||
shrinkWrap: true,
|
||||
children: [
|
||||
const RadioListTile<String>(value: '', title: Text('System default')),
|
||||
if (groups.isEmpty)
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 8),
|
||||
child: Text(
|
||||
'This device offers no other voices.',
|
||||
style: TextStyle(color: theme.colorScheme.onSurfaceVariant),
|
||||
),
|
||||
),
|
||||
for (final group in groups) ...[
|
||||
Padding(
|
||||
padding: const EdgeInsetsDirectional.fromSTEB(24, 16, 24, 4),
|
||||
child: Semantics(
|
||||
header: true,
|
||||
child: Text(speechLanguageName(group.language), style: header),
|
||||
),
|
||||
),
|
||||
for (final voice in group.voices)
|
||||
// The sample is its own control beside the choice, not merged
|
||||
// into the radio tile's semantics.
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: RadioListTile<String>(
|
||||
value: _keyOf(voice),
|
||||
title: Text(voice.name),
|
||||
subtitle: Text(voice.locale),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: const EdgeInsetsDirectional.only(end: 12),
|
||||
child: IconButton(
|
||||
tooltip: 'Play sample of ${voice.name}',
|
||||
onPressed: () => unawaited(widget.speech.sample(voice)),
|
||||
icon: const Icon(Icons.play_arrow_outlined),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user