Skip to content

Commit

Permalink
Pass cross-axis known_dimension when computing flex item min size (#545)
Browse files Browse the repository at this point in the history
* Pass cross-axis known_dimension when computing flex item min size

(fixes sizing of children that have inherent aspect ratio (e.g. images))

* Update changelog for inherent aspect ratio fix
  • Loading branch information
nicoburns authored Oct 2, 2023
1 parent 65bedf1 commit 120bb7a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
6 changes: 6 additions & 0 deletions RELEASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ Example usage change:
- All types from the `node`, `data`, `layout`, `error` and `cache` modules have been moved to the the `tree` module.
- Fixed misspelling: `RunMode::PeformLayout` renamed into `RunMode::PerformLayout` (added missing `r`).

## 0.3.14

### Fixes

- Flex: Fix issue where constraints were not being propagated, causing nodes with inherent aspect-ratio (typically images) to not apply that aspect-ratio (#545) (Fixes bevyengine/bevy#9841)

## 0.3.13

### Fixes
Expand Down
32 changes: 14 additions & 18 deletions src/compute/flexbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,7 @@ fn determine_flex_base_size(

// Parent size for child sizing
let cross_axis_parent_size = constants.node_inner_size.cross(dir);
let child_parent_size = Size::NONE.with_cross(dir, cross_axis_parent_size);

// Available space for child sizing
let cross_axis_margin_sum = constants.margin.cross_axis_sum(dir);
Expand All @@ -638,6 +639,18 @@ fn determine_flex_base_size(
.map_definite_value(|val| cross_axis_parent_size.unwrap_or(val))
.maybe_clamp(child_min_cross, child_max_cross);

// Known dimensions for child sizing
let child_known_dimensions = {
let mut ckd = child.size.with_main(dir, None);
if child.align_self == AlignSelf::Stretch && ckd.cross(dir).is_none() {
ckd.set_cross(
dir,
cross_axis_available_space.into_option().maybe_sub(child.margin.cross_axis_sum(dir)),
);
}
ckd
};

child.flex_basis = 'flex_basis: {
// A. If the item has a definite used flex basis, that’s the flex base size.

Expand Down Expand Up @@ -679,7 +692,6 @@ fn determine_flex_base_size(
// is auto and not definite, in this calculation use fit-content as the
// flex item’s cross size. The flex base size is the item’s resulting main size.

let child_parent_size = Size::NONE.with_cross(dir, cross_axis_parent_size);
let child_available_space = Size::MAX_CONTENT
.with_main(
dir,
Expand All @@ -692,21 +704,6 @@ fn determine_flex_base_size(
)
.with_cross(dir, cross_axis_available_space);

let child_known_dimensions = {
let mut ckd = child.size;
if child.align_self == AlignSelf::Stretch && ckd.cross(dir).is_none() {
ckd.set_cross(
dir,
child_available_space
.cross(dir)
.into_option()
.maybe_clamp(child_min_cross, child_max_cross)
.maybe_sub(child.margin.cross_axis_sum(dir)),
);
}
ckd
};

break 'flex_basis tree
.measure_child_size(
child.node,
Expand Down Expand Up @@ -756,12 +753,11 @@ fn determine_flex_base_size(

child.resolved_minimum_main_size = style_min_main_size.unwrap_or({
let min_content_size = {
let child_parent_size = Size::NONE.with_cross(dir, cross_axis_parent_size);
let child_available_space = Size::MIN_CONTENT.with_cross(dir, cross_axis_available_space);

tree.measure_child_size(
child.node,
Size::NONE,
child_known_dimensions,
child_parent_size,
child_available_space,
SizingMode::ContentSize,
Expand Down

0 comments on commit 120bb7a

Please sign in to comment.