-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Expand file tree
/
Copy pathdisclosure_toggle.rs
More file actions
159 lines (149 loc) · 4.46 KB
/
Copy pathdisclosure_toggle.rs
File metadata and controls
159 lines (149 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
use bevy_app::{App, Plugin, PreUpdate};
use bevy_ecs::{
entity::Entity,
hierarchy::Children,
lifecycle::RemovedComponents,
query::{Added, Has, Or, With},
reflect::ReflectComponent,
schedule::IntoScheduleConfigs,
system::{Commands, Query},
};
use bevy_input_focus::tab_navigation::TabIndex;
use bevy_math::Rot2;
use bevy_picking::PickingSystems;
use bevy_reflect::std_traits::ReflectDefault;
use bevy_reflect::Reflect;
use bevy_scene::{bsn, Scene, SceneComponent};
use bevy_ui::{
px, AlignItems, Checked, Display, InteractionDisabled, JustifyContent, Node, UiTransform,
};
use bevy_ui_widgets::Checkbox;
use bevy_window::SystemCursorIcon;
use crate::{
constants::icons, cursor::EntityCursor, display::icon, focus::FocusIndicator,
theme::InheritableThemeTextColor, tokens,
};
/// A toggle button which shows a chevron that points either right or down, used to expand or
/// collapse a panel. Functionally, this is equivalent to a checkbox, and has a [`Checked`]
/// state.
///
/// This is spawnable by inheriting it as a "scene component".
#[derive(SceneComponent, Default, Clone, Reflect)]
#[reflect(Component, Default, Clone)]
pub struct FeathersDisclosureToggle;
impl FeathersDisclosureToggle {
fn scene() -> impl Scene {
bsn!(
Node {
width: px(12),
height: px(12),
display: Display::Flex,
align_items: AlignItems::Center,
justify_content: JustifyContent::Center,
}
Checkbox
EntityCursor::System(SystemCursorIcon::Pointer)
FocusIndicator
InheritableThemeTextColor(tokens::BUTTON_TEXT)
TabIndex(0)
Children [
icon(icons::CHEVRON_RIGHT)
]
)
}
}
fn update_toggle_styles(
mut q_toggle: Query<
(
Entity,
Has<InteractionDisabled>,
Has<Checked>,
&mut UiTransform,
&InheritableThemeTextColor,
),
(
With<FeathersDisclosureToggle>,
Or<(Added<Checkbox>, Added<Checked>, Added<InteractionDisabled>)>,
),
>,
mut commands: Commands,
) {
for (ent, disabled, checked, mut transform, text_color) in q_toggle.iter_mut() {
set_toggle_styles(
ent,
disabled,
checked,
transform.as_mut(),
text_color,
&mut commands,
);
}
}
fn update_toggle_styles_remove(
mut q_toggle: Query<
(
Has<InteractionDisabled>,
Has<Checked>,
&mut UiTransform,
&InheritableThemeTextColor,
),
With<FeathersDisclosureToggle>,
>,
mut removed_disabled: RemovedComponents<InteractionDisabled>,
mut removed_checked: RemovedComponents<Checked>,
mut commands: Commands,
) {
removed_disabled
.read()
.chain(removed_checked.read())
.for_each(|ent| {
if let Ok((disabled, checked, mut transform, text_color)) = q_toggle.get_mut(ent) {
set_toggle_styles(
ent,
disabled,
checked,
transform.as_mut(),
text_color,
&mut commands,
);
}
});
}
fn set_toggle_styles(
entity: Entity,
disabled: bool,
checked: bool,
transform: &mut UiTransform,
text_color: &InheritableThemeTextColor,
commands: &mut Commands,
) {
// It's effectively the same color as the caption of a "plain" variant tool button with an icon.
let new_text_color_token = match disabled {
true => tokens::BUTTON_TEXT_DISABLED,
false => tokens::BUTTON_TEXT,
};
// Change icon color
if new_text_color_token != text_color.0 {
commands
.entity(entity)
.insert(InheritableThemeTextColor(new_text_color_token));
}
match checked {
true => {
transform.rotation = Rot2::turn_fraction(0.25);
}
false => {
transform.rotation = Rot2::turn_fraction(0.0);
}
};
}
/// Plugin which registers the systems for updating the toggle switch styles.
pub struct DisclosureTogglePlugin;
impl Plugin for DisclosureTogglePlugin {
fn build(&self, app: &mut App) {
app.add_systems(
PreUpdate,
(update_toggle_styles, update_toggle_styles_remove).in_set(PickingSystems::Last),
);
}
}