Skip to content

Commit fcee70c

Browse files
committed
Add TaffyTree_GetLayout method
1 parent 497d66e commit fcee70c

File tree

3 files changed

+103
-34
lines changed

3 files changed

+103
-34
lines changed

ctaffy/include/taffy.h

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -311,23 +311,35 @@ typedef struct TaffyGridPlacementResult {
311311

312312
typedef struct TaffyTree *TaffyTreeOwnedRef;
313313

314+
typedef struct TaffyTree *TaffyTreeMutRef;
315+
314316
typedef struct TaffyNodeId {
315317
uint64_t _0;
316318
} TaffyNodeId;
317319

320+
typedef const struct TaffyTree *TaffyTreeConstRef;
321+
318322
typedef struct TaffyNodeIdResult {
319323
enum TaffyReturnCode return_code;
320324
struct TaffyNodeId value;
321325
} TaffyNodeIdResult;
322326

323-
typedef struct TaffyTree *TaffyTreeMutRef;
324-
325327
typedef struct TaffyStyleMutRefResult {
326328
enum TaffyReturnCode return_code;
327329
TaffyStyleMutRef value;
328330
} TaffyStyleMutRefResult;
329331

330-
typedef const struct TaffyTree *TaffyTreeConstRef;
332+
typedef struct TaffyLayout {
333+
float x;
334+
float y;
335+
float width;
336+
float height;
337+
} TaffyLayout;
338+
339+
typedef struct TaffyResult_TaffyLayout {
340+
enum TaffyReturnCode return_code;
341+
struct TaffyLayout value;
342+
} TaffyResult_TaffyLayout;
331343

332344
#ifdef __cplusplus
333345
extern "C" {
@@ -510,12 +522,27 @@ struct TaffyGridPlacementResult TaffyStyle_GetGridColumn(TaffyStyleMutRef raw_st
510522
// Set grid item's column placement
511523
enum TaffyReturnCode TaffyStyle_SetGridColumn(TaffyStyleMutRef raw_style, struct TaffyGridPlacement placement);
512524

525+
// Get grid item's row placement
526+
struct TaffyGridPlacementResult TaffyStyle_GetGridRow(TaffyStyleMutRef raw_style);
527+
528+
// Set grid item's row placement
529+
enum TaffyReturnCode TaffyStyle_SetGridRow(TaffyStyleMutRef raw_style, struct TaffyGridPlacement placement);
530+
513531
// Create a TaffyTree instance
514532
TaffyTreeOwnedRef TaffyTree_New(void);
515533

516534
// Free a TaffyTree instance
517535
enum TaffyReturnCode TaffyTree_Free(TaffyTreeOwnedRef raw_tree);
518536

537+
// Create a new Node in the TaffyTree. Returns a NodeId handle to the node.
538+
enum TaffyReturnCode TaffyTree_ComputeLayout(TaffyTreeMutRef raw_tree,
539+
struct TaffyNodeId node_id,
540+
float available_width,
541+
float available_height);
542+
543+
// Create a new Node in the TaffyTree. Returns a NodeId handle to the node.
544+
enum TaffyReturnCode TaffyTree_PrintTree(TaffyTreeConstRef raw_tree, struct TaffyNodeId node_id);
545+
519546
// Create a new Node in the TaffyTree. Returns a NodeId handle to the node.
520547
struct TaffyNodeIdResult TaffyTree_NewNode(TaffyTreeMutRef raw_tree);
521548

@@ -528,16 +555,10 @@ enum TaffyReturnCode TaffyTree_AppendChild(TaffyTreeMutRef raw_tree,
528555
struct TaffyNodeId child_node_id);
529556

530557
// Create a new Node in the TaffyTree. Returns a NodeId handle to the node.
531-
struct TaffyStyleMutRefResult TaffyTree_GetStyleMutRef(TaffyTreeMutRef raw_tree, struct TaffyNodeId node_id);
558+
struct TaffyStyleMutRefResult TaffyTree_GetStyleMut(TaffyTreeMutRef raw_tree, struct TaffyNodeId node_id);
532559

533560
// Create a new Node in the TaffyTree. Returns a NodeId handle to the node.
534-
enum TaffyReturnCode TaffyTree_ComputeLayout(TaffyTreeMutRef raw_tree,
535-
struct TaffyNodeId node_id,
536-
float available_width,
537-
float available_height);
538-
539-
// Create a new Node in the TaffyTree. Returns a NodeId handle to the node.
540-
enum TaffyReturnCode TaffyTree_PrintTree(TaffyTreeConstRef raw_tree, struct TaffyNodeId node_id);
561+
struct TaffyResult_TaffyLayout TaffyTree_GetLayout(TaffyTreeConstRef raw_tree, struct TaffyNodeId node_id);
541562

542563
#ifdef __cplusplus
543564
} // extern "C"

ctaffy/src/tree.rs

Lines changed: 58 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::{
2-
bail, bail_if_null, ok, try_or, TaffyFFIDefault, TaffyFFIResult, TaffyResult, TaffyReturnCode, TaffyStyleMutRef,
2+
bail, bail_if_null, ok, try_or, TaffyFFIDefault, TaffyFFIResult, TaffyLayout, TaffyResult, TaffyReturnCode,
3+
TaffyStyleMutRef,
34
};
45
use taffy::prelude as core;
56
use taffy::Taffy as CoreTaffy;
@@ -55,6 +56,10 @@ fn available_space_from_f32(input: f32) -> core::AvailableSpace {
5556
}
5657
}
5758

59+
// -------------------------------------------------
60+
// Create and Free
61+
// -------------------------------------------------
62+
5863
/// Create a TaffyTree instance
5964
#[no_mangle]
6065
#[allow(clippy::missing_safety_doc)]
@@ -71,6 +76,43 @@ pub unsafe extern "C" fn TaffyTree_Free(raw_tree: TaffyTreeOwnedRef) -> TaffyRet
7176
TaffyReturnCode::Ok
7277
}
7378

79+
// -------------------------------------------------
80+
// Compute and Print
81+
// -------------------------------------------------
82+
83+
/// Create a new Node in the TaffyTree. Returns a NodeId handle to the node.
84+
#[no_mangle]
85+
#[allow(clippy::missing_safety_doc)]
86+
pub unsafe extern "C" fn TaffyTree_ComputeLayout(
87+
raw_tree: TaffyTreeMutRef,
88+
node_id: TaffyNodeId,
89+
available_width: f32,
90+
available_height: f32,
91+
) -> TaffyReturnCode {
92+
with_tree_mut!(raw_tree, tree, {
93+
let available_space = core::Size {
94+
width: available_space_from_f32(available_width),
95+
height: available_space_from_f32(available_height),
96+
};
97+
try_or!(InvalidNodeId, tree.inner.compute_layout(node_id.into(), available_space));
98+
TaffyReturnCode::Ok
99+
})
100+
}
101+
102+
/// Create a new Node in the TaffyTree. Returns a NodeId handle to the node.
103+
#[no_mangle]
104+
#[allow(clippy::missing_safety_doc)]
105+
pub unsafe extern "C" fn TaffyTree_PrintTree(raw_tree: TaffyTreeConstRef, node_id: TaffyNodeId) -> TaffyReturnCode {
106+
with_tree!(raw_tree, tree, {
107+
taffy::util::print_tree(&tree.inner, node_id.into());
108+
TaffyReturnCode::Ok
109+
})
110+
}
111+
112+
// -------------------------------------------------
113+
// Tree manipulation
114+
// -------------------------------------------------
115+
74116
/// Create a new Node in the TaffyTree. Returns a NodeId handle to the node.
75117
#[no_mangle]
76118
#[allow(clippy::missing_safety_doc)]
@@ -106,10 +148,14 @@ pub unsafe extern "C" fn TaffyTree_AppendChild(
106148
})
107149
}
108150

151+
// -------------------------------------------------
152+
// Style and Layout access
153+
// -------------------------------------------------
154+
109155
/// Create a new Node in the TaffyTree. Returns a NodeId handle to the node.
110156
#[no_mangle]
111157
#[allow(clippy::missing_safety_doc)]
112-
pub unsafe extern "C" fn TaffyTree_GetStyleMutRef(
158+
pub unsafe extern "C" fn TaffyTree_GetStyleMut(
113159
raw_tree: TaffyTreeMutRef,
114160
node_id: TaffyNodeId,
115161
) -> TaffyResult<TaffyStyleMutRef> {
@@ -122,28 +168,17 @@ pub unsafe extern "C" fn TaffyTree_GetStyleMutRef(
122168
/// Create a new Node in the TaffyTree. Returns a NodeId handle to the node.
123169
#[no_mangle]
124170
#[allow(clippy::missing_safety_doc)]
125-
pub unsafe extern "C" fn TaffyTree_ComputeLayout(
126-
raw_tree: TaffyTreeMutRef,
171+
pub unsafe extern "C" fn TaffyTree_GetLayout(
172+
raw_tree: TaffyTreeConstRef,
127173
node_id: TaffyNodeId,
128-
available_width: f32,
129-
available_height: f32,
130-
) -> TaffyReturnCode {
131-
with_tree_mut!(raw_tree, tree, {
132-
let available_space = core::Size {
133-
width: available_space_from_f32(available_width),
134-
height: available_space_from_f32(available_height),
135-
};
136-
try_or!(InvalidNodeId, tree.inner.compute_layout(node_id.into(), available_space));
137-
TaffyReturnCode::Ok
138-
})
139-
}
140-
141-
/// Create a new Node in the TaffyTree. Returns a NodeId handle to the node.
142-
#[no_mangle]
143-
#[allow(clippy::missing_safety_doc)]
144-
pub unsafe extern "C" fn TaffyTree_PrintTree(raw_tree: TaffyTreeConstRef, node_id: TaffyNodeId) -> TaffyReturnCode {
174+
) -> TaffyResult<TaffyLayout> {
145175
with_tree!(raw_tree, tree, {
146-
taffy::util::print_tree(&tree.inner, node_id.into());
147-
TaffyReturnCode::Ok
176+
let layout = try_or!(InvalidNodeId, tree.inner.layout(node_id.into()));
177+
ok!(TaffyLayout {
178+
x: layout.location.x,
179+
y: layout.location.y,
180+
width: layout.size.width,
181+
height: layout.size.height
182+
});
148183
})
149184
}

ctaffy/src/value.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,19 @@ pub enum TaffyUnit {
4646
Fr,
4747
}
4848

49+
#[repr(C)]
50+
pub struct TaffyLayout {
51+
pub x: f32,
52+
pub y: f32,
53+
pub width: f32,
54+
pub height: f32,
55+
}
56+
impl TaffyFFIDefault for TaffyLayout {
57+
fn default() -> Self {
58+
TaffyLayout { x: 0.0, y: 0.0, width: 0.0, height: 0.0 }
59+
}
60+
}
61+
4962
#[derive(Debug, Clone, Copy, PartialEq)]
5063
#[repr(C)]
5164
pub struct TaffyDimension {

0 commit comments

Comments
 (0)