Move accessibility setup to accessibility module (#12784)

# Objective

- Reduce the size of `create_windows` and isolate accessibility setup
logic.

## Solution

- Move accessibility setup for new windows to the `accessibility`
module.

## Comments

This is a small refactor, no behavior changes.
This commit is contained in:
UkoeHB 2024-03-29 11:02:25 -05:00 committed by GitHub
parent 7363268ea8
commit 61b4b38ad0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 40 additions and 27 deletions

View file

@ -8,7 +8,7 @@ use std::{
use accesskit_winit::Adapter; use accesskit_winit::Adapter;
use bevy_a11y::{ use bevy_a11y::{
accesskit::{ accesskit::{
ActionHandler, ActionRequest, NodeBuilder, NodeClassSet, NodeId, Role, TreeUpdate, ActionHandler, ActionRequest, NodeBuilder, NodeClassSet, NodeId, Role, Tree, TreeUpdate,
}, },
AccessibilityNode, AccessibilityRequested, AccessibilitySystem, Focus, AccessibilityNode, AccessibilityRequested, AccessibilitySystem, Focus,
}; };
@ -44,6 +44,37 @@ impl ActionHandler for WinitActionHandler {
} }
} }
/// Prepares accessibility for a winit window.
pub(crate) fn prepare_accessibility_for_window(
winit_window: &winit::window::Window,
entity: Entity,
name: String,
accessibility_requested: AccessibilityRequested,
adapters: &mut AccessKitAdapters,
handlers: &mut WinitActionHandlers,
) {
let mut root_builder = NodeBuilder::new(Role::Window);
root_builder.set_name(name.into_boxed_str());
let root = root_builder.build(&mut NodeClassSet::lock_global());
let accesskit_window_id = NodeId(entity.to_bits());
let handler = WinitActionHandler::default();
let adapter = Adapter::with_action_handler(
winit_window,
move || {
accessibility_requested.set(true);
TreeUpdate {
nodes: vec![(accesskit_window_id, root)],
tree: Some(Tree::new(accesskit_window_id)),
focus: accesskit_window_id,
}
},
Box::new(handler.clone()),
);
adapters.insert(entity, adapter);
handlers.insert(entity, handler);
}
fn window_closed( fn window_closed(
mut adapters: NonSendMut<AccessKitAdapters>, mut adapters: NonSendMut<AccessKitAdapters>,
mut receivers: ResMut<WinitActionHandlers>, mut receivers: ResMut<WinitActionHandlers>,

View file

@ -1,8 +1,4 @@
use accesskit_winit::Adapter; use bevy_a11y::AccessibilityRequested;
use bevy_a11y::{
accesskit::{NodeBuilder, NodeClassSet, NodeId, Role, Tree, TreeUpdate},
AccessibilityRequested,
};
use bevy_ecs::entity::Entity; use bevy_ecs::entity::Entity;
use bevy_ecs::entity::EntityHashMap; use bevy_ecs::entity::EntityHashMap;
@ -15,7 +11,7 @@ use winit::{
}; };
use crate::{ use crate::{
accessibility::{AccessKitAdapters, WinitActionHandler, WinitActionHandlers}, accessibility::{prepare_accessibility_for_window, AccessKitAdapters, WinitActionHandlers},
converters::{convert_enabled_buttons, convert_window_level, convert_window_theme}, converters::{convert_enabled_buttons, convert_window_level, convert_window_theme},
}; };
@ -212,28 +208,14 @@ impl WinitWindows {
let winit_window = winit_window_builder.build(event_loop).unwrap(); let winit_window = winit_window_builder.build(event_loop).unwrap();
let name = window.title.clone(); let name = window.title.clone();
prepare_accessibility_for_window(
let mut root_builder = NodeBuilder::new(Role::Window);
root_builder.set_name(name.into_boxed_str());
let root = root_builder.build(&mut NodeClassSet::lock_global());
let accesskit_window_id = NodeId(entity.to_bits());
let handler = WinitActionHandler::default();
let accessibility_requested = accessibility_requested.clone();
let adapter = Adapter::with_action_handler(
&winit_window, &winit_window,
move || { entity,
accessibility_requested.set(true); name,
TreeUpdate { accessibility_requested.clone(),
nodes: vec![(accesskit_window_id, root)], adapters,
tree: Some(Tree::new(accesskit_window_id)), handlers,
focus: accesskit_window_id,
}
},
Box::new(handler.clone()),
); );
adapters.insert(entity, adapter);
handlers.insert(entity, handler);
// Do not set the grab mode on window creation if it's none. It can fail on mobile. // Do not set the grab mode on window creation if it's none. It can fail on mobile.
if window.cursor.grab_mode != CursorGrabMode::None { if window.cursor.grab_mode != CursorGrabMode::None {