|
| 1 | +# C API for Taffy (ctaffy) |
| 2 | + |
| 3 | +Taffy is a flexible, high-performance, cross-platform UI layout library written in Rust. |
| 4 | + |
| 5 | +This directory contains C bindings for Taffy. The API is a pure C API (no C++ features are used), and is designed to be easy to build other language bindings on top of. In addition to the documentation below, you may to read through the header file (`include/taffy.h`). |
| 6 | + |
| 7 | +## Examples |
| 8 | + |
| 9 | +There are readable examples in the examples directory. |
| 10 | + |
| 11 | +Assuming you have Rust and Cargo installed (and a C compiler), then this should work to run the basic example: |
| 12 | + |
| 13 | +```bash |
| 14 | +git clone https://github.com/DioxusLabs/taffy.git |
| 15 | +cd taffy/ctaffy/examples |
| 16 | +./compile-basic.sh |
| 17 | +./basic |
| 18 | +``` |
| 19 | + |
| 20 | +## Naming Conventions |
| 21 | + |
| 22 | +- Everything in the Taffy C API is prefixed with `Taffy`, except enum variant names which are prefixed with `TAFFY_` |
| 23 | +- Structs and Enums are named in UpperCamelCase (e.g. `TaffyTree`, `TaffyStyle`) |
| 24 | +- Functions begin with the name of the struct they apply to, followed by an underscore, followed by the name of the function in UpperCamelCase (e.g. `TaffyTree_New`, `TaffyStyle_SetFlexBasis`) |
| 25 | +- Enum variants are SCREAMING_SNAKE_CASE |
| 26 | + |
| 27 | +## Error Handling |
| 28 | + |
| 29 | +Error handling is managed by the use of return codes and "result" structs. All functions in the API return either an `enum TaffyReturnCode` or one of the `struct Taffy*Result` structs (or `void` in the case of infallible operations that don't return anything). |
| 30 | + |
| 31 | +### Return codes |
| 32 | + |
| 33 | +Error handling is managed by the use of an enum `TaffyReturnCode`: |
| 34 | + |
| 35 | +```c |
| 36 | +typedef enum TaffyReturnCode { |
| 37 | + TAFFY_RETURN_CODE_OK, |
| 38 | + TAFFY_RETURN_CODE_NULL_STYLE_POINTER, |
| 39 | + TAFFY_RETURN_CODE_NULL_TREE_POINTER, |
| 40 | + // ... (see header file for full definition) |
| 41 | +} TaffyReturnCode; |
| 42 | +``` |
| 43 | + |
| 44 | +`TAFFY_RETURN_CODE_OK` indicates that the operation succeeded. All other variant indicate |
| 45 | + |
| 46 | +### Result structs |
| 47 | + |
| 48 | +"Result structs" are used for functions that need to return another value in addition to a `TaffyReturnCode` indicating success/failure (such as style getters which need to return the relevant style value). As C doesn't support generic structs, there are several "Result structs": one for each type of value. But each struct follows the same structure as the following example (varying only in the name of the struct and the type of the `value` field): |
| 49 | + |
| 50 | +<table> |
| 51 | +<tr><th>TaffyIntResult</th><th>TaffyDimensionResult</th></tr> |
| 52 | +<tr> |
| 53 | +<td> |
| 54 | + |
| 55 | +```c |
| 56 | +typedef struct TaffyIntResult { |
| 57 | + enum TaffyReturnCode return_code; |
| 58 | + int32_t value; |
| 59 | +} TaffyIntResult; |
| 60 | +``` |
| 61 | + |
| 62 | +</td> |
| 63 | +<td> |
| 64 | + |
| 65 | +```c |
| 66 | +typedef struct TaffyDimensionResult { |
| 67 | + enum TaffyReturnCode return_code; |
| 68 | + struct TaffyDimension value; |
| 69 | +} TaffyDimensionResult; |
| 70 | +``` |
| 71 | + |
| 72 | +</td> |
| 73 | +</tr> |
| 74 | +</table> |
| 75 | + |
| 76 | +Functions that return Result structs will either return a `TAFFY_RETURN_CODE_OK` along with a meaningful value, or a error variant of `TaffyReturnCode` along with |
| 77 | + |
| 78 | +## API Usage |
| 79 | + |
| 80 | +### Tree Creation and Manipulation |
| 81 | + |
| 82 | +The `TaffyTree` struct is the entrypoint to the Taffy C API and represents an entire tree of UI nodes. Taffy uses arena allocation, so the `TaffyTree` owns the entire tree of nodes at all times. |
| 83 | + |
| 84 | +You only ever get access to a `TaffyNodeId` handle which can be used to manipulate the structure of the tree, or pointers to `TaffyStyle` object (which can be used to set styles on a Node, but are considered borrowed pointers and thus must only be held temporarily). |
| 85 | + |
| 86 | +#### Lifecycle |
| 87 | + |
| 88 | +- `TaffyTree_New` allocates a new `TaffyTree` and returns an owned pointer to it. |
| 89 | +- `TaffyTree_Free` can be used to free the tree once you are done with it. |
| 90 | + |
| 91 | +All other functions in the API which accept a pointer to a `TaffyTree` have borrowing semantics: they access the tree during the duration of the function (and if the pointer is not a `const` pointer, may modify the tree), but will not store the pointer or take ownership of the tree. |
| 92 | + |
| 93 | +#### Node creation and manipulation |
| 94 | + |
| 95 | +- `TaffyTree_NewNode` creates a new Node within the tree and returns a `TaffyNodeId` handle to it. The node will initially have the default styles |
| 96 | + |
| 97 | +### Setting styles on node |
0 commit comments