Path: blob/main/crates/bevy_ui/src/interaction_states.rs
6849 views
/// This module contains components that are used to track the interaction state of UI widgets.1use bevy_a11y::AccessibilityNode;2use bevy_ecs::{3component::Component,4lifecycle::{Add, Remove},5observer::On,6world::DeferredWorld,7};89/// A component indicating that a widget is disabled and should be "grayed out".10/// This is used to prevent user interaction with the widget. It should not, however, prevent11/// the widget from being updated or rendered, or from acquiring keyboard focus.12///13/// For apps which support a11y: if a widget (such as a slider) contains multiple entities,14/// the `InteractionDisabled` component should be added to the root entity of the widget - the15/// same entity that contains the `AccessibilityNode` component. This will ensure that16/// the a11y tree is updated correctly.17#[derive(Component, Debug, Clone, Copy, Default)]18pub struct InteractionDisabled;1920pub(crate) fn on_add_disabled(add: On<Add, InteractionDisabled>, mut world: DeferredWorld) {21let mut entity = world.entity_mut(add.entity);22if let Some(mut accessibility) = entity.get_mut::<AccessibilityNode>() {23accessibility.set_disabled();24}25}2627pub(crate) fn on_remove_disabled(28remove: On<Remove, InteractionDisabled>,29mut world: DeferredWorld,30) {31let mut entity = world.entity_mut(remove.entity);32if let Some(mut accessibility) = entity.get_mut::<AccessibilityNode>() {33accessibility.clear_disabled();34}35}3637/// Component that indicates whether a button or widget is currently in a pressed or "held down"38/// state.39#[derive(Component, Default, Debug)]40pub struct Pressed;4142/// Component that indicates that a widget can be checked.43#[derive(Component, Default, Debug)]44pub struct Checkable;4546/// Component that indicates whether a checkbox or radio button is in a checked state.47#[derive(Component, Default, Debug)]48pub struct Checked;4950pub(crate) fn on_add_checkable(add: On<Add, Checked>, mut world: DeferredWorld) {51let mut entity = world.entity_mut(add.entity);52let checked = entity.get::<Checked>().is_some();53if let Some(mut accessibility) = entity.get_mut::<AccessibilityNode>() {54accessibility.set_toggled(match checked {55true => accesskit::Toggled::True,56false => accesskit::Toggled::False,57});58}59}6061pub(crate) fn on_remove_checkable(add: On<Add, Checked>, mut world: DeferredWorld) {62// Remove the 'toggled' attribute entirely.63let mut entity = world.entity_mut(add.entity);64if let Some(mut accessibility) = entity.get_mut::<AccessibilityNode>() {65accessibility.clear_toggled();66}67}6869pub(crate) fn on_add_checked(add: On<Add, Checked>, mut world: DeferredWorld) {70let mut entity = world.entity_mut(add.entity);71if let Some(mut accessibility) = entity.get_mut::<AccessibilityNode>() {72accessibility.set_toggled(accesskit::Toggled::True);73}74}7576pub(crate) fn on_remove_checked(remove: On<Remove, Checked>, mut world: DeferredWorld) {77let mut entity = world.entity_mut(remove.entity);78if let Some(mut accessibility) = entity.get_mut::<AccessibilityNode>() {79accessibility.set_toggled(accesskit::Toggled::False);80}81}828384