gioser

tawasuyu/taffy

A high performance rust-powered UI layout library

Rust 501HTML 254C 19JavaScript 81414 Sutichaykuna

Min values should override max values (#262)

* Fix for #261

* Update all implementations of `maybe_clamp`

* Add entry to RELEASES.md for min/max fix #261

* Add tests for min/max overrides

* Release notes clarity

Co-authored-by: TimJentzsch <tim-jentzsch@gmx.de>

* Remove duplicate heading from RELEASES.md

* Add generated tests for min_size overriding max_size

* Add newline in RELEASES.md

Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
Co-authored-by: TimJentzsch <tim-jentzsch@gmx.de>

b952c2b38dc58e87f43dc263e17beb4174bc68e2 · ruwaq George Atkinson · (3 wata ñawpaq) · tayta e8f362338 · kay commitpi sach'ata qhaway

Semántico sayk'uy · 18

benches/generated/min_height_overrides_max_height.rs

benches/generated/min_width_overrides_max_width.rs

benches/generated/mod.rs

src/math.rs

tests/generated/min_height_overrides_max_height.rs

tests/generated/min_width_overrides_max_width.rs

tests/generated/mod.rs

tests/min_max_overrides.rs

11 qillqakuna · +216 −5

diff --git a/RELEASES.md b/RELEASES.md
index 261ad1913..34d08f9ae 100644
--- a/RELEASES.md
+++ b/RELEASES.md
@@ -3,8 +3,11 @@
 ## 0.2.1 (unreleased)
 
 ### Fixes
+
 - `taffy::compute_layout` has been made public allowing Taffy to be used with custom storage (#263)
 
+- In case of conflicts, `min_size` now overrides `max_size` which overrides `size` (#261)
+
 ## 0.2.0
 
 ### New features
diff --git a/benches/generated/min_height_overrides_max_height.rs b/benches/generated/min_height_overrides_max_height.rs
new file mode 100644
index 000000000..3cade50ed
--- /dev/null
+++ b/benches/generated/min_height_overrides_max_height.rs
@@ -0,0 +1,21 @@
+pub fn compute() {
+    let mut taffy = taffy::Taffy::new();
+    let node0 = taffy
+        .new_with_children(
+            taffy::style::Style {
+                min_size: taffy::geometry::Size {
+                    height: taffy::style::Dimension::Points(100f32),
+                    ..Default::default()
+                },
+                max_size: taffy::geometry::Size {
+                    height: taffy::style::Dimension::Points(50f32),
+                    ..Default::default()
+                },
+                ..Default::default()
+            },
+            &[],
+        )
+        .unwrap();
+    let node = taffy.new_with_children(taffy::style::Style { ..Default::default() }, &[node0]).unwrap();
+    taffy.compute_layout(node, taffy::geometry::Size::MAX_CONTENT).unwrap();
+}
diff --git a/benches/generated/min_width_overrides_max_width.rs b/benches/generated/min_width_overrides_max_width.rs
new file mode 100644
index 000000000..42544e84c
--- /dev/null
+++ b/benches/generated/min_width_overrides_max_width.rs
@@ -0,0 +1,18 @@
+pub fn compute() {
+    let mut taffy = taffy::Taffy::new();
+    let node0 = taffy
+        .new_with_children(
+            taffy::style::Style {
+                min_size: taffy::geometry::Size {
+                    width: taffy::style::Dimension::Points(100f32),
+                    ..Default::default()
+                },
+                max_size: taffy::geometry::Size { width: taffy::style::Dimension::Points(50f32), ..Default::default() },
+                ..Default::default()
+            },
+            &[],
+        )
+        .unwrap();
+    let node = taffy.new_with_children(taffy::style::Style { ..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 3db294a42..557da7503 100644
--- a/benches/generated/mod.rs
+++ b/benches/generated/mod.rs
@@ -198,8 +198,10 @@ mod max_width_overrides_width_on_root;
 mod min_height;
 mod min_height_overrides_height;
 mod min_height_overrides_height_on_root;
+mod min_height_overrides_max_height;
 mod min_max_percent_no_width_height;
 mod min_width;
+mod min_width_overrides_max_width;
 mod min_width_overrides_width;
 mod min_width_overrides_width_on_root;
 mod nested_overflowing_child;
@@ -474,8 +476,10 @@ fn benchmark(c: &mut Criterion) {
             min_height::compute();
             min_height_overrides_height::compute();
             min_height_overrides_height_on_root::compute();
+            min_height_overrides_max_height::compute();
             min_max_percent_no_width_height::compute();
             min_width::compute();
+            min_width_overrides_max_width::compute();
             min_width_overrides_width::compute();
             min_width_overrides_width_on_root::compute();
             nested_overflowing_child::compute();
diff --git a/src/math.rs b/src/math.rs
index eedeaf785..56dddeb61 100644
--- a/src/math.rs
+++ b/src/math.rs
@@ -46,7 +46,7 @@ impl MaybeMath<Option<f32>, Option<f32>> for Option<f32> {
 
     fn maybe_clamp(self, min: Option<f32>, max: Option<f32>) -> Option<f32> {
         match (self, min, max) {
-            (Some(base), Some(min), Some(max)) => Some(base.max(min).min(max)),
+            (Some(base), Some(min), Some(max)) => Some(base.min(max).max(min)),
             (Some(base), None, Some(max)) => Some(base.min(max)),
             (Some(base), Some(min), None) => Some(base.max(min)),
             (Some(_), None, None) => self,
@@ -83,7 +83,7 @@ impl MaybeMath<f32, Option<f32>> for Option<f32> {
     }
 
     fn maybe_clamp(self, min: f32, max: f32) -> Option<f32> {
-        self.map(|val| val.max(min).min(max))
+        self.map(|val| val.min(max).max(min))
     }
 
     fn maybe_add(self, rhs: f32) -> Option<f32> {
@@ -112,7 +112,7 @@ impl MaybeMath<Option<f32>, f32> for f32 {
 
     fn maybe_clamp(self, min: Option<f32>, max: Option<f32>) -> f32 {
         match (min, max) {
-            (Some(min), Some(max)) => self.max(min).min(max),
+            (Some(min), Some(max)) => self.min(max).max(min),
             (None, Some(max)) => self.min(max),
             (Some(min), None) => self.max(min),
             (None, None) => self,
@@ -152,7 +152,7 @@ impl MaybeMath<f32, AvailableSpace> for AvailableSpace {
 
     fn maybe_clamp(self, min: f32, max: f32) -> AvailableSpace {
         match self {
-            AvailableSpace::Definite(val) => AvailableSpace::Definite(val.max(min).min(max)),
+            AvailableSpace::Definite(val) => AvailableSpace::Definite(val.min(max).max(min)),
             AvailableSpace::MinContent => AvailableSpace::MinContent,
             AvailableSpace::MaxContent => AvailableSpace::MaxContent,
         }
@@ -196,7 +196,7 @@ impl MaybeMath<Option<f32>, AvailableSpace> for AvailableSpace {
 
     fn maybe_clamp(self, min: Option<f32>, max: Option<f32>) -> AvailableSpace {
         match (self, min, max) {
-            (AvailableSpace::Definite(val), Some(min), Some(max)) => AvailableSpace::Definite(val.max(min).min(max)),
+            (AvailableSpace::Definite(val), Some(min), Some(max)) => AvailableSpace::Definite(val.min(max).max(min)),
             (AvailableSpace::Definite(val), None, Some(max)) => AvailableSpace::Definite(val.min(max)),
             (AvailableSpace::Definite(val), Some(min), None) => AvailableSpace::Definite(val.max(min)),
             (AvailableSpace::Definite(val), None, None) => AvailableSpace::Definite(val),
diff --git a/test_fixtures/min_height_overrides_max_height.html b/test_fixtures/min_height_overrides_max_height.html
new file mode 100644
index 000000000..78a93c38d
--- /dev/null
+++ b/test_fixtures/min_height_overrides_max_height.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">
+  <div style="min-height: 100px; max-height: 50px;"></div>
+</div>
+
+</body>
+</html>
\ No newline at end of file
diff --git a/test_fixtures/min_width_overrides_max_width.html b/test_fixtures/min_width_overrides_max_width.html
new file mode 100644
index 000000000..7405fb94a
--- /dev/null
+++ b/test_fixtures/min_width_overrides_max_width.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">
+  <div style="min-width: 100px; max-width: 50px;"></div>
+</div>
+
+</body>
+</html>
\ No newline at end of file
diff --git a/tests/generated/min_height_overrides_max_height.rs b/tests/generated/min_height_overrides_max_height.rs
new file mode 100644
index 000000000..8d72d8d16
--- /dev/null
+++ b/tests/generated/min_height_overrides_max_height.rs
@@ -0,0 +1,30 @@
+#[test]
+fn min_height_overrides_max_height() {
+    let mut taffy = taffy::Taffy::new();
+    let node0 = taffy
+        .new_with_children(
+            taffy::style::Style {
+                min_size: taffy::geometry::Size {
+                    height: taffy::style::Dimension::Points(100f32),
+                    ..Default::default()
+                },
+                max_size: taffy::geometry::Size {
+                    height: taffy::style::Dimension::Points(50f32),
+                    ..Default::default()
+                },
+                ..Default::default()
+            },
+            &[],
+        )
+        .unwrap();
+    let node = taffy.new_with_children(taffy::style::Style { ..Default::default() }, &[node0]).unwrap();
+    taffy.compute_layout(node, taffy::geometry::Size::MAX_CONTENT).unwrap();
+    assert_eq!(taffy.layout(node).unwrap().size.width, 0f32);
+    assert_eq!(taffy.layout(node).unwrap().size.height, 100f32);
+    assert_eq!(taffy.layout(node).unwrap().location.x, 0f32);
+    assert_eq!(taffy.layout(node).unwrap().location.y, 0f32);
+    assert_eq!(taffy.layout(node0).unwrap().size.width, 0f32);
+    assert_eq!(taffy.layout(node0).unwrap().size.height, 100f32);
+    assert_eq!(taffy.layout(node0).unwrap().location.x, 0f32);
+    assert_eq!(taffy.layout(node0).unwrap().location.y, 0f32);
+}
diff --git a/tests/generated/min_width_overrides_max_width.rs b/tests/generated/min_width_overrides_max_width.rs
new file mode 100644
index 000000000..ae8d7dea5
--- /dev/null
+++ b/tests/generated/min_width_overrides_max_width.rs
@@ -0,0 +1,27 @@
+#[test]
+fn min_width_overrides_max_width() {
+    let mut taffy = taffy::Taffy::new();
+    let node0 = taffy
+        .new_with_children(
+            taffy::style::Style {
+                min_size: taffy::geometry::Size {
+                    width: taffy::style::Dimension::Points(100f32),
+                    ..Default::default()
+                },
+                max_size: taffy::geometry::Size { width: taffy::style::Dimension::Points(50f32), ..Default::default() },
+                ..Default::default()
+            },
+            &[],
+        )
+        .unwrap();
+    let node = taffy.new_with_children(taffy::style::Style { ..Default::default() }, &[node0]).unwrap();
+    taffy.compute_layout(node, taffy::geometry::Size::MAX_CONTENT).unwrap();
+    assert_eq!(taffy.layout(node).unwrap().size.width, 100f32);
+    assert_eq!(taffy.layout(node).unwrap().size.height, 0f32);
+    assert_eq!(taffy.layout(node).unwrap().location.x, 0f32);
+    assert_eq!(taffy.layout(node).unwrap().location.y, 0f32);
+    assert_eq!(taffy.layout(node0).unwrap().size.width, 100f32);
+    assert_eq!(taffy.layout(node0).unwrap().size.height, 0f32);
+    assert_eq!(taffy.layout(node0).unwrap().location.x, 0f32);
+    assert_eq!(taffy.layout(node0).unwrap().location.y, 0f32);
+}
diff --git a/tests/generated/mod.rs b/tests/generated/mod.rs
index cda3125d5..86c85b642 100644
--- a/tests/generated/mod.rs
+++ b/tests/generated/mod.rs
@@ -197,8 +197,10 @@ mod max_width_overrides_width_on_root;
 mod min_height;
 mod min_height_overrides_height;
 mod min_height_overrides_height_on_root;
+mod min_height_overrides_max_height;
 mod min_max_percent_no_width_height;
 mod min_width;
+mod min_width_overrides_max_width;
 mod min_width_overrides_width;
 mod min_width_overrides_width_on_root;
 mod nested_overflowing_child;
diff --git a/tests/min_max_overrides.rs b/tests/min_max_overrides.rs
new file mode 100644
index 000000000..a3eccd757
--- /dev/null
+++ b/tests/min_max_overrides.rs
@@ -0,0 +1,72 @@
+#[cfg(test)]
+mod min_max_overrides {
+
+    use taffy::prelude::*;
+
+    #[test]
+    fn min_overrides_max() {
+        let mut taffy = Taffy::new();
+
+        let child = taffy
+            .new_leaf(Style {
+                size: Size { width: Dimension::Points(50.0), height: Dimension::Points(50.0) },
+                min_size: Size { width: Dimension::Points(100.0), height: Dimension::Points(100.0) },
+                max_size: Size { width: Dimension::Points(10.0), height: Dimension::Points(10.0) },
+                ..Default::default()
+            })
+            .unwrap();
+
+        taffy
+            .compute_layout(
+                child,
+                Size { height: AvailableSpace::Definite(100.0), width: AvailableSpace::Definite(100.0) },
+            )
+            .unwrap();
+
+        assert_eq!(taffy.layout(child).unwrap().size, Size { width: 100.0, height: 100.0 });
+    }
+
+    #[test]
+    fn max_overrides_size() {
+        let mut taffy = Taffy::new();
+
+        let child = taffy
+            .new_leaf(Style {
+                size: Size { width: Dimension::Points(50.0), height: Dimension::Points(50.0) },
+                max_size: Size { width: Dimension::Points(10.0), height: Dimension::Points(10.0) },
+                ..Default::default()
+            })
+            .unwrap();
+
+        taffy
+            .compute_layout(
+                child,
+                Size { height: AvailableSpace::Definite(100.0), width: AvailableSpace::Definite(100.0) },
+            )
+            .unwrap();
+
+        assert_eq!(taffy.layout(child).unwrap().size, Size { width: 10.0, height: 10.0 });
+    }
+
+    #[test]
+    fn min_overrides_size() {
+        let mut taffy = Taffy::new();
+
+        let child = taffy
+            .new_leaf(Style {
+                size: Size { width: Dimension::Points(50.0), height: Dimension::Points(50.0) },
+                min_size: Size { width: Dimension::Points(100.0), height: Dimension::Points(100.0) },
+                ..Default::default()
+            })
+            .unwrap();
+
+        taffy
+            .compute_layout(
+                child,
+                Size { height: AvailableSpace::Definite(100.0), width: AvailableSpace::Definite(100.0) },
+            )
+            .unwrap();
+
+        assert_eq!(taffy.layout(child).unwrap().size, Size { width: 100.0, height: 100.0 });
+    }
+}