From 7ff739f10a21717386436a2d94caa464030f9521 Mon Sep 17 00:00:00 2001 From: Nico Burns Date: Thu, 5 Jan 2023 01:10:25 +0000 Subject: [PATCH] Apply aspect ratio to absolute children of flex container --- ...ct_ratio_flex_column_percent_fill_width.rs | 33 +++++++++++++ ...spect_ratio_leaf_fill_min_width_percent.rs | 27 ----------- benches/generated/mod.rs | 4 +- src/compute/flexbox.rs | 10 ++-- ...ratio_flex_column_absolute_fill_width.html | 17 +++++++ ..._ratio_flex_column_percent_fill_width.html | 17 +++++++ ...ct_ratio_flex_column_percent_fill_width.rs | 48 +++++++++++++++++++ ...spect_ratio_leaf_fill_min_width_percent.rs | 42 ---------------- tests/generated/mod.rs | 2 +- 9 files changed, 125 insertions(+), 75 deletions(-) create mode 100644 benches/generated/aspect_ratio_flex_column_percent_fill_width.rs delete mode 100644 benches/generated/aspect_ratio_leaf_fill_min_width_percent.rs create mode 100644 test_fixtures/aspect_ratio_flex_column_absolute_fill_width.html create mode 100644 test_fixtures/aspect_ratio_flex_column_percent_fill_width.html create mode 100644 tests/generated/aspect_ratio_flex_column_percent_fill_width.rs delete mode 100644 tests/generated/aspect_ratio_leaf_fill_min_width_percent.rs diff --git a/benches/generated/aspect_ratio_flex_column_percent_fill_width.rs b/benches/generated/aspect_ratio_flex_column_percent_fill_width.rs new file mode 100644 index 000000000..3c2c0401b --- /dev/null +++ b/benches/generated/aspect_ratio_flex_column_percent_fill_width.rs @@ -0,0 +1,33 @@ +pub fn compute() { + #[allow(unused_imports)] + use taffy::prelude::*; + let mut taffy = taffy::Taffy::new(); + let node0 = taffy + .new_leaf(taffy::style::Style { + position: taffy::style::Position::Absolute, + size: taffy::geometry::Size { width: taffy::style::Dimension::Percent(0.2f32), height: auto() }, + aspect_ratio: Some(3f32), + inset: taffy::geometry::Rect { + left: taffy::style::LengthPercentageAuto::Percent(0.05f32), + right: auto(), + top: taffy::style::LengthPercentageAuto::Percent(0.05f32), + bottom: auto(), + }, + ..Default::default() + }) + .unwrap(); + let node = taffy + .new_with_children( + taffy::style::Style { + display: taffy::style::Display::Flex, + size: taffy::geometry::Size { + width: taffy::style::Dimension::Points(1280f32), + height: taffy::style::Dimension::Points(720f32), + }, + ..Default::default() + }, + &[node0], + ) + .unwrap(); + taffy.compute_layout(node, taffy::geometry::Size::MAX_CONTENT).unwrap(); +} diff --git a/benches/generated/aspect_ratio_leaf_fill_min_width_percent.rs b/benches/generated/aspect_ratio_leaf_fill_min_width_percent.rs deleted file mode 100644 index 651028550..000000000 --- a/benches/generated/aspect_ratio_leaf_fill_min_width_percent.rs +++ /dev/null @@ -1,27 +0,0 @@ -pub fn compute() { - #[allow(unused_imports)] - use taffy::prelude::*; - let mut taffy = taffy::Taffy::new(); - let node0 = taffy - .new_leaf(taffy::style::Style { - min_size: taffy::geometry::Size { width: auto(), height: taffy::style::Dimension::Percent(0.5f32) }, - aspect_ratio: Some(2f32), - ..Default::default() - }) - .unwrap(); - let node = taffy - .new_with_children( - taffy::style::Style { - display: taffy::style::Display::Grid, - align_items: Some(taffy::style::AlignItems::Start), - size: taffy::geometry::Size { - width: taffy::style::Dimension::Points(100f32), - height: taffy::style::Dimension::Points(100f32), - }, - ..Default::default() - }, - &[node0], - ) - .unwrap(); - taffy.compute_layout(node, taffy::geometry::Size::MAX_CONTENT).unwrap(); -} diff --git a/benches/generated/mod.rs b/benches/generated/mod.rs index 01b5772fa..0dba349a3 100644 --- a/benches/generated/mod.rs +++ b/benches/generated/mod.rs @@ -119,6 +119,7 @@ mod aspect_ratio_flex_column_fill_min_height; mod aspect_ratio_flex_column_fill_min_width; mod aspect_ratio_flex_column_fill_width; mod aspect_ratio_flex_column_fill_width_flex; +mod aspect_ratio_flex_column_percent_fill_width; mod aspect_ratio_flex_column_stretch_fill_height; mod aspect_ratio_flex_column_stretch_fill_max_height; mod aspect_ratio_flex_column_stretch_fill_max_width; @@ -141,7 +142,6 @@ mod aspect_ratio_leaf_fill_max_height; mod aspect_ratio_leaf_fill_max_width; mod aspect_ratio_leaf_fill_min_height; mod aspect_ratio_leaf_fill_min_width; -mod aspect_ratio_leaf_fill_min_width_percent; mod aspect_ratio_leaf_fill_width; mod aspect_ratio_leaf_overriden_by_explicit_sizes; mod aspect_ratio_leaf_overriden_by_explicit_sizes_flex; @@ -666,6 +666,7 @@ fn benchmark(c: &mut Criterion) { aspect_ratio_flex_column_fill_min_width::compute(); aspect_ratio_flex_column_fill_width::compute(); aspect_ratio_flex_column_fill_width_flex::compute(); + aspect_ratio_flex_column_percent_fill_width::compute(); aspect_ratio_flex_column_stretch_fill_height::compute(); aspect_ratio_flex_column_stretch_fill_max_height::compute(); aspect_ratio_flex_column_stretch_fill_max_width::compute(); @@ -688,7 +689,6 @@ fn benchmark(c: &mut Criterion) { aspect_ratio_leaf_fill_max_width::compute(); aspect_ratio_leaf_fill_min_height::compute(); aspect_ratio_leaf_fill_min_width::compute(); - aspect_ratio_leaf_fill_min_width_percent::compute(); aspect_ratio_leaf_fill_width::compute(); aspect_ratio_leaf_overriden_by_explicit_sizes::compute(); aspect_ratio_leaf_overriden_by_explicit_sizes_flex::compute(); diff --git a/src/compute/flexbox.rs b/src/compute/flexbox.rs index 85a80a909..37a303cb1 100644 --- a/src/compute/flexbox.rs +++ b/src/compute/flexbox.rs @@ -1622,9 +1622,10 @@ fn perform_absolute_layout_on_absolute_children(tree: &mut impl LayoutTree, node let (start_cross, end_cross) = if constants.is_row { (top, bottom) } else { (start, end) }; // Compute known dimensions from min/max/inherent size styles - let style_size = child_style.size.maybe_resolve(constants.container_size); - let min_size = child_style.min_size.maybe_resolve(constants.container_size); - let max_size = child_style.max_size.maybe_resolve(constants.container_size); + let aspect_ratio = child_style.aspect_ratio; + let style_size = child_style.size.maybe_resolve(constants.container_size).maybe_apply_aspect_ratio(aspect_ratio); + let min_size = child_style.min_size.maybe_resolve(constants.container_size).maybe_apply_aspect_ratio(aspect_ratio); + let max_size = child_style.max_size.maybe_resolve(constants.container_size).maybe_apply_aspect_ratio(aspect_ratio); let mut known_dimensions = style_size.maybe_clamp(min_size, max_size); // Fill in width from left/right and height from top/bottom is appropriate @@ -1635,6 +1636,9 @@ fn perform_absolute_layout_on_absolute_children(tree: &mut impl LayoutTree, node known_dimensions.height = Some(container_height.maybe_sub(top).maybe_sub(bottom)); } + // Re-apply aspect ratio + let known_dimensions = known_dimensions.maybe_apply_aspect_ratio(aspect_ratio); + let preliminary_size = compute_node_layout( tree, child, diff --git a/test_fixtures/aspect_ratio_flex_column_absolute_fill_width.html b/test_fixtures/aspect_ratio_flex_column_absolute_fill_width.html new file mode 100644 index 000000000..f65dfaad7 --- /dev/null +++ b/test_fixtures/aspect_ratio_flex_column_absolute_fill_width.html @@ -0,0 +1,17 @@ + + + + + + + Test description + + + + +
+
+
+ + + \ No newline at end of file diff --git a/test_fixtures/aspect_ratio_flex_column_percent_fill_width.html b/test_fixtures/aspect_ratio_flex_column_percent_fill_width.html new file mode 100644 index 000000000..f65dfaad7 --- /dev/null +++ b/test_fixtures/aspect_ratio_flex_column_percent_fill_width.html @@ -0,0 +1,17 @@ + + + + + + + Test description + + + + +
+
+
+ + + \ No newline at end of file diff --git a/tests/generated/aspect_ratio_flex_column_percent_fill_width.rs b/tests/generated/aspect_ratio_flex_column_percent_fill_width.rs new file mode 100644 index 000000000..01c0515ea --- /dev/null +++ b/tests/generated/aspect_ratio_flex_column_percent_fill_width.rs @@ -0,0 +1,48 @@ +#[test] +fn aspect_ratio_flex_column_percent_fill_width() { + use slotmap::Key; + #[allow(unused_imports)] + use taffy::{layout::Layout, prelude::*}; + let mut taffy = taffy::Taffy::new(); + let node0 = taffy + .new_leaf(taffy::style::Style { + position: taffy::style::Position::Absolute, + size: taffy::geometry::Size { width: taffy::style::Dimension::Percent(0.2f32), height: auto() }, + aspect_ratio: Some(3f32), + inset: taffy::geometry::Rect { + left: taffy::style::LengthPercentageAuto::Percent(0.05f32), + right: auto(), + top: taffy::style::LengthPercentageAuto::Percent(0.05f32), + bottom: auto(), + }, + ..Default::default() + }) + .unwrap(); + let node = taffy + .new_with_children( + taffy::style::Style { + display: taffy::style::Display::Flex, + size: taffy::geometry::Size { + width: taffy::style::Dimension::Points(1280f32), + height: taffy::style::Dimension::Points(720f32), + }, + ..Default::default() + }, + &[node0], + ) + .unwrap(); + taffy.compute_layout(node, taffy::geometry::Size::MAX_CONTENT).unwrap(); + println!("\nComputed tree:"); + taffy::debug::print_tree(&taffy, node); + println!(); + let Layout { size, location, .. } = taffy.layout(node).unwrap(); + assert_eq!(size.width, 1280f32, "width of node {:?}. Expected {}. Actual {}", node.data(), 1280f32, size.width); + assert_eq!(size.height, 720f32, "height of node {:?}. Expected {}. Actual {}", node.data(), 720f32, size.height); + assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node.data(), 0f32, location.x); + assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node.data(), 0f32, location.y); + let Layout { size, location, .. } = taffy.layout(node0).unwrap(); + assert_eq!(size.width, 256f32, "width of node {:?}. Expected {}. Actual {}", node0.data(), 256f32, size.width); + assert_eq!(size.height, 85f32, "height of node {:?}. Expected {}. Actual {}", node0.data(), 85f32, size.height); + assert_eq!(location.x, 64f32, "x of node {:?}. Expected {}. Actual {}", node0.data(), 64f32, location.x); + assert_eq!(location.y, 36f32, "y of node {:?}. Expected {}. Actual {}", node0.data(), 36f32, location.y); +} diff --git a/tests/generated/aspect_ratio_leaf_fill_min_width_percent.rs b/tests/generated/aspect_ratio_leaf_fill_min_width_percent.rs deleted file mode 100644 index ddddeee6f..000000000 --- a/tests/generated/aspect_ratio_leaf_fill_min_width_percent.rs +++ /dev/null @@ -1,42 +0,0 @@ -#[test] -fn aspect_ratio_leaf_fill_min_width_percent() { - use slotmap::Key; - #[allow(unused_imports)] - use taffy::{layout::Layout, prelude::*}; - let mut taffy = taffy::Taffy::new(); - let node0 = taffy - .new_leaf(taffy::style::Style { - min_size: taffy::geometry::Size { width: auto(), height: taffy::style::Dimension::Percent(0.5f32) }, - aspect_ratio: Some(2f32), - ..Default::default() - }) - .unwrap(); - let node = taffy - .new_with_children( - taffy::style::Style { - display: taffy::style::Display::Grid, - align_items: Some(taffy::style::AlignItems::Start), - size: taffy::geometry::Size { - width: taffy::style::Dimension::Points(100f32), - height: taffy::style::Dimension::Points(100f32), - }, - ..Default::default() - }, - &[node0], - ) - .unwrap(); - taffy.compute_layout(node, taffy::geometry::Size::MAX_CONTENT).unwrap(); - println!("\nComputed tree:"); - taffy::debug::print_tree(&taffy, node); - println!(); - let Layout { size, location, .. } = taffy.layout(node).unwrap(); - assert_eq!(size.width, 100f32, "width of node {:?}. Expected {}. Actual {}", node.data(), 100f32, size.width); - assert_eq!(size.height, 100f32, "height of node {:?}. Expected {}. Actual {}", node.data(), 100f32, size.height); - assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node.data(), 0f32, location.x); - assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node.data(), 0f32, location.y); - let Layout { size, location, .. } = taffy.layout(node0).unwrap(); - assert_eq!(size.width, 100f32, "width of node {:?}. Expected {}. Actual {}", node0.data(), 100f32, size.width); - assert_eq!(size.height, 50f32, "height of node {:?}. Expected {}. Actual {}", node0.data(), 50f32, size.height); - assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node0.data(), 0f32, location.x); - assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node0.data(), 0f32, location.y); -} diff --git a/tests/generated/mod.rs b/tests/generated/mod.rs index a65e1813e..6d0eefc50 100644 --- a/tests/generated/mod.rs +++ b/tests/generated/mod.rs @@ -118,6 +118,7 @@ mod aspect_ratio_flex_column_fill_min_height; mod aspect_ratio_flex_column_fill_min_width; mod aspect_ratio_flex_column_fill_width; mod aspect_ratio_flex_column_fill_width_flex; +mod aspect_ratio_flex_column_percent_fill_width; mod aspect_ratio_flex_column_stretch_fill_height; mod aspect_ratio_flex_column_stretch_fill_max_height; mod aspect_ratio_flex_column_stretch_fill_max_width; @@ -140,7 +141,6 @@ mod aspect_ratio_leaf_fill_max_height; mod aspect_ratio_leaf_fill_max_width; mod aspect_ratio_leaf_fill_min_height; mod aspect_ratio_leaf_fill_min_width; -mod aspect_ratio_leaf_fill_min_width_percent; mod aspect_ratio_leaf_fill_width; mod aspect_ratio_leaf_overriden_by_explicit_sizes; mod aspect_ratio_leaf_overriden_by_explicit_sizes_flex;