tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

HTML 231Rust 9TOML 3CSS 1345 Sutichaykuna

⎇ 3341abe49e152023dbcf9a4a5155e941a9d7a828 README.md Markdown · 114 siq'ikuna · 6.3 KB Ñawpa willay ch'uya

Implement CSS Block layout (`Display::Block`) (#474) 544399619 · Nico Burns ·

1# Taffy
2
3[![GitHub CI](https://github.com/DioxusLabs/taffy/actions/workflows/ci.yml/badge.svg)](https://github.com/DioxusLabs/taffy/actions/workflows/ci.yml)
4[![crates.io](https://img.shields.io/crates/v/taffy.svg)](https://crates.io/crates/taffy)
5[![docs.rs](https://img.shields.io/docsrs/taffy)](https://docs.rs/taffy)
6
7Taffy is a flexible, high-performance, cross-platform UI layout library written in [Rust](https://www.rust-lang.org).
8
9It currently implements the CSS **Block**, **Flexbox** and **CSS Grid** layout algorithms. Support for other paradigms is planned. For more information on this and other future development plans see the [roadmap issue](https://github.com/DioxusLabs/taffy/issues/345).
10
11This crate is a collaborative, cross-team project, and is designed to be used as a dependency for other UI and GUI libraries.
12Right now, it powers:
13
14- [Dioxus](https://dioxuslabs.com/): a React-like library for building fast, portable, and beautiful user interfaces with Rust
15- [Bevy](https://bevyengine.org/): an ergonomic, ECS-first Rust game engine
16
17## Usage
18
19```rust
20use taffy::prelude::*;
21
22// First create an instance of Taffy
23let mut taffy = Taffy::new();
24
25// Create a tree of nodes using `taffy.new_leaf` and `taffy.new_with_children`.
26// These functions both return a node id which can be used to refer to that node
27// The Style struct is used to specify styling information
28let header_node = taffy
29    .new_leaf(
30        Style {
31            size: Size { width: length(800.0), height: length(100.0) },
32            ..Default::default()
33        },
34    ).unwrap();
35
36let body_node = taffy
37    .new_leaf(
38        Style {
39            size: Size { width: length(800.0), height: auto() },
40            flex_grow: 1.0,
41            ..Default::default()
42        },
43    ).unwrap();
44
45let root_node = taffy
46    .new_with_children(
47        Style {
48            flex_direction: FlexDirection::Column,
49            size: Size { width: length(800.0), height: length(600.0) },
50            ..Default::default()
51        },
52        &[header_node, body_node],
53    )
54    .unwrap();
55
56// Call compute_layout on the root of your tree to run the layout algorithm
57taffy.compute_layout(root_node, Size::MAX_CONTENT).unwrap();
58
59// Inspect the computed layout using taffy.layout
60assert_eq!(taffy.layout(root_node).unwrap().size.width, 800.0);
61assert_eq!(taffy.layout(root_node).unwrap().size.height, 600.0);
62assert_eq!(taffy.layout(header_node).unwrap().size.width, 800.0);
63assert_eq!(taffy.layout(header_node).unwrap().size.height, 100.0);
64assert_eq!(taffy.layout(body_node).unwrap().size.width, 800.0);
65assert_eq!(taffy.layout(body_node).unwrap().size.height, 500.0); // This value was not set explicitly, but was computed by Taffy
66
67```
68
69## Learning Resources
70
71Taffy implements the Flexbox and CSS Grid specifications faithfully, so documentation designed for the web should translate cleanly to Taffy's implementation. For reference documentation on individual style properties we recommend the MDN documentation (for example [this page](https://developer.mozilla.org/en-US/docs/Web/CSS/width) on the `width` property). Such pages can usually be found by searching for "MDN property-name" using a search engine.
72
73If you are interested in guide-level documentation on CSS layout, then we recommend the following resources:
74
75### Flexbox
76
77- [Flexbox Froggy](https://flexboxfroggy.com/). This is an interactive tutorial/game that allows you to learn the essential parts of Flexbox in a fun engaging way.
78- [A Complete Guide To Flexbox](https://css-tricks.com/snippets/css/a-guide-to-flexbox/) by CSS Tricks. This is detailed guide with illustrations and comphrehensive written explanation of the different Flexbox properties and how they work.
79
80### CSS Grid
81
82- [CSS Grid Garden](https://cssgridgarden.com/). This is an interactive tutorial/game that allows you to learn the essential parts of CSS Grid in a fun engaging way.
83- [A Complete Guide To CSS Grid](https://css-tricks.com/snippets/css/complete-guide-grid/) by CSS Tricks. This is detailed guide with illustrations and comphrehensive written explanation of the different CSS Grid properties and how they work.
84
85## Benchmarks (vs. [Yoga](https://github.com/facebook/yoga))
86
87- Run on a 2021 MacBook Pro with M1 Pro processor using [criterion](https://github.com/bheisler/criterion.rs)
88- The benchmarks measure layout computation only. They do not measure tree creation.
89- Yoga benchmarks were run via the [yoga](https://github.com/bschwind/yoga-rs) crate (Rust bindings)
90- Most popular websites seem to have between 3,000 and 10,000 nodes (although they also require text layout, which neither yoga nor taffy implement).
91
92Note that the table below contains multiple different units (milliseconds vs. microseconds)
93
94| Benchmark          | Node Count | Depth | Yoga ([ba27f9d]) | Taffy ([71027a8]) |
95| ---                | ---        | ---   | ---              | ---               |
96| yoga 'huge nested' | 1,000      | 3     | 364.60 µs        | 329.04 µs         |
97| yoga 'huge nested' | 10,000     | 4     | 4.1988 ms        | 4.3486 ms         |
98| yoga 'huge nested' | 100,000    | 5     | 45.804 ms        | 38.559 ms         |
99| big trees (wide)   | 1,000      | 1     | 737.77 µs        | 505.99 µs         |
100| big trees (wide)   | 10,000     | 1     | 7.1007 ms        | 8.3395 ms         |
101| big trees (wide)   | 100,000    | 1     | 135.78 ms        | 247.42 ms         |
102| big trees (deep)   | 4,000      | 12    | 2.2333 ms        | 1.7400 ms         |
103| big trees (deep)   | 10,000     | 14    | 5.9477 ms        | 4.4445 ms         |
104| big trees (deep)   | 100,000    | 17    | 76.755 ms        | 63.778 ms         |
105| super deep         | 1,000      | 1,000 | 555.32 µs        | 472.85 µs         |
106
107[ba27f9d]: https://github.com/facebook/yoga/commit/ba27f9d1ecfa7518019845b84b035d3d4a2a6658
108[71027a8]: https://github.com/DioxusLabs/taffy/commit/71027a8de03b343e120852b84bb7dca9fb4651c5
109
110## Contributions
111
112[Contributions welcome](https://github.com/DioxusLabs/taffy/blob/main/CONTRIBUTING.md):
113if you'd like to use, improve or build `taffy`, feel free to join the conversation, open an [issue](https://github.com/DioxusLabs/taffy/issues) or submit a [PR](https://github.com/DioxusLabs/taffy/pulls).
114If you have questions about how to use `taffy`, open a [discussion](https://github.com/DioxusLabs/taffy/discussions) so we can answer your questions in a way that others can find.

README.md

Taffy

GitHub CI crates.io docs.rs

Taffy is a flexible, high-performance, cross-platform UI layout library written in Rust.

It currently implements the CSS Block, Flexbox and CSS Grid layout algorithms. Support for other paradigms is planned. For more information on this and other future development plans see the roadmap issue.

This crate is a collaborative, cross-team project, and is designed to be used as a dependency for other UI and GUI libraries. Right now, it powers:

Usage

use taffy::prelude::*;

// First create an instance of Taffy
let mut taffy = Taffy::new();

// Create a tree of nodes using `taffy.new_leaf` and `taffy.new_with_children`.
// These functions both return a node id which can be used to refer to that node
// The Style struct is used to specify styling information
let header_node = taffy
    .new_leaf(
        Style {
            size: Size { width: length(800.0), height: length(100.0) },
            ..Default::default()
        },
    ).unwrap();

let body_node = taffy
    .new_leaf(
        Style {
            size: Size { width: length(800.0), height: auto() },
            flex_grow: 1.0,
            ..Default::default()
        },
    ).unwrap();

let root_node = taffy
    .new_with_children(
        Style {
            flex_direction: FlexDirection::Column,
            size: Size { width: length(800.0), height: length(600.0) },
            ..Default::default()
        },
        &[header_node, body_node],
    )
    .unwrap();

// Call compute_layout on the root of your tree to run the layout algorithm
taffy.compute_layout(root_node, Size::MAX_CONTENT).unwrap();

// Inspect the computed layout using taffy.layout
assert_eq!(taffy.layout(root_node).unwrap().size.width, 800.0);
assert_eq!(taffy.layout(root_node).unwrap().size.height, 600.0);
assert_eq!(taffy.layout(header_node).unwrap().size.width, 800.0);
assert_eq!(taffy.layout(header_node).unwrap().size.height, 100.0);
assert_eq!(taffy.layout(body_node).unwrap().size.width, 800.0);
assert_eq!(taffy.layout(body_node).unwrap().size.height, 500.0); // This value was not set explicitly, but was computed by Taffy

Learning Resources

Taffy implements the Flexbox and CSS Grid specifications faithfully, so documentation designed for the web should translate cleanly to Taffy's implementation. For reference documentation on individual style properties we recommend the MDN documentation (for example this page on the width property). Such pages can usually be found by searching for "MDN property-name" using a search engine.

If you are interested in guide-level documentation on CSS layout, then we recommend the following resources:

Flexbox

CSS Grid

Benchmarks (vs. Yoga)

Note that the table below contains multiple different units (milliseconds vs. microseconds)

BenchmarkNode CountDepthYoga (ba27f9d)Taffy (71027a8)
yoga 'huge nested'1,0003364.60 µs329.04 µs
yoga 'huge nested'10,00044.1988 ms4.3486 ms
yoga 'huge nested'100,000545.804 ms38.559 ms
big trees (wide)1,0001737.77 µs505.99 µs
big trees (wide)10,00017.1007 ms8.3395 ms
big trees (wide)100,0001135.78 ms247.42 ms
big trees (deep)4,000122.2333 ms1.7400 ms
big trees (deep)10,000145.9477 ms4.4445 ms
big trees (deep)100,0001776.755 ms63.778 ms
super deep1,0001,000555.32 µs472.85 µs

Contributions

Contributions welcome: if you'd like to use, improve or build taffy, feel free to join the conversation, open an issue or submit a PR. If you have questions about how to use taffy, open a discussion so we can answer your questions in a way that others can find.