Skip to content

Commit b87622b

Browse files
committed
text aspect ratio bug fix (#6825)
## Objective Bevy UI uses a `MeasureFunc` that preserves the aspect ratio of text, not just images. This means that the extent of flex-items containing text may be calculated incorrectly depending on the ratio of the text size compared to the size of its containing node. Fixes #6748 Related to #6724 with Bevy 0.9: ![Capture_cols_0 9](https://user-images.githubusercontent.com/27962798/205435999-386d3400-fe9b-475a-aab1-18e61c4c074f.PNG) with this PR (accurately matching the behavior of Flexbox): ![Capture_fixed](https://user-images.githubusercontent.com/27962798/205436005-6bafbcc2-cd87-4eb7-b5c6-9dbcb30fc795.PNG) ## Solution Only perform the aspect ratio calculations if the uinode contains an image. ## Changelog * Added a field `preserve_aspect_ratio` to `CalculatedSize` * The `MeasureFunc` only preserves the aspect ratio when `preserve_aspect_ratio` is true. * `update_image_calculated_size_system` sets `preserve_aspect_ratio` to true for nodes with images.
1 parent f8e4b75 commit b87622b

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

crates/bevy_ui/src/flex/mod.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,15 @@ impl FlexSurface {
8686
match (constraints.width, constraints.height) {
8787
(Number::Undefined, Number::Undefined) => {}
8888
(Number::Defined(width), Number::Undefined) => {
89-
size.height = width * size.height / size.width;
89+
if calculated_size.preserve_aspect_ratio {
90+
size.height = width * size.height / size.width;
91+
}
9092
size.width = width;
9193
}
9294
(Number::Undefined, Number::Defined(height)) => {
93-
size.width = height * size.width / size.height;
95+
if calculated_size.preserve_aspect_ratio {
96+
size.width = height * size.width / size.height;
97+
}
9498
size.height = height;
9599
}
96100
(Number::Defined(width), Number::Defined(height)) => {
@@ -236,12 +240,6 @@ pub fn flex_node_system(
236240
let logical_to_physical_factor = windows.scale_factor(WindowId::primary());
237241
let scale_factor = logical_to_physical_factor * ui_scale.scale;
238242

239-
if scale_factor_events.iter().next_back().is_some() || ui_scale.is_changed() {
240-
update_changed(&mut flex_surface, scale_factor, full_node_query);
241-
} else {
242-
update_changed(&mut flex_surface, scale_factor, node_query);
243-
}
244-
245243
fn update_changed<F: ReadOnlyWorldQuery>(
246244
flex_surface: &mut FlexSurface,
247245
scaling_factor: f64,
@@ -258,6 +256,12 @@ pub fn flex_node_system(
258256
}
259257
}
260258

259+
if scale_factor_events.iter().next_back().is_some() || ui_scale.is_changed() {
260+
update_changed(&mut flex_surface, scale_factor, full_node_query);
261+
} else {
262+
update_changed(&mut flex_surface, scale_factor, node_query);
263+
}
264+
261265
for (entity, style, calculated_size) in &changed_size_query {
262266
flex_surface.upsert_leaf(entity, style, *calculated_size, scale_factor);
263267
}

crates/bevy_ui/src/ui_node.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,8 @@ pub enum FlexWrap {
434434
pub struct CalculatedSize {
435435
/// The size of the node
436436
pub size: Size,
437+
/// Whether to attempt to preserve the aspect ratio when determing the layout for this item
438+
pub preserve_aspect_ratio: bool,
437439
}
438440

439441
/// The background color of the node

crates/bevy_ui/src/widget/image.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub fn update_image_calculated_size_system(
2121
// Update only if size has changed to avoid needless layout calculations
2222
if size != calculated_size.size {
2323
calculated_size.size = size;
24+
calculated_size.preserve_aspect_ratio = true;
2425
}
2526
}
2627
}

0 commit comments

Comments
 (0)