mirror of
https://github.com/emilk/egui.git
synced 2026-08-29 12:50:04 -04:00
Remove dependency on tts (#3651)
We were using [`tts`](https://github.com/ndarilek/tts-rs) for the web-only screen reader. This was overkill, to say the least. It is now replaced with ten lines of `web-sys` calls.
This commit is contained in:
@@ -32,6 +32,7 @@ default = [
|
||||
"default_fonts",
|
||||
"glow",
|
||||
"wayland",
|
||||
"web_screen_reader",
|
||||
"winit/default",
|
||||
"x11",
|
||||
]
|
||||
@@ -84,7 +85,10 @@ wayland = ["egui-winit/wayland"]
|
||||
## Enable screen reader support (requires `ctx.options_mut(|o| o.screen_reader = true);`) on web.
|
||||
##
|
||||
## For other platforms, use the `accesskit` feature instead.
|
||||
web_screen_reader = ["tts"]
|
||||
web_screen_reader = [
|
||||
"web-sys/SpeechSynthesis",
|
||||
"web-sys/SpeechSynthesisUtterance",
|
||||
]
|
||||
|
||||
## Use [`wgpu`](https://docs.rs/wgpu) for painting (via [`egui-wgpu`](https://github.com/emilk/egui/tree/master/crates/egui-wgpu)).
|
||||
## This overrides the `glow` feature.
|
||||
@@ -207,5 +211,4 @@ web-sys = { version = "0.3.58", features = [
|
||||
# optional web:
|
||||
egui-wgpu = { version = "0.24.0", path = "../egui-wgpu", optional = true } # if wgpu is used, use it without (!) winit
|
||||
raw-window-handle = { workspace = true, optional = true }
|
||||
tts = { version = "0.25", optional = true, default-features = false }
|
||||
wgpu = { workspace = true, optional = true }
|
||||
|
||||
@@ -13,7 +13,6 @@ pub struct AppRunner {
|
||||
app: Box<dyn epi::App>,
|
||||
pub(crate) needs_repaint: std::sync::Arc<NeedRepaint>,
|
||||
last_save_time: f64,
|
||||
screen_reader: super::screen_reader::ScreenReader,
|
||||
pub(crate) text_cursor_pos: Option<egui::Pos2>,
|
||||
pub(crate) mutable_text_under_cursor: bool,
|
||||
|
||||
@@ -113,7 +112,6 @@ impl AppRunner {
|
||||
app,
|
||||
needs_repaint,
|
||||
last_save_time: now_sec(),
|
||||
screen_reader: Default::default(),
|
||||
text_cursor_pos: None,
|
||||
mutable_text_under_cursor: false,
|
||||
textures_delta: Default::default(),
|
||||
@@ -235,9 +233,9 @@ impl AppRunner {
|
||||
}
|
||||
|
||||
fn handle_platform_output(&mut self, platform_output: egui::PlatformOutput) {
|
||||
#[cfg(feature = "web_screen_reader")]
|
||||
if self.egui_ctx.options(|o| o.screen_reader) {
|
||||
self.screen_reader
|
||||
.speak(&platform_output.events_description());
|
||||
super::screen_reader::speak(&platform_output.events_description());
|
||||
}
|
||||
|
||||
let egui::PlatformOutput {
|
||||
|
||||
@@ -12,6 +12,7 @@ mod web_logger;
|
||||
mod web_runner;
|
||||
|
||||
/// Access to the browser screen reader.
|
||||
#[cfg(feature = "web_screen_reader")]
|
||||
pub mod screen_reader;
|
||||
|
||||
/// Access to local browser storage.
|
||||
|
||||
@@ -1,51 +1,20 @@
|
||||
/// Screen reader support.
|
||||
pub struct ScreenReader {
|
||||
#[cfg(feature = "tts")]
|
||||
tts: Option<tts::Tts>,
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "tts"))]
|
||||
#[allow(clippy::derivable_impls)] // False positive
|
||||
impl Default for ScreenReader {
|
||||
fn default() -> Self {
|
||||
Self {}
|
||||
/// Speak the given text out loud.
|
||||
pub fn speak(text: &str) {
|
||||
if text.is_empty() {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "tts")]
|
||||
impl Default for ScreenReader {
|
||||
fn default() -> Self {
|
||||
let tts = match tts::Tts::default() {
|
||||
Ok(screen_reader) => {
|
||||
log::debug!("Initialized screen reader.");
|
||||
Some(screen_reader)
|
||||
}
|
||||
Err(err) => {
|
||||
log::warn!("Failed to load screen reader: {}", err);
|
||||
None
|
||||
}
|
||||
};
|
||||
Self { tts }
|
||||
}
|
||||
}
|
||||
if let Some(window) = web_sys::window() {
|
||||
log::debug!("Speaking {text:?}");
|
||||
|
||||
impl ScreenReader {
|
||||
/// Speak the given text out loud.
|
||||
#[cfg(not(feature = "tts"))]
|
||||
#[allow(clippy::unused_self)]
|
||||
pub fn speak(&mut self, _text: &str) {}
|
||||
if let Ok(speech_synthesis) = window.speech_synthesis() {
|
||||
speech_synthesis.cancel(); // interrupt previous speech, if any
|
||||
|
||||
/// Speak the given text out loud.
|
||||
#[cfg(feature = "tts")]
|
||||
pub fn speak(&mut self, text: &str) {
|
||||
if text.is_empty() {
|
||||
return;
|
||||
}
|
||||
if let Some(tts) = &mut self.tts {
|
||||
log::debug!("Speaking: {:?}", text);
|
||||
let interrupt = true;
|
||||
if let Err(err) = tts.speak(text, interrupt) {
|
||||
log::warn!("Failed to read: {}", err);
|
||||
if let Ok(utterance) = web_sys::SpeechSynthesisUtterance::new_with_text(text) {
|
||||
utterance.set_rate(1.0);
|
||||
utterance.set_pitch(1.0);
|
||||
utterance.set_volume(1.0);
|
||||
speech_synthesis.speak(&utterance);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user