tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

Rust 1082HTML 1014Markdown 12TOML 83222 Definitions

Add more tests

bc2a1a531e3cb707df30f546c5885e8dd753baa1 · by Emil Sjölander · (7 years ago) · parent 155d30356 · browse the tree at this commit

Semantic delta · 13

src/algo.rs

tests/lib.rs

2 files · +533 −27

diff --git a/src/algo.rs b/src/algo.rs
index 680397a8b..987f8de4a 100644
--- a/src/algo.rs
+++ b/src/algo.rs
@@ -145,21 +145,21 @@ fn compute_internal(node: &style::Node, main: SizeConstraint, cross: SizeConstra
     main.max 
         - node.main_margin_start().resolve(main.max, 0.0) 
         - node.main_margin_end().resolve(main.max, 0.0) 
-        - node.main_padding_start().resolve(main.max, 0.0) 
-        - node.main_padding_end().resolve(main.max, 0.0) 
-        - node.main_border_start().resolve(main.max, 0.0)
-        - node.main_border_end().resolve(main.max, 0.0)
-  });
+  }) 
+  - node.main_padding_start().resolve(main.max, 0.0) 
+  - node.main_padding_end().resolve(main.max, 0.0) 
+  - node.main_border_start().resolve(main.max, 0.0)
+  - node.main_border_end().resolve(main.max, 0.0);
 
   let avaliable_cross = node.cross_size().resolve(cross.max, {
     cross.max 
         - node.cross_margin_start().resolve(cross.max, 0.0) 
         - node.cross_margin_end().resolve(cross.max, 0.0) 
-        - node.cross_padding_start().resolve(cross.max, 0.0) 
-        - node.cross_padding_end().resolve(cross.max, 0.0) 
-        - node.cross_border_start().resolve(cross.max, 0.0)
-        - node.cross_border_end().resolve(cross.max, 0.0)
-  });
+  })
+  - node.cross_padding_start().resolve(cross.max, 0.0) 
+  - node.cross_padding_end().resolve(cross.max, 0.0) 
+  - node.cross_border_start().resolve(cross.max, 0.0)
+  - node.cross_border_end().resolve(cross.max, 0.0);
   
   // 3. Determine the flex base size and hypothetical main size of each item:
   for child in &mut flex_items {
@@ -262,13 +262,14 @@ fn compute_internal(node: &style::Node, main: SizeConstraint, cross: SizeConstra
 
       for child in flex_items {
         line_length += child.hypothetical_outer_main_size;
-        line.items.push(child);
-        
-        if line_length > avaliable_main {
-          line_length = 0.0;
+
+        if line_length > avaliable_main && line.items.len() > 0 {
+          line_length = child.hypothetical_outer_main_size;
           lines.push(line);
           line = FlexLine { items: vec![], cross_size: 0.0, offset_cross: 0.0 };
         }
+
+        line.items.push(child);
       }
 
       lines.push(line);
@@ -635,8 +636,8 @@ fn compute_internal(node: &style::Node, main: SizeConstraint, cross: SizeConstra
         let mut is_first = true;
 
         for child in &mut line.items {
-          child.main_margin_start = 0.0;
-          child.main_margin_end = 0.0;
+          child.main_margin_start = child.node.main_margin_start().resolve(container_main_size, 0.0);
+          child.main_margin_end = child.node.main_margin_end().resolve(container_main_size, 0.0);
 
           child.offset_main = match node.justify_content {
             style::JustifyContent::FlexStart => 0.0,
@@ -727,9 +728,16 @@ fn compute_internal(node: &style::Node, main: SizeConstraint, cross: SizeConstra
   }
 
   let mut children: Vec<layout::Node> = vec![];
-  for line in &mut flex_lines {
-    let mut sibling_offset = 0.0;
 
+  let mut total_offset_cross =
+      node.cross_padding_start().resolve(container_cross_size, 0.0) + 
+      node.cross_border_start().resolve(container_cross_size, 0.0);
+
+  for line in &mut flex_lines {
+    let mut total_offset_main =
+        node.main_padding_start().resolve(container_main_size, 0.0) + 
+        node.main_border_start().resolve(container_main_size, 0.0);
+        
     for child in &mut line.items {
       let result = compute_internal(
         child.node, 
@@ -737,19 +745,16 @@ fn compute_internal(node: &style::Node, main: SizeConstraint, cross: SizeConstra
         SizeConstraint::exactly(child.target_cross_size));
 
       let offset_main = {
+        total_offset_main +
         child.offset_main +
-        child.main_margin_start + 
-        sibling_offset + 
-        node.main_padding_start().resolve(container_main_size, 0.0) + 
-        node.main_border_start().resolve(container_main_size, 0.0)
+        child.main_margin_start
       };
 
       let offset_cross = {
+        total_offset_cross +
         child.offset_cross + 
-        line.offset_cross + 
-        child.cross_margin_start +
-        node.cross_padding_start().resolve(container_cross_size, 0.0) +
-        node.cross_border_start().resolve(container_cross_size, 0.0)
+        line.offset_cross +
+        child.cross_margin_start
       };
 
       children.push(layout::Node {
@@ -760,8 +765,10 @@ fn compute_internal(node: &style::Node, main: SizeConstraint, cross: SizeConstra
         children: result.children,
       });
 
-      sibling_offset += offset_main + result.size.main + child.main_margin_end;
+      total_offset_main = offset_main + result.size.main + child.main_margin_end;
     }
+
+    total_offset_cross += line.offset_cross + line.cross_size;
   }
 
   ComputeResult {
diff --git a/tests/lib.rs b/tests/lib.rs
index 649965792..7d02c7323 100644
--- a/tests/lib.rs
+++ b/tests/lib.rs
@@ -134,4 +134,503 @@ mod tests {
         assert_eq!(layout.children[1].x, 50.0);
         assert_eq!(layout.children[1].y, 0.0);
     }
+
+    #[test]
+    fn test_6() {
+        let layout = stretch::compute(&stretch::style::Node {
+            width: stretch::style::Dimension::Points(100.0),
+            height: stretch::style::Dimension::Points(100.0),
+
+            children: vec![
+                stretch::style::Node {
+                    width: stretch::style::Dimension::Points(50.0),
+
+                    ..Default::default()
+                },
+                stretch::style::Node {
+                    width: stretch::style::Dimension::Points(20.0),
+                    margin: stretch::style::Edges {
+                        start: stretch::style::Dimension::Points(30.0),
+                        ..Default::default()
+                    },
+
+                    ..Default::default()
+                },
+            ],
+          
+            ..Default::default()
+        });
+
+        assert_eq!(layout.children[0].width, 50.0);
+        assert_eq!(layout.children[0].height, 100.0);
+        assert_eq!(layout.children[0].x, 0.0);
+        assert_eq!(layout.children[0].y, 0.0);
+
+        assert_eq!(layout.children[1].width, 20.0);
+        assert_eq!(layout.children[1].height, 100.0);
+        assert_eq!(layout.children[1].x, 80.0);
+        assert_eq!(layout.children[1].y, 0.0);
+    }
+
+    #[test]
+    fn test_7() {
+        let layout = stretch::compute(&stretch::style::Node {
+            width: stretch::style::Dimension::Points(100.0),
+            height: stretch::style::Dimension::Points(100.0),
+            padding: stretch::style::Edges {
+                start: stretch::style::Dimension::Points(10.0),
+                end: stretch::style::Dimension::Points(10.0),
+                top: stretch::style::Dimension::Points(10.0),
+                bottom: stretch::style::Dimension::Points(10.0),
+            },
+
+            children: vec![
+                stretch::style::Node {
+                    width: stretch::style::Dimension::Points(40.0),
+
+                    ..Default::default()
+                },
+                stretch::style::Node {
+                    width: stretch::style::Dimension::Points(40.0),
+
+                    ..Default::default()
+                },
+            ],
+          
+            ..Default::default()
+        });
+
+        assert_eq!(layout.children[0].width, 40.0);
+        assert_eq!(layout.children[0].height, 80.0);
+        assert_eq!(layout.children[0].x, 10.0);
+        assert_eq!(layout.children[0].y, 10.0);
+
+        assert_eq!(layout.children[1].width, 40.0);
+        assert_eq!(layout.children[1].height, 80.0);
+        assert_eq!(layout.children[1].x, 50.0);
+        assert_eq!(layout.children[1].y, 10.0);
+    }
+
+    #[test]
+    fn test_8() {
+        let layout = stretch::compute(&stretch::style::Node {
+            width: stretch::style::Dimension::Points(100.0),
+            height: stretch::style::Dimension::Points(100.0),
+            border: stretch::style::Edges {
+                start: stretch::style::Dimension::Points(10.0),
+                end: stretch::style::Dimension::Points(10.0),
+                top: stretch::style::Dimension::Points(10.0),
+                bottom: stretch::style::Dimension::Points(10.0),
+            },
+
+            children: vec![
+                stretch::style::Node {
+                    width: stretch::style::Dimension::Points(40.0),
+
+                    ..Default::default()
+                },
+                stretch::style::Node {
+                    width: stretch::style::Dimension::Points(40.0),
+
+                    ..Default::default()
+                },
+            ],
+          
+            ..Default::default()
+        });
+
+        assert_eq!(layout.children[0].width, 40.0);
+        assert_eq!(layout.children[0].height, 80.0);
+        assert_eq!(layout.children[0].x, 10.0);
+        assert_eq!(layout.children[0].y, 10.0);
+
+        assert_eq!(layout.children[1].width, 40.0);
+        assert_eq!(layout.children[1].height, 80.0);
+        assert_eq!(layout.children[1].x, 50.0);
+        assert_eq!(layout.children[1].y, 10.0);
+    }
+
+    #[test]
+    fn test_9() {
+        let layout = stretch::compute(&stretch::style::Node {
+            width: stretch::style::Dimension::Points(100.0),
+            height: stretch::style::Dimension::Points(100.0),
+            justify_content: stretch::style::JustifyContent::Center,
+
+            children: vec![
+                stretch::style::Node {
+                    width: stretch::style::Dimension::Points(20.0),
+                    height: stretch::style::Dimension::Points(20.0),
+
+                    ..Default::default()
+                },
+                stretch::style::Node {
+                    width: stretch::style::Dimension::Points(20.0),
+                    height: stretch::style::Dimension::Points(20.0),
+
+                    ..Default::default()
+                },
+            ],
+          
+            ..Default::default()
+        });
+
+        assert_eq!(layout.children[0].width, 20.0);
+        assert_eq!(layout.children[0].height, 20.0);
+        assert_eq!(layout.children[0].x, 30.0);
+        assert_eq!(layout.children[0].y, 0.0);
+
+        assert_eq!(layout.children[1].width, 20.0);
+        assert_eq!(layout.children[1].height, 20.0);
+        assert_eq!(layout.children[1].x, 50.0);
+        assert_eq!(layout.children[1].y, 0.0);
+    }
+
+    #[test]
+    fn test_10() {
+        let layout = stretch::compute(&stretch::style::Node {
+            width: stretch::style::Dimension::Points(100.0),
+            height: stretch::style::Dimension::Points(100.0),
+            justify_content: stretch::style::JustifyContent::FlexEnd,
+
+            children: vec![
+                stretch::style::Node {
+                    width: stretch::style::Dimension::Points(20.0),
+                    height: stretch::style::Dimension::Points(20.0),
+
+                    ..Default::default()
+                },
+                stretch::style::Node {
+                    width: stretch::style::Dimension::Points(20.0),
+                    height: stretch::style::Dimension::Points(20.0),
+
+                    ..Default::default()
+                },
+            ],
+          
+            ..Default::default()
+        });
+
+        assert_eq!(layout.children[0].width, 20.0);
+        assert_eq!(layout.children[0].height, 20.0);
+        assert_eq!(layout.children[0].x, 60.0);
+        assert_eq!(layout.children[0].y, 0.0);
+
+        assert_eq!(layout.children[1].width, 20.0);
+        assert_eq!(layout.children[1].height, 20.0);
+        assert_eq!(layout.children[1].x, 80.0);
+        assert_eq!(layout.children[1].y, 0.0);
+    }
+
+    #[test]
+    fn test_11() {
+        let layout = stretch::compute(&stretch::style::Node {
+            width: stretch::style::Dimension::Points(100.0),
+            height: stretch::style::Dimension::Points(100.0),
+            justify_content: stretch::style::JustifyContent::SpaceBetween,
+
+            children: vec![
+                stretch::style::Node {
+                    width: stretch::style::Dimension::Points(20.0),
+                    height: stretch::style::Dimension::Points(20.0),
+
+                    ..Default::default()
+                },
+                stretch::style::Node {
+                    width: stretch::style::Dimension::Points(20.0),
+                    height: stretch::style::Dimension::Points(20.0),
+
+                    ..Default::default()
+                },
+            ],
+          
+            ..Default::default()
+        });
+
+        assert_eq!(layout.children[0].width, 20.0);
+        assert_eq!(layout.children[0].height, 20.0);
+        assert_eq!(layout.children[0].x, 0.0);
+        assert_eq!(layout.children[0].y, 0.0);
+
+        assert_eq!(layout.children[1].width, 20.0);
+        assert_eq!(layout.children[1].height, 20.0);
+        assert_eq!(layout.children[1].x, 80.0);
+        assert_eq!(layout.children[1].y, 0.0);
+    }
+
+    #[test]
+    fn test_12() {
+        let layout = stretch::compute(&stretch::style::Node {
+            width: stretch::style::Dimension::Points(100.0),
+            height: stretch::style::Dimension::Points(100.0),
+            align_items: stretch::style::AlignItems::FlexEnd,
+
+            children: vec![
+                stretch::style::Node {
+                    width: stretch::style::Dimension::Points(20.0),
+                    height: stretch::style::Dimension::Points(20.0),
+
+                    ..Default::default()
+                },
+                stretch::style::Node {
+                    width: stretch::style::Dimension::Points(20.0),
+                    height: stretch::style::Dimension::Points(20.0),
+
+                    ..Default::default()
+                },
+            ],
+          
+            ..Default::default()
+        });
+
+        assert_eq!(layout.children[0].width, 20.0);
+        assert_eq!(layout.children[0].height, 20.0);
+        assert_eq!(layout.children[0].x, 0.0);
+        assert_eq!(layout.children[0].y, 80.0);
+
+        assert_eq!(layout.children[1].width, 20.0);
+        assert_eq!(layout.children[1].height, 20.0);
+        assert_eq!(layout.children[1].x, 20.0);
+        assert_eq!(layout.children[1].y, 80.0);
+    }
+
+    #[test]
+    fn test_13() {
+        let layout = stretch::compute(&stretch::style::Node {
+            width: stretch::style::Dimension::Points(100.0),
+            height: stretch::style::Dimension::Points(100.0),
+            align_items: stretch::style::AlignItems::Center,
+
+            children: vec![
+                stretch::style::Node {
+                    width: stretch::style::Dimension::Points(20.0),
+                    height: stretch::style::Dimension::Points(20.0),
+
+                    ..Default::default()
+                },
+                stretch::style::Node {
+                    width: stretch::style::Dimension::Points(20.0),
+                    height: stretch::style::Dimension::Points(20.0),
+
+                    ..Default::default()
+                },
+            ],
+          
+            ..Default::default()
+        });
+
+        assert_eq!(layout.children[0].width, 20.0);
+        assert_eq!(layout.children[0].height, 20.0);
+        assert_eq!(layout.children[0].x, 0.0);
+        assert_eq!(layout.children[0].y, 40.0);
+
+        assert_eq!(layout.children[1].width, 20.0);
+        assert_eq!(layout.children[1].height, 20.0);
+        assert_eq!(layout.children[1].x, 20.0);
+        assert_eq!(layout.children[1].y, 40.0);
+    }
+
+    #[test]
+    fn test_14() {
+        let layout = stretch::compute(&stretch::style::Node {
+            width: stretch::style::Dimension::Points(100.0),
+            height: stretch::style::Dimension::Points(100.0),
+            align_items: stretch::style::AlignItems::Center,
+
+            children: vec![
+                stretch::style::Node {
+                    width: stretch::style::Dimension::Points(20.0),
+                    height: stretch::style::Dimension::Points(20.0),
+
+                    ..Default::default()
+                },
+                stretch::style::Node {
+                    width: stretch::style::Dimension::Points(20.0),
+                    height: stretch::style::Dimension::Points(20.0),
+                    align_self: stretch::style::AlignSelf::FlexStart,
+
+                    ..Default::default()
+                },
+            ],
+          
+            ..Default::default()
+        });
+
+        assert_eq!(layout.children[0].width, 20.0);
+        assert_eq!(layout.children[0].height, 20.0);
+        assert_eq!(layout.children[0].x, 0.0);
+        assert_eq!(layout.children[0].y, 40.0);
+
+        assert_eq!(layout.children[1].width, 20.0);
+        assert_eq!(layout.children[1].height, 20.0);
+        assert_eq!(layout.children[1].x, 20.0);
+        assert_eq!(layout.children[1].y, 0.0);
+    }
+
+    #[test]
+    fn test_15() {
+        let layout = stretch::compute(&stretch::style::Node {
+            width: stretch::style::Dimension::Points(100.0),
+
+            children: vec![
+                stretch::style::Node {
+                    width: stretch::style::Dimension::Points(40.0),
+                    height: stretch::style::Dimension::Points(40.0),
+
+                    ..Default::default()
+                },
+                stretch::style::Node {
+                    width: stretch::style::Dimension::Points(40.0),
+                    height: stretch::style::Dimension::Points(40.0),
+
+                    ..Default::default()
+                },
+            ],
+          
+            ..Default::default()
+        });
+
+        assert_eq!(layout.height, 40.0);
+
+        assert_eq!(layout.children[0].width, 40.0);
+        assert_eq!(layout.children[0].height, 40.0);
+        assert_eq!(layout.children[0].x, 0.0);
+        assert_eq!(layout.children[0].y, 0.0);
+
+        assert_eq!(layout.children[1].width, 40.0);
+        assert_eq!(layout.children[1].height, 40.0);
+        assert_eq!(layout.children[1].x, 40.0);
+        assert_eq!(layout.children[1].y, 0.0);
+    }
+
+    #[test]
+    fn test_16() {
+        let layout = stretch::compute(&stretch::style::Node {
+            width: stretch::style::Dimension::Points(100.0),
+            wrap: stretch::style::Wrap::Wrap,
+
+            children: vec![
+                stretch::style::Node {
+                    width: stretch::style::Dimension::Points(40.0),
+                    height: stretch::style::Dimension::Points(40.0),
+
+                    ..Default::default()
+                },
+                stretch::style::Node {
+                    width: stretch::style::Dimension::Points(40.0),
+                    height: stretch::style::Dimension::Points(40.0),
+
+                    ..Default::default()
+                },
+                stretch::style::Node {
+                    width: stretch::style::Dimension::Points(40.0),
+                    height: stretch::style::Dimension::Points(40.0),
+
+                    ..Default::default()
+                },
+                stretch::style::Node {
+                    width: stretch::style::Dimension::Points(40.0),
+                    height: stretch::style::Dimension::Points(40.0),
+
+                    ..Default::default()
+                },
+                stretch::style::Node {
+                    width: stretch::style::Dimension::Points(40.0),
+                    height: stretch::style::Dimension::Points(40.0),
+
+                    ..Default::default()
+                },
+            ],
+          
+            ..Default::default()
+        });
+
+        assert_eq!(layout.height, 120.0);
+
+        assert_eq!(layout.children[0].width, 40.0);
+        assert_eq!(layout.children[0].height, 40.0);
+        assert_eq!(layout.children[0].x, 0.0);
+        assert_eq!(layout.children[0].y, 0.0);
+
+        assert_eq!(layout.children[1].width, 40.0);
+        assert_eq!(layout.children[1].height, 40.0);
+        assert_eq!(layout.children[1].x, 40.0);
+        assert_eq!(layout.children[1].y, 0.0);
+
+        assert_eq!(layout.children[2].width, 40.0);
+        assert_eq!(layout.children[2].height, 40.0);
+        assert_eq!(layout.children[2].x, 0.0);
+        assert_eq!(layout.children[2].y, 40.0);
+
+        assert_eq!(layout.children[3].width, 40.0);
+        assert_eq!(layout.children[3].height, 40.0);
+        assert_eq!(layout.children[3].x, 40.0);
+        assert_eq!(layout.children[3].y, 40.0);
+
+        assert_eq!(layout.children[4].width, 40.0);
+        assert_eq!(layout.children[4].height, 40.0);
+        assert_eq!(layout.children[4].x, 0.0);
+        assert_eq!(layout.children[4].y, 80.0);
+    }
+
+    #[test]
+    fn test_17() {
+        let layout = stretch::compute(&stretch::style::Node {
+            width: stretch::style::Dimension::Points(100.0),
+            height: stretch::style::Dimension::Points(100.0),
+            align_content: stretch::style::AlignContent::Center,
+            wrap: stretch::style::Wrap::Wrap,
+
+            children: vec![
+                stretch::style::Node {
+                    width: stretch::style::Dimension::Points(40.0),
+                    height: stretch::style::Dimension::Points(40.0),
+
+                    ..Default::default()
+                },
+                stretch::style::Node {
+                    width: stretch::style::Dimension::Points(40.0),
+                    height: stretch::style::Dimension::Points(40.0),
+
+                    ..Default::default()
+                },
+                stretch::style::Node {
+                    width: stretch::style::Dimension::Points(40.0),
+                    height: stretch::style::Dimension::Points(40.0),
+
+                    ..Default::default()
+                },
+                stretch::style::Node {
+                    width: stretch::style::Dimension::Points(40.0),
+                    height: stretch::style::Dimension::Points(40.0),
+
+                    ..Default::default()
+                },
+            ],
+          
+            ..Default::default()
+        });
+
+        assert_eq!(layout.height, 100.0);
+
+        assert_eq!(layout.children[0].width, 40.0);
+        assert_eq!(layout.children[0].height, 40.0);
+        assert_eq!(layout.children[0].x, 0.0);
+        assert_eq!(layout.children[0].y, 10.0);
+
+        assert_eq!(layout.children[1].width, 40.0);
+        assert_eq!(layout.children[1].height, 40.0);
+        assert_eq!(layout.children[1].x, 40.0);
+        assert_eq!(layout.children[1].y, 10.0);
+
+        assert_eq!(layout.children[2].width, 40.0);
+        assert_eq!(layout.children[2].height, 40.0);
+        assert_eq!(layout.children[2].x, 0.0);
+        assert_eq!(layout.children[2].y, 50.0);
+
+        assert_eq!(layout.children[3].width, 40.0);
+        assert_eq!(layout.children[3].height, 40.0);
+        assert_eq!(layout.children[3].x, 40.0);
+        assert_eq!(layout.children[3].y, 50.0);
+    }
 }