Skip to content

Commit 3aaadd9

Browse files
authored
Minor refactoring of box_shadow example (#19404)
# Objective Minimal effort to address feedback here: #19345 (comment) more thoroughly. ## Solution - Remove hardcoded label string comparisons and make more use of the new enum added during review - Resist temptation to let this snowball this into a huge refactor - Maybe come back later for a few other small improvements ## Testing `cargo run --example box_shadow`
1 parent 13e89a1 commit 3aaadd9

File tree

1 file changed

+46
-45
lines changed

1 file changed

+46
-45
lines changed

examples/ui/box_shadow.rs

Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,19 @@ enum SettingType {
9494
Shape,
9595
Samples,
9696
}
97+
impl SettingType {
98+
fn label(&self) -> &str {
99+
match self {
100+
SettingType::XOffset => "X Offset",
101+
SettingType::YOffset => "Y Offset",
102+
SettingType::Blur => "Blur",
103+
SettingType::Spread => "Spread",
104+
SettingType::Count => "Count",
105+
SettingType::Shape => "Shape",
106+
SettingType::Samples => "Samples",
107+
}
108+
}
109+
}
97110

98111
#[derive(Resource, Default)]
99112
struct HeldButton {
@@ -191,50 +204,50 @@ fn setup(
191204
))
192205
.insert(children![
193206
build_setting_row(
194-
"Shape:",
207+
SettingType::Shape,
195208
SettingsButton::ShapePrev,
196209
SettingsButton::ShapeNext,
197210
shape.index as f32,
198211
&asset_server,
199212
),
200213
build_setting_row(
201-
"X Offset:",
214+
SettingType::XOffset,
202215
SettingsButton::XOffsetDec,
203216
SettingsButton::XOffsetInc,
204217
shadow.x_offset,
205218
&asset_server,
206219
),
207220
build_setting_row(
208-
"Y Offset:",
221+
SettingType::YOffset,
209222
SettingsButton::YOffsetDec,
210223
SettingsButton::YOffsetInc,
211224
shadow.y_offset,
212225
&asset_server,
213226
),
214227
build_setting_row(
215-
"Blur:",
228+
SettingType::Blur,
216229
SettingsButton::BlurDec,
217230
SettingsButton::BlurInc,
218231
shadow.blur,
219232
&asset_server,
220233
),
221234
build_setting_row(
222-
"Spread:",
235+
SettingType::Spread,
223236
SettingsButton::SpreadDec,
224237
SettingsButton::SpreadInc,
225238
shadow.spread,
226239
&asset_server,
227240
),
228241
build_setting_row(
229-
"Count:",
242+
SettingType::Count,
230243
SettingsButton::CountDec,
231244
SettingsButton::CountInc,
232245
shadow.count as f32,
233246
&asset_server,
234247
),
235248
// Add BoxShadowSamples as a setting row
236249
build_setting_row(
237-
"Samples:",
250+
SettingType::Samples,
238251
SettingsButton::SamplesDec,
239252
SettingsButton::SamplesInc,
240253
shadow.samples as f32,
@@ -278,22 +291,18 @@ fn setup(
278291

279292
// Helper to return an input to the children! macro for a setting row
280293
fn build_setting_row(
281-
label: &str,
294+
setting_type: SettingType,
282295
dec: SettingsButton,
283296
inc: SettingsButton,
284297
value: f32,
285298
asset_server: &Res<AssetServer>,
286299
) -> impl Bundle {
287-
let label_type = match label {
288-
"X Offset:" => SettingType::XOffset,
289-
"Y Offset:" => SettingType::YOffset,
290-
"Blur:" => SettingType::Blur,
291-
"Spread:" => SettingType::Spread,
292-
"Count:" => SettingType::Count,
293-
"Shape:" => SettingType::Shape,
294-
"Samples:" => SettingType::Samples,
295-
_ => panic!("Unknown label: {}", label),
300+
let value_text = match setting_type {
301+
SettingType::Shape => SHAPES[value as usize % SHAPES.len()].0.to_string(),
302+
SettingType::Count => format!("{}", value as usize),
303+
_ => format!("{:.1}", value),
296304
};
305+
297306
(
298307
Node {
299308
flex_direction: FlexDirection::Row,
@@ -311,7 +320,7 @@ fn build_setting_row(
311320
},
312321
// Attach SettingType to the value label node, not the parent row
313322
children![(
314-
Text::new(label),
323+
Text::new(setting_type.label()),
315324
TextFont {
316325
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
317326
font_size: 16.0,
@@ -333,7 +342,11 @@ fn build_setting_row(
333342
BorderRadius::all(Val::Px(6.)),
334343
dec,
335344
children![(
336-
Text::new(if label == "Shape:" { "<" } else { "-" }),
345+
Text::new(if setting_type == SettingType::Shape {
346+
"<"
347+
} else {
348+
"-"
349+
}),
337350
TextFont {
338351
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
339352
font_size: 18.0,
@@ -352,31 +365,15 @@ fn build_setting_row(
352365
},
353366
BorderRadius::all(Val::Px(6.)),
354367
children![{
355-
if label_type == SettingType::Shape {
356-
(
357-
Text::new(SHAPES[value as usize % SHAPES.len()].0),
358-
TextFont {
359-
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
360-
font_size: 16.0,
361-
..default()
362-
},
363-
label_type,
364-
)
365-
} else {
366-
(
367-
Text::new(if label_type == SettingType::Count {
368-
format!("{}", value as usize)
369-
} else {
370-
format!("{:.1}", value)
371-
}),
372-
TextFont {
373-
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
374-
font_size: 16.0,
375-
..default()
376-
},
377-
label_type,
378-
)
379-
}
368+
(
369+
Text::new(value_text),
370+
TextFont {
371+
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
372+
font_size: 16.0,
373+
..default()
374+
},
375+
setting_type,
376+
)
380377
}],
381378
),
382379
(
@@ -392,7 +389,11 @@ fn build_setting_row(
392389
BorderRadius::all(Val::Px(6.)),
393390
inc,
394391
children![(
395-
Text::new(if label == "Shape:" { ">" } else { "+" }),
392+
Text::new(if setting_type == SettingType::Shape {
393+
">"
394+
} else {
395+
"+"
396+
}),
396397
TextFont {
397398
font: asset_server.load("fonts/FiraSans-Bold.ttf"),
398399
font_size: 18.0,

0 commit comments

Comments
 (0)