Files
pipelines/app/lib/features/chat/speech/app_speech.dart
T

51 lines
1.8 KiB
Dart

import 'package:flutter/foundation.dart';
import 'speech_controller.dart';
import 'speech_reading.dart';
/// The app's single reader, owned by the app (this window's engine), never by
/// a conversation view: a reply keeps reading across chat switches, panes and
/// the chat list. Each window (a detached window is its own app) has its own.
SpeechController get speechController =>
_speech ??= _bind<SpeechController>(SpeechController());
/// The app's reader if anything has used it yet. Code that only needs to stop
/// or retitle a reading uses this, so it never creates a platform engine.
SpeechController? get speechControllerIfCreated => _speech;
/// Replaces the app's reader (a fake engine in tests and capture fixtures);
/// null restores the platform one.
set debugSpeechController(SpeechController? controller) =>
_speech = _bind(controller);
/// The conversation the app's reader is reading, for the speaking indicator
/// and the global Stop. It follows the reader [debugSpeechController]
/// installs, and stays null until a reader exists.
ValueListenable<SpeechReading?> get speechReading => _reading;
SpeechController? _speech;
final _reading = _AppSpeechReading();
T _bind<T extends SpeechController?>(T controller) {
_reading.bind(controller);
return controller;
}
class _AppSpeechReading extends ChangeNotifier
implements ValueListenable<SpeechReading?> {
SpeechController? _source;
@override
SpeechReading? get value => _source?.reading.value;
void bind(SpeechController? source) {
final before = value;
_source?.reading.removeListener(notifyListeners);
_source = source;
source?.reading.addListener(notifyListeners);
// The reader is created lazily, often while a reply builds: a fresh one
// reads nothing, so it changes nothing and must not notify mid-build.
if (value != before) notifyListeners();
}
}