-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Core Checkbox #19665
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
Merged
+877
−283
Merged
Core Checkbox #19665
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
use accesskit::Role; | ||
use bevy_a11y::AccessibilityNode; | ||
use bevy_app::{App, Plugin}; | ||
use bevy_ecs::event::{EntityEvent, Event}; | ||
use bevy_ecs::query::{Has, Without}; | ||
use bevy_ecs::system::{In, ResMut}; | ||
use bevy_ecs::{ | ||
component::Component, | ||
observer::On, | ||
system::{Commands, Query, SystemId}, | ||
}; | ||
use bevy_input::keyboard::{KeyCode, KeyboardInput}; | ||
use bevy_input::ButtonState; | ||
use bevy_input_focus::{FocusedInput, InputFocus, InputFocusVisible}; | ||
use bevy_picking::events::{Click, Pointer}; | ||
use bevy_ui::{Checkable, Checked, InteractionDisabled}; | ||
|
||
/// Headless widget implementation for checkboxes. The [`Checked`] component represents the current | ||
/// state of the checkbox. The `on_change` field is an optional system id that will be run when the | ||
/// checkbox is clicked, or when the `Enter` or `Space` key is pressed while the checkbox is | ||
/// focused. If the `on_change` field is `None`, then instead of calling a callback, the checkbox | ||
/// will update its own [`Checked`] state directly. | ||
/// | ||
/// # Toggle switches | ||
/// | ||
/// The [`CoreCheckbox`] component can be used to implement other kinds of toggle widgets. If you | ||
/// are going to do a toggle switch, you should override the [`AccessibilityNode`] component with | ||
/// the `Switch` role instead of the `Checkbox` role. | ||
#[derive(Component, Debug, Default)] | ||
#[require(AccessibilityNode(accesskit::Node::new(Role::CheckBox)), Checkable)] | ||
pub struct CoreCheckbox { | ||
/// One-shot system that is run when the checkbox state needs to be changed. | ||
pub on_change: Option<SystemId<In<bool>>>, | ||
} | ||
|
||
fn checkbox_on_key_input( | ||
mut ev: On<FocusedInput<KeyboardInput>>, | ||
q_checkbox: Query<(&CoreCheckbox, Has<Checked>), Without<InteractionDisabled>>, | ||
mut commands: Commands, | ||
) { | ||
if let Ok((checkbox, is_checked)) = q_checkbox.get(ev.target()) { | ||
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); | ||
set_checkbox_state(&mut commands, ev.target(), checkbox, !is_checked); | ||
} | ||
} | ||
} | ||
|
||
fn checkbox_on_pointer_click( | ||
mut ev: On<Pointer<Click>>, | ||
q_checkbox: Query<(&CoreCheckbox, Has<Checked>, Has<InteractionDisabled>)>, | ||
focus: Option<ResMut<InputFocus>>, | ||
focus_visible: Option<ResMut<InputFocusVisible>>, | ||
mut commands: Commands, | ||
) { | ||
if let Ok((checkbox, is_checked, disabled)) = q_checkbox.get(ev.target()) { | ||
// Clicking on a button makes it the focused input, | ||
// and hides the focus ring if it was visible. | ||
if let Some(mut focus) = focus { | ||
focus.0 = Some(ev.target()); | ||
} | ||
if let Some(mut focus_visible) = focus_visible { | ||
focus_visible.0 = false; | ||
} | ||
|
||
ev.propagate(false); | ||
if !disabled { | ||
set_checkbox_state(&mut commands, ev.target(), checkbox, !is_checked); | ||
} | ||
} | ||
} | ||
|
||
/// Event which can be triggered on a checkbox to set the checked state. This can be used to control | ||
/// the checkbox via gamepad buttons or other inputs. | ||
/// | ||
/// # Example: | ||
/// | ||
/// ``` | ||
/// use bevy_ecs::system::Commands; | ||
/// use bevy_core_widgets::{CoreCheckbox, SetChecked}; | ||
/// | ||
/// fn setup(mut commands: Commands) { | ||
/// // Create a checkbox | ||
/// let checkbox = commands.spawn(( | ||
/// CoreCheckbox::default(), | ||
/// )).id(); | ||
/// | ||
/// // Set to checked | ||
/// commands.trigger_targets(SetChecked(true), checkbox); | ||
/// } | ||
/// ``` | ||
#[derive(Event, EntityEvent)] | ||
pub struct SetChecked(pub bool); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Small nit: This should probably be an enum instead of a boolean. |
||
|
||
/// Event which can be triggered on a checkbox to toggle the checked state. This can be used to | ||
/// control the checkbox via gamepad buttons or other inputs. | ||
/// | ||
/// # Example: | ||
/// | ||
/// ``` | ||
/// use bevy_ecs::system::Commands; | ||
/// use bevy_core_widgets::{CoreCheckbox, ToggleChecked}; | ||
/// | ||
/// fn setup(mut commands: Commands) { | ||
/// // Create a checkbox | ||
/// let checkbox = commands.spawn(( | ||
/// CoreCheckbox::default(), | ||
/// )).id(); | ||
/// | ||
/// // Set to checked | ||
/// commands.trigger_targets(ToggleChecked, checkbox); | ||
/// } | ||
/// ``` | ||
#[derive(Event, EntityEvent)] | ||
pub struct ToggleChecked; | ||
|
||
fn checkbox_on_set_checked( | ||
mut ev: On<SetChecked>, | ||
q_checkbox: Query<(&CoreCheckbox, Has<Checked>, Has<InteractionDisabled>)>, | ||
mut commands: Commands, | ||
) { | ||
if let Ok((checkbox, is_checked, disabled)) = q_checkbox.get(ev.target()) { | ||
ev.propagate(false); | ||
if disabled { | ||
return; | ||
} | ||
|
||
let will_be_checked = ev.event().0; | ||
if will_be_checked != is_checked { | ||
set_checkbox_state(&mut commands, ev.target(), checkbox, will_be_checked); | ||
} | ||
} | ||
} | ||
|
||
fn checkbox_on_toggle_checked( | ||
mut ev: On<ToggleChecked>, | ||
q_checkbox: Query<(&CoreCheckbox, Has<Checked>, Has<InteractionDisabled>)>, | ||
mut commands: Commands, | ||
) { | ||
if let Ok((checkbox, is_checked, disabled)) = q_checkbox.get(ev.target()) { | ||
ev.propagate(false); | ||
if disabled { | ||
return; | ||
} | ||
|
||
set_checkbox_state(&mut commands, ev.target(), checkbox, !is_checked); | ||
} | ||
} | ||
|
||
fn set_checkbox_state( | ||
commands: &mut Commands, | ||
entity: impl Into<bevy_ecs::entity::Entity>, | ||
checkbox: &CoreCheckbox, | ||
new_state: bool, | ||
) { | ||
if let Some(on_change) = checkbox.on_change { | ||
commands.run_system_with(on_change, new_state); | ||
} else if new_state { | ||
commands.entity(entity.into()).insert(Checked); | ||
} else { | ||
commands.entity(entity.into()).remove::<Checked>(); | ||
} | ||
} | ||
|
||
/// Plugin that adds the observers for the [`CoreCheckbox`] widget. | ||
pub struct CoreCheckboxPlugin; | ||
|
||
impl Plugin for CoreCheckboxPlugin { | ||
fn build(&self, app: &mut App) { | ||
app.add_observer(checkbox_on_key_input) | ||
.add_observer(checkbox_on_pointer_click) | ||
.add_observer(checkbox_on_set_checked) | ||
.add_observer(checkbox_on_toggle_checked); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Oh nice, I like the piped one shot systems here!