From 8759e92cdc955ae8da3287da4ff6227e24abece8 Mon Sep 17 00:00:00 2001 From: meloalright Date: Wed, 13 Nov 2024 23:48:10 +0800 Subject: [PATCH] feat(flex): add support of set flex wrap and test case walk through --- include/safe.h | 1 + src/lib.rs | 18 ++++++--- src/safe.cc | 18 +++++++++ src/safe.rs | 1 + tests/flex_wrap_test.rs | 87 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 119 insertions(+), 6 deletions(-) create mode 100644 tests/flex_wrap_test.rs diff --git a/include/safe.h b/include/safe.h index 9e3903d..cec51f4 100644 --- a/include/safe.h +++ b/include/safe.h @@ -29,6 +29,7 @@ void set_flex_grow(std::unique_ptr & node, double flex_grow); 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 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 5dd887e..6e5042e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,12 +8,6 @@ pub struct TaitankSafeNode { unique_ptr: UniquePtr, } -// impl Drop for TaitankSafeNode { -// fn drop(&mut self) { -// ffi:: -// } -// } - #[repr(i32)] pub enum Direction { Inherit = 0, @@ -29,6 +23,13 @@ pub enum FlexDirection { FlexDirectionColumnReverse = 3, } +#[repr(i32)] +pub enum FlexWrapNode { + FlexNoWrap = 0, + FlexWrap = 1, + FlexWrapReverse = 2, +} + pub fn node_create() -> TaitankSafeNode { TaitankSafeNode { unique_ptr: ffi::node_create(), @@ -58,6 +59,11 @@ pub fn set_flex_basis(node: &mut TaitankSafeNode, flex_basis: f64) { pub fn set_flex_direction(node: &mut TaitankSafeNode, flex_direction: FlexDirection) { ffi::set_flex_direction(&mut node.unique_ptr, flex_direction as i32); } + +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 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 a814ced..4a8719d 100644 --- a/src/safe.cc +++ b/src/safe.cc @@ -79,6 +79,24 @@ void set_flex_direction(std::unique_ptr & node, int flex_direct } } +void set_flex_wrap(std::unique_ptr & node, int flex_wrap_node) { + switch (flex_wrap_node) { + case 0: { + taitank::SetFlexWrap(node->ptr, taitank::FlexWrapMode::FLEX_NO_WRAP); + break; + } + case 1: { + taitank::SetFlexWrap(node->ptr, taitank::FlexWrapMode::FLEX_WRAP); + break; + } + case 2: { + taitank::SetFlexWrap(node->ptr, taitank::FlexWrapMode::FLEX_WRAP_REVERSE); + 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 9d531e6..77e2fc6 100644 --- a/src/safe.rs +++ b/src/safe.rs @@ -14,6 +14,7 @@ pub mod ffi { fn set_flex_shrink(node: &mut UniquePtr, flex_shrink: f64); 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 insert_child( node: &mut UniquePtr, child: &mut UniquePtr, diff --git a/tests/flex_wrap_test.rs b/tests/flex_wrap_test.rs new file mode 100644 index 0000000..a102645 --- /dev/null +++ b/tests/flex_wrap_test.rs @@ -0,0 +1,87 @@ +#[cfg(test)] +mod tests { + extern crate taitank_safe; + use taitank_safe::*; + + #[test] + fn wrap_column() { + let mut root = node_create(); + set_flex_wrap(&mut root, FlexWrapNode::FlexWrap); + set_height(&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!(60.0, get_width(&mut root)); + assert_eq!(100.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!(0.0, get_left(&mut root_child1)); + assert_eq!(30.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!(0.0, get_left(&mut root_child2)); + assert_eq!(60.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!(30.0, get_left(&mut root_child3)); + assert_eq!(0.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!(60.0, get_width(&mut root)); + assert_eq!(100.0, get_height(&mut root)); + + assert_eq!(30.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!(30.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!(30.0, get_left(&mut root_child2)); + assert_eq!(60.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!(0.0, get_top(&mut root_child3)); + assert_eq!(30.0, get_width(&mut root_child3)); + assert_eq!(30.0, get_height(&mut root_child3)); + + } +}