Skip to content

Commit 7a18fa5

Browse files
oracle58ickshonpe
andcommitted
use children! instead of with_children in box_shadow example
Co-authored-by: ickshonpe <[email protected]>
1 parent 661bf86 commit 7a18fa5

File tree

1 file changed

+133
-145
lines changed

1 file changed

+133
-145
lines changed

examples/ui/box_shadow.rs

Lines changed: 133 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const SHAPES: &[(&str, fn(&mut Node, &mut BorderRadius))] = &[
3737
node.height = Val::Px(80.);
3838
*radius = BorderRadius::all(Val::Px(32.));
3939
}),
40-
("5", |node, radius| {
40+
("5", |node, radius| {
4141
node.width = Val::Px(80.);
4242
node.height = Val::Px(240.);
4343
*radius = BorderRadius::all(Val::Px(32.));
@@ -120,7 +120,7 @@ fn setup(
120120
},
121121
BackgroundColor(GRAY.into()),
122122
))
123-
.with_children(|parent| {
123+
.insert(children![{
124124
let mut node = Node {
125125
width: Val::Px(164.),
126126
height: Val::Px(164.),
@@ -132,7 +132,7 @@ fn setup(
132132
let mut radius = BorderRadius::ZERO;
133133
SHAPES[shape.index % SHAPES.len()].1(&mut node, &mut radius);
134134

135-
parent.spawn((
135+
(
136136
node,
137137
BorderColor(WHITE.into()),
138138
radius,
@@ -145,8 +145,8 @@ fn setup(
145145
blur_radius: Val::Px(shadow.blur),
146146
}]),
147147
ShadowNode,
148-
));
149-
});
148+
)
149+
}]);
150150

151151
// Settings Panel
152152
commands
@@ -169,129 +169,123 @@ fn setup(
169169
BorderRadius::all(Val::Px(12.0)),
170170
ZIndex(10),
171171
))
172-
.with_children(|parent| {
172+
.insert(children![
173173
// Shape settings
174-
spawn_setting(
175-
parent,
174+
spawn_setting_children(
176175
"Shape:",
177176
SettingsButton::ShapePrev,
178177
SettingsButton::ShapeNext,
179178
shape.index as f32,
180179
&asset_server,
181-
);
180+
),
182181
// Shadow settings
183-
for (label, dec, inc, value) in [
184-
(
185-
"X Offset:",
186-
SettingsButton::XOffsetDec,
187-
SettingsButton::XOffsetInc,
188-
shadow.x_offset,
189-
),
190-
(
191-
"Y Offset:",
192-
SettingsButton::YOffsetDec,
193-
SettingsButton::YOffsetInc,
194-
shadow.y_offset,
195-
),
196-
(
197-
"Blur:",
198-
SettingsButton::BlurDec,
199-
SettingsButton::BlurInc,
200-
shadow.blur,
201-
),
202-
(
203-
"Spread:",
204-
SettingsButton::SpreadDec,
205-
SettingsButton::SpreadInc,
206-
shadow.spread,
207-
),
208-
(
209-
"Count:",
210-
SettingsButton::CountDec,
211-
SettingsButton::CountInc,
212-
shadow.count as f32,
213-
),
214-
] {
215-
spawn_setting(parent, label, dec, inc, value, &asset_server);
216-
}
217-
182+
spawn_setting_children(
183+
"X Offset:",
184+
SettingsButton::XOffsetDec,
185+
SettingsButton::XOffsetInc,
186+
shadow.x_offset,
187+
&asset_server,
188+
),
189+
spawn_setting_children(
190+
"Y Offset:",
191+
SettingsButton::YOffsetDec,
192+
SettingsButton::YOffsetInc,
193+
shadow.y_offset,
194+
&asset_server,
195+
),
196+
spawn_setting_children(
197+
"Blur:",
198+
SettingsButton::BlurDec,
199+
SettingsButton::BlurInc,
200+
shadow.blur,
201+
&asset_server,
202+
),
203+
spawn_setting_children(
204+
"Spread:",
205+
SettingsButton::SpreadDec,
206+
SettingsButton::SpreadInc,
207+
shadow.spread,
208+
&asset_server,
209+
),
210+
spawn_setting_children(
211+
"Count:",
212+
SettingsButton::CountDec,
213+
SettingsButton::CountInc,
214+
shadow.count as f32,
215+
&asset_server,
216+
),
218217
// Reset button
219-
parent
220-
.spawn(Node {
218+
(
219+
Node {
221220
flex_direction: FlexDirection::Row,
222221
align_items: AlignItems::Center,
223222
height: Val::Px(36.0),
224223
margin: UiRect::top(Val::Px(12.0)),
225224
justify_content: JustifyContent::Center,
226225
..default()
227-
})
228-
.with_children(|row| {
229-
row.spawn((
230-
Button,
231-
Node {
232-
width: Val::Px(90.),
233-
height: Val::Px(32.),
234-
justify_content: JustifyContent::Center,
235-
align_items: AlignItems::Center,
226+
},
227+
children![(
228+
Button,
229+
Node {
230+
width: Val::Px(90.),
231+
height: Val::Px(32.),
232+
justify_content: JustifyContent::Center,
233+
align_items: AlignItems::Center,
234+
..default()
235+
},
236+
BackgroundColor(Color::WHITE),
237+
BorderRadius::all(Val::Px(8.)),
238+
SettingsButton::Reset,
239+
children![(
240+
Text::new("Reset"),
241+
TextFont {
242+
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
243+
font_size: 16.0,
236244
..default()
237245
},
238-
BackgroundColor(Color::WHITE),
239-
BorderRadius::all(Val::Px(8.)),
240-
SettingsButton::Reset,
241-
))
242-
.with_children(|btn| {
243-
btn.spawn((
244-
Text::new("Reset"),
245-
TextFont {
246-
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
247-
font_size: 16.0,
248-
..default()
249-
},
250-
TextColor(Color::WHITE),
251-
));
252-
});
253-
});
254-
});
246+
TextColor(Color::WHITE),
247+
)],
248+
)],
249+
),
250+
]);
255251
}
256252

257253
// --- UI Helper Functions ---
258254

259-
// Shadow settings (now also used for shape selector)
260-
fn spawn_setting(
261-
parent: &mut ChildSpawnerCommands,
255+
// Helper to return children! macro output for a setting row
256+
fn spawn_setting_children(
262257
label: &str,
263258
dec: SettingsButton,
264259
inc: SettingsButton,
265260
value: f32,
266261
asset_server: &Res<AssetServer>,
267-
) {
268-
parent
269-
.spawn(Node {
262+
) -> impl Bundle {
263+
(
264+
Node {
270265
flex_direction: FlexDirection::Row,
271266
align_items: AlignItems::Center,
272267
height: Val::Px(32.0),
273268
..default()
274-
})
275-
.with_children(|row| {
276-
row.spawn((Node {
277-
width: Val::Px(80.0),
278-
justify_content: JustifyContent::FlexEnd,
279-
align_items: AlignItems::Center,
280-
..default()
281-
},))
282-
.with_children(|label_node| {
283-
label_node.spawn((
284-
Text::new(label),
285-
TextFont {
286-
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
287-
font_size: 16.0,
288-
..default()
289-
},
290-
TextColor(Color::WHITE),
291-
));
292-
});
293-
// Decrement button
294-
row.spawn((
269+
},
270+
children![
271+
(
272+
Node {
273+
width: Val::Px(80.0),
274+
justify_content: JustifyContent::FlexEnd,
275+
align_items: AlignItems::Center,
276+
..default()
277+
},
278+
children![(
279+
Text::new(label),
280+
TextFont {
281+
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
282+
font_size: 16.0,
283+
..default()
284+
},
285+
TextColor(Color::WHITE),
286+
)],
287+
),
288+
(
295289
Button,
296290
Node {
297291
width: Val::Px(28.),
@@ -304,20 +298,17 @@ fn spawn_setting(
304298
BackgroundColor(Color::WHITE),
305299
BorderRadius::all(Val::Px(6.)),
306300
dec,
307-
))
308-
.with_children(|btn| {
309-
btn.spawn((
301+
children![(
310302
Text::new(if label == "Shape:" { "<" } else { "-" }),
311303
TextFont {
312304
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
313305
font_size: 18.0,
314306
..default()
315307
},
316308
TextColor(Color::WHITE),
317-
));
318-
});
319-
// Value display
320-
row.spawn((
309+
)],
310+
),
311+
(
321312
Node {
322313
width: Val::Px(48.),
323314
height: Val::Px(28.),
@@ -328,39 +319,37 @@ fn spawn_setting(
328319
},
329320
BackgroundColor(Color::WHITE.with_alpha(0.08)),
330321
BorderRadius::all(Val::Px(6.)),
331-
))
332-
.with_children(|val_node| {
333-
// For shape selector, show the shape label, else show the value
334-
if label == "Shape:" {
335-
val_node.spawn((
336-
Text::new(SHAPES[value as usize % SHAPES.len()].0),
337-
TextFont {
338-
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
339-
font_size: 16.0,
340-
..default()
341-
},
342-
TextColor(Color::WHITE),
343-
ValueLabel(label.to_string()),
344-
));
345-
} else {
346-
val_node.spawn((
347-
Text::new(if label == "Count:" {
348-
format!("{}", value as usize)
349-
} else {
350-
format!("{:.1}", value)
351-
}),
352-
TextFont {
353-
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
354-
font_size: 16.0,
355-
..default()
356-
},
357-
TextColor(Color::WHITE),
358-
ValueLabel(label.to_string()),
359-
));
360-
}
361-
});
362-
// Increment button
363-
row.spawn((
322+
children![{
323+
if label == "Shape:" {
324+
(
325+
Text::new(SHAPES[value as usize % SHAPES.len()].0),
326+
TextFont {
327+
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
328+
font_size: 16.0,
329+
..default()
330+
},
331+
TextColor(Color::WHITE),
332+
ValueLabel(label.to_string()),
333+
)
334+
} else {
335+
(
336+
Text::new(if label == "Count:" {
337+
format!("{}", value as usize)
338+
} else {
339+
format!("{:.1}", value)
340+
}),
341+
TextFont {
342+
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
343+
font_size: 16.0,
344+
..default()
345+
},
346+
TextColor(Color::WHITE),
347+
ValueLabel(label.to_string()),
348+
)
349+
}
350+
}],
351+
),
352+
(
364353
Button,
365354
Node {
366355
width: Val::Px(28.),
@@ -372,19 +361,18 @@ fn spawn_setting(
372361
BackgroundColor(Color::WHITE),
373362
BorderRadius::all(Val::Px(6.)),
374363
inc,
375-
))
376-
.with_children(|btn| {
377-
btn.spawn((
364+
children![(
378365
Text::new(if label == "Shape:" { ">" } else { "+" }),
379366
TextFont {
380367
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
381368
font_size: 18.0,
382369
..default()
383370
},
384371
TextColor(Color::WHITE),
385-
));
386-
});
387-
});
372+
)],
373+
),
374+
],
375+
)
388376
}
389377

390378
// --- SYSTEMS ---

0 commit comments

Comments
 (0)