Skip to main content

taitank_safe/
lib.rs

1pub mod macros;
2mod safe;
3
4use cxx::UniquePtr;
5use safe::ffi;
6use std::fmt::{Debug, Formatter, Result};
7
8pub struct TaitankSafeNode {
9    unique_ptr: UniquePtr<ffi::TaitankSafeNode>,
10}
11
12impl Debug for TaitankSafeNode {
13    fn fmt(&self, f: &mut Formatter) -> Result {
14        Ok(ffi::print(&self.unique_ptr))
15    }
16}
17
18unsafe impl Send for TaitankSafeNode {}
19unsafe impl Sync for TaitankSafeNode {}
20
21
22#[repr(i32)]
23pub enum Direction {
24    Inherit = 0,
25    LTR = 1,
26    RTL = 2,
27}
28
29#[repr(i32)]
30pub enum FlexDirection {
31    FlexDirectionRow = 0,
32    FlexDirectionRowReverse = 1,
33    FlexDirectionColumn = 2,
34    FlexDirectionColumnReverse = 3,
35}
36
37#[repr(i32)]
38pub enum FlexWrapNode {
39    FlexNoWrap = 0,
40    FlexWrap = 1,
41    FlexWrapReverse = 2,
42}
43
44#[repr(i32)]
45pub enum FlexAlign {
46    FlexAlignAuto = 0,
47    FlexAlignStart = 1,
48    FlexAlignCenter = 2,
49    FlexAlignEnd = 3,
50    FlexAlignStretch = 4,
51    FlexAlignBaseLine = 5,
52    FlexAlignSpaceBetween = 6,
53    FlexAlignSpaceAround = 7,
54    FlexAlignSpaceEvenly = 8,
55}
56
57#[repr(i32)]
58pub enum CSSDirection {
59    CSSLeft = 0,
60    CSSTop = 1,
61    CSSRight = 2,
62    CSSBottom = 3,
63    CSSStart = 4,
64    CSSEnd = 5,
65    CSSHorizontal = 6,
66    CSSVertical = 7,
67    CSSAll = 8,
68    CSSNone = -1,
69}
70
71
72/// Create and get a TaitankSafeNode.
73///
74/// # Examples
75///
76/// ```
77/// let root = taitank_safe::node_create();
78/// println!("{:?}", root);
79/// ```
80pub fn node_create() -> TaitankSafeNode {
81    TaitankSafeNode {
82        unique_ptr: ffi::node_create(),
83    }
84}
85pub fn set_width(node: &mut TaitankSafeNode, width: f64) {
86    ffi::set_width(&mut node.unique_ptr, width);
87}
88pub fn set_height(node: &mut TaitankSafeNode, height: f64) {
89    ffi::set_height(&mut node.unique_ptr, height);
90}
91pub fn set_direction(node: &mut TaitankSafeNode, direction: Direction) {
92    ffi::set_direction(&mut node.unique_ptr, direction as i32);
93}
94pub fn set_flex(node: &mut TaitankSafeNode, flex: f64) {
95    ffi::set_flex(&mut node.unique_ptr, flex);
96}
97pub fn set_flex_grow(node: &mut TaitankSafeNode, flex_grow: f64) {
98    ffi::set_flex_grow(&mut node.unique_ptr, flex_grow);
99}
100pub fn set_flex_shrink(node: &mut TaitankSafeNode, flex_shrink: f64) {
101    ffi::set_flex_shrink(&mut node.unique_ptr, flex_shrink);
102}
103pub fn set_flex_basis(node: &mut TaitankSafeNode, flex_basis: f64) {
104    ffi::set_flex_basis(&mut node.unique_ptr, flex_basis);
105}
106pub fn set_flex_direction(node: &mut TaitankSafeNode, flex_direction: FlexDirection) {
107    ffi::set_flex_direction(&mut node.unique_ptr, flex_direction as i32);
108}
109
110pub fn set_flex_wrap(node: &mut TaitankSafeNode, flex_wrap_node: FlexWrapNode) {
111    ffi::set_flex_wrap(&mut node.unique_ptr, flex_wrap_node as i32);
112}
113
114pub fn set_align_items(node: &mut TaitankSafeNode, flex_align: FlexAlign) {
115    ffi::set_align_items(&mut node.unique_ptr, flex_align as i32);
116}
117
118pub fn set_min_width(node: &mut TaitankSafeNode, min_width: f64) {
119    ffi::set_min_width(&mut node.unique_ptr, min_width);
120}
121
122pub fn set_max_height(node: &mut TaitankSafeNode, max_height: f64) {
123    ffi::set_max_height(&mut node.unique_ptr, max_height);
124}
125
126pub fn set_margin(node: &mut TaitankSafeNode, css_direction: CSSDirection, value: f64) {
127    ffi::set_margin(&mut node.unique_ptr, css_direction as i32, value);
128}
129
130pub fn set_align_content(node: &mut TaitankSafeNode, flex_align: FlexAlign) {
131    ffi::set_align_content(&mut node.unique_ptr, flex_align as i32);
132}
133
134pub fn set_justify_content(node: &mut TaitankSafeNode, flex_align: FlexAlign) {
135    ffi::set_justify_content(&mut node.unique_ptr, flex_align as i32);
136}
137
138pub fn insert_child(node: &mut TaitankSafeNode, child: &mut TaitankSafeNode, index: i32) {
139    ffi::insert_child(&mut node.unique_ptr, &mut child.unique_ptr, index);
140}
141
142/// Layout the node.
143///
144/// # Examples
145///
146/// ```
147/// let mut root = taitank_safe::node_create();
148/// taitank_safe::set_width(&mut root, 200.0);
149/// taitank_safe::do_layout(&mut root, std::f64::NAN, std::f64::NAN, taitank_safe::Direction::LTR);
150/// ```
151pub fn do_layout(
152    node: &mut TaitankSafeNode,
153    parent_width: f64,
154    parent_height: f64,
155    direction: Direction,
156) {
157    ffi::do_layout(
158        &mut node.unique_ptr,
159        parent_width,
160        parent_height,
161        direction as i32,
162    );
163}
164pub fn get_width(node: &TaitankSafeNode) -> f64 {
165    ffi::get_width(&node.unique_ptr)
166}
167pub fn get_height(node: &TaitankSafeNode) -> f64 {
168    ffi::get_height(&node.unique_ptr)
169}
170pub fn get_left(node: &TaitankSafeNode) -> f64 {
171    ffi::get_left(&node.unique_ptr)
172}
173pub fn get_top(node: &TaitankSafeNode) -> f64 {
174    ffi::get_top(&node.unique_ptr)
175}