mirror of
https://github.com/emilk/egui.git
synced 2026-08-29 21:00:03 -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -199,7 +199,7 @@ pub struct Options {
|
||||
///
|
||||
/// Screen readers is an experimental feature of egui, and not supported on all platforms.
|
||||
///
|
||||
/// `eframe` supports it only on web, using the `web_screen_reader` feature flag,
|
||||
/// `eframe` supports it only on web,
|
||||
/// but you should consider using [AccessKit](https://github.com/AccessKit/accesskit) instead,
|
||||
/// which `eframe` supports.
|
||||
pub screen_reader: bool,
|
||||
|
||||
@@ -19,7 +19,7 @@ crate-type = ["cdylib", "rlib"]
|
||||
default = ["glow", "persistence"]
|
||||
|
||||
# image_viewer adds about 0.9 MB of WASM
|
||||
web_app = ["http", "persistence", "web_screen_reader"]
|
||||
web_app = ["http", "persistence"]
|
||||
|
||||
http = ["ehttp", "image", "poll-promise", "egui_extras/image"]
|
||||
image_viewer = ["image", "egui_extras/all_loaders", "rfd"]
|
||||
@@ -27,7 +27,6 @@ persistence = ["eframe/persistence", "egui/persistence", "serde"]
|
||||
puffin = ["eframe/puffin", "dep:puffin", "dep:puffin_http"]
|
||||
serde = ["dep:serde", "egui_demo_lib/serde", "egui/serde"]
|
||||
syntect = ["egui_demo_lib/syntect"]
|
||||
web_screen_reader = ["eframe/web_screen_reader"] # experimental
|
||||
|
||||
glow = ["eframe/glow"]
|
||||
wgpu = ["eframe/wgpu", "bytemuck"]
|
||||
@@ -37,7 +36,9 @@ chrono = { version = "0.4", default-features = false, features = [
|
||||
"js-sys",
|
||||
"wasmbind",
|
||||
] }
|
||||
eframe = { version = "0.24.0", path = "../eframe", default-features = false }
|
||||
eframe = { version = "0.24.0", path = "../eframe", default-features = false, features = [
|
||||
"web_screen_reader",
|
||||
] }
|
||||
egui = { version = "0.24.0", path = "../egui", features = [
|
||||
"callstack",
|
||||
"extra_debug_asserts",
|
||||
|
||||
@@ -94,15 +94,12 @@ impl BackendPanel {
|
||||
self.egui_windows.checkboxes(ui);
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
if ui.ctx().style().debug.debug_on_hover_with_all_modifiers {
|
||||
ui.separator();
|
||||
if ui.ctx().style().debug.debug_on_hover_with_all_modifiers {
|
||||
ui.label("Press down all modifiers and hover a widget to see a callstack for it");
|
||||
}
|
||||
ui.label("Press down all modifiers and hover a widget to see a callstack for it");
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
#[cfg(feature = "web_screen-reader")]
|
||||
{
|
||||
ui.separator();
|
||||
let mut screen_reader = ui.ctx().options(|o| o.screen_reader);
|
||||
|
||||
Reference in New Issue
Block a user