Skip to content

Commit 5d941d5

Browse files
Remove custom window size from flex_layout example (#11876)
# Objective The example showcase doesn't seem to work well with the portrait aspect ratio used in this example, which is possibly something to be fixed there, but there's also no reason this *needs* a custom size. This custom window size is also sightly too tall for my particular display which is a very common display size when accounting for the macOS task bar and window title, so the content at the bottom is clipped. ## Solution - Remove the custom window size - Swap the order of the justify / align nested loops so that the content fits the new aspect ratio - Make the containers responsive to window size, and make all the gaps even ## Before <img width="870" alt="Screenshot 2024-02-15 at 10 56 11 AM" src="https://github.com/bevyengine/bevy/assets/200550/803217dd-e311-4f9e-aabf-2656f7f67615"> ## After <img width="1280" alt="Screenshot 2024-02-15 at 10 56 25 AM" src="https://github.com/bevyengine/bevy/assets/200550/bf1e4920-f053-4d42-ab0b-3efea6835cae"> Co-authored-by: Alice Cecile <[email protected]>
1 parent fa179ba commit 5d941d5

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

examples/ui/flex_layout.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ use bevy::prelude::*;
33

44
const ALIGN_ITEMS_COLOR: LegacyColor = LegacyColor::rgb(1., 0.066, 0.349);
55
const JUSTIFY_CONTENT_COLOR: LegacyColor = LegacyColor::rgb(0.102, 0.522, 1.);
6-
const MARGIN: Val = Val::Px(5.);
6+
const MARGIN: Val = Val::Px(12.);
77

88
fn main() {
99
App::new()
1010
.add_plugins(DefaultPlugins.set(WindowPlugin {
1111
primary_window: Some(Window {
12-
resolution: [870., 1066.].into(),
1312
title: "Bevy Flex Layout Example".to_string(),
1413
..Default::default()
1514
}),
@@ -27,8 +26,11 @@ fn spawn_layout(mut commands: Commands, asset_server: Res<AssetServer>) {
2726
style: Style {
2827
// fill the entire window
2928
width: Val::Percent(100.),
29+
height: Val::Percent(100.),
3030
flex_direction: FlexDirection::Column,
3131
align_items: AlignItems::Center,
32+
padding: UiRect::all(MARGIN),
33+
row_gap: MARGIN,
3234
..Default::default()
3335
},
3436
background_color: BackgroundColor(LegacyColor::BLACK),
@@ -40,7 +42,6 @@ fn spawn_layout(mut commands: Commands, asset_server: Res<AssetServer>) {
4042
.spawn(NodeBundle {
4143
style: Style {
4244
flex_direction: FlexDirection::Row,
43-
margin: UiRect::top(MARGIN),
4445
..Default::default()
4546
},
4647
..Default::default()
@@ -65,9 +66,10 @@ fn spawn_layout(mut commands: Commands, asset_server: Res<AssetServer>) {
6566
builder
6667
.spawn(NodeBundle {
6768
style: Style {
68-
width: Val::Px(850.),
69-
height: Val::Px(1020.),
69+
width: Val::Percent(100.),
70+
height: Val::Percent(100.),
7071
flex_direction: FlexDirection::Column,
72+
row_gap: MARGIN,
7173
..Default::default()
7274
},
7375
..Default::default()
@@ -89,17 +91,20 @@ fn spawn_layout(mut commands: Commands, asset_server: Res<AssetServer>) {
8991
AlignItems::FlexEnd,
9092
AlignItems::Stretch,
9193
];
92-
for justify_content in justifications {
94+
for align_items in alignments {
9395
builder
9496
.spawn(NodeBundle {
9597
style: Style {
98+
width: Val::Percent(100.),
99+
height: Val::Percent(100.),
96100
flex_direction: FlexDirection::Row,
101+
column_gap: MARGIN,
97102
..Default::default()
98103
},
99104
..Default::default()
100105
})
101106
.with_children(|builder| {
102-
for align_items in alignments {
107+
for justify_content in justifications {
103108
spawn_child_node(
104109
builder,
105110
font.clone(),
@@ -125,9 +130,8 @@ fn spawn_child_node(
125130
flex_direction: FlexDirection::Column,
126131
align_items,
127132
justify_content,
128-
width: Val::Px(160.),
129-
height: Val::Px(160.),
130-
margin: UiRect::all(MARGIN),
133+
width: Val::Percent(100.),
134+
height: Val::Percent(100.),
131135
..Default::default()
132136
},
133137
background_color: BackgroundColor(LegacyColor::DARK_GRAY),

0 commit comments

Comments
 (0)