diff --git a/crates/eframe/src/native/epi_integration.rs b/crates/eframe/src/native/epi_integration.rs index aec3f4ee..0cda6fcb 100644 --- a/crates/eframe/src/native/epi_integration.rs +++ b/crates/eframe/src/native/epi_integration.rs @@ -272,8 +272,17 @@ impl EpiIntegration { window: &winit::window::Window, event_loop_proxy: winit::event_loop::EventLoopProxy, ) { + let egui_ctx = self.egui_ctx.clone(); self.egui_winit - .init_accesskit(window, event_loop_proxy, self.egui_ctx.clone()); + .init_accesskit(window, event_loop_proxy, move || { + // This function is called when an accessibility client + // (e.g. screen reader) makes its first request. If we got here, + // we know that an accessibility tree is actually wanted. + egui_ctx.enable_accesskit(); + // Enqueue a repaint so we'll receive a full tree update soon. + egui_ctx.request_repaint(); + egui::accesskit_placeholder_tree_update() + }); } pub fn warm_up(&mut self, app: &mut dyn epi::App, window: &winit::window::Window) { diff --git a/crates/egui-winit/src/lib.rs b/crates/egui-winit/src/lib.rs index 25ed30a3..fe802e19 100644 --- a/crates/egui-winit/src/lib.rs +++ b/crates/egui-winit/src/lib.rs @@ -132,21 +132,11 @@ impl State { &mut self, window: &winit::window::Window, event_loop_proxy: winit::event_loop::EventLoopProxy, - egui_ctx: egui::Context, + initial_tree_update_factory: impl 'static + FnOnce() -> accesskit::TreeUpdate + Send, ) { self.accesskit = Some(accesskit_winit::Adapter::new( window, - move || { - // This function is called when an accessibility client - // (e.g. screen reader) makes its first request. If we got here, - // we know that an accessibility tree is actually wanted. - // Tell egui that AccessKit is active, and return a placeholder - // tree for now. `egui::Context::accesskit_activated` - // will request a repaint, and that will provide the first - // real accessibility tree. - egui_ctx.accesskit_activated(); - egui::accesskit_placeholder_tree_update() - }, + initial_tree_update_factory, event_loop_proxy, )); } diff --git a/crates/egui/src/context.rs b/crates/egui/src/context.rs index 19918c9e..e14d12d1 100644 --- a/crates/egui/src/context.rs +++ b/crates/egui/src/context.rs @@ -69,7 +69,7 @@ struct ContextImpl { layer_rects_prev_frame: ahash::HashMap>, #[cfg(feature = "accesskit")] - was_accesskit_activated: bool, + is_accesskit_enabled: bool, } impl ContextImpl { @@ -110,7 +110,7 @@ impl ContextImpl { ); #[cfg(feature = "accesskit")] - if self.was_accesskit_activated { + if self.is_accesskit_enabled { assert!(self.output.accesskit_update.is_none()); let id = crate::accesskit_root_id(); let accesskit_id = id.accesskit_id(); @@ -1607,20 +1607,18 @@ impl Context { self.output().accesskit_update.is_some() } - /// Indicates that AccessKit has been activated and egui should generate - /// AccessKit tree updates for all subsequent frames. Also requests a repaint - /// so a full AccessKit tree will be available as soon as the repaint - /// is done. As soon as the egui integration knows that accessibility support - /// is desired, it must call this method and provide a placeholder tree - /// to AccessKit through the [`crate::accesskit_placeholder_tree_update`] method. + /// Enable generation of AccessKit tree updates in all future frames. + /// + /// If it's practical for the egui integration to immediately run the egui + /// application when it is either initializing the AccessKit adapter or + /// being called by the AccessKit adapter to provide the initial tree update, + /// then it should do so, to provide a complete AccessKit tree to the adapter + /// immediately. Otherwise, it should enqueue a repaint and use the + /// placeholder tree update from [`crate::accesskit_placeholder_tree_update`] + /// in the meantime. #[cfg(feature = "accesskit")] - pub fn accesskit_activated(&self) { - let mut ctx = self.write(); - if !ctx.was_accesskit_activated { - ctx.was_accesskit_activated = true; - drop(ctx); - self.request_repaint(); - } + pub fn enable_accesskit(&self) { + self.write().is_accesskit_enabled = true; } } diff --git a/crates/egui/src/lib.rs b/crates/egui/src/lib.rs index 126dc562..67aa46c6 100644 --- a/crates/egui/src/lib.rs +++ b/crates/egui/src/lib.rs @@ -559,8 +559,8 @@ pub fn accesskit_root_id() -> Id { } /// Return a tree update that the egui integration should provide to the -/// AccessKit adapter before a real tree update is available. See also -/// [`crate::Context::accesskit_activated`]. +/// AccessKit adapter if it cannot immediately run the egui application +/// to get a full tree update after running [`Context::enable_accesskit`]. #[cfg(feature = "accesskit")] pub fn accesskit_placeholder_tree_update() -> accesskit::TreeUpdate { use accesskit::{Node, Role, Tree, TreeUpdate};