Skip to content

Commit

Permalink
feat(flex): add support of set min width and set align content and se…
Browse files Browse the repository at this point in the history
…t justify content
  • Loading branch information
meloalright committed Nov 13, 2024
1 parent b8b1073 commit 2aea11f
Show file tree
Hide file tree
Showing 5 changed files with 243 additions and 10 deletions.
13 changes: 3 additions & 10 deletions include/safe.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,6 @@ class TaitankSafeNode {
public:
TaitankSafeNode();
~TaitankSafeNode();
// void set_width(double width) const;
// void set_height(double height) const;
//
// void set_direction(int direction) const;
//
// void do_layout(double parent_width, double parent_height) const;
// double get_left() const;
// double get_top() const;
// double get_width() const;
// double get_height() const;
public:
taitank::TaitankNodeRef ptr;
};
Expand All @@ -31,6 +21,9 @@ 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 set_min_width(std::unique_ptr<TaitankSafeNode> & node, double width);
void set_align_content(std::unique_ptr<TaitankSafeNode> & node, int align_content);
void set_justify_content(std::unique_ptr<TaitankSafeNode> & node, int justify_content);
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
12 changes: 12 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ 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 set_min_width(node: &mut TaitankSafeNode, min_width: f64) {
ffi::set_min_width(&mut node.unique_ptr, min_width);
}

pub fn set_align_content(node: &mut TaitankSafeNode, flex_align: FlexAlign) {
ffi::set_align_content(&mut node.unique_ptr, flex_align as i32);
}

pub fn set_justify_content(node: &mut TaitankSafeNode, flex_align: FlexAlign) {
ffi::set_justify_content(&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
86 changes: 86 additions & 0 deletions src/safe.cc
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,92 @@ void do_layout(std::unique_ptr<TaitankSafeNode> & node, double parent_width, dou
}
}

void set_min_width(std::unique_ptr<TaitankSafeNode> & node, double min_width) {
taitank::SetMinWidth(node->ptr, min_width);
}

void set_align_content(std::unique_ptr<TaitankSafeNode> & node, int flex_align) {
switch (flex_align) {
case 0: {
taitank::SetAlignContent(node->ptr, taitank::FlexAlign::FLEX_ALIGN_AUTO);
break;
}
case 1: {
taitank::SetAlignContent(node->ptr, taitank::FlexAlign::FLEX_ALIGN_START);
break;
}
case 2: {
taitank::SetAlignContent(node->ptr, taitank::FlexAlign::FLEX_ALIGN_CENTER);
break;
}
case 3: {
taitank::SetAlignContent(node->ptr, taitank::FlexAlign::FLEX_ALIGN_END);
break;
}
case 4: {
taitank::SetAlignContent(node->ptr, taitank::FlexAlign::FLEX_ALIGN_STRETCH);
break;
}
case 5: {
taitank::SetAlignContent(node->ptr, taitank::FlexAlign::FLEX_ALIGN_BASE_LINE);
break;
}
case 6: {
taitank::SetAlignContent(node->ptr, taitank::FlexAlign::FLEX_ALIGN_SPACE_BETWEEN);
break;
}
case 7: {
taitank::SetAlignContent(node->ptr, taitank::FlexAlign::FLEX_ALIGN_SPACE_AROUND);
break;
}
case 8: {
taitank::SetAlignContent(node->ptr, taitank::FlexAlign::FLEX_ALIGN_SPACE_EVENLY);
break;
}
}
}

void set_justify_content(std::unique_ptr<TaitankSafeNode> & node, int justify) {
switch (justify) {
case 0: {
taitank::SetJustifyContent(node->ptr, taitank::FlexAlign::FLEX_ALIGN_AUTO);
break;
}
case 1: {
taitank::SetJustifyContent(node->ptr, taitank::FlexAlign::FLEX_ALIGN_START);
break;
}
case 2: {
taitank::SetJustifyContent(node->ptr, taitank::FlexAlign::FLEX_ALIGN_CENTER);
break;
}
case 3: {
taitank::SetJustifyContent(node->ptr, taitank::FlexAlign::FLEX_ALIGN_END);
break;
}
case 4: {
taitank::SetJustifyContent(node->ptr, taitank::FlexAlign::FLEX_ALIGN_STRETCH);
break;
}
case 5: {
taitank::SetJustifyContent(node->ptr, taitank::FlexAlign::FLEX_ALIGN_BASE_LINE);
break;
}
case 6: {
taitank::SetJustifyContent(node->ptr, taitank::FlexAlign::FLEX_ALIGN_SPACE_BETWEEN);
break;
}
case 7: {
taitank::SetJustifyContent(node->ptr, taitank::FlexAlign::FLEX_ALIGN_SPACE_AROUND);
break;
}
case 8: {
taitank::SetJustifyContent(node->ptr, taitank::FlexAlign::FLEX_ALIGN_SPACE_EVENLY);
break;
}
}
}

double get_width(std::unique_ptr<TaitankSafeNode> & node) {
return taitank::GetWidth(node->ptr);
}
Expand Down
3 changes: 3 additions & 0 deletions src/safe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ pub mod ffi {
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 set_min_width(node: &mut UniquePtr<TaitankSafeNode>, min_width: f64);
fn set_align_content(node: &mut UniquePtr<TaitankSafeNode>, flex_align: i32);
fn set_justify_content(node: &mut UniquePtr<TaitankSafeNode>, flex_align: i32);
fn insert_child(
node: &mut UniquePtr<TaitankSafeNode>,
child: &mut UniquePtr<TaitankSafeNode>,
Expand Down
139 changes: 139 additions & 0 deletions tests/flex_wrap_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,4 +251,143 @@ mod tests {
assert_eq!(30.0, get_height(&mut root_child3));

}

#[test]
fn wrap_row_align_items_center() {
let mut root = node_create();
set_flex_direction(&mut root, FlexDirection::FlexDirectionRow);
set_align_items(&mut root, FlexAlign::FlexAlignCenter);
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!(10.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!(5.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!(10.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!(5.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));

}

#[test]
fn flex_wrap_children_with_min_main_overriding_flex_basis() {
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_flex_basis(&mut root_child0, 50.0);
set_min_width(&mut root_child0, 55.0);
set_height(&mut root_child0, 50.0);
insert_child(&mut root, &mut root_child0, 0);

let mut root_child1 = node_create();
set_flex_basis(&mut root_child0, 50.0);
set_min_width(&mut root_child1, 55.0);
set_height(&mut root_child1, 50.0);
insert_child(&mut root, &mut root_child1, 1);

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!(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!(55.0, get_width(&mut root_child0));
assert_eq!(50.0, get_height(&mut root_child0));

assert_eq!(0.0, get_left(&mut root_child1));
assert_eq!(50.0, get_top(&mut root_child1));
assert_eq!(55.0, get_width(&mut root_child1));
assert_eq!(50.0, get_height(&mut root_child1));

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!(100.0, get_height(&mut root));

assert_eq!(45.0, get_left(&mut root_child0));
assert_eq!(0.0, get_top(&mut root_child0));
assert_eq!(55.0, get_width(&mut root_child0));
assert_eq!(50.0, get_height(&mut root_child0));

assert_eq!(45.0, get_left(&mut root_child1));
assert_eq!(50.0, get_top(&mut root_child1));
assert_eq!(55.0, get_width(&mut root_child1));
assert_eq!(50.0, get_height(&mut root_child1));

}
}

0 comments on commit 2aea11f

Please sign in to comment.