Skip to content

Commit

Permalink
feat(flex): add support of set max height and set margin
Browse files Browse the repository at this point in the history
  • Loading branch information
meloalright committed Nov 13, 2024
1 parent 80e372d commit 1b6c76c
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 1 deletion.
4 changes: 3 additions & 1 deletion include/safe.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +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_min_width(std::unique_ptr<TaitankSafeNode> & node, double min_width);
void set_max_height(std::unique_ptr<TaitankSafeNode> & node, double max_height);
void set_margin(std::unique_ptr<TaitankSafeNode> & node, int css_direction, double value);
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);
Expand Down
22 changes: 22 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ pub enum FlexAlign {
FlexAlignSpaceEvenly = 8,
}

#[repr(i32)]
pub enum CSSDirection {
CSSLeft = 0,
CSSTop = 1,
CSSRight = 2,
CSSBottom = 3,
CSSStart = 4,
CSSEnd = 5,
CSSHorizontal = 6,
CSSVertical = 7,
CSSAll = 8,
CSSNone = -1,
}


pub fn node_create() -> TaitankSafeNode {
TaitankSafeNode {
Expand Down Expand Up @@ -86,6 +100,14 @@ pub fn set_min_width(node: &mut TaitankSafeNode, min_width: f64) {
ffi::set_min_width(&mut node.unique_ptr, min_width);
}

pub fn set_max_height(node: &mut TaitankSafeNode, max_height: f64) {
ffi::set_max_height(&mut node.unique_ptr, max_height);
}

pub fn set_margin(node: &mut TaitankSafeNode, css_direction: CSSDirection, value: f64) {
ffi::set_margin(&mut node.unique_ptr, css_direction as i32, value);
}

pub fn set_align_content(node: &mut TaitankSafeNode, flex_align: FlexAlign) {
ffi::set_align_content(&mut node.unique_ptr, flex_align as i32);
}
Expand Down
49 changes: 49 additions & 0 deletions src/safe.cc
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,55 @@ void set_min_width(std::unique_ptr<TaitankSafeNode> & node, double min_width) {
taitank::SetMinWidth(node->ptr, min_width);
}

void set_max_height(std::unique_ptr<TaitankSafeNode> & node, double max_height) {
taitank::SetMaxHeight(node->ptr, max_height);
}

void set_margin(std::unique_ptr<TaitankSafeNode> & node, int css_direction, double value) {
switch (css_direction) {
case 0: {
taitank::SetMargin(node->ptr, taitank::CSS_LEFT, value);
break;
}
case 1: {
taitank::SetMargin(node->ptr, taitank::CSS_TOP, value);
break;
}
case 2: {
taitank::SetMargin(node->ptr, taitank::CSS_RIGHT, value);
break;
}
case 3: {
taitank::SetMargin(node->ptr, taitank::CSS_BOTTOM, value);
break;
}
case 4: {
taitank::SetMargin(node->ptr, taitank::CSS_START, value);
break;
}
case 5: {
taitank::SetMargin(node->ptr, taitank::CSS_END, value);
break;
}
case 6: {
taitank::SetMargin(node->ptr, taitank::CSS_HORIZONTAL, value);
break;
}
case 7: {
taitank::SetMargin(node->ptr, taitank::CSS_VERTICAL, value);
break;
}
case 8: {
taitank::SetMargin(node->ptr, taitank::CSS_ALL, value);
break;
}
case -1: {
taitank::SetMargin(node->ptr, taitank::CSS_NONE, value);
break;
}
}
}

void set_align_content(std::unique_ptr<TaitankSafeNode> & node, int flex_align) {
switch (flex_align) {
case 0: {
Expand Down
2 changes: 2 additions & 0 deletions src/safe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ pub mod ffi {
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_max_height(node: &mut UniquePtr<TaitankSafeNode>, max_height: f64);
fn set_margin(node: &mut UniquePtr<TaitankSafeNode>, css_direction: i32, value: 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(
Expand Down
74 changes: 74 additions & 0 deletions tests/flex_wrap_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1309,4 +1309,78 @@ mod tests {
assert_eq!(get_height(&mut root_child0_child1), 80.0);
}

#[test]
fn wrapped_column_max_height() {
let mut root = node_create();
set_justify_content(&mut root, FlexAlign::FlexAlignCenter);
set_align_content(&mut root, FlexAlign::FlexAlignCenter);
set_align_items(&mut root, FlexAlign::FlexAlignStretch);
set_flex_wrap(&mut root, FlexWrapNode::FlexWrap);
set_width(&mut root, 700.0);
set_height(&mut root, 500.0);

let mut root_child0 = node_create();
set_width(&mut root_child0, 100.0);
set_height(&mut root_child0, 500.0);
set_max_height(&mut root_child0, 200.0);
insert_child(&mut root, &mut root_child0, 0);

let mut root_child1 = node_create();
/*set_margin(&mut root_child1, CssEdge::Left, 20.0);
set_margin(&mut root_child1, CssEdge::Top, 20.0);
set_margin(&mut root_child1, CssEdge::Right, 20.0);
set_margin(&mut root_child1, CssEdge::Bottom, 20.0);
set_width(&mut root_child1, 200.0);
set_height(&mut root_child1, 200.0);
insert_child(&mut root, &mut root_child1, 1);
let mut root_child2 = node_create();
set_width(&mut root_child2, 100.0);
set_height(&mut root_child2, 100.0);
insert_child(&mut root, &mut root_child2, 2);
layout!(&mut root);
assert_eq!(get_left(&mut root), 0.0);
assert_eq!(get_top(&mut root), 0.0);
assert_eq!(get_width(&mut root), 700.0);
assert_eq!(get_height(&mut root), 500.0);
assert_eq!(get_left(&mut root_child0), 250.0);
assert_eq!(get_top(&mut root_child0), 30.0);
assert_eq!(get_width(&mut root_child0), 100.0);
assert_eq!(get_height(&mut root_child0), 200.0);
assert_eq!(get_left(&mut root_child1), 200.0);
assert_eq!(get_top(&mut root_child1), 250.0);
assert_eq!(get_width(&mut root_child1), 200.0);
assert_eq!(get_height(&mut root_child1), 200.0);
assert_eq!(get_left(&mut root_child2), 420.0);
assert_eq!(get_top(&mut root_child2), 200.0);
assert_eq!(get_width(&mut root_child2), 100.0);
assert_eq!(get_height(&mut root_child2), 100.0);
layout!(&mut root, std::f64::NAN, std::f64::NAN, Direction::RTL);
assert_eq!(get_left(&mut root), 0.0);
assert_eq!(get_top(&mut root), 0.0);
assert_eq!(get_width(&mut root), 700.0);
assert_eq!(get_height(&mut root), 500.0);
assert_eq!(get_left(&mut root_child0), 350.0);
assert_eq!(get_top(&mut root_child0), 30.0);
assert_eq!(get_width(&mut root_child0), 100.0);
assert_eq!(get_height(&mut root_child0), 200.0);
assert_eq!(get_left(&mut root_child1), 300.0);
assert_eq!(get_top(&mut root_child1), 250.0);
assert_eq!(get_width(&mut root_child1), 200.0);
assert_eq!(get_height(&mut root_child1), 200.0);
assert_eq!(get_left(&mut root_child2), 180.0);
assert_eq!(get_top(&mut root_child2), 200.0);
assert_eq!(get_width(&mut root_child2), 100.0);
assert_eq!(get_height(&mut root_child2), 100.0);*/
}
}

0 comments on commit 1b6c76c

Please sign in to comment.