gioser

tawasuyu/taffy

A high performance rust-powered UI layout library

HTML 5544Rust 88Markdown 11TOML 91926 Definiciones

Reserve space for padding/borders/scrollbars in lead nodes with measure functions (#497)

568f643327ec6b4879df0087def7f4e66b6acafb · por Nico Burns · (hace 3 años) · padre 9a29acdb5 · ver el árbol en este commit

Delta semántico · 16

src/compute/leaf.rs

  • cambió por dentro fn compute pub

tests/generated/leaf_overflow_scrollbars_take_up_space_both_axis.rs

tests/generated/leaf_overflow_scrollbars_take_up_space_x_axis.rs

tests/generated/leaf_overflow_scrollbars_take_up_space_y_axis.rs

tests/generated/leaf_with_content_and_border.rs

tests/generated/leaf_with_content_and_padding.rs

tests/generated/leaf_with_content_and_padding_border.rs

14 archivos · +239 −25

diff --git a/src/compute/leaf.rs b/src/compute/leaf.rs
index 91ad06a40..8041e0cf8 100644
--- a/src/compute/leaf.rs
+++ b/src/compute/leaf.rs
@@ -142,7 +142,8 @@ pub fn compute(
 
         // Measure node
         let measured_size = measurable.measure(known_dimensions, available_space);
-        let clamped_size = node_size.unwrap_or(measured_size).maybe_clamp(node_min_size, node_max_size);
+        let clamped_size =
+            node_size.unwrap_or(measured_size + content_box_inset.sum_axes()).maybe_clamp(node_min_size, node_max_size);
         let size = Size {
             width: clamped_size.width,
             height: f32_max(clamped_size.height, aspect_ratio.map(|ratio| clamped_size.width / ratio).unwrap_or(0.0)),
diff --git a/test_fixtures/leaf_overflow_scrollbars_take_up_space_both_axis.html b/test_fixtures/leaf_overflow_scrollbars_take_up_space_both_axis.html
index 0cce3ee7d..753ca274d 100644
--- a/test_fixtures/leaf_overflow_scrollbars_take_up_space_both_axis.html
+++ b/test_fixtures/leaf_overflow_scrollbars_take_up_space_both_axis.html
@@ -9,7 +9,7 @@
 <head/>
 <body>
 
-<div id="test-root" style="overflow: scroll;"></div>
+<div id="test-root" style="overflow: scroll;">HH</div>
 
 </body>
 </html>
\ No newline at end of file
diff --git a/test_fixtures/leaf_overflow_scrollbars_take_up_space_x_axis.html b/test_fixtures/leaf_overflow_scrollbars_take_up_space_x_axis.html
index 20f73b929..bb0ea3ce2 100644
--- a/test_fixtures/leaf_overflow_scrollbars_take_up_space_x_axis.html
+++ b/test_fixtures/leaf_overflow_scrollbars_take_up_space_x_axis.html
@@ -9,7 +9,7 @@
 <head/>
 <body>
 
-<div id="test-root" style="overflow-x: scroll;"></div>
+<div id="test-root" style="overflow-x: scroll;">HH</div>
 
 </body>
 </html>
\ No newline at end of file
diff --git a/test_fixtures/leaf_overflow_scrollbars_take_up_space_y_axis.html b/test_fixtures/leaf_overflow_scrollbars_take_up_space_y_axis.html
index 9764af536..1af7085c2 100644
--- a/test_fixtures/leaf_overflow_scrollbars_take_up_space_y_axis.html
+++ b/test_fixtures/leaf_overflow_scrollbars_take_up_space_y_axis.html
@@ -9,7 +9,7 @@
 <head/>
 <body>
 
-<div id="test-root" style="overflow-y: scroll;"></div>
+<div id="test-root" style="overflow-y: scroll;">HH</div>
 
 </body>
 </html>
\ No newline at end of file
diff --git a/test_fixtures/leaf_with_content_and_border.html b/test_fixtures/leaf_with_content_and_border.html
new file mode 100644
index 000000000..e4dd38f42
--- /dev/null
+++ b/test_fixtures/leaf_with_content_and_border.html
@@ -0,0 +1,15 @@
+<!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="border-width: 2px 4px 6px 8px;">HHHH</div>
+
+</body>
+</html>
\ No newline at end of file
diff --git a/test_fixtures/leaf_with_content_and_padding.html b/test_fixtures/leaf_with_content_and_padding.html
new file mode 100644
index 000000000..29efd1cf2
--- /dev/null
+++ b/test_fixtures/leaf_with_content_and_padding.html
@@ -0,0 +1,15 @@
+<!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="padding: 2px 4px 6px 8px;">HHHH</div>
+
+</body>
+</html>
\ No newline at end of file
diff --git a/test_fixtures/leaf_with_content_and_padding_border.html b/test_fixtures/leaf_with_content_and_padding_border.html
new file mode 100644
index 000000000..323568fe6
--- /dev/null
+++ b/test_fixtures/leaf_with_content_and_padding_border.html
@@ -0,0 +1,15 @@
+<!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="padding: 2px 4px 6px 8px; border-width: 1px 3px 5px 7px;">HHHH</div>
+
+</body>
+</html>
\ No newline at end of file
diff --git a/tests/generated/leaf_overflow_scrollbars_take_up_space_both_axis.rs b/tests/generated/leaf_overflow_scrollbars_take_up_space_both_axis.rs
index 8b581b9c7..fba8b3074 100644
--- a/tests/generated/leaf_overflow_scrollbars_take_up_space_both_axis.rs
+++ b/tests/generated/leaf_overflow_scrollbars_take_up_space_both_axis.rs
@@ -4,19 +4,34 @@ fn leaf_overflow_scrollbars_take_up_space_both_axis() {
     use taffy::{prelude::*, tree::Layout};
     let mut taffy = taffy::Taffy::new();
     let node = taffy
-        .new_leaf(taffy::style::Style {
-            overflow: taffy::geometry::Point { x: taffy::style::Overflow::Scroll, y: taffy::style::Overflow::Scroll },
-            scrollbar_width: 15f32,
-            ..Default::default()
-        })
+        .new_leaf_with_measure(
+            taffy::style::Style {
+                overflow: taffy::geometry::Point {
+                    x: taffy::style::Overflow::Scroll,
+                    y: taffy::style::Overflow::Scroll,
+                },
+                scrollbar_width: 15f32,
+                ..Default::default()
+            },
+            taffy::tree::MeasureFunc::Raw(|known_dimensions, available_space| {
+                const TEXT: &str = "HH";
+                super::measure_standard_text(
+                    known_dimensions,
+                    available_space,
+                    TEXT,
+                    super::WritingMode::Horizontal,
+                    None,
+                )
+            }),
+        )
         .unwrap();
     taffy.compute_layout(node, taffy::geometry::Size::MAX_CONTENT).unwrap();
     println!("\nComputed tree:");
     taffy::util::print_tree(&taffy, node);
     println!();
     let Layout { size, location, .. } = taffy.layout(node).unwrap();
-    assert_eq!(size.width, 15f32, "width of node {:?}. Expected {}. Actual {}", node, 15f32, size.width);
-    assert_eq!(size.height, 15f32, "height of node {:?}. Expected {}. Actual {}", node, 15f32, size.height);
+    assert_eq!(size.width, 35f32, "width of node {:?}. Expected {}. Actual {}", node, 35f32, size.width);
+    assert_eq!(size.height, 25f32, "height of node {:?}. Expected {}. Actual {}", node, 25f32, size.height);
     assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node, 0f32, location.x);
     assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node, 0f32, location.y);
 }
diff --git a/tests/generated/leaf_overflow_scrollbars_take_up_space_x_axis.rs b/tests/generated/leaf_overflow_scrollbars_take_up_space_x_axis.rs
index 9116c4fd3..e5a563365 100644
--- a/tests/generated/leaf_overflow_scrollbars_take_up_space_x_axis.rs
+++ b/tests/generated/leaf_overflow_scrollbars_take_up_space_x_axis.rs
@@ -4,19 +4,34 @@ fn leaf_overflow_scrollbars_take_up_space_x_axis() {
     use taffy::{prelude::*, tree::Layout};
     let mut taffy = taffy::Taffy::new();
     let node = taffy
-        .new_leaf(taffy::style::Style {
-            overflow: taffy::geometry::Point { x: taffy::style::Overflow::Scroll, y: taffy::style::Overflow::Visible },
-            scrollbar_width: 15f32,
-            ..Default::default()
-        })
+        .new_leaf_with_measure(
+            taffy::style::Style {
+                overflow: taffy::geometry::Point {
+                    x: taffy::style::Overflow::Scroll,
+                    y: taffy::style::Overflow::Visible,
+                },
+                scrollbar_width: 15f32,
+                ..Default::default()
+            },
+            taffy::tree::MeasureFunc::Raw(|known_dimensions, available_space| {
+                const TEXT: &str = "HH";
+                super::measure_standard_text(
+                    known_dimensions,
+                    available_space,
+                    TEXT,
+                    super::WritingMode::Horizontal,
+                    None,
+                )
+            }),
+        )
         .unwrap();
     taffy.compute_layout(node, taffy::geometry::Size::MAX_CONTENT).unwrap();
     println!("\nComputed tree:");
     taffy::util::print_tree(&taffy, node);
     println!();
     let Layout { size, location, .. } = taffy.layout(node).unwrap();
-    assert_eq!(size.width, 0f32, "width of node {:?}. Expected {}. Actual {}", node, 0f32, size.width);
-    assert_eq!(size.height, 15f32, "height of node {:?}. Expected {}. Actual {}", node, 15f32, size.height);
+    assert_eq!(size.width, 20f32, "width of node {:?}. Expected {}. Actual {}", node, 20f32, size.width);
+    assert_eq!(size.height, 25f32, "height of node {:?}. Expected {}. Actual {}", node, 25f32, size.height);
     assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node, 0f32, location.x);
     assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node, 0f32, location.y);
 }
diff --git a/tests/generated/leaf_overflow_scrollbars_take_up_space_y_axis.rs b/tests/generated/leaf_overflow_scrollbars_take_up_space_y_axis.rs
index c4af93575..d7f00d9cc 100644
--- a/tests/generated/leaf_overflow_scrollbars_take_up_space_y_axis.rs
+++ b/tests/generated/leaf_overflow_scrollbars_take_up_space_y_axis.rs
@@ -4,19 +4,34 @@ fn leaf_overflow_scrollbars_take_up_space_y_axis() {
     use taffy::{prelude::*, tree::Layout};
     let mut taffy = taffy::Taffy::new();
     let node = taffy
-        .new_leaf(taffy::style::Style {
-            overflow: taffy::geometry::Point { x: taffy::style::Overflow::Visible, y: taffy::style::Overflow::Scroll },
-            scrollbar_width: 15f32,
-            ..Default::default()
-        })
+        .new_leaf_with_measure(
+            taffy::style::Style {
+                overflow: taffy::geometry::Point {
+                    x: taffy::style::Overflow::Visible,
+                    y: taffy::style::Overflow::Scroll,
+                },
+                scrollbar_width: 15f32,
+                ..Default::default()
+            },
+            taffy::tree::MeasureFunc::Raw(|known_dimensions, available_space| {
+                const TEXT: &str = "HH";
+                super::measure_standard_text(
+                    known_dimensions,
+                    available_space,
+                    TEXT,
+                    super::WritingMode::Horizontal,
+                    None,
+                )
+            }),
+        )
         .unwrap();
     taffy.compute_layout(node, taffy::geometry::Size::MAX_CONTENT).unwrap();
     println!("\nComputed tree:");
     taffy::util::print_tree(&taffy, node);
     println!();
     let Layout { size, location, .. } = taffy.layout(node).unwrap();
-    assert_eq!(size.width, 15f32, "width of node {:?}. Expected {}. Actual {}", node, 15f32, size.width);
-    assert_eq!(size.height, 0f32, "height of node {:?}. Expected {}. Actual {}", node, 0f32, size.height);
+    assert_eq!(size.width, 35f32, "width of node {:?}. Expected {}. Actual {}", node, 35f32, size.width);
+    assert_eq!(size.height, 10f32, "height of node {:?}. Expected {}. Actual {}", node, 10f32, size.height);
     assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node, 0f32, location.x);
     assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node, 0f32, location.y);
 }
diff --git a/tests/generated/leaf_with_content_and_border.rs b/tests/generated/leaf_with_content_and_border.rs
new file mode 100644
index 000000000..87a55e27c
--- /dev/null
+++ b/tests/generated/leaf_with_content_and_border.rs
@@ -0,0 +1,38 @@
+#[test]
+fn leaf_with_content_and_border() {
+    #[allow(unused_imports)]
+    use taffy::{prelude::*, tree::Layout};
+    let mut taffy = taffy::Taffy::new();
+    let node = taffy
+        .new_leaf_with_measure(
+            taffy::style::Style {
+                border: taffy::geometry::Rect {
+                    left: taffy::style::LengthPercentage::Length(8f32),
+                    right: taffy::style::LengthPercentage::Length(4f32),
+                    top: taffy::style::LengthPercentage::Length(2f32),
+                    bottom: taffy::style::LengthPercentage::Length(6f32),
+                },
+                ..Default::default()
+            },
+            taffy::tree::MeasureFunc::Raw(|known_dimensions, available_space| {
+                const TEXT: &str = "HHHH";
+                super::measure_standard_text(
+                    known_dimensions,
+                    available_space,
+                    TEXT,
+                    super::WritingMode::Horizontal,
+                    None,
+                )
+            }),
+        )
+        .unwrap();
+    taffy.compute_layout(node, taffy::geometry::Size::MAX_CONTENT).unwrap();
+    println!("\nComputed tree:");
+    taffy::util::print_tree(&taffy, node);
+    println!();
+    let Layout { size, location, .. } = taffy.layout(node).unwrap();
+    assert_eq!(size.width, 52f32, "width of node {:?}. Expected {}. Actual {}", node, 52f32, size.width);
+    assert_eq!(size.height, 18f32, "height of node {:?}. Expected {}. Actual {}", node, 18f32, size.height);
+    assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node, 0f32, location.x);
+    assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node, 0f32, location.y);
+}
diff --git a/tests/generated/leaf_with_content_and_padding.rs b/tests/generated/leaf_with_content_and_padding.rs
new file mode 100644
index 000000000..7908c32b3
--- /dev/null
+++ b/tests/generated/leaf_with_content_and_padding.rs
@@ -0,0 +1,38 @@
+#[test]
+fn leaf_with_content_and_padding() {
+    #[allow(unused_imports)]
+    use taffy::{prelude::*, tree::Layout};
+    let mut taffy = taffy::Taffy::new();
+    let node = taffy
+        .new_leaf_with_measure(
+            taffy::style::Style {
+                padding: taffy::geometry::Rect {
+                    left: taffy::style::LengthPercentage::Length(8f32),
+                    right: taffy::style::LengthPercentage::Length(4f32),
+                    top: taffy::style::LengthPercentage::Length(2f32),
+                    bottom: taffy::style::LengthPercentage::Length(6f32),
+                },
+                ..Default::default()
+            },
+            taffy::tree::MeasureFunc::Raw(|known_dimensions, available_space| {
+                const TEXT: &str = "HHHH";
+                super::measure_standard_text(
+                    known_dimensions,
+                    available_space,
+                    TEXT,
+                    super::WritingMode::Horizontal,
+                    None,
+                )
+            }),
+        )
+        .unwrap();
+    taffy.compute_layout(node, taffy::geometry::Size::MAX_CONTENT).unwrap();
+    println!("\nComputed tree:");
+    taffy::util::print_tree(&taffy, node);
+    println!();
+    let Layout { size, location, .. } = taffy.layout(node).unwrap();
+    assert_eq!(size.width, 52f32, "width of node {:?}. Expected {}. Actual {}", node, 52f32, size.width);
+    assert_eq!(size.height, 18f32, "height of node {:?}. Expected {}. Actual {}", node, 18f32, size.height);
+    assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node, 0f32, location.x);
+    assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node, 0f32, location.y);
+}
diff --git a/tests/generated/leaf_with_content_and_padding_border.rs b/tests/generated/leaf_with_content_and_padding_border.rs
new file mode 100644
index 000000000..ff28b11d3
--- /dev/null
+++ b/tests/generated/leaf_with_content_and_padding_border.rs
@@ -0,0 +1,44 @@
+#[test]
+fn leaf_with_content_and_padding_border() {
+    #[allow(unused_imports)]
+    use taffy::{prelude::*, tree::Layout};
+    let mut taffy = taffy::Taffy::new();
+    let node = taffy
+        .new_leaf_with_measure(
+            taffy::style::Style {
+                padding: taffy::geometry::Rect {
+                    left: taffy::style::LengthPercentage::Length(8f32),
+                    right: taffy::style::LengthPercentage::Length(4f32),
+                    top: taffy::style::LengthPercentage::Length(2f32),
+                    bottom: taffy::style::LengthPercentage::Length(6f32),
+                },
+                border: taffy::geometry::Rect {
+                    left: taffy::style::LengthPercentage::Length(7f32),
+                    right: taffy::style::LengthPercentage::Length(3f32),
+                    top: taffy::style::LengthPercentage::Length(1f32),
+                    bottom: taffy::style::LengthPercentage::Length(5f32),
+                },
+                ..Default::default()
+            },
+            taffy::tree::MeasureFunc::Raw(|known_dimensions, available_space| {
+                const TEXT: &str = "HHHH";
+                super::measure_standard_text(
+                    known_dimensions,
+                    available_space,
+                    TEXT,
+                    super::WritingMode::Horizontal,
+                    None,
+                )
+            }),
+        )
+        .unwrap();
+    taffy.compute_layout(node, taffy::geometry::Size::MAX_CONTENT).unwrap();
+    println!("\nComputed tree:");
+    taffy::util::print_tree(&taffy, node);
+    println!();
+    let Layout { size, location, .. } = taffy.layout(node).unwrap();
+    assert_eq!(size.width, 62f32, "width of node {:?}. Expected {}. Actual {}", node, 62f32, size.width);
+    assert_eq!(size.height, 24f32, "height of node {:?}. Expected {}. Actual {}", node, 24f32, size.height);
+    assert_eq!(location.x, 0f32, "x of node {:?}. Expected {}. Actual {}", node, 0f32, location.x);
+    assert_eq!(location.y, 0f32, "y of node {:?}. Expected {}. Actual {}", node, 0f32, location.y);
+}
diff --git a/tests/generated/mod.rs b/tests/generated/mod.rs
index d9a53a857..1b78dfd3e 100644
--- a/tests/generated/mod.rs
+++ b/tests/generated/mod.rs
@@ -1033,6 +1033,9 @@ mod leaf_overflow_scrollbars_take_up_space_y_axis;
 mod leaf_padding_border_overrides_max_size;
 mod leaf_padding_border_overrides_min_size;
 mod leaf_padding_border_overrides_size;
+mod leaf_with_content_and_border;
+mod leaf_with_content_and_padding;
+mod leaf_with_content_and_padding_border;
 mod margin_and_flex_column;
 mod margin_and_flex_row;
 mod margin_and_stretch_column;