Skip to content

Commit b4b7b0b

Browse files
Spacing *_all helper methods for even spacing on all sides
1 parent c5a319e commit b4b7b0b

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

crates/bevy_ui/src/layout_components.rs

+39
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,19 @@ impl Spacing {
157157
}
158158
}
159159

160+
/// Sets the margin on all sides to `val`
161+
pub const fn margin_all(val: Val) -> Spacing {
162+
Spacing {
163+
margin: UiRect {
164+
left: val,
165+
right: val,
166+
bottom: val,
167+
top: val,
168+
},
169+
..Self::DEFAULT
170+
}
171+
}
172+
160173
/// Sets only the padding
161174
pub const fn padding(rect: UiRect<Val>) -> Spacing {
162175
Spacing {
@@ -165,13 +178,39 @@ impl Spacing {
165178
}
166179
}
167180

181+
/// Sets the padding on all sides to `val`
182+
pub const fn padding_all(val: Val) -> Spacing {
183+
Spacing {
184+
padding: UiRect {
185+
left: val,
186+
right: val,
187+
bottom: val,
188+
top: val,
189+
},
190+
..Self::DEFAULT
191+
}
192+
}
193+
168194
/// Sets only the padding
169195
pub const fn border(rect: UiRect<Val>) -> Spacing {
170196
Spacing {
171197
border: rect,
172198
..Self::DEFAULT
173199
}
174200
}
201+
202+
/// Sets the border on all sides to `val`
203+
pub const fn border_all(val: Val) -> Spacing {
204+
Spacing {
205+
border: UiRect {
206+
left: val,
207+
right: val,
208+
bottom: val,
209+
top: val,
210+
},
211+
..Self::DEFAULT
212+
}
213+
}
175214
}
176215

177216
/// Defines the text direction

examples/games/game_menu.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ mod game {
172172
color: TEXT_COLOR,
173173
},
174174
))
175-
.insert(Spacing::margin(UiRect::all(Val::Px(50.0))));
175+
.insert(Spacing::margin_all(Val::Px(50.0)));
176176

177177
parent
178178
.spawn_bundle(TextBundle::from_sections([
@@ -201,7 +201,7 @@ mod game {
201201
},
202202
),
203203
]))
204-
.insert(Spacing::margin(UiRect::all(Val::Px(50.0))));
204+
.insert(Spacing::margin_all(Val::Px(50.0)));
205205
});
206206
// Spawn a 5 seconds timer to trigger going back to the menu
207207
commands.insert_resource(GameTimer(Timer::from_seconds(5.0, false)));

examples/ui/ui.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
3939
Val::Px(200.0),
4040
Val::Percent(100.0),
4141
),
42-
spacing: Spacing::border(UiRect::all(Val::Px(2.0))),
42+
spacing: Spacing::border_all(Val::Px(2.0)),
4343
color: Color::rgb(0.65, 0.65, 0.65).into(),
4444
..default()
4545
})
@@ -66,7 +66,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
6666
color: Color::WHITE,
6767
},
6868
))
69-
.insert(Spacing::margin(UiRect::all(Val::Px(5.0))));
69+
.insert(Spacing::margin_all(Val::Px(5.0)));
7070
});
7171
});
7272
// right vertical fill

examples/window/scale_factor_override.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
3636
parent
3737
.spawn_bundle(NodeBundle {
3838
size_constraints: SizeConstraints::suggested(Val::Px(200.), Val::FULL),
39-
spacing: Spacing::border(UiRect::all(Val::Px(2.0))),
39+
spacing: Spacing::border_all(Val::Px(2.0)),
4040
color: Color::rgb(0.65, 0.65, 0.65).into(),
4141
..default()
4242
})

0 commit comments

Comments
 (0)