Skip to content

Commit

Permalink
feat(flex): add support of set align items and passed test case
Browse files Browse the repository at this point in the history
  • Loading branch information
meloalright committed Nov 13, 2024
1 parent 8759e92 commit b8b1073
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/safe.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ void set_flex_shrink(std::unique_ptr<TaitankSafeNode> & node, double flex_shrink
void set_flex_basis(std::unique_ptr<TaitankSafeNode> & node, double flex_basis);
void set_flex_direction(std::unique_ptr<TaitankSafeNode> & node, int direction);
void set_flex_wrap(std::unique_ptr<TaitankSafeNode> & node, int flex_wrap_node);
void set_align_items(std::unique_ptr<TaitankSafeNode> & node, int flex_align);
void insert_child(std::unique_ptr<TaitankSafeNode> & node, std::unique_ptr<TaitankSafeNode> & child, int index);
void do_layout(std::unique_ptr<TaitankSafeNode> & node, double parent_width, double parent_height, int direction);
double get_width(std::unique_ptr<TaitankSafeNode> & node);
Expand Down
18 changes: 18 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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);
}
Expand Down
42 changes: 42 additions & 0 deletions src/safe.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,48 @@ void set_flex_wrap(std::unique_ptr<TaitankSafeNode> & node, int flex_wrap_node)
}


void set_align_items(std::unique_ptr<TaitankSafeNode> & 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<TaitankSafeNode> & node, std::unique_ptr<TaitankSafeNode> & child, int index) {
taitank::InsertChild(node->ptr, child->ptr, index);
}
Expand Down
1 change: 1 addition & 0 deletions src/safe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub mod ffi {
fn set_flex_basis(node: &mut UniquePtr<TaitankSafeNode>, flex_basis: f64);
fn set_flex_direction(node: &mut UniquePtr<TaitankSafeNode>, flex_direction: i32);
fn set_flex_wrap(node: &mut UniquePtr<TaitankSafeNode>, flex_wrap_node: i32);
fn set_align_items(node: &mut UniquePtr<TaitankSafeNode>, flex_align: i32);
fn insert_child(
node: &mut UniquePtr<TaitankSafeNode>,
child: &mut UniquePtr<TaitankSafeNode>,
Expand Down
167 changes: 167 additions & 0 deletions tests/flex_wrap_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));

}
}

0 comments on commit b8b1073

Please sign in to comment.