tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

Rust 583HTML 275Markdown 8TOML 41586 Definitions

Ignore align_content in non-wrapping flexbox containers (#383)

* Add tests for bevyengine/bevy#7976

* Simplify code in compute_constants

* Ignore align_contents if flex_wrap is set to no_wrap

* Remove commented out code

0dc80beaa71f614837d7b77a5b2e4759354f3559 · by Nico Burns · (3 years ago) · parent cab6b0bf6 · browse the tree at this commit

Semantic delta · 8

src/compute/flexbox.rs

tests/generated/bevy_issue_7976_3_level.rs

tests/generated/bevy_issue_7976_4_level.rs

tests/generated/bevy_issue_7976_reduced.rs

tests/generated/mod.rs

8 files · +269 −8

diff --git a/src/compute/flexbox.rs b/src/compute/flexbox.rs
index 10f136520..d5888c13d 100644
--- a/src/compute/flexbox.rs
+++ b/src/compute/flexbox.rs
@@ -456,10 +456,7 @@ fn compute_constants(
     let padding_border = padding + border;
 
     let node_outer_size = known_dimensions;
-    let node_inner_size = Size {
-        width: node_outer_size.width.maybe_sub(padding_border.horizontal_axis_sum()),
-        height: node_outer_size.height.maybe_sub(padding_border.vertical_axis_sum()),
-    };
+    let node_inner_size = node_outer_size.maybe_sub(padding_border.sum_axes());
     let gap = style.gap.resolve_or_zero(node_inner_size.or(Size::zero()));
 
     let container_size = Size::zero();
@@ -1307,12 +1304,14 @@ fn calculate_cross_size(
 ) {
     // Note: AlignContent::SpaceEvenly and AlignContent::SpaceAround behave like AlignContent::Stretch when there is only
     // a single flex line in the container. See: https://www.w3.org/TR/css-flexbox-1/#align-content-property
+    // Also: align_content is ignored entirely (and thus behaves like Stretch) when `flex_wrap` is set to `nowrap`.
     if flex_lines.len() == 1
         && node_size.cross(constants.dir).is_some()
-        && matches!(
-            constants.align_content,
-            AlignContent::Stretch | AlignContent::SpaceEvenly | AlignContent::SpaceAround
-        )
+        && (!constants.is_wrap
+            || matches!(
+                constants.align_content,
+                AlignContent::Stretch | AlignContent::SpaceEvenly | AlignContent::SpaceAround
+            ))
     {
         flex_lines[0].cross_size =
             (node_size.cross(constants.dir).maybe_sub(constants.padding_border.cross_axis_sum(constants.dir)))
diff --git a/test_fixtures/bevy_issue_7976_3_level.html b/test_fixtures/bevy_issue_7976_3_level.html
new file mode 100644
index 000000000..50f3617b4
--- /dev/null
+++ b/test_fixtures/bevy_issue_7976_3_level.html
@@ -0,0 +1,19 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+  <script src="../scripts/gentest/test_helper.js"></script>
+  <link rel="stylesheet" type="text/css" href="../scripts/gentest/test_base_style.css">
+  <title>
+    Test description
+  </title>
+<head/>
+<body>
+
+<div id="test-root" style="width: 200px; height: 200px; align-content: start">
+  <div style="min-width: 40px;min-height:40px;margin:5px;padding:5px;background-color:red">
+    <div style="width:100%;height:100%;padding:5px;background-color:green"></div>
+  </div>
+</div>
+
+</body>
+</html>
\ No newline at end of file
diff --git a/test_fixtures/bevy_issue_7976_4_level.html b/test_fixtures/bevy_issue_7976_4_level.html
new file mode 100644
index 000000000..80497279b
--- /dev/null
+++ b/test_fixtures/bevy_issue_7976_4_level.html
@@ -0,0 +1,21 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+  <script src="../scripts/gentest/test_helper.js"></script>
+  <link rel="stylesheet" type="text/css" href="../scripts/gentest/test_base_style.css">
+  <title>
+    Test description
+  </title>
+<head/>
+<body>
+
+<div id="test-root" style="width: 200px; height: 200px; align-content: start">
+  <div style="min-width: 40px;min-height:40px;margin:5px;padding:5px;background-color:red">
+    <div style="width:100%;height:100%;padding:5px;background-color:green">
+      <div></div>
+    </div>
+  </div>
+</div>
+
+</body>
+</html>
\ No newline at end of file
diff --git a/test_fixtures/bevy_issue_7976_reduced.html b/test_fixtures/bevy_issue_7976_reduced.html
new file mode 100644
index 000000000..85d9c37ac
--- /dev/null
+++ b/test_fixtures/bevy_issue_7976_reduced.html
@@ -0,0 +1,17 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+  <script src="../scripts/gentest/test_helper.js"></script>
+  <link rel="stylesheet" type="text/css" href="../scripts/gentest/test_base_style.css">
+  <title>
+    Test description
+  </title>
+<head/>
+<body>
+
+<div id="test-root" style="height:200px;align-content: start">
+    <div style="width:40px;"></div>
+</div>
+
+</body>
+</html>
\ No newline at end of file
diff --git a/tests/generated/bevy_issue_7976_3_level.rs b/tests/generated/bevy_issue_7976_3_level.rs
new file mode 100644
index 000000000..ef9771efe
--- /dev/null
+++ b/tests/generated/bevy_issue_7976_3_level.rs
@@ -0,0 +1,78 @@
+#[test]
+fn bevy_issue_7976_3_level() {
+    use slotmap::Key;
+    #[allow(unused_imports)]
+    use taffy::{layout::Layout, prelude::*};
+    let mut taffy = taffy::Taffy::new();
+    let node00 = taffy
+        .new_leaf(taffy::style::Style {
+            size: taffy::geometry::Size {
+                width: taffy::style::Dimension::Percent(1f32),
+                height: taffy::style::Dimension::Percent(1f32),
+            },
+            padding: taffy::geometry::Rect {
+                left: taffy::style::LengthPercentage::Points(5f32),
+                right: taffy::style::LengthPercentage::Points(5f32),
+                top: taffy::style::LengthPercentage::Points(5f32),
+                bottom: taffy::style::LengthPercentage::Points(5f32),
+            },
+            ..Default::default()
+        })
+        .unwrap();
+    let node0 = taffy
+        .new_with_children(
+            taffy::style::Style {
+                min_size: taffy::geometry::Size {
+                    width: taffy::style::Dimension::Points(40f32),
+                    height: taffy::style::Dimension::Points(40f32),
+                },
+                margin: taffy::geometry::Rect {
+                    left: taffy::style::LengthPercentageAuto::Points(5f32),
+                    right: taffy::style::LengthPercentageAuto::Points(5f32),
+                    top: taffy::style::LengthPercentageAuto::Points(5f32),
+                    bottom: taffy::style::LengthPercentageAuto::Points(5f32),
+                },
+                padding: taffy::geometry::Rect {
+                    left: taffy::style::LengthPercentage::Points(5f32),
+                    right: taffy::style::LengthPercentage::Points(5f32),
+                    top: taffy::style::LengthPercentage::Points(5f32),
+                    bottom: taffy::style::LengthPercentage::Points(5f32),
+                },
+                ..Default::default()
+            },
+            &[node00],
+        )
+        .unwrap();
+    let node = taffy
+        .new_with_children(
+            taffy::style::Style {
+                align_content: Some(taffy::style::AlignContent::Start),
+                size: taffy::geometry::Size {
+                    width: taffy::style::Dimension::Points(200f32),
+                    height: taffy::style::Dimension::Points(200f32),
+                },
+                ..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, 200f32, "width of node {:?}. Expected {}. Actual {}", node.data(), 200f32, size.width);
+    assert_eq!(size.height, 200f32, "height of node {:?}. Expected {}. Actual {}", node.data(), 200f32, 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, 40f32, "width of node {:?}. Expected {}. Actual {}", node0.data(), 40f32, size.width);
+    assert_eq!(size.height, 190f32, "height of node {:?}. Expected {}. Actual {}", node0.data(), 190f32, size.height);
+    assert_eq!(location.x, 5f32, "x of node {:?}. Expected {}. Actual {}", node0.data(), 5f32, location.x);
+    assert_eq!(location.y, 5f32, "y of node {:?}. Expected {}. Actual {}", node0.data(), 5f32, location.y);
+    let Layout { size, location, .. } = taffy.layout(node00).unwrap();
+    assert_eq!(size.width, 30f32, "width of node {:?}. Expected {}. Actual {}", node00.data(), 30f32, size.width);
+    assert_eq!(size.height, 180f32, "height of node {:?}. Expected {}. Actual {}", node00.data(), 180f32, size.height);
+    assert_eq!(location.x, 5f32, "x of node {:?}. Expected {}. Actual {}", node00.data(), 5f32, location.x);
+    assert_eq!(location.y, 5f32, "y of node {:?}. Expected {}. Actual {}", node00.data(), 5f32, location.y);
+}
diff --git a/tests/generated/bevy_issue_7976_4_level.rs b/tests/generated/bevy_issue_7976_4_level.rs
new file mode 100644
index 000000000..c6f2a03c4
--- /dev/null
+++ b/tests/generated/bevy_issue_7976_4_level.rs
@@ -0,0 +1,87 @@
+#[test]
+fn bevy_issue_7976_4_level() {
+    use slotmap::Key;
+    #[allow(unused_imports)]
+    use taffy::{layout::Layout, prelude::*};
+    let mut taffy = taffy::Taffy::new();
+    let node000 = taffy.new_leaf(taffy::style::Style { ..Default::default() }).unwrap();
+    let node00 = taffy
+        .new_with_children(
+            taffy::style::Style {
+                size: taffy::geometry::Size {
+                    width: taffy::style::Dimension::Percent(1f32),
+                    height: taffy::style::Dimension::Percent(1f32),
+                },
+                padding: taffy::geometry::Rect {
+                    left: taffy::style::LengthPercentage::Points(5f32),
+                    right: taffy::style::LengthPercentage::Points(5f32),
+                    top: taffy::style::LengthPercentage::Points(5f32),
+                    bottom: taffy::style::LengthPercentage::Points(5f32),
+                },
+                ..Default::default()
+            },
+            &[node000],
+        )
+        .unwrap();
+    let node0 = taffy
+        .new_with_children(
+            taffy::style::Style {
+                min_size: taffy::geometry::Size {
+                    width: taffy::style::Dimension::Points(40f32),
+                    height: taffy::style::Dimension::Points(40f32),
+                },
+                margin: taffy::geometry::Rect {
+                    left: taffy::style::LengthPercentageAuto::Points(5f32),
+                    right: taffy::style::LengthPercentageAuto::Points(5f32),
+                    top: taffy::style::LengthPercentageAuto::Points(5f32),
+                    bottom: taffy::style::LengthPercentageAuto::Points(5f32),
+                },
+                padding: taffy::geometry::Rect {
+                    left: taffy::style::LengthPercentage::Points(5f32),
+                    right: taffy::style::LengthPercentage::Points(5f32),
+                    top: taffy::style::LengthPercentage::Points(5f32),
+                    bottom: taffy::style::LengthPercentage::Points(5f32),
+                },
+                ..Default::default()
+            },
+            &[node00],
+        )
+        .unwrap();
+    let node = taffy
+        .new_with_children(
+            taffy::style::Style {
+                align_content: Some(taffy::style::AlignContent::Start),
+                size: taffy::geometry::Size {
+                    width: taffy::style::Dimension::Points(200f32),
+                    height: taffy::style::Dimension::Points(200f32),
+                },
+                ..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, 200f32, "width of node {:?}. Expected {}. Actual {}", node.data(), 200f32, size.width);
+    assert_eq!(size.height, 200f32, "height of node {:?}. Expected {}. Actual {}", node.data(), 200f32, 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, 40f32, "width of node {:?}. Expected {}. Actual {}", node0.data(), 40f32, size.width);
+    assert_eq!(size.height, 190f32, "height of node {:?}. Expected {}. Actual {}", node0.data(), 190f32, size.height);
+    assert_eq!(location.x, 5f32, "x of node {:?}. Expected {}. Actual {}", node0.data(), 5f32, location.x);
+    assert_eq!(location.y, 5f32, "y of node {:?}. Expected {}. Actual {}", node0.data(), 5f32, location.y);
+    let Layout { size, location, .. } = taffy.layout(node00).unwrap();
+    assert_eq!(size.width, 30f32, "width of node {:?}. Expected {}. Actual {}", node00.data(), 30f32, size.width);
+    assert_eq!(size.height, 180f32, "height of node {:?}. Expected {}. Actual {}", node00.data(), 180f32, size.height);
+    assert_eq!(location.x, 5f32, "x of node {:?}. Expected {}. Actual {}", node00.data(), 5f32, location.x);
+    assert_eq!(location.y, 5f32, "y of node {:?}. Expected {}. Actual {}", node00.data(), 5f32, location.y);
+    let Layout { size, location, .. } = taffy.layout(node000).unwrap();
+    assert_eq!(size.width, 0f32, "width of node {:?}. Expected {}. Actual {}", node000.data(), 0f32, size.width);
+    assert_eq!(size.height, 170f32, "height of node {:?}. Expected {}. Actual {}", node000.data(), 170f32, size.height);
+    assert_eq!(location.x, 5f32, "x of node {:?}. Expected {}. Actual {}", node000.data(), 5f32, location.x);
+    assert_eq!(location.y, 5f32, "y of node {:?}. Expected {}. Actual {}", node000.data(), 5f32, location.y);
+}
diff --git a/tests/generated/bevy_issue_7976_reduced.rs b/tests/generated/bevy_issue_7976_reduced.rs
new file mode 100644
index 000000000..10ea0ddcd
--- /dev/null
+++ b/tests/generated/bevy_issue_7976_reduced.rs
@@ -0,0 +1,37 @@
+#[test]
+fn bevy_issue_7976_reduced() {
+    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 {
+            size: taffy::geometry::Size { width: taffy::style::Dimension::Points(40f32), height: auto() },
+            ..Default::default()
+        })
+        .unwrap();
+    let node = taffy
+        .new_with_children(
+            taffy::style::Style {
+                align_content: Some(taffy::style::AlignContent::Start),
+                size: taffy::geometry::Size { width: auto(), height: taffy::style::Dimension::Points(200f32) },
+                ..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, 40f32, "width of node {:?}. Expected {}. Actual {}", node.data(), 40f32, size.width);
+    assert_eq!(size.height, 200f32, "height of node {:?}. Expected {}. Actual {}", node.data(), 200f32, 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, 40f32, "width of node {:?}. Expected {}. Actual {}", node0.data(), 40f32, size.width);
+    assert_eq!(size.height, 200f32, "height of node {:?}. Expected {}. Actual {}", node0.data(), 200f32, 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 dfe95c1e0..8a7799d86 100644
--- a/tests/generated/mod.rs
+++ b/tests/generated/mod.rs
@@ -196,6 +196,9 @@ mod aspect_ratio_flex_row_stretch_fill_height;
 mod aspect_ratio_flex_row_stretch_fill_max_height;
 mod aspect_ratio_flex_row_stretch_fill_max_width;
 mod aspect_ratio_flex_row_stretch_fill_width;
+mod bevy_issue_7976_3_level;
+mod bevy_issue_7976_4_level;
+mod bevy_issue_7976_reduced;
 mod border_center_child;
 mod border_container_match_child;
 mod border_flex_child;