From b8b1073e8fd3bed506c44b3c16b0dedf4e26fbe3 Mon Sep 17 00:00:00 2001 From: meloalright Date: Thu, 14 Nov 2024 00:25:40 +0800 Subject: [PATCH] feat(flex): add support of set align items and passed test case --- include/safe.h | 1 + src/lib.rs | 18 +++++ src/safe.cc | 42 ++++++++++ src/safe.rs | 1 + tests/flex_wrap_test.rs | 167 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 229 insertions(+) diff --git a/include/safe.h b/include/safe.h index cec51f4..9dd3ed9 100644 --- a/include/safe.h +++ b/include/safe.h @@ -30,6 +30,7 @@ void set_flex_shrink(std::unique_ptr & node, double flex_shrink void set_flex_basis(std::unique_ptr & node, double flex_basis); void set_flex_direction(std::unique_ptr & node, int direction); void set_flex_wrap(std::unique_ptr & node, int flex_wrap_node); +void set_align_items(std::unique_ptr & node, int flex_align); void insert_child(std::unique_ptr & node, std::unique_ptr & child, int index); void do_layout(std::unique_ptr & node, double parent_width, double parent_height, int direction); double get_width(std::unique_ptr & node); diff --git a/src/lib.rs b/src/lib.rs index 6e5042e..8c9f0c6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -30,6 +30,20 @@ pub enum FlexWrapNode { FlexWrapReverse = 2, } +#[repr(i32)] +pub enum FlexAlign { + FlexAlignAuto = 0, + FlexAlignStart = 1, + FlexAlignCenter = 2, + FlexAlignEnd = 3, + FlexAlignStretch = 4, + FlexAlignBaseLine = 5, + FlexAlignSpaceBetween = 6, + FlexAlignSpaceAround = 7, + FlexAlignSpaceEvenly = 8, +} + + pub fn node_create() -> TaitankSafeNode { TaitankSafeNode { unique_ptr: ffi::node_create(), @@ -64,6 +78,10 @@ pub fn set_flex_wrap(node: &mut TaitankSafeNode, flex_wrap_node: FlexWrapNode) { ffi::set_flex_wrap(&mut node.unique_ptr, flex_wrap_node as i32); } +pub fn set_align_items(node: &mut TaitankSafeNode, flex_align: FlexAlign) { + ffi::set_align_items(&mut node.unique_ptr, flex_align as i32); +} + pub fn insert_child(node: &mut TaitankSafeNode, child: &mut TaitankSafeNode, index: i32) { ffi::insert_child(&mut node.unique_ptr, &mut child.unique_ptr, index); } diff --git a/src/safe.cc b/src/safe.cc index 4a8719d..23adca9 100644 --- a/src/safe.cc +++ b/src/safe.cc @@ -97,6 +97,48 @@ void set_flex_wrap(std::unique_ptr & node, int flex_wrap_node) } +void set_align_items(std::unique_ptr & node, int flex_align) { + switch (flex_align) { + case 0: { + taitank::SetAlignItems(node->ptr, taitank::FlexAlign::FLEX_ALIGN_AUTO); + break; + } + case 1: { + taitank::SetAlignItems(node->ptr, taitank::FlexAlign::FLEX_ALIGN_START); + break; + } + case 2: { + taitank::SetAlignItems(node->ptr, taitank::FlexAlign::FLEX_ALIGN_CENTER); + break; + } + case 3: { + taitank::SetAlignItems(node->ptr, taitank::FlexAlign::FLEX_ALIGN_END); + break; + } + case 4: { + taitank::SetAlignItems(node->ptr, taitank::FlexAlign::FLEX_ALIGN_STRETCH); + break; + } + case 5: { + taitank::SetAlignItems(node->ptr, taitank::FlexAlign::FLEX_ALIGN_BASE_LINE); + break; + } + case 6: { + taitank::SetAlignItems(node->ptr, taitank::FlexAlign::FLEX_ALIGN_SPACE_BETWEEN); + break; + } + case 7: { + taitank::SetAlignItems(node->ptr, taitank::FlexAlign::FLEX_ALIGN_SPACE_AROUND); + break; + } + case 8: { + taitank::SetAlignItems(node->ptr, taitank::FlexAlign::FLEX_ALIGN_SPACE_EVENLY); + break; + } + } +} + + void insert_child(std::unique_ptr & node, std::unique_ptr & child, int index) { taitank::InsertChild(node->ptr, child->ptr, index); } diff --git a/src/safe.rs b/src/safe.rs index 77e2fc6..260402c 100644 --- a/src/safe.rs +++ b/src/safe.rs @@ -15,6 +15,7 @@ pub mod ffi { fn set_flex_basis(node: &mut UniquePtr, flex_basis: f64); fn set_flex_direction(node: &mut UniquePtr, flex_direction: i32); fn set_flex_wrap(node: &mut UniquePtr, flex_wrap_node: i32); + fn set_align_items(node: &mut UniquePtr, flex_align: i32); fn insert_child( node: &mut UniquePtr, child: &mut UniquePtr, diff --git a/tests/flex_wrap_test.rs b/tests/flex_wrap_test.rs index a102645..10f1b10 100644 --- a/tests/flex_wrap_test.rs +++ b/tests/flex_wrap_test.rs @@ -84,4 +84,171 @@ mod tests { assert_eq!(30.0, get_height(&mut root_child3)); } + + #[test] + fn wrap_row() { + let mut root = node_create(); + set_flex_direction(&mut root, FlexDirection::FlexDirectionRow); + set_flex_wrap(&mut root, FlexWrapNode::FlexWrap); + set_width(&mut root, 100.0); + + let mut root_child0 = node_create(); + set_width(&mut root_child0, 30.0); + set_height(&mut root_child0, 30.0); + insert_child(&mut root, &mut root_child0, 0); + + let mut root_child1 = node_create(); + set_width(&mut root_child1, 30.0); + set_height(&mut root_child1, 30.0); + insert_child(&mut root, &mut root_child1, 1); + + let mut root_child2 = node_create(); + set_width(&mut root_child2, 30.0); + set_height(&mut root_child2, 30.0); + insert_child(&mut root, &mut root_child2, 2); + + let mut root_child3 = node_create(); + set_width(&mut root_child3, 30.0); + set_height(&mut root_child3, 30.0); + insert_child(&mut root, &mut root_child3, 3); + + layout!(&mut root); + + assert_eq!(0.0, get_left(&mut root)); + assert_eq!(0.0, get_top(&mut root)); + assert_eq!(100.0, get_width(&mut root)); + assert_eq!(60.0, get_height(&mut root)); + + assert_eq!(0.0, get_left(&mut root_child0)); + assert_eq!(0.0, get_top(&mut root_child0)); + assert_eq!(30.0, get_width(&mut root_child0)); + assert_eq!(30.0, get_height(&mut root_child0)); + + assert_eq!(30.0, get_left(&mut root_child1)); + assert_eq!(0.0, get_top(&mut root_child1)); + assert_eq!(30.0, get_width(&mut root_child1)); + assert_eq!(30.0, get_height(&mut root_child1)); + + assert_eq!(60.0, get_left(&mut root_child2)); + assert_eq!(0.0, get_top(&mut root_child2)); + assert_eq!(30.0, get_width(&mut root_child2)); + assert_eq!(30.0, get_height(&mut root_child2)); + + assert_eq!(0.0, get_left(&mut root_child3)); + assert_eq!(30.0, get_top(&mut root_child3)); + assert_eq!(30.0, get_width(&mut root_child3)); + assert_eq!(30.0, get_height(&mut root_child3)); + + layout!(&mut root, std::f64::NAN, std::f64::NAN, Direction::RTL); + + assert_eq!(0.0, get_left(&mut root)); + assert_eq!(0.0, get_top(&mut root)); + assert_eq!(100.0, get_width(&mut root)); + assert_eq!(60.0, get_height(&mut root)); + + assert_eq!(70.0, get_left(&mut root_child0)); + assert_eq!(0.0, get_top(&mut root_child0)); + assert_eq!(30.0, get_width(&mut root_child0)); + assert_eq!(30.0, get_height(&mut root_child0)); + + assert_eq!(40.0, get_left(&mut root_child1)); + assert_eq!(0.0, get_top(&mut root_child1)); + assert_eq!(30.0, get_width(&mut root_child1)); + assert_eq!(30.0, get_height(&mut root_child1)); + + assert_eq!(10.0, get_left(&mut root_child2)); + assert_eq!(0.0, get_top(&mut root_child2)); + assert_eq!(30.0, get_width(&mut root_child2)); + assert_eq!(30.0, get_height(&mut root_child2)); + + assert_eq!(70.0, get_left(&mut root_child3)); + assert_eq!(30.0, get_top(&mut root_child3)); + assert_eq!(30.0, get_width(&mut root_child3)); + assert_eq!(30.0, get_height(&mut root_child3)); + + } + + #[test] + fn wrap_row_align_items_flex_end() { + let mut root = node_create(); + set_flex_direction(&mut root, FlexDirection::FlexDirectionRow); + set_align_items(&mut root, FlexAlign::FlexAlignEnd); + set_flex_wrap(&mut root, FlexWrapNode::FlexWrap); + set_width(&mut root, 100.0); + + let mut root_child0 = node_create(); + set_width(&mut root_child0, 30.0); + set_height(&mut root_child0, 10.0); + insert_child(&mut root, &mut root_child0, 0); + + let mut root_child1 = node_create(); + set_width(&mut root_child1, 30.0); + set_height(&mut root_child1, 20.0); + insert_child(&mut root, &mut root_child1, 1); + + let mut root_child2 = node_create(); + set_width(&mut root_child2, 30.0); + set_height(&mut root_child2, 30.0); + insert_child(&mut root, &mut root_child2, 2); + + let mut root_child3 = node_create(); + set_width(&mut root_child3, 30.0); + set_height(&mut root_child3, 30.0); + insert_child(&mut root, &mut root_child3, 3); + + layout!(&mut root); + + assert_eq!(0.0, get_left(&mut root)); + assert_eq!(0.0, get_top(&mut root)); + assert_eq!(100.0, get_width(&mut root)); + assert_eq!(60.0, get_height(&mut root)); + + assert_eq!(0.0, get_left(&mut root_child0)); + assert_eq!(20.0, get_top(&mut root_child0)); + assert_eq!(30.0, get_width(&mut root_child0)); + assert_eq!(10.0, get_height(&mut root_child0)); + + assert_eq!(30.0, get_left(&mut root_child1)); + assert_eq!(10.0, get_top(&mut root_child1)); + assert_eq!(30.0, get_width(&mut root_child1)); + assert_eq!(20.0, get_height(&mut root_child1)); + + assert_eq!(60.0, get_left(&mut root_child2)); + assert_eq!(0.0, get_top(&mut root_child2)); + assert_eq!(30.0, get_width(&mut root_child2)); + assert_eq!(30.0, get_height(&mut root_child2)); + + assert_eq!(0.0, get_left(&mut root_child3)); + assert_eq!(30.0, get_top(&mut root_child3)); + assert_eq!(30.0, get_width(&mut root_child3)); + assert_eq!(30.0, get_height(&mut root_child3)); + + layout!(&mut root, std::f64::NAN, std::f64::NAN, Direction::RTL); + + assert_eq!(0.0, get_left(&mut root)); + assert_eq!(0.0, get_top(&mut root)); + assert_eq!(100.0, get_width(&mut root)); + assert_eq!(60.0, get_height(&mut root)); + + assert_eq!(70.0, get_left(&mut root_child0)); + assert_eq!(20.0, get_top(&mut root_child0)); + assert_eq!(30.0, get_width(&mut root_child0)); + assert_eq!(10.0, get_height(&mut root_child0)); + + assert_eq!(40.0, get_left(&mut root_child1)); + assert_eq!(10.0, get_top(&mut root_child1)); + assert_eq!(30.0, get_width(&mut root_child1)); + assert_eq!(20.0, get_height(&mut root_child1)); + + assert_eq!(10.0, get_left(&mut root_child2)); + assert_eq!(0.0, get_top(&mut root_child2)); + assert_eq!(30.0, get_width(&mut root_child2)); + assert_eq!(30.0, get_height(&mut root_child2)); + + assert_eq!(70.0, get_left(&mut root_child3)); + assert_eq!(30.0, get_top(&mut root_child3)); + assert_eq!(30.0, get_width(&mut root_child3)); + assert_eq!(30.0, get_height(&mut root_child3)); + + } }