Add checkbox in demo app to turn screen reader on/off

This commit is contained in:
Emil Ernerfeldt 2021-03-24 21:35:29 +01:00
parent cbe6faa83b
commit 70c6f4596a
7 changed files with 32 additions and 9 deletions

View file

@ -13,8 +13,7 @@ rm -f docs/${CRATE_NAME}_bg.wasm
echo "Building rust…"
BUILD=release
FEATURES="http,persistence" # screen_reader is experimental
# FEATURES="http,persistence,screen_reader" # screen_reader is experimental
FEATURES="http,persistence,screen_reader"
(
cd egui_demo_app && cargo build \
@ -30,6 +29,9 @@ TARGET_NAME="${CRATE_NAME}.wasm"
wasm-bindgen "target/wasm32-unknown-unknown/$BUILD/$TARGET_NAME" \
--out-dir docs --no-modules --no-typescript
echo "Finished: docs/${CRATE_NAME}.wasm"
# brew install wabt # to get wasm-strip
wasm-strip docs/${CRATE_NAME}_bg.wasm
echo "Finished: docs/${CRATE_NAME}_bg.wasm"
open http://localhost:8888/index.html

View file

@ -357,6 +357,8 @@ impl Context {
self.frame_state.lock().available_rect()
}
/// Stores all the egui state.
/// If you want to store/restore egui, serialize this.
pub fn memory(&self) -> MutexGuard<'_, Memory> {
self.memory.lock()
}
@ -365,6 +367,7 @@ impl Context {
self.graphics.lock()
}
/// What egui outputs each frame.
pub fn output(&self) -> MutexGuard<'_, Output> {
self.output.lock()
}

View file

@ -18,7 +18,7 @@ use epaint::color::{Color32, Hsva};
#[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "persistence", serde(default))]
pub struct Memory {
pub(crate) options: Options,
pub options: Options,
/// new scale that will be applied at the start of the next frame
pub(crate) new_pixels_per_point: Option<f32>,
@ -61,10 +61,11 @@ pub struct Memory {
// ----------------------------------------------------------------------------
/// Some global options that you can read and write.
#[derive(Clone, Debug, Default)]
#[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "persistence", serde(default))]
pub(crate) struct Options {
pub struct Options {
/// The default style for new `Ui`:s.
#[cfg_attr(feature = "persistence", serde(skip))]
pub(crate) style: std::sync::Arc<Style>,
@ -72,6 +73,11 @@ pub(crate) struct Options {
pub(crate) tessellation_options: epaint::TessellationOptions,
/// Font sizes etc.
pub(crate) font_definitions: epaint::text::FontDefinitions,
/// This does not at all change the behavior of egui,
/// but is a signal to any backend that we want the [`crate::Output::events`] read out loud.
/// Screen readers is an experimental feature of egui, and not supported on all platforms.
pub screen_reader: bool,
}
// ----------------------------------------------------------------------------

View file

@ -14,7 +14,7 @@ eframe = { version = "0.10.0", path = "../eframe", features = ["time"] }
egui_demo_lib = { version = "0.10.0", path = "../egui_demo_lib" }
[features]
default = ["persistence"]
default = ["persistence", "screen_reader"]
http = ["eframe/http", "egui_demo_lib/http"]
persistence = ["eframe/persistence", "egui_demo_lib/persistence"]
screen_reader = ["eframe/screen_reader"] # experimental

View file

@ -315,9 +315,17 @@ impl BackendPanel {
}
}
let mut screen_reader = ui.ctx().memory().options.screen_reader;
ui.checkbox(&mut screen_reader, "Screen reader").on_hover_text("Experimental feature: checking this will turn on the screen reader on supported platforms");
ui.ctx().memory().options.screen_reader = screen_reader;
ui.collapsing("Output events", |ui| {
ui.set_max_width(450.0);
ui.label("Recent output events from egui:");
ui.label(
"Recent output events from egui. \
These are emitted when you switch selected widget with tab, \
and can be hooked up to a screen reader on supported platforms.",
);
ui.advance_cursor(8.0);
for event in &self.output_event_history {
ui.label(format!("{:?}", event));

View file

@ -280,7 +280,9 @@ pub fn run(mut app: Box<dyn epi::App>) -> ! {
};
}
if ctx.memory().options.screen_reader {
screen_reader.speak(&egui_output.events_description());
}
if current_cursor_icon != egui_output.cursor_icon {
// call only when changed to prevent flickering near frame boundary
// when Windows OS tries to control cursor icon for window resizing

View file

@ -219,7 +219,9 @@ impl AppRunner {
let egui_ctx = &self.web_backend.ctx;
self.app.update(egui_ctx, &mut frame);
let (egui_output, clipped_meshes) = self.web_backend.end_frame()?;
if self.web_backend.ctx.memory().options.screen_reader {
self.screen_reader.speak(&egui_output.events_description());
}
handle_output(&egui_output);
{