Path: blob/main/crates/bevy_ui/src/interaction_states.rs
6598 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(event: On<Add, InteractionDisabled>, mut world: DeferredWorld) {21let mut entity = world.entity_mut(event.entity());22if let Some(mut accessibility) = entity.get_mut::<AccessibilityNode>() {23accessibility.set_disabled();24}25}2627pub(crate) fn on_remove_disabled(event: On<Remove, InteractionDisabled>, mut world: DeferredWorld) {28let mut entity = world.entity_mut(event.entity());29if let Some(mut accessibility) = entity.get_mut::<AccessibilityNode>() {30accessibility.clear_disabled();31}32}3334/// Component that indicates whether a button or widget is currently in a pressed or "held down"35/// state.36#[derive(Component, Default, Debug)]37pub struct Pressed;3839/// Component that indicates that a widget can be checked.40#[derive(Component, Default, Debug)]41pub struct Checkable;4243/// Component that indicates whether a checkbox or radio button is in a checked state.44#[derive(Component, Default, Debug)]45pub struct Checked;4647pub(crate) fn on_add_checkable(event: On<Add, Checked>, mut world: DeferredWorld) {48let mut entity = world.entity_mut(event.entity());49let checked = entity.get::<Checked>().is_some();50if let Some(mut accessibility) = entity.get_mut::<AccessibilityNode>() {51accessibility.set_toggled(match checked {52true => accesskit::Toggled::True,53false => accesskit::Toggled::False,54});55}56}5758pub(crate) fn on_remove_checkable(event: On<Add, Checked>, mut world: DeferredWorld) {59// Remove the 'toggled' attribute entirely.60let mut entity = world.entity_mut(event.entity());61if let Some(mut accessibility) = entity.get_mut::<AccessibilityNode>() {62accessibility.clear_toggled();63}64}6566pub(crate) fn on_add_checked(event: On<Add, Checked>, mut world: DeferredWorld) {67let mut entity = world.entity_mut(event.entity());68if let Some(mut accessibility) = entity.get_mut::<AccessibilityNode>() {69accessibility.set_toggled(accesskit::Toggled::True);70}71}7273pub(crate) fn on_remove_checked(event: On<Remove, Checked>, mut world: DeferredWorld) {74let mut entity = world.entity_mut(event.entity());75if let Some(mut accessibility) = entity.get_mut::<AccessibilityNode>() {76accessibility.set_toggled(accesskit::Toggled::False);77}78}798081