Skip to content

Commit 9ec9e0c

Browse files
viridiabenfrankelalice-i-cecilecart
authored
Delete Callback and use observers to control widgets for 0.17 (#21247)
Removed the `Callback` type. Updated all widgets and examples. Added an `observe` helper which adds an observer via a bundle effect. Note: I would have updated the release notes, but they are gone already. --------- Co-authored-by: Ben Frankel <[email protected]> Co-authored-by: Alice Cecile <[email protected]> Co-authored-by: Carter Anderson <[email protected]>
1 parent 2133fcd commit 9ec9e0c

File tree

18 files changed

+615
-762
lines changed

18 files changed

+615
-762
lines changed

crates/bevy_feathers/src/controls/button.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ use bevy_ecs::{
99
reflect::ReflectComponent,
1010
schedule::IntoScheduleConfigs,
1111
spawn::{SpawnRelated, SpawnableList},
12-
system::{Commands, In, Query},
12+
system::{Commands, Query},
1313
};
1414
use bevy_input_focus::tab_navigation::TabIndex;
1515
use bevy_picking::{hover::Hovered, PickingSystems};
1616
use bevy_reflect::{prelude::ReflectDefault, Reflect};
1717
use bevy_ui::{AlignItems, InteractionDisabled, JustifyContent, Node, Pressed, UiRect, Val};
18-
use bevy_ui_widgets::{Activate, Button, Callback};
18+
use bevy_ui_widgets::Button;
1919

2020
use crate::{
2121
constants::{fonts, size},
@@ -47,8 +47,6 @@ pub struct ButtonProps {
4747
pub variant: ButtonVariant,
4848
/// Rounded corners options
4949
pub corners: RoundedCorners,
50-
/// Click handler
51-
pub on_click: Callback<In<Activate>>,
5250
}
5351

5452
/// Template function to spawn a button.
@@ -71,9 +69,7 @@ pub fn button<C: SpawnableList<ChildOf> + Send + Sync + 'static, B: Bundle>(
7169
flex_grow: 1.0,
7270
..Default::default()
7371
},
74-
Button {
75-
on_activate: props.on_click,
76-
},
72+
Button,
7773
props.variant,
7874
Hovered::default(),
7975
EntityCursor::System(bevy_window::SystemCursorIcon::Pointer),

crates/bevy_feathers/src/controls/checkbox.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use bevy_ecs::{
1111
reflect::ReflectComponent,
1212
schedule::IntoScheduleConfigs,
1313
spawn::{Spawn, SpawnRelated, SpawnableList},
14-
system::{Commands, In, Query},
14+
system::{Commands, Query},
1515
};
1616
use bevy_input_focus::tab_navigation::TabIndex;
1717
use bevy_math::Rot2;
@@ -21,7 +21,7 @@ use bevy_ui::{
2121
AlignItems, BorderRadius, Checked, Display, FlexDirection, InteractionDisabled, JustifyContent,
2222
Node, PositionType, UiRect, UiTransform, Val,
2323
};
24-
use bevy_ui_widgets::{Callback, Checkbox, ValueChange};
24+
use bevy_ui_widgets::Checkbox;
2525

2626
use crate::{
2727
constants::{fonts, size},
@@ -32,13 +32,6 @@ use crate::{
3232
tokens,
3333
};
3434

35-
/// Parameters for the checkbox template, passed to [`checkbox`] function.
36-
#[derive(Default)]
37-
pub struct CheckboxProps {
38-
/// Change handler
39-
pub on_change: Callback<In<ValueChange<bool>>>,
40-
}
41-
4235
/// Marker for the checkbox frame (contains both checkbox and label)
4336
#[derive(Component, Default, Clone, Reflect)]
4437
#[reflect(Component, Clone, Default)]
@@ -61,7 +54,6 @@ struct CheckboxMark;
6154
/// * `overrides` - a bundle of components that are merged in with the normal checkbox components.
6255
/// * `label` - the label of the checkbox.
6356
pub fn checkbox<C: SpawnableList<ChildOf> + Send + Sync + 'static, B: Bundle>(
64-
props: CheckboxProps,
6557
overrides: B,
6658
label: C,
6759
) -> impl Bundle {
@@ -74,9 +66,7 @@ pub fn checkbox<C: SpawnableList<ChildOf> + Send + Sync + 'static, B: Bundle>(
7466
column_gap: Val::Px(4.0),
7567
..Default::default()
7668
},
77-
Checkbox {
78-
on_change: props.on_change,
79-
},
69+
Checkbox,
8070
CheckboxFrame,
8171
Hovered::default(),
8272
EntityCursor::System(bevy_window::SystemCursorIcon::Pointer),

crates/bevy_feathers/src/controls/color_slider.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use bevy_ecs::{
1212
query::{Changed, Or, With},
1313
schedule::IntoScheduleConfigs,
1414
spawn::SpawnRelated,
15-
system::{In, Query},
15+
system::Query,
1616
};
1717
use bevy_input_focus::tab_navigation::TabIndex;
1818
use bevy_log::warn_once;
@@ -23,9 +23,7 @@ use bevy_ui::{
2323
UiRect, UiTransform, Val, Val2, ZIndex,
2424
};
2525
use bevy_ui_render::ui_material::MaterialNode;
26-
use bevy_ui_widgets::{
27-
Callback, Slider, SliderRange, SliderThumb, SliderValue, TrackClick, ValueChange,
28-
};
26+
use bevy_ui_widgets::{Slider, SliderRange, SliderThumb, SliderValue, TrackClick};
2927

3028
use crate::{
3129
alpha_pattern::{AlphaPattern, AlphaPatternMaterial},
@@ -146,8 +144,6 @@ pub struct SliderBaseColor(pub Color);
146144
pub struct ColorSliderProps {
147145
/// Slider current value
148146
pub value: f32,
149-
/// On-change handler
150-
pub on_change: Callback<In<ValueChange<f32>>>,
151147
/// Which color component we're editing
152148
pub channel: ColorChannel,
153149
}
@@ -156,7 +152,6 @@ impl Default for ColorSliderProps {
156152
fn default() -> Self {
157153
Self {
158154
value: 0.0,
159-
on_change: Callback::Ignore,
160155
channel: ColorChannel::Alpha,
161156
}
162157
}
@@ -195,7 +190,6 @@ pub fn color_slider<B: Bundle>(props: ColorSliderProps, overrides: B) -> impl Bu
195190
..Default::default()
196191
},
197192
Slider {
198-
on_change: props.on_change,
199193
track_click: TrackClick::Snap,
200194
},
201195
ColorSlider {

crates/bevy_feathers/src/controls/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ mod toggle_switch;
1111
mod virtual_keyboard;
1212

1313
pub use button::{button, ButtonPlugin, ButtonProps, ButtonVariant};
14-
pub use checkbox::{checkbox, CheckboxPlugin, CheckboxProps};
14+
pub use checkbox::{checkbox, CheckboxPlugin};
1515
pub use color_slider::{
1616
color_slider, ColorChannel, ColorSlider, ColorSliderPlugin, ColorSliderProps, SliderBaseColor,
1717
};
1818
pub use color_swatch::{color_swatch, ColorSwatch, ColorSwatchFg};
1919
pub use radio::{radio, RadioPlugin};
2020
pub use slider::{slider, SliderPlugin, SliderProps};
21-
pub use toggle_switch::{toggle_switch, ToggleSwitchPlugin, ToggleSwitchProps};
22-
pub use virtual_keyboard::virtual_keyboard;
21+
pub use toggle_switch::{toggle_switch, ToggleSwitchPlugin};
22+
pub use virtual_keyboard::{virtual_keyboard, VirtualKeyPressed};
2323

2424
use crate::alpha_pattern::AlphaPatternPlugin;
2525

crates/bevy_feathers/src/controls/slider.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use bevy_ecs::{
1313
reflect::ReflectComponent,
1414
schedule::IntoScheduleConfigs,
1515
spawn::SpawnRelated,
16-
system::{Commands, In, Query, Res},
16+
system::{Commands, Query, Res},
1717
};
1818
use bevy_input_focus::tab_navigation::TabIndex;
1919
use bevy_picking::PickingSystems;
@@ -23,7 +23,7 @@ use bevy_ui::{
2323
InteractionDisabled, InterpolationColorSpace, JustifyContent, LinearGradient, Node,
2424
PositionType, UiRect, Val,
2525
};
26-
use bevy_ui_widgets::{Callback, Slider, SliderRange, SliderValue, TrackClick, ValueChange};
26+
use bevy_ui_widgets::{Slider, SliderRange, SliderValue, TrackClick};
2727

2828
use crate::{
2929
constants::{fonts, size},
@@ -43,8 +43,6 @@ pub struct SliderProps {
4343
pub min: f32,
4444
/// Slider maximum value
4545
pub max: f32,
46-
/// On-change handler
47-
pub on_change: Callback<In<ValueChange<f32>>>,
4846
}
4947

5048
impl Default for SliderProps {
@@ -53,7 +51,6 @@ impl Default for SliderProps {
5351
value: 0.0,
5452
min: 0.0,
5553
max: 1.0,
56-
on_change: Callback::Ignore,
5754
}
5855
}
5956
}
@@ -86,7 +83,6 @@ pub fn slider<B: Bundle>(props: SliderProps, overrides: B) -> impl Bundle {
8683
..Default::default()
8784
},
8885
Slider {
89-
on_change: props.on_change,
9086
track_click: TrackClick::Drag,
9187
},
9288
SliderStyle,

crates/bevy_feathers/src/controls/toggle_switch.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ use bevy_ecs::{
1212
reflect::ReflectComponent,
1313
schedule::IntoScheduleConfigs,
1414
spawn::SpawnRelated,
15-
system::{Commands, In, Query},
15+
system::{Commands, Query},
1616
world::Mut,
1717
};
1818
use bevy_input_focus::tab_navigation::TabIndex;
1919
use bevy_picking::{hover::Hovered, PickingSystems};
2020
use bevy_reflect::{prelude::ReflectDefault, Reflect};
2121
use bevy_ui::{BorderRadius, Checked, InteractionDisabled, Node, PositionType, UiRect, Val};
22-
use bevy_ui_widgets::{Callback, Checkbox, ValueChange};
22+
use bevy_ui_widgets::Checkbox;
2323

2424
use crate::{
2525
constants::size,
@@ -28,13 +28,6 @@ use crate::{
2828
tokens,
2929
};
3030

31-
/// Parameters for the toggle switch template, passed to [`toggle_switch`] function.
32-
#[derive(Default)]
33-
pub struct ToggleSwitchProps {
34-
/// Change handler
35-
pub on_change: Callback<In<ValueChange<bool>>>,
36-
}
37-
3831
/// Marker for the toggle switch outline
3932
#[derive(Component, Default, Clone, Reflect)]
4033
#[reflect(Component, Clone, Default)]
@@ -50,17 +43,15 @@ struct ToggleSwitchSlide;
5043
/// # Arguments
5144
/// * `props` - construction properties for the toggle switch.
5245
/// * `overrides` - a bundle of components that are merged in with the normal toggle switch components.
53-
pub fn toggle_switch<B: Bundle>(props: ToggleSwitchProps, overrides: B) -> impl Bundle {
46+
pub fn toggle_switch<B: Bundle>(overrides: B) -> impl Bundle {
5447
(
5548
Node {
5649
width: size::TOGGLE_WIDTH,
5750
height: size::TOGGLE_HEIGHT,
5851
border: UiRect::all(Val::Px(2.0)),
5952
..Default::default()
6053
},
61-
Checkbox {
62-
on_change: props.on_change,
63-
},
54+
Checkbox,
6455
ToggleSwitchOutline,
6556
BorderRadius::all(Val::Px(5.0)),
6657
ThemeBackgroundColor(tokens::SWITCH_BG),
Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
1-
use bevy_ecs::{
2-
bundle::Bundle,
3-
component::Component,
4-
hierarchy::{ChildOf, Children},
5-
relationship::RelatedSpawner,
6-
spawn::{Spawn, SpawnRelated, SpawnWith},
7-
system::{In, SystemId},
8-
};
1+
use bevy_ecs::prelude::*;
92
use bevy_input_focus::tab_navigation::TabGroup;
103
use bevy_ui::Node;
114
use bevy_ui::Val;
125
use bevy_ui::{widget::Text, FlexDirection};
13-
use bevy_ui_widgets::{Activate, Callback};
6+
use bevy_ui_widgets::{observe, Activate};
147

158
use crate::controls::{button, ButtonProps};
169

10+
/// Fired whenever a virtual key is pressed.
11+
#[derive(EntityEvent)]
12+
pub struct VirtualKeyPressed<T> {
13+
/// The virtual keyboard entity
14+
pub entity: Entity,
15+
/// The pressed virtual key
16+
pub key: T,
17+
}
18+
1719
/// Function to spawn a virtual keyboard
1820
pub fn virtual_keyboard<T>(
19-
keys: impl Iterator<Item = Vec<(String, T)>> + Send + Sync + 'static,
20-
on_key_press: SystemId<In<Activate>>,
21+
keys: impl Iterator<Item = Vec<T>> + Send + Sync + 'static,
2122
) -> impl Bundle
2223
where
23-
T: Component,
24+
T: AsRef<str> + Clone + Send + Sync + 'static,
2425
{
2526
(
2627
Node {
@@ -29,28 +30,33 @@ where
2930
..Default::default()
3031
},
3132
TabGroup::new(0),
32-
Children::spawn((SpawnWith(move |parent: &mut RelatedSpawner<ChildOf>| {
33-
for row in keys {
34-
parent.spawn((
35-
Node {
36-
flex_direction: FlexDirection::Row,
37-
column_gap: Val::Px(4.),
38-
..Default::default()
39-
},
40-
Children::spawn(SpawnWith(move |parent: &mut RelatedSpawner<ChildOf>| {
41-
for (label, key_id) in row.into_iter() {
42-
parent.spawn(button(
43-
ButtonProps {
44-
on_click: Callback::System(on_key_press),
45-
..Default::default()
46-
},
47-
(key_id,),
48-
Spawn(Text::new(label)),
49-
));
50-
}
51-
})),
52-
));
53-
}
54-
}),)),
33+
Children::spawn(SpawnIter(keys.map(move |row| {
34+
(
35+
Node {
36+
flex_direction: FlexDirection::Row,
37+
column_gap: Val::Px(4.),
38+
..Default::default()
39+
},
40+
Children::spawn(SpawnIter(row.into_iter().map(move |key| {
41+
(
42+
button(ButtonProps::default(), (), Spawn(Text::new(key.as_ref()))),
43+
observe(
44+
move |activate: On<Activate>,
45+
mut commands: Commands,
46+
query: Query<&ChildOf>|
47+
-> Result {
48+
let virtual_keyboard =
49+
query.get(query.get(activate.entity)?.parent())?.parent();
50+
commands.trigger(VirtualKeyPressed {
51+
entity: virtual_keyboard,
52+
key: key.clone(),
53+
});
54+
Ok(())
55+
},
56+
),
57+
)
58+
}))),
59+
)
60+
}))),
5561
)
5662
}

0 commit comments

Comments
 (0)