Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move accessibility setup to accessibility module #12784

Merged
merged 3 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion crates/bevy_winit/src/accessibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{
use accesskit_winit::Adapter;
use bevy_a11y::{
accesskit::{
ActionHandler, ActionRequest, NodeBuilder, NodeClassSet, NodeId, Role, TreeUpdate,
ActionHandler, ActionRequest, NodeBuilder, NodeClassSet, NodeId, Role, Tree, TreeUpdate,
},
AccessibilityNode, AccessibilityRequested, AccessibilitySystem, Focus,
};
Expand Down Expand Up @@ -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(
mut adapters: NonSendMut<AccessKitAdapters>,
mut receivers: ResMut<WinitActionHandlers>,
Expand Down
34 changes: 8 additions & 26 deletions crates/bevy_winit/src/winit_windows.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use accesskit_winit::Adapter;
use bevy_a11y::{
accesskit::{NodeBuilder, NodeClassSet, NodeId, Role, Tree, TreeUpdate},
AccessibilityRequested,
};
use bevy_a11y::AccessibilityRequested;
use bevy_ecs::entity::Entity;

use bevy_ecs::entity::EntityHashMap;
Expand All @@ -15,7 +11,7 @@ use winit::{
};

use crate::{
accessibility::{AccessKitAdapters, WinitActionHandler, WinitActionHandlers},
accessibility::{prepare_accessibility_for_window, AccessKitAdapters, WinitActionHandlers},
converters::{convert_enabled_buttons, convert_window_level, convert_window_theme},
};

Expand Down Expand Up @@ -212,28 +208,14 @@ impl WinitWindows {

let winit_window = winit_window_builder.build(event_loop).unwrap();
let name = window.title.clone();

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(
prepare_accessibility_for_window(
&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()),
entity,
name,
accessibility_requested.clone(),
adapters,
handlers,
);
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.
if window.cursor.grab_mode != CursorGrabMode::None {
Expand Down