Skip to content

Commit

Permalink
feat(flex): support the flex_direction set and pass the cases
Browse files Browse the repository at this point in the history
  • Loading branch information
meloalright committed Nov 11, 2024
1 parent 8f8f663 commit 547893c
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 14 deletions.
5 changes: 3 additions & 2 deletions include/safe.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once
#include <memory>
#include "taitank-safe/include/taitank/src/taitank.h"
#include "./taitank/src/taitank.h"

class TaitankSafeNode {
public:
Expand Down Expand Up @@ -28,8 +28,9 @@ void set_flex(const std::unique_ptr<TaitankSafeNode> & node, double flex);
void set_flex_grow(const std::unique_ptr<TaitankSafeNode> & node, double flex_grow);
void set_flex_shrink(const std::unique_ptr<TaitankSafeNode> & node, double flex_shrink);
void set_flex_basis(const std::unique_ptr<TaitankSafeNode> & node, double flex_basis);
void set_flex_direction(const std::unique_ptr<TaitankSafeNode> & node, int direction);
void insert_child(const std::unique_ptr<TaitankSafeNode> & node, const std::unique_ptr<TaitankSafeNode> & child, int index);
void do_layout(const std::unique_ptr<TaitankSafeNode> & node, double parent_width, double parent_height);
void do_layout(const std::unique_ptr<TaitankSafeNode> & node, double parent_width, double parent_height, int direction);
double get_width(const std::unique_ptr<TaitankSafeNode> & node);
double get_height(const std::unique_ptr<TaitankSafeNode> & node);
double get_left(const std::unique_ptr<TaitankSafeNode> & node);
Expand Down
71 changes: 66 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ pub enum Direction {
RTL = 2,
}

#[repr(i32)]
pub enum FlexDirection {
FlexDirectionRow,
FlexDirectionRowReverse,
FlexDirectionColumn,
FlexDirectionColumnReverse,
}


pub fn node_create() -> TaitankSafeNode {
TaitankSafeNode {
Expand Down Expand Up @@ -42,11 +50,14 @@ pub fn set_flex_shrink(node: &TaitankSafeNode, flex_shrink: f64) {
pub fn set_flex_basis(node: &TaitankSafeNode, flex_basis: f64) {
ffi::set_flex_basis(&node.unique_ptr, flex_basis);
}
pub fn set_flex_direction(node: &TaitankSafeNode, flex_direction: FlexDirection) {
ffi::set_flex_direction(&node.unique_ptr, flex_direction as i32);
}
pub fn insert_child(node: &TaitankSafeNode, child: &TaitankSafeNode, index: i32) {
ffi::insert_child(&node.unique_ptr, &child.unique_ptr, index);
}
pub fn do_layout(node: &TaitankSafeNode, parent_width: f64, parent_height: f64) {
ffi::do_layout(&node.unique_ptr, parent_width, parent_height);
pub fn do_layout(node: &TaitankSafeNode, parent_width: f64, parent_height: f64, direction: Direction) {
ffi::do_layout(&node.unique_ptr, parent_width, parent_height, direction as i32);
}
pub fn get_width(node: &TaitankSafeNode) -> f64 {
ffi::get_width(&node.unique_ptr)
Expand All @@ -71,7 +82,7 @@ mod tests {
set_width(&node, 100.0);
set_height(&node, 100.0);
set_direction(&node, Direction::LTR);
do_layout(&node, std::f64::NAN, std::f64::NAN);
do_layout(&node, std::f64::NAN, std::f64::NAN, Direction::LTR);

assert_eq!(get_left(&node), 0.0);
assert_eq!(get_top(&node), 0.0);
Expand All @@ -92,7 +103,7 @@ mod tests {
let root_child1 = node_create();
set_flex_grow(&root_child1, 1.0);
insert_child(&root, &root_child1, 1);
do_layout(&root, std::f64::NAN, std::f64::NAN);
do_layout(&root, std::f64::NAN, std::f64::NAN, Direction::LTR);

assert_eq!(0.0, get_left(&root));
assert_eq!(0.0, get_top(&root));
Expand All @@ -109,7 +120,7 @@ mod tests {
assert_eq!(100.0, get_width(&root_child1));
assert_eq!(25.0, get_height(&root_child1));

do_layout(&root, std::f64::NAN, std::f64::NAN);
do_layout(&root, std::f64::NAN, std::f64::NAN, Direction::LTR);

assert_eq!(0.0, get_left(&root));
assert_eq!(0.0, get_top(&root));
Expand All @@ -127,4 +138,54 @@ mod tests {
assert_eq!(25.0, get_height(&root_child1));

}

#[test]
fn flex_basis_flex_grow_row() {
let root = node_create();
set_flex_direction(&root, FlexDirection::FlexDirectionRow);
set_width(&root, 100.0);
set_height(&root, 100.0);
let root_child0 = node_create();
set_flex_grow(&root_child0, 1.0);
set_flex_basis(&root_child0, 50.0);
insert_child(&root, &root_child0, 0);

let root_child1 = node_create();
set_flex_grow(&root_child1, 1.0);
insert_child(&root, &root_child1, 1);
do_layout(&root, std::f64::NAN, std::f64::NAN, Direction::LTR);

assert_eq!(0.0, get_left(&root));
assert_eq!(0.0, get_top(&root));
assert_eq!(100.0, get_width(&root));
assert_eq!(100.0, get_height(&root));

assert_eq!(0.0, get_left(&root_child0));
assert_eq!(0.0, get_top(&root_child0));
assert_eq!(75.0, get_width(&root_child0));
assert_eq!(100.0, get_height(&root_child0));

assert_eq!(75.0, get_left(&root_child1));
assert_eq!(0.0, get_top(&root_child1));
assert_eq!(25.0, get_width(&root_child1));
assert_eq!(100.0, get_height(&root_child1));

do_layout(&root, std::f64::NAN, std::f64::NAN, Direction::RTL);

assert_eq!(0.0, get_left(&root));
assert_eq!(0.0, get_top(&root));
assert_eq!(100.0, get_width(&root));
assert_eq!(100.0, get_height(&root));

assert_eq!(25.0, get_left(&root_child0));
assert_eq!(0.0, get_top(&root_child0));
assert_eq!(75.0, get_width(&root_child0));
assert_eq!(100.0, get_height(&root_child0));

assert_eq!(0.0, get_left(&root_child1));
assert_eq!(0.0, get_top(&root_child1));
assert_eq!(25.0, get_width(&root_child1));
assert_eq!(100.0, get_height(&root_child1));

}
}
46 changes: 40 additions & 6 deletions src/safe.cc
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include <iostream>
#include "taitank-safe/include/safe.h"
#include "taitank-safe/include/taitank/src/taitank_util.h"
#include "taitank-safe/include/taitank/src/taitank_node.h"
#include "taitank-safe/include/taitank/src/taitank.h"
#include "../include/safe.h"
#include "../include/taitank/src/taitank_util.h"
#include "../include/taitank/src/taitank_node.h"
#include "../include/taitank/src/taitank.h"

TaitankSafeNode::TaitankSafeNode() {
ptr = new taitank::TaitankNode();
Expand Down Expand Up @@ -57,12 +57,46 @@ void set_flex_basis(const std::unique_ptr<TaitankSafeNode> & node, double flex_b
taitank::SetFlexBasis(node->ptr, flex_basis);
}

void set_flex_direction(const std::unique_ptr<TaitankSafeNode> & node, int flex_direction) {
switch (flex_direction) {
case 0: {
taitank::SetFlexDirection(node->ptr, taitank::FlexDirection::FLEX_DIRECTION_ROW);
break;
}
case 1: {
taitank::SetFlexDirection(node->ptr, taitank::FlexDirection::FLEX_DIRECTION_ROW_REVERSE);
break;
}
case 2: {
taitank::SetFlexDirection(node->ptr, taitank::FlexDirection::FLEX_DIRECTION_COLUMN);
break;
}
case 3: {
taitank::SetFlexDirection(node->ptr, taitank::FlexDirection::FLEX_DIRECTION_COLUNM_REVERSE);
break;
}
}
}

void insert_child(const std::unique_ptr<TaitankSafeNode> & node, const std::unique_ptr<TaitankSafeNode> & child, int index) {
taitank::InsertChild(node->ptr, child->ptr, index);
}

void do_layout(const std::unique_ptr<TaitankSafeNode> & node, double parent_width, double parent_height) {
taitank::DoLayout(node->ptr, parent_width, parent_height);
void do_layout(const std::unique_ptr<TaitankSafeNode> & node, double parent_width, double parent_height, int direction) {
switch (direction) {
case 0: {
taitank::DoLayout(node->ptr, parent_width, parent_height, taitank::TaitankDirection::DIRECTION_INHERIT);
break;
}
case 1: {
taitank::DoLayout(node->ptr, parent_width, parent_height, taitank::TaitankDirection::DIRECTION_LTR);
break;
}
case 2: {
taitank::DoLayout(node->ptr, parent_width, parent_height, taitank::TaitankDirection::DIRECTION_RTL);
break;
}
}
}

double get_width(const std::unique_ptr<TaitankSafeNode> & node) {
Expand Down
3 changes: 2 additions & 1 deletion src/safe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ pub mod ffi {
fn set_flex_grow(node: &UniquePtr<TaitankSafeNode>, flex_grow: f64);
fn set_flex_shrink(node: &UniquePtr<TaitankSafeNode>, flex_shrink: f64);
fn set_flex_basis(node: &UniquePtr<TaitankSafeNode>, flex_basis: f64);
fn set_flex_direction(node: &UniquePtr<TaitankSafeNode>, flex_direction: i32);
fn insert_child(node: &UniquePtr<TaitankSafeNode>, child: &UniquePtr<TaitankSafeNode>, index: i32);
fn do_layout(node: &UniquePtr<TaitankSafeNode>, parent_width: f64, parent_height: f64);
fn do_layout(node: &UniquePtr<TaitankSafeNode>, parent_width: f64, parent_height: f64, direction: i32);
fn get_width(node: &UniquePtr<TaitankSafeNode>) -> f64;
fn get_height(node: &UniquePtr<TaitankSafeNode>) -> f64;
fn get_left(node: &UniquePtr<TaitankSafeNode>) -> f64;
Expand Down

0 comments on commit 547893c

Please sign in to comment.