Skip to content

Commit 15d0807

Browse files
committed
misc example fixes
1 parent 8845d31 commit 15d0807

File tree

2 files changed

+39
-33
lines changed

2 files changed

+39
-33
lines changed

examples/counter.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,14 @@ fn counter_button(counter_holder: LazyEntity, color: Color, label: &'static str,
117117
// Attach observers to the entity
118118
.observe(move |on: On<Pointer<Click>>, mut counters: Query<&mut Counter>| {
119119
if matches!(on.button, PointerButton::Primary) {
120-
// Use the fulfilled `LazyEntity` to get mutable access to the `Counter` component on our
121-
// state-holding entity.
122-
if let Ok(mut counter) = counters.get_mut(counter_holder.get()) {
123-
// --- State Mutation ---
124-
// Because our text display has a signal that reads this component, this change will
125-
// automatically trigger a UI update at the end of the frame.
126-
**counter += step;
127-
}
120+
// Use the fulfilled `LazyEntity` to get mutable access to the `Counter` component on our
121+
// state-holding entity.
122+
if let Ok(mut counter) = counters.get_mut(counter_holder.get()) {
123+
// --- State Mutation ---
124+
// Because our text display has a signal that reads this component, this change will
125+
// automatically trigger a UI update at the end of the frame.
126+
**counter += step;
127+
}
128128
}
129129
})
130130
.child(JonmoBuilder::from((Text::from(label), TextFont::from_font_size(25.))))

examples/letters.rs

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Simple key press counter, showcasing map reactivity.
1+
//! Key press counter with swappable save states, showcasing map reactivity.
22
mod utils;
33
use utils::*;
44

@@ -87,7 +87,6 @@ fn ui_root() -> JonmoBuilder {
8787
.child(
8888
JonmoBuilder::from(Node {
8989
align_self: AlignSelf::Center,
90-
justify_content: JustifyContent::SpaceBetween,
9190
width: Val::Percent(100.),
9291
..default()
9392
})
@@ -102,22 +101,25 @@ fn ui_root() -> JonmoBuilder {
102101
}))),
103102
)
104103
.child(
105-
text_node()
106-
.insert((TextColor(BLUE), TextFont::from_font_size(LETTER_SIZE)))
107-
.with_component::<Node>(|mut node| node.height = Val::Px(100.))
108-
.component_signal(
109-
active_save_signal
110-
.clone()
111-
.switch_signal_vec(move |In(active_save): In<ActiveSave>, save_states: Res<SaveStates>| {
112-
get_active_map(&save_states, active_save).signal_vec_entries()
113-
})
114-
.map_in(|(_, LetterData { count, .. })| count)
115-
.sum()
116-
.dedupe()
117-
.map_in_ref(ToString::to_string)
118-
.map_in(Text)
119-
.map_in(Some),
120-
),
104+
sum_container()
105+
.child(
106+
text_node()
107+
.insert((TextColor(BLUE), TextFont::from_font_size(LETTER_SIZE)))
108+
.with_component::<Node>(|mut node| node.height = Val::Px(100.))
109+
.component_signal(
110+
active_save_signal
111+
.clone()
112+
.switch_signal_vec(move |In(active_save): In<ActiveSave>, save_states: Res<SaveStates>| {
113+
get_active_map(&save_states, active_save).signal_vec_entries()
114+
})
115+
.map_in(|(_, LetterData { count, .. })| count)
116+
.sum()
117+
.dedupe()
118+
.map_in_ref(ToString::to_string)
119+
.map_in(Text)
120+
.map_in(Some),
121+
)
122+
),
121123
),
122124
)
123125
.children(ROWS.into_iter().map(clone!((active_save_signal) move |row| {
@@ -139,13 +141,7 @@ fn ui_root() -> JonmoBuilder {
139141
}),
140142
))
141143
.child(
142-
JonmoBuilder::from(Node {
143-
align_self: AlignSelf::Center,
144-
justify_content: JustifyContent::FlexEnd,
145-
flex_grow: 1.,
146-
padding: UiRect::all(Val::Px(GAP * 2.)),
147-
..default()
148-
})
144+
sum_container()
149145
.child(
150146
text_node()
151147
.insert((TextColor(BLUE), TextFont::from_font_size(LETTER_SIZE)))
@@ -171,6 +167,16 @@ fn ui_root() -> JonmoBuilder {
171167

172168
const GAP: f32 = 5.;
173169

170+
fn sum_container() -> JonmoBuilder {
171+
JonmoBuilder::from(Node {
172+
align_self: AlignSelf::Center,
173+
justify_content: JustifyContent::FlexEnd,
174+
flex_grow: 1.,
175+
padding: UiRect::all(Val::Px(GAP * 2.)),
176+
..default()
177+
})
178+
}
179+
174180
fn save_card(save_char: char, active_save_signal: impl Signal<Item = ActiveSave> + Clone) -> JonmoBuilder {
175181
JonmoBuilder::from((
176182
Node {

0 commit comments

Comments
 (0)