Skip to content

Commit 61b4b38

Browse files
authored
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.
1 parent 7363268 commit 61b4b38

File tree

2 files changed

+40
-27
lines changed

2 files changed

+40
-27
lines changed

crates/bevy_winit/src/accessibility.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use std::{
88
use accesskit_winit::Adapter;
99
use bevy_a11y::{
1010
accesskit::{
11-
ActionHandler, ActionRequest, NodeBuilder, NodeClassSet, NodeId, Role, TreeUpdate,
11+
ActionHandler, ActionRequest, NodeBuilder, NodeClassSet, NodeId, Role, Tree, TreeUpdate,
1212
},
1313
AccessibilityNode, AccessibilityRequested, AccessibilitySystem, Focus,
1414
};
@@ -44,6 +44,37 @@ impl ActionHandler for WinitActionHandler {
4444
}
4545
}
4646

47+
/// Prepares accessibility for a winit window.
48+
pub(crate) fn prepare_accessibility_for_window(
49+
winit_window: &winit::window::Window,
50+
entity: Entity,
51+
name: String,
52+
accessibility_requested: AccessibilityRequested,
53+
adapters: &mut AccessKitAdapters,
54+
handlers: &mut WinitActionHandlers,
55+
) {
56+
let mut root_builder = NodeBuilder::new(Role::Window);
57+
root_builder.set_name(name.into_boxed_str());
58+
let root = root_builder.build(&mut NodeClassSet::lock_global());
59+
60+
let accesskit_window_id = NodeId(entity.to_bits());
61+
let handler = WinitActionHandler::default();
62+
let adapter = Adapter::with_action_handler(
63+
winit_window,
64+
move || {
65+
accessibility_requested.set(true);
66+
TreeUpdate {
67+
nodes: vec![(accesskit_window_id, root)],
68+
tree: Some(Tree::new(accesskit_window_id)),
69+
focus: accesskit_window_id,
70+
}
71+
},
72+
Box::new(handler.clone()),
73+
);
74+
adapters.insert(entity, adapter);
75+
handlers.insert(entity, handler);
76+
}
77+
4778
fn window_closed(
4879
mut adapters: NonSendMut<AccessKitAdapters>,
4980
mut receivers: ResMut<WinitActionHandlers>,

crates/bevy_winit/src/winit_windows.rs

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
use accesskit_winit::Adapter;
2-
use bevy_a11y::{
3-
accesskit::{NodeBuilder, NodeClassSet, NodeId, Role, Tree, TreeUpdate},
4-
AccessibilityRequested,
5-
};
1+
use bevy_a11y::AccessibilityRequested;
62
use bevy_ecs::entity::Entity;
73

84
use bevy_ecs::entity::EntityHashMap;
@@ -15,7 +11,7 @@ use winit::{
1511
};
1612

1713
use crate::{
18-
accessibility::{AccessKitAdapters, WinitActionHandler, WinitActionHandlers},
14+
accessibility::{prepare_accessibility_for_window, AccessKitAdapters, WinitActionHandlers},
1915
converters::{convert_enabled_buttons, convert_window_level, convert_window_theme},
2016
};
2117

@@ -212,28 +208,14 @@ impl WinitWindows {
212208

213209
let winit_window = winit_window_builder.build(event_loop).unwrap();
214210
let name = window.title.clone();
215-
216-
let mut root_builder = NodeBuilder::new(Role::Window);
217-
root_builder.set_name(name.into_boxed_str());
218-
let root = root_builder.build(&mut NodeClassSet::lock_global());
219-
220-
let accesskit_window_id = NodeId(entity.to_bits());
221-
let handler = WinitActionHandler::default();
222-
let accessibility_requested = accessibility_requested.clone();
223-
let adapter = Adapter::with_action_handler(
211+
prepare_accessibility_for_window(
224212
&winit_window,
225-
move || {
226-
accessibility_requested.set(true);
227-
TreeUpdate {
228-
nodes: vec![(accesskit_window_id, root)],
229-
tree: Some(Tree::new(accesskit_window_id)),
230-
focus: accesskit_window_id,
231-
}
232-
},
233-
Box::new(handler.clone()),
213+
entity,
214+
name,
215+
accessibility_requested.clone(),
216+
adapters,
217+
handlers,
234218
);
235-
adapters.insert(entity, adapter);
236-
handlers.insert(entity, handler);
237219

238220
// Do not set the grab mode on window creation if it's none. It can fail on mobile.
239221
if window.cursor.grab_mode != CursorGrabMode::None {

0 commit comments

Comments
 (0)