Skip to content

Commit

Permalink
feat(dev): make cast direction work ok
Browse files Browse the repository at this point in the history
  • Loading branch information
meloalright committed Nov 9, 2024
1 parent a5a4a16 commit ca2f601
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 10 deletions.
3 changes: 3 additions & 0 deletions include/taitank_safe.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ class 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;
Expand Down
50 changes: 40 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use cxx::UniquePtr;

#[cxx::bridge]
pub mod ffi {
mod ffi {

unsafe extern "C++" {
include!("taitank-safe/include/taitank_safe.h");
Expand All @@ -8,6 +10,7 @@ pub mod ffi {
fn node_create() -> UniquePtr<TaitankSafeNode>;
fn set_width(self: &TaitankSafeNode, width: f64);
fn set_height(self: &TaitankSafeNode, height: f64);
fn set_direction(self: &TaitankSafeNode, direction: i32);
fn do_layout(self: &TaitankSafeNode, parent_width: f64, parent_height: f64);
fn get_left(self: &TaitankSafeNode) -> f64;
fn get_top(self: &TaitankSafeNode) -> f64;
Expand All @@ -16,20 +19,47 @@ pub mod ffi {
}
}

struct TaitankSafeNode {
unique_ptr: UniquePtr<ffi::TaitankSafeNode>
}


#[repr(i32)]
pub enum Direction {
Inherit = 0,
LTR = 1,
RTL = 2,
}


pub fn node_create() -> TaitankSafeNode {
TaitankSafeNode {
unique_ptr: ffi::node_create()
}
}
pub fn set_direction(node: &mut TaitankSafeNode, direction: Direction) {
match direction {
x => {
node.unique_ptr.set_direction(x as i32);
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_works() {
let node = ffi::node_create();
node.set_width(100.0);
node.set_height(100.0);
node.do_layout(std::f64::NAN, std::f64::NAN);

assert_eq!(node.get_left(), 0.0);
assert_eq!(node.get_top(), 0.0);
assert_eq!(node.get_width(), 100.0);
assert_eq!(node.get_height(), 100.0);
let mut node = node_create();
node.unique_ptr.set_width(100.0);
node.unique_ptr.set_height(100.0);
set_direction(&mut node, Direction::LTR);
node.unique_ptr.do_layout(std::f64::NAN, std::f64::NAN);

assert_eq!(node.unique_ptr.get_left(), 0.0);
assert_eq!(node.unique_ptr.get_top(), 0.0);
assert_eq!(node.unique_ptr.get_width(), 100.0);
assert_eq!(node.unique_ptr.get_height(), 100.0);
}
}
17 changes: 17 additions & 0 deletions src/taitank_safe.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@ void TaitankSafeNode::set_height(double height) const {
taitank::SetHeight(ptr, height);
}

void TaitankSafeNode::set_direction(int direction) const {
switch (direction) {
case 0: {
taitank::SetDirection(ptr, taitank::TaitankDirection::DIRECTION_INHERIT);
break;
}
case 1: {
taitank::SetDirection(ptr, taitank::TaitankDirection::DIRECTION_LTR);
break;
}
case 2: {
taitank::SetDirection(ptr, taitank::TaitankDirection::DIRECTION_RTL);
break;
}
}
}

void TaitankSafeNode::do_layout(double parent_width, double parent_height) const {
taitank::DoLayout(ptr, parent_width, parent_height);
}
Expand Down

0 comments on commit ca2f601

Please sign in to comment.