From cb10127e6b8422b9ca3f269b616d6edf1cb79abf Mon Sep 17 00:00:00 2001 From: ickshonpe Date: Mon, 7 Apr 2025 13:23:42 +0100 Subject: [PATCH 1/3] Set `InputFocus` to the button entity in the button example so the accessibility systems will recognise it. --- examples/ui/button.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/examples/ui/button.rs b/examples/ui/button.rs index 33210b4df6f89..0120e5c678707 100644 --- a/examples/ui/button.rs +++ b/examples/ui/button.rs @@ -1,13 +1,15 @@ //! This example illustrates how to create a button that changes color and text based on its //! interaction state. -use bevy::{color::palettes::basic::*, prelude::*, winit::WinitSettings}; +use bevy::{color::palettes::basic::*, input_focus::InputFocus, prelude::*, winit::WinitSettings}; fn main() { App::new() .add_plugins(DefaultPlugins) // Only run the app when there is user input. This will significantly reduce CPU/GPU use. .insert_resource(WinitSettings::desktop_app()) + // `InputFocus` must be set for accessibility to recognise the button. + .init_resource::() .add_systems(Startup, setup) .add_systems(Update, button_system) .run(); @@ -18,8 +20,10 @@ const HOVERED_BUTTON: Color = Color::srgb(0.25, 0.25, 0.25); const PRESSED_BUTTON: Color = Color::srgb(0.35, 0.75, 0.35); fn button_system( + mut input_focus: ResMut, mut interaction_query: Query< ( + Entity, &Interaction, &mut BackgroundColor, &mut BorderColor, @@ -29,20 +33,24 @@ fn button_system( >, mut text_query: Query<&mut Text>, ) { - for (interaction, mut color, mut border_color, children) in &mut interaction_query { + for (entity, interaction, mut color, mut border_color, children) in &mut interaction_query { let mut text = text_query.get_mut(children[0]).unwrap(); + match *interaction { Interaction::Pressed => { + input_focus.set(entity); **text = "Press".to_string(); *color = PRESSED_BUTTON.into(); border_color.0 = RED.into(); } Interaction::Hovered => { + input_focus.set(entity); **text = "Hover".to_string(); *color = HOVERED_BUTTON.into(); border_color.0 = Color::WHITE; } Interaction::None => { + input_focus.clear(); **text = "Button".to_string(); *color = NORMAL_BUTTON.into(); border_color.0 = Color::BLACK; From e439bca01fce1f6e6cc471dc27958811e3ae990d Mon Sep 17 00:00:00 2001 From: ickshonpe Date: Mon, 7 Apr 2025 15:01:38 +0100 Subject: [PATCH 2/3] Fix spelling --- examples/ui/button.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/ui/button.rs b/examples/ui/button.rs index 0120e5c678707..cfb3d58198f69 100644 --- a/examples/ui/button.rs +++ b/examples/ui/button.rs @@ -8,7 +8,7 @@ fn main() { .add_plugins(DefaultPlugins) // Only run the app when there is user input. This will significantly reduce CPU/GPU use. .insert_resource(WinitSettings::desktop_app()) - // `InputFocus` must be set for accessibility to recognise the button. + // `InputFocus` must be set for accessibility to recognize the button. .init_resource::() .add_systems(Startup, setup) .add_systems(Update, button_system) From ad26afe3281b0baf4c25a03ce35b3a1d6c91265b Mon Sep 17 00:00:00 2001 From: ickshonpe Date: Mon, 12 May 2025 12:13:32 +0100 Subject: [PATCH 3/3] Call `set_changed` on the `Button` component when the button's state changes to hovered or pressed. The accessibility system's only update the button's state when the `Button` component is marked as changed. --- examples/ui/button.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/examples/ui/button.rs b/examples/ui/button.rs index cfb3d58198f69..8436d937b1af6 100644 --- a/examples/ui/button.rs +++ b/examples/ui/button.rs @@ -27,13 +27,16 @@ fn button_system( &Interaction, &mut BackgroundColor, &mut BorderColor, + &mut Button, &Children, ), - (Changed, With