taffy/
lib.rs

1//! # Taffy
2//!
3//! Taffy is a flexible, high-performance library for **UI layout**.
4//! It currently implements the Block, Flexbox and Grid layout algorithms from the CSS specification. Support for other paradigms is planned.
5//! For more information on this and other future development plans see the [roadmap issue](https://github.com/DioxusLabs/taffy/issues/345).
6//!
7//! ## Architecture
8//!
9//! Taffy is based on a tree of "UI nodes" similar to the tree of DOM nodes that one finds in web-based UI. Each node has:
10//!   - A [`Style`] struct which holds a set of CSS styles which function as the primary input to the layout computations.
11//!   - A [`Layout`] struct containing a position (x/y) and a size (width/height) which function as the output of the layout computations.
12//!   - Optionally:
13//!       - A `Vec` set of child nodes
14//!       - "Context": arbitary user-defined data (which you can access when using a "measure function" to integrate Taffy with other kinds of layout such as text layout)
15//!
16//! Usage of Taffy consists of constructing a tree of UI nodes (with associated styles, children and context), then calling function(s)
17//! from Taffy to translate those styles, parent-child relationships and measure functions into a size and position in 2d space for each node
18//! in the tree.
19//!
20//! ## High-level API vs. Low-level API
21//!
22//! Taffy has two APIs: a high-level API that is simpler and easier to get started with, and a low-level API that is more flexible gives greater control.
23//!
24//! We would generally recommend the high-level API for users using Taffy standalone and the low-level API for users wanting to embed Taffy as part of
25//! a wider layout system or as part of a UI framework that already has it's own node/widget tree representation.
26//!
27//! ### High-level API
28//!
29//! The high-level API** consists of the [`TaffyTree`] struct which contains a tree implementation and provides methods that allow you to construct
30//! a tree of UI nodes. Once constructed, you can call the [`compute_layout_with_measure`](crate::TaffyTree::compute_layout_with_measure) method to compute the layout (passing in a "measure function" closure which is used to compute the size of leaf nodes), and then access
31//! the layout of each node using the [`layout`](crate::TaffyTree::layout) method.
32//!
33//! When using the high-level API, Taffy will take care of node storage, caching and dispatching to the correct layout algorithm for a given node for you.
34//! See the [`TaffyTree`] struct for more details on this API.
35//!
36//! Examples which show usage of the high-level API include:
37//!
38//!   - [basic](https://github.com/DioxusLabs/taffy/blob/main/examples/basic.rs)
39//!   - [flexbox_gap](https://github.com/DioxusLabs/taffy/blob/main/examples/flexbox_gap.rs)
40//!   - [grid_holy_grail](https://github.com/DioxusLabs/taffy/blob/main/examples/basic.rs)
41//!   - [measure](https://github.com/DioxusLabs/taffy/blob/main/examples/measure.rs)
42//!
43//! In particular, the "measure" example shows how to integrate Taffy layout with other layout modalities such as text or image layout when using the high level API.
44//!
45//! ### Low-level API
46//!
47//! The low-level API consists of a set of traits (notably the [`PartialLayoutTree`] trait) which define an interface behind which you must implement your own
48//! tree implementation, and a set of functions such as [`compute_flexbox_layout`] and [`compute_grid_layout`] which implement the layout algorithms (for a single node at a time), and are designed to be flexible
49//! and easy to integrate into a wider layout or UI system.
50//!
51//! When using this API, you must handle node storage, caching, and dispatching to the correct layout algorithm for a given node yourself.
52//! See the [`PartialLayoutTree`] trait for more details on this API.
53//!
54//! Examples which show usage of the high-level API are:
55//!
56//!   - [custom_layout_tree_vec](https://github.com/DioxusLabs/taffy/blob/main/examples/custom_layout_tree_vec.rs) which implements a custom Taffy tree using a `Vec` as an arena with NodeId's being index's into the Vec.
57//!   - [custom_layout_tree_owned](https://github.com/DioxusLabs/taffy/blob/main/examples/custom_layout_tree_owned.rs) which implements a custom Taffy tree using directly owned children with NodeId's being pointers.
58
59#![cfg_attr(not(feature = "std"), no_std)]
60#![deny(unsafe_code)]
61#![forbid(unsafe_code)]
62#![warn(missing_docs)]
63#![warn(clippy::missing_docs_in_private_items)]
64
65// We always need std for the tests
66// See <https://github.com/la10736/rstest/issues/149#issuecomment-1156402989>
67#[cfg(all(test, not(feature = "std")))]
68#[macro_use]
69extern crate std;
70
71#[cfg(all(not(feature = "std"), feature = "alloc"))]
72extern crate alloc;
73
74#[cfg_attr(feature = "serde", macro_use)]
75#[cfg(feature = "serde")]
76extern crate serde;
77
78pub mod compute;
79pub mod geometry;
80pub mod prelude;
81pub mod style;
82pub mod style_helpers;
83pub mod tree;
84#[macro_use]
85pub mod util;
86
87mod readme_doctest {
88    #![doc = include_str!("../README.md")]
89}
90
91#[cfg(feature = "block_layout")]
92#[doc(inline)]
93pub use crate::compute::compute_block_layout;
94#[cfg(feature = "flexbox")]
95#[doc(inline)]
96pub use crate::compute::compute_flexbox_layout;
97#[cfg(feature = "grid")]
98#[doc(inline)]
99pub use crate::compute::compute_grid_layout;
100#[doc(inline)]
101pub use crate::compute::{
102    compute_cached_layout, compute_hidden_layout, compute_layout, compute_leaf_layout, round_layout,
103};
104#[doc(inline)]
105pub use crate::style::Style;
106#[cfg(feature = "taffy_tree")]
107#[doc(inline)]
108pub use crate::tree::TaffyTree;
109#[doc(inline)]
110pub use crate::tree::{LayoutTree, PartialLayoutTree};
111#[cfg(feature = "std")]
112#[doc(inline)]
113pub use crate::util::print_tree;
114
115pub use crate::geometry::*;
116pub use crate::style::*;
117pub use crate::tree::*;
118pub use crate::util::*;