Skip to content

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
merged 5 commits into from
Jun 20, 2025
Merged

Core Checkbox #19665

merged 5 commits into from
Jun 20, 2025

Conversation

viridia
Copy link
Contributor

@viridia viridia commented Jun 15, 2025

Objective

This is part of the "core widgets" effort: #19236.

Solution

This adds the "core checkbox" widget type.

Testing

Tested using examples core_widgets and core_widgets_observers.

Note to reviewers: I reorganized the code in the examples, so the diffs are large because of code moves.

@alice-i-cecile alice-i-cecile added this to the 0.17 milestone Jun 15, 2025
@alice-i-cecile alice-i-cecile added A-UI Graphical user interfaces, styles, layouts, and widgets S-Needs-Review Needs reviewer attention (from anyone!) to move forward C-Feature A new feature, making something new possible M-Needs-Release-Note Work that should be called out in the blog due to impact labels Jun 15, 2025

fn checkbox_on_key_input(
mut ev: On<FocusedInput<KeyboardInput>>,
q_checkbox: Query<(&CoreCheckbox, Has<Checked>, Has<InteractionDisabled>)>,
Copy link
Contributor

@ickshonpe ickshonpe Jun 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason for querying for Has<InteractedDisabled> instead of using a Without<InteractionDisabled filter, or is it just stylistic?

Comment on lines +420 to +424
display: Display::Flex,
flex_direction: FlexDirection::Row,
justify_content: JustifyContent::FlexStart,
align_items: AlignItems::Center,
align_content: AlignContent::Center,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These constraints don't do anything?

Suggested change
display: Display::Flex,
flex_direction: FlexDirection::Row,
justify_content: JustifyContent::FlexStart,
align_items: AlignItems::Center,
align_content: AlignContent::Center,
align_items: AlignItems::Center,

Comment on lines +436 to +456
Node {
display: Display::Flex,
width: Val::Px(16.0),
height: Val::Px(16.0),
border: UiRect::all(Val::Px(2.0)),
..default()
},
BorderColor::all(CHECKBOX_OUTLINE), // Border color for the checkbox
BorderRadius::all(Val::Px(3.0)),
children![
// Checkbox inner
(
Node {
display: Display::Flex,
width: Val::Px(8.0),
height: Val::Px(8.0),
position_type: PositionType::Absolute,
left: Val::Px(2.0),
top: Val::Px(2.0),
..default()
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Node {
display: Display::Flex,
width: Val::Px(16.0),
height: Val::Px(16.0),
border: UiRect::all(Val::Px(2.0)),
..default()
},
BorderColor::all(CHECKBOX_OUTLINE), // Border color for the checkbox
BorderRadius::all(Val::Px(3.0)),
children![
// Checkbox inner
(
Node {
display: Display::Flex,
width: Val::Px(8.0),
height: Val::Px(8.0),
position_type: PositionType::Absolute,
left: Val::Px(2.0),
top: Val::Px(2.0),
..default()
},
Node {
width: Val::Px(16.0),
height: Val::Px(16.0),
border: UiRect::all(Val::Px(2.0)),
padding: UiRect::all(Val::Px(2.0)),
..default()
},
BorderColor::all(CHECKBOX_OUTLINE), // Border color for the checkbox
BorderRadius::all(Val::Px(3.0)),
children![
// Checkbox inner
(
Node {
flex_grow: 1.,
..default()
},

/// 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`, the checkbox will update its own state.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"will update its own state" is quite unclear. What exactly will it do that you should be mimicking?

#[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>>>,
Copy link
Member

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!

@viridia
Copy link
Contributor Author

viridia commented Jun 19, 2025

@cart I wanted to opine a bit on the whole Checked / Checkable thing. I changed it as per your request, but I don't actually think it's better. I think in terms of ergonomics it's robbing Peter to pay Paul: it reduces boilerplate for some cases and increases it for others.

The argument for making Checked a marker / ZST is that you can have separate observers for the checked vs. unchecked transition. This is a win if (a) you only care about one of these and not the other, or (b) the actions are asymmetrical in some way. But I'm not convinced that this will be true for the typical checkbox: if all I am doing is forwarding a boolean to some other process, then having it be a marker means I now need two observers instead of one.

This marker argument makes more sense for bistable transitions where the up transition is handled differently than the down transition. (Whether "pressed" falls under this criteria I don't know, but an argument can be made).

Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good, I'm content with moving forward here :)

/// }
/// ```
#[derive(Event, EntityEvent)]
pub struct SetChecked(pub bool);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small nit: This should probably be an enum instead of a boolean.

@alice-i-cecile alice-i-cecile added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jun 20, 2025
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Jun 20, 2025
Merged via the queue into bevyengine:main with commit 9fdddf7 Jun 20, 2025
34 checks passed
@viridia viridia deleted the core_checkbox branch June 20, 2025 18:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-UI Graphical user interfaces, styles, layouts, and widgets C-Feature A new feature, making something new possible M-Needs-Release-Note Work that should be called out in the blog due to impact S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants