Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_ui/src/interaction_states.rs
6598 views
1
/// This module contains components that are used to track the interaction state of UI widgets.
2
use bevy_a11y::AccessibilityNode;
3
use bevy_ecs::{
4
component::Component,
5
lifecycle::{Add, Remove},
6
observer::On,
7
world::DeferredWorld,
8
};
9
10
/// A component indicating that a widget is disabled and should be "grayed out".
11
/// This is used to prevent user interaction with the widget. It should not, however, prevent
12
/// the widget from being updated or rendered, or from acquiring keyboard focus.
13
///
14
/// For apps which support a11y: if a widget (such as a slider) contains multiple entities,
15
/// the `InteractionDisabled` component should be added to the root entity of the widget - the
16
/// same entity that contains the `AccessibilityNode` component. This will ensure that
17
/// the a11y tree is updated correctly.
18
#[derive(Component, Debug, Clone, Copy, Default)]
19
pub struct InteractionDisabled;
20
21
pub(crate) fn on_add_disabled(event: On<Add, InteractionDisabled>, mut world: DeferredWorld) {
22
let mut entity = world.entity_mut(event.entity());
23
if let Some(mut accessibility) = entity.get_mut::<AccessibilityNode>() {
24
accessibility.set_disabled();
25
}
26
}
27
28
pub(crate) fn on_remove_disabled(event: On<Remove, InteractionDisabled>, mut world: DeferredWorld) {
29
let mut entity = world.entity_mut(event.entity());
30
if let Some(mut accessibility) = entity.get_mut::<AccessibilityNode>() {
31
accessibility.clear_disabled();
32
}
33
}
34
35
/// Component that indicates whether a button or widget is currently in a pressed or "held down"
36
/// state.
37
#[derive(Component, Default, Debug)]
38
pub struct Pressed;
39
40
/// Component that indicates that a widget can be checked.
41
#[derive(Component, Default, Debug)]
42
pub struct Checkable;
43
44
/// Component that indicates whether a checkbox or radio button is in a checked state.
45
#[derive(Component, Default, Debug)]
46
pub struct Checked;
47
48
pub(crate) fn on_add_checkable(event: On<Add, Checked>, mut world: DeferredWorld) {
49
let mut entity = world.entity_mut(event.entity());
50
let checked = entity.get::<Checked>().is_some();
51
if let Some(mut accessibility) = entity.get_mut::<AccessibilityNode>() {
52
accessibility.set_toggled(match checked {
53
true => accesskit::Toggled::True,
54
false => accesskit::Toggled::False,
55
});
56
}
57
}
58
59
pub(crate) fn on_remove_checkable(event: On<Add, Checked>, mut world: DeferredWorld) {
60
// Remove the 'toggled' attribute entirely.
61
let mut entity = world.entity_mut(event.entity());
62
if let Some(mut accessibility) = entity.get_mut::<AccessibilityNode>() {
63
accessibility.clear_toggled();
64
}
65
}
66
67
pub(crate) fn on_add_checked(event: On<Add, Checked>, mut world: DeferredWorld) {
68
let mut entity = world.entity_mut(event.entity());
69
if let Some(mut accessibility) = entity.get_mut::<AccessibilityNode>() {
70
accessibility.set_toggled(accesskit::Toggled::True);
71
}
72
}
73
74
pub(crate) fn on_remove_checked(event: On<Remove, Checked>, mut world: DeferredWorld) {
75
let mut entity = world.entity_mut(event.entity());
76
if let Some(mut accessibility) = entity.get_mut::<AccessibilityNode>() {
77
accessibility.set_toggled(accesskit::Toggled::False);
78
}
79
}
80
81