Skip to content

Commit 8487a0f

Browse files
Let BorderColor::all take impl Into<Color> (#20311)
# Objective Remove some friction when building UI by changing `BorderColor::all` to accept any `impl Into<Color>` type. --------- Co-authored-by: Alice Cecile <[email protected]>
1 parent 967fa23 commit 8487a0f

File tree

12 files changed

+44
-26
lines changed

12 files changed

+44
-26
lines changed

crates/bevy_ui/src/ui_node.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2116,10 +2116,17 @@ impl<T: Into<Color>> From<T> for BorderColor {
21162116

21172117
impl BorderColor {
21182118
/// Border color is transparent by default.
2119-
pub const DEFAULT: Self = BorderColor::all(Color::NONE);
2119+
pub const DEFAULT: Self = BorderColor {
2120+
top: Color::NONE,
2121+
right: Color::NONE,
2122+
bottom: Color::NONE,
2123+
left: Color::NONE,
2124+
};
21202125

21212126
/// Helper to create a `BorderColor` struct with all borders set to the given color
2122-
pub const fn all(color: Color) -> Self {
2127+
#[inline]
2128+
pub fn all(color: impl Into<Color>) -> Self {
2129+
let color = color.into();
21232130
Self {
21242131
top: color,
21252132
bottom: color,

examples/animation/animation_graph.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ fn setup_node_rects(commands: &mut Commands) {
301301
justify_content: JustifyContent::Center,
302302
..default()
303303
},
304-
BorderColor::all(WHITE.into()),
304+
BorderColor::all(WHITE),
305305
Outline::new(Val::Px(1.), Val::ZERO, Color::WHITE),
306306
));
307307

@@ -355,7 +355,7 @@ fn setup_node_lines(commands: &mut Commands) {
355355
border: UiRect::bottom(Val::Px(1.0)),
356356
..default()
357357
},
358-
BorderColor::all(WHITE.into()),
358+
BorderColor::all(WHITE),
359359
));
360360
}
361361

@@ -370,7 +370,7 @@ fn setup_node_lines(commands: &mut Commands) {
370370
border: UiRect::left(Val::Px(1.0)),
371371
..default()
372372
},
373-
BorderColor::all(WHITE.into()),
373+
BorderColor::all(WHITE),
374374
));
375375
}
376376
}

examples/helpers/widgets.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ pub struct RadioButtonText;
2828
pub const BUTTON_BORDER: UiRect = UiRect::all(Val::Px(1.0));
2929

3030
/// The color of the border that surrounds buttons.
31-
pub const BUTTON_BORDER_COLOR: BorderColor = BorderColor::all(Color::WHITE);
31+
pub const BUTTON_BORDER_COLOR: BorderColor = BorderColor {
32+
left: Color::WHITE,
33+
right: Color::WHITE,
34+
top: Color::WHITE,
35+
bottom: Color::WHITE,
36+
};
3237

3338
/// The amount of rounding to apply to button corners.
3439
pub const BUTTON_BORDER_RADIUS_SIZE: Val = Val::Px(6.0);

examples/testbed/full_ui.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
221221
justify_content: JustifyContent::Center,
222222
..default()
223223
},
224-
BorderColor::all(LIME.into()),
224+
BorderColor::all(LIME),
225225
BackgroundColor(Color::srgb(0.8, 0.8, 1.)),
226226
))
227227
.with_children(|parent| {

examples/testbed/ui.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ mod borders {
233233
..default()
234234
},
235235
BackgroundColor(MAROON.into()),
236-
BorderColor::all(RED.into()),
236+
BorderColor::all(RED),
237237
Outline {
238238
width: Val::Px(10.),
239239
offset: Val::Px(10.),
@@ -325,7 +325,7 @@ mod box_shadow {
325325
border: UiRect::all(Val::Px(2.)),
326326
..default()
327327
},
328-
BorderColor::all(WHITE.into()),
328+
BorderColor::all(WHITE),
329329
border_radius,
330330
BackgroundColor(BLUE.into()),
331331
BoxShadow::new(
@@ -423,7 +423,7 @@ mod overflow {
423423
overflow,
424424
..default()
425425
},
426-
BorderColor::all(RED.into()),
426+
BorderColor::all(RED),
427427
BackgroundColor(Color::WHITE),
428428
))
429429
.with_children(|parent| {
@@ -545,7 +545,7 @@ mod layout_rounding {
545545
..Default::default()
546546
},
547547
BackgroundColor(MAROON.into()),
548-
BorderColor::all(DARK_BLUE.into()),
548+
BorderColor::all(DARK_BLUE),
549549
));
550550
}
551551
});

examples/ui/box_shadow.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ fn setup(
172172

173173
(
174174
node,
175-
BorderColor::all(WHITE.into()),
175+
BorderColor::all(WHITE),
176176
radius,
177177
BackgroundColor(Color::srgb(0.21, 0.21, 0.21)),
178178
BoxShadow(vec![ShadowStyle {

examples/ui/button.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ fn button_system(
4444
input_focus.set(entity);
4545
**text = "Press".to_string();
4646
*color = PRESSED_BUTTON.into();
47-
*border_color = BorderColor::all(RED.into());
47+
*border_color = BorderColor::all(RED);
4848

4949
// The accessibility system's only update the button's state when the `Button` component is marked as changed.
5050
button.set_changed();

examples/ui/directional_navigation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ fn highlight_focused_element(
361361
if input_focus.0 == Some(entity) && input_focus_visible.0 {
362362
// Don't change the border size / radius here,
363363
// as it would result in wiggling buttons when they are focused
364-
*border_color = BorderColor::all(FOCUSED_BORDER.into());
364+
*border_color = BorderColor::all(FOCUSED_BORDER);
365365
} else {
366366
*border_color = BorderColor::DEFAULT;
367367
}

examples/ui/gradients.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ fn setup(mut commands: Commands) {
202202
)]
203203
)).observe(
204204
|_trigger: On<Pointer<Over>>, mut border_query: Query<&mut BorderColor, With<Button>>| {
205-
*border_query.single_mut().unwrap() = BorderColor::all(RED.into());
205+
*border_query.single_mut().unwrap() = BorderColor::all(RED);
206206

207207

208208
})

examples/ui/tab_navigation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ fn button_system(
3434
match *interaction {
3535
Interaction::Pressed => {
3636
*color = PRESSED_BUTTON.into();
37-
*border_color = BorderColor::all(RED.into());
37+
*border_color = BorderColor::all(RED);
3838
}
3939
Interaction::Hovered => {
4040
*color = HOVERED_BUTTON.into();

0 commit comments

Comments
 (0)