-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Make Radio Button behaviour modular and consistent with other widgets #21294
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -6,7 +6,7 @@ use bevy_ecs::{ | |||||||||||||||||||||||||||||||||||||||||||
entity::Entity, | ||||||||||||||||||||||||||||||||||||||||||||
hierarchy::{ChildOf, Children}, | ||||||||||||||||||||||||||||||||||||||||||||
observer::On, | ||||||||||||||||||||||||||||||||||||||||||||
query::{Has, With}, | ||||||||||||||||||||||||||||||||||||||||||||
query::{Has, With, Without}, | ||||||||||||||||||||||||||||||||||||||||||||
reflect::ReflectComponent, | ||||||||||||||||||||||||||||||||||||||||||||
system::{Commands, Query}, | ||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||
|
@@ -39,12 +39,18 @@ use crate::ValueChange; | |||||||||||||||||||||||||||||||||||||||||||
#[require(AccessibilityNode(accesskit::Node::new(Role::RadioGroup)))] | ||||||||||||||||||||||||||||||||||||||||||||
pub struct RadioGroup; | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
/// Headless widget implementation for radio buttons. These should be enclosed within a | ||||||||||||||||||||||||||||||||||||||||||||
/// [`RadioGroup`] widget, which is responsible for the mutual exclusion logic. | ||||||||||||||||||||||||||||||||||||||||||||
/// Headless widget implementation for radio buttons. They can be used independently, | ||||||||||||||||||||||||||||||||||||||||||||
/// but enclosing them in a [`RadioGroup`] widget allows them to behave as a single, | ||||||||||||||||||||||||||||||||||||||||||||
/// mutually exclusive unit. | ||||||||||||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||||||||||||
/// According to the WAI-ARIA best practices document, radio buttons should not be focusable, | ||||||||||||||||||||||||||||||||||||||||||||
/// but rather the enclosing group should be focusable. | ||||||||||||||||||||||||||||||||||||||||||||
/// See <https://www.w3.org/WAI/ARIA/apg/patterns/radio>/ | ||||||||||||||||||||||||||||||||||||||||||||
/// | ||||||||||||||||||||||||||||||||||||||||||||
/// The widget emits a [`ValueChange<bool>`] event with the value `true` whenever it becomes checked, | ||||||||||||||||||||||||||||||||||||||||||||
/// either through a mouse click or when a [`RadioGroup`] checks the widget. | ||||||||||||||||||||||||||||||||||||||||||||
/// If the [`RadioButton`] is focusable, it can also be checked using the `Enter` or `Space` keys, | ||||||||||||||||||||||||||||||||||||||||||||
/// in which case the event will likewise be emitted. | ||||||||||||||||||||||||||||||||||||||||||||
#[derive(Component, Debug)] | ||||||||||||||||||||||||||||||||||||||||||||
#[require(AccessibilityNode(accesskit::Node::new(Role::RadioButton)), Checkable)] | ||||||||||||||||||||||||||||||||||||||||||||
#[derive(Reflect)] | ||||||||||||||||||||||||||||||||||||||||||||
|
@@ -132,7 +138,12 @@ fn radio_group_on_key_input( | |||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
let (next_id, _) = radio_buttons[next_index]; | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
// Trigger the on_change event for the newly checked radio button | ||||||||||||||||||||||||||||||||||||||||||||
// Trigger the value change event on the radio button | ||||||||||||||||||||||||||||||||||||||||||||
commands.trigger(ValueChange::<bool> { | ||||||||||||||||||||||||||||||||||||||||||||
source: next_id, | ||||||||||||||||||||||||||||||||||||||||||||
value: true, | ||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||
// Trigger the on_change event for the newly checked radio button on radio group | ||||||||||||||||||||||||||||||||||||||||||||
commands.trigger(ValueChange::<Entity> { | ||||||||||||||||||||||||||||||||||||||||||||
source: ev.focused_entity, | ||||||||||||||||||||||||||||||||||||||||||||
value: next_id, | ||||||||||||||||||||||||||||||||||||||||||||
|
@@ -141,82 +152,101 @@ fn radio_group_on_key_input( | |||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
fn radio_group_on_button_click( | ||||||||||||||||||||||||||||||||||||||||||||
mut ev: On<Pointer<Click>>, | ||||||||||||||||||||||||||||||||||||||||||||
// Provides functionality for standalone focusable [`RadioButton`] to react | ||||||||||||||||||||||||||||||||||||||||||||
// on `Space` or `Enter` key press. | ||||||||||||||||||||||||||||||||||||||||||||
fn radio_button_on_key_input( | ||||||||||||||||||||||||||||||||||||||||||||
mut ev: On<FocusedInput<KeyboardInput>>, | ||||||||||||||||||||||||||||||||||||||||||||
q_radio_button: Query<Has<Checked>, (With<RadioButton>, Without<InteractionDisabled>)>, | ||||||||||||||||||||||||||||||||||||||||||||
q_group: Query<(), With<RadioGroup>>, | ||||||||||||||||||||||||||||||||||||||||||||
q_radio: Query<(Has<Checked>, Has<InteractionDisabled>), With<RadioButton>>, | ||||||||||||||||||||||||||||||||||||||||||||
q_parents: Query<&ChildOf>, | ||||||||||||||||||||||||||||||||||||||||||||
q_children: Query<&Children>, | ||||||||||||||||||||||||||||||||||||||||||||
mut commands: Commands, | ||||||||||||||||||||||||||||||||||||||||||||
) { | ||||||||||||||||||||||||||||||||||||||||||||
if q_group.contains(ev.entity) { | ||||||||||||||||||||||||||||||||||||||||||||
// Starting with the original target, search upward for a radio button. | ||||||||||||||||||||||||||||||||||||||||||||
let radio_id = if q_radio.contains(ev.original_event_target()) { | ||||||||||||||||||||||||||||||||||||||||||||
ev.original_event_target() | ||||||||||||||||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||||||||||||||||
// Search ancestors for the first radio button | ||||||||||||||||||||||||||||||||||||||||||||
let mut found_radio = None; | ||||||||||||||||||||||||||||||||||||||||||||
for ancestor in q_parents.iter_ancestors(ev.original_event_target()) { | ||||||||||||||||||||||||||||||||||||||||||||
if q_group.contains(ancestor) { | ||||||||||||||||||||||||||||||||||||||||||||
// We reached a radio group before finding a radio button, bail out | ||||||||||||||||||||||||||||||||||||||||||||
return; | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
if q_radio.contains(ancestor) { | ||||||||||||||||||||||||||||||||||||||||||||
found_radio = Some(ancestor); | ||||||||||||||||||||||||||||||||||||||||||||
break; | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
let Ok(checked) = q_radio_button.get(ev.focused_entity) else { | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
// Not a radio button | ||||||||||||||||||||||||||||||||||||||||||||
return; | ||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
// Radio button already checked | ||||||||||||||||||||||||||||||||||||||||||||
if checked { | ||||||||||||||||||||||||||||||||||||||||||||
return; | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
match found_radio { | ||||||||||||||||||||||||||||||||||||||||||||
Some(radio) => radio, | ||||||||||||||||||||||||||||||||||||||||||||
None => return, // No radio button found in the ancestor chain | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||
let event = &ev.event().input; | ||||||||||||||||||||||||||||||||||||||||||||
if event.state == ButtonState::Pressed | ||||||||||||||||||||||||||||||||||||||||||||
&& !event.repeat | ||||||||||||||||||||||||||||||||||||||||||||
&& (event.key_code == KeyCode::Enter || event.key_code == KeyCode::Space) | ||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||
ev.propagate(false); | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
// Radio button is disabled. | ||||||||||||||||||||||||||||||||||||||||||||
if q_radio.get(radio_id).unwrap().1 { | ||||||||||||||||||||||||||||||||||||||||||||
return; | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
trigger_radio_button_and_radio_group_value_change( | ||||||||||||||||||||||||||||||||||||||||||||
ev.focused_entity, | ||||||||||||||||||||||||||||||||||||||||||||
&q_group, | ||||||||||||||||||||||||||||||||||||||||||||
&q_parents, | ||||||||||||||||||||||||||||||||||||||||||||
&mut commands, | ||||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
// Gather all the enabled radio group descendants for exclusion. | ||||||||||||||||||||||||||||||||||||||||||||
let radio_buttons = q_children | ||||||||||||||||||||||||||||||||||||||||||||
.iter_descendants(ev.entity) | ||||||||||||||||||||||||||||||||||||||||||||
.filter_map(|child_id| match q_radio.get(child_id) { | ||||||||||||||||||||||||||||||||||||||||||||
Ok((checked, false)) => Some((child_id, checked)), | ||||||||||||||||||||||||||||||||||||||||||||
Ok((_, true)) | Err(_) => None, | ||||||||||||||||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||||||||||||||||
.collect::<Vec<_>>(); | ||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||
if radio_buttons.is_empty() { | ||||||||||||||||||||||||||||||||||||||||||||
return; // No enabled radio buttons in the group | ||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||
fn radio_button_on_click( | ||||||||||||||||||||||||||||||||||||||||||||
mut ev: On<Pointer<Click>>, | ||||||||||||||||||||||||||||||||||||||||||||
q_group: Query<(), With<RadioGroup>>, | ||||||||||||||||||||||||||||||||||||||||||||
q_radio: Query<Has<Checked>, (With<RadioButton>, Without<InteractionDisabled>)>, | ||||||||||||||||||||||||||||||||||||||||||||
q_parents: Query<&ChildOf>, | ||||||||||||||||||||||||||||||||||||||||||||
mut commands: Commands, | ||||||||||||||||||||||||||||||||||||||||||||
) { | ||||||||||||||||||||||||||||||||||||||||||||
let Ok(checked) = q_radio.get(ev.entity) else { | ||||||||||||||||||||||||||||||||||||||||||||
|
// Radio button is disabled. | |
if q_radio.get(radio_id).unwrap().1 { | |
return; | |
} | |
// Gather all the enabled radio group descendants for exclusion. | |
let radio_buttons = q_children | |
.iter_descendants(ev.entity) | |
.filter_map(|child_id| match q_radio.get(child_id) { | |
Ok((checked, false)) => Some((child_id, checked)), | |
Ok((_, true)) | Err(_) => None, | |
}) | |
.collect::<Vec<_>>(); | |
if radio_buttons.is_empty() { | |
return; // No enabled radio buttons in the group | |
} | |
// Pick out the radio button that is currently checked. | |
ev.propagate(false); | |
let current_radio = radio_buttons |
I can fix that too in this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 9806468
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like an odd way to use
ValueChange
:true
), then we should triggerActivate
instead.Activate
is meant for widgets whose state never changes.ValueChange(false)
when the button gets deselected because of mutual exclusion. However, that's more complicated because now you would have to pass around the entity id of the previously checked button (which is not a problem here, but might be further down).More generally, if we're going to use
ValueChange
, it should always reflect the current state of the widget triggering it: a listener that only listens toValueChange
should always know what state the source is in. We can't do this if it only sendsValueChange
for some state transitions and not others.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can use
Activate
. But i usedValueChange
instead because all other Checkable widgets raise ValueChange event.And for me it seemed logical to think that RadioButton can only be checked by interacting with it. To uncheck it users need to add other widgets / radio group / logic that can uncheck it.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I would prefer
ValueChange<bool>
because all checkable widgets use that event.EDIT: P.S. But activate is fine with me too.