Crate taffy
Expand description
§Taffy
Taffy is a flexible, high-performance library for UI layout. It currently implements the Flexbox, Grid and Block layout algorithms from the CSS specification. Support for other paradigms is planned. For more information on this and other future development plans see the roadmap issue.
§Architecture
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:
- A
Stylestruct which holds a set of CSS styles which function as the primary input to the layout computations. - A
Layoutstruct containing a position (x/y) and a size (width/height) which function as the output of the layout computations. - Optionally:
- A
Vecset of child nodes - “Context”: arbitrary user-defined data (which you can access when using a “measure function” to integrate Taffy with other kinds of layout such as text layout)
- A
Usage of Taffy consists of constructing a tree of UI nodes (with associated styles, children and context), then calling function(s) from Taffy to translate those styles, parent-child relationships and measure functions into a size and position in 2d space for each node in the tree.
§High-level API vs. Low-level API
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. 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 a wider layout system or as part of a UI framework that already has it’s own node/widget tree representation.
§High-level API
The high-level API consists of the TaffyTree struct which contains a tree implementation and provides methods that allow you to construct
a tree of UI nodes. Once constructed, you can call the 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
the layout of each node using the layout method.
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.
See the TaffyTree struct for more details on this API.
Examples which show usage of the high-level API include:
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.
§Low-level API
The low-level API consists of a set of traits (notably the LayoutPartialTree trait) which define an interface behind which you must implement your own
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
and easy to integrate into a wider layout or UI system.
When using this API, you must handle node storage, caching, and dispatching to the correct layout algorithm for a given node yourself.
See the crate::tree::traits module for more details on this API.
Examples which show usage of the low-level API are:
- custom_tree_vec which implements a custom Taffy tree using a
Vecas an arena with NodeId’s being index’s into the Vec. - custom_tree_owned_partial which implements a custom Taffy tree using directly owned children with NodeId’s being index’s into vec on parent node.
- custom_tree_owned_unsafe which implements a custom Taffy tree using directly owned children with NodeId’s being pointers.
Modules§
- compute
- Low-level access to the layout algorithms themselves. For a higher-level API, see the
TaffyTreestruct. - geometry
- Geometric primitives useful for layout
- prelude
- Commonly used types
- style
- A typed representation of CSS style properties in Rust. Used as input to layout computation.
- style_
helpers - Helper functions which it make it easier to create instances of types in the
styleandgeometrymodules. - tree
- Contains both a high-level interface to Taffy using a ready-made node tree, and a set of traits for defining custom node trees.
- util
- Helpful misc. utilities such as a function to debug print a tree
Structs§
- Block
Context - Context for each individual Block within a Block Formatting Context
- Block
Formatting Context - Context for positioning Block and Float boxes within a Block Formatting Context
- Cache
- A cache for caching the results of a sizing a Grid Item or Flexbox Item
- Collapsible
Margin Set - A set of margins that are available for collapsing with for block layout’s margin collapsing
- Compact
Length - A representation of a length as a compact 64-bit tagged pointer
- Content
Slot - An empty “slot” that avoids floats that is suitable for non-floated content to be laid out into
- Detailed
Grid Info - Information from the computation of grid
- Detailed
Grid Items Info - Grid area information from the placement algorithm
- Detailed
Grid Tracks Info - Information from the computation of grids tracks
- Dimension
- A unit of linear measurement
- Float
Context - A context for placing floated boxes
- Float
Intrinsic Width Calculator - Context for computing the intrinsic width contribution of a set of floats
- Grid
Template Area - Defines a grid area
- Grid
Template Repetition - A typed representation of a
repeat(..)ingrid-template-*value - Layout
- The final result of a layout algorithm for a single node.
- Layout
Input - A struct containing the inputs constraints/hints for laying out a node, which are passed in by the parent
- Layout
Output - A struct containing the result of laying a single node, which is returned up to the parent node
- Length
Percentage - A unit of linear measurement
- Length
Percentage Auto - A unit of linear measurement
- Line
- An abstract “line”. Represents any type that has a start and an end
- MaxTrack
Sizing Function - Maximum track sizing function
- MinMax
- Generic struct which holds a “min” value and a “max” value
- MinTrack
Sizing Function - Minimum track sizing function
- Named
Grid Line - Defines a named grid line
- NodeId
- A type representing the id of a single node in a tree of nodes
- Point
- A 2-dimensional coordinate.
- Rect
- An axis-aligned UI rectangle
- Size
- The width and height of a
Rect - Style
- A typed representation of the CSS style information for a single node.
- Taffy
Tree - An entire tree of UI nodes. The entry point to Taffy’s high-level API.
Enums§
- Absolute
Axis - The simple absolute horizontal and vertical axis
- Abstract
Axis - The CSS abstract axis https://www.w3.org/TR/css-writing-modes-3/#abstract-axes
- Align
Content - Sets the distribution of space between and around content items For Flexbox it controls alignment in the cross axis For Grid it controls alignment in the block axis
- Align
Items - Used to control how child nodes are aligned. For Flexbox it controls alignment in the cross axis For Grid it controls alignment in the block axis
- Available
Space - The amount of space available to a node in a given axis https://www.w3.org/TR/css-sizing-3/#available
- BoxGeneration
Mode - An abstracted version of the CSS
displayproperty where any value other than “none” is represented by “normal” See: https://www.w3.org/TR/css-display-3/#box-generation - BoxSizing
- Specifies whether size styles for this node are assigned to the node’s “content box” or “border box”
- Clear
- Gives a box “clearance”, which moves it below floated boxes which precede it in the tree.
- Clear
State - Clear operation outcome. See
Cache::clear - Detailed
Layout Info - The additional information from layout algorithm
- Direction
- Sets the direction of text, table and grid columns, and horizontal overflow. https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/direction
- Display
- Sets the layout used for the children of this node
- Flex
Direction - The direction of the flexbox layout main axis.
- Flex
Wrap - Controls whether flex items are forced onto one line or can wrap onto multiple lines.
- Float
- Floats a box to the left or right. This property only applies to children of a block layout
- Float
Direction - Whether a box that is definitely floated is floated to the left of to the right.
- Generic
Grid Placement - A grid line placement specification which is generic over the coordinate system that it uses to define grid line positions.
- Generic
Grid Template Component - A type representing a component in a
grid-template-*defintion where the type representingrepeat()s is generic - Grid
Auto Flow - Controls whether grid items are placed row-wise or column-wise. And whether the sparse or dense packing algorithm is used.
- Grid
Placement - A grid line placement specification. Used for grid-[row/column]-[start/end]. Named tracks are not implemented.
- Grid
Template Component - An element in a
grid-template-columnsorgrid-template-rowsdefinition. Either a track sizing function or a repeat(). - Overflow
- How children overflowing their container should affect layout
- Position
- The positioning strategy for this item.
- Repetition
Count - The first argument to a repeated track definition. This type represents the type of automatic repetition to perform.
- Requested
Axis - An axis that layout algorithms can be requested to compute a size for
- RunMode
- Whether we are performing a full layout, or we merely need to size the node
- Sizing
Mode - Whether styles should be taken into account when computing size
- Taffy
Error - An error that occurs while trying to access or modify a node’s children by index.
- Text
Align - Used by block layout to implement the legacy behaviour of
<center>and<div align="left | right | center">
Traits§
- Block
Container Style - The set of styles required for a Block layout container
- Block
Item Style - The set of styles required for a Block layout item (child of a Block container)
- Cache
Tree - Trait used by the
compute_cached_layoutmethod which allows cached layout results to be stored and retrieved. - Cheap
Clone Str - Trait that represents a cheaply clonable string. If you’re unsure what to use here
consider
Arc<str>orstring_cache::Atom. - Core
Style - The core set of styles that are shared between all CSS layout nodes
- Flexbox
Container Style - The set of styles required for a Flexbox container
- Flexbox
Item Style - The set of styles required for a Flexbox item (child of a Flexbox container)
- Generic
Repetition - A trait to represent a
repeat()clause in agrid-template-*definition - Grid
Container Style - The set of styles required for a CSS Grid container
- Grid
Item Style - The set of styles required for a CSS Grid item (child of a CSS Grid container)
- Layout
Block Container - Extends
LayoutPartialTreewith getters for the styles required for CSS Block layout - Layout
Flexbox Container - Extends
LayoutPartialTreewith getters for the styles required for Flexbox layout - Layout
Grid Container - Extends
LayoutPartialTreewith getters for the styles required for CSS Grid layout - Layout
Partial Tree - Any type that implements
LayoutPartialTreecan be laid out using Taffy’s algorithms - Maybe
Math - A trait to conveniently calculate minimums and maximums when some data may not be defined
- Maybe
Resolve - Trait to encapsulate behaviour where we need to resolve from a potentially context-dependent size or dimension into a context-independent size or dimension.
- Print
Tree - Trait used by the
print_treemethod which prints a debug representation - Resolve
OrZero - Trait to encapsulate behaviour where we need to resolve from a potentially context-dependent size or dimension into a context-independent size or dimension.
- Round
Tree - Trait used by the
round_layoutmethod which takes a tree of unrounded float-valued layouts and performs rounding to snap the values to the pixel grid. - Template
Line Names - A nested list of line names. This is effectively a generic representation of
Vec<Vec<String>>that allows both the collection and string type to be customised. - Traverse
Partial Tree - Taffy’s abstraction for downward tree traversal.
- Traverse
Tree - A marker trait which extends
TraversePartialTree
Functions§
- compute_
block_ layout - Computes the layout of
LayoutPartialTreeaccording to the block layout algorithm - compute_
cached_ layout - Attempts to find a cached layout for the specified node and layout inputs.
- compute_
flexbox_ layout - Computes the layout of a box according to the flexbox algorithm
- compute_
grid_ layout - Grid layout algorithm This consists of a few phases:
- compute_
hidden_ layout - Creates a layout for this node and its children, recursively. Each hidden node has zero size and is placed at the origin
- compute_
leaf_ layout - Compute the size of a leaf node (node with no children)
- compute_
root_ layout - Compute layout for the root node in the tree
- print_
tree - Prints a debug representation of the computed layout for a tree of nodes, starting with the passed root node.
- round_
layout - Rounds the calculated layout to exact pixel values
- write_
tree - Writes a debug representation of the computed layout to the writer, starting with the passed root node.
Type Aliases§
- Align
Self - Controls alignment of an individual node
- Justify
Content - Sets the distribution of space between and around content items For Flexbox it controls alignment in the main axis For Grid it controls alignment in the inline axis
- Justify
Items - Used to control how child nodes are aligned. Does not apply to Flexbox, and will be ignored if specified on a flex container For Grid it controls alignment in the inline axis
- Justify
Self - Controls alignment of an individual node
- Taffy
Result - The error Taffy generates on invalid operations
- Track
Sizing Function - The sizing function for a grid track (row/column)