Skip to content

Commit

Permalink
feat(flex): add support of set flex wrap and test case walk through
Browse files Browse the repository at this point in the history
  • Loading branch information
meloalright committed Nov 13, 2024
1 parent 2293ff6 commit 8759e92
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 6 deletions.
1 change: 1 addition & 0 deletions include/safe.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ void set_flex_grow(std::unique_ptr<TaitankSafeNode> & node, double flex_grow);
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 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: 12 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ pub struct TaitankSafeNode {
unique_ptr: UniquePtr<ffi::TaitankSafeNode>,
}

// impl Drop for TaitankSafeNode {
// fn drop(&mut self) {
// ffi::
// }
// }

#[repr(i32)]
pub enum Direction {
Inherit = 0,
Expand All @@ -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(),
Expand Down Expand Up @@ -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);
}
Expand Down
18 changes: 18 additions & 0 deletions src/safe.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,24 @@ void set_flex_direction(std::unique_ptr<TaitankSafeNode> & node, int flex_direct
}
}

void set_flex_wrap(std::unique_ptr<TaitankSafeNode> & 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<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 @@ -14,6 +14,7 @@ pub mod ffi {
fn set_flex_shrink(node: &mut UniquePtr<TaitankSafeNode>, flex_shrink: f64);
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 insert_child(
node: &mut UniquePtr<TaitankSafeNode>,
child: &mut UniquePtr<TaitankSafeNode>,
Expand Down
87 changes: 87 additions & 0 deletions tests/flex_wrap_test.rs
Original file line number Diff line number Diff line change
@@ -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));

}
}

0 comments on commit 8759e92

Please sign in to comment.