gioser

tawasuyu/taffy

A high performance rust-powered UI layout library

HTML 5544Rust 88Markdown 11TOML 91926 Definiciones

⎇ 01babab32d8138f3c3fa05095fb6c65fca0d3967 README.md Markdown · 96 líneas · 4.8 KB Historia crudo

Make CSS Grid feature a default feature (#297) 01babab32 · 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
7`taffy` is a flexible, high-performance, cross-platform UI layout library written in [Rust](https://www.rust-lang.org).
8
9Currently Taffy implements only CSS based layout algorithms:
10
11- The stable `0.2.x` releases of Taffy implements the [flexbox](https://css-tricks.com/snippets/css/a-guide-to-flexbox/) layout algorithm.
12- Support for [CSS Grid](https://css-tricks.com/snippets/css/complete-guide-grid/) is in preview. If you wish to try this out then you should use the `0.3.x` alpha releases ~and enable the `experimental_grid` cargo feature~ (from `0.3.0-alpha2` the CSS Grid feature is enabled by default). For information, see the [release notes](https://github.com/DioxusLabs/taffy/blob/main/RELEASES.md) and the [tracking issue](https://github.com/DioxusLabs/taffy/issues/204). Experimentation with the CSS Grid implementation is encouraged, and feedback and bug reports are welcomed.
13
14Support for other paradigms [is planned](https://github.com/DioxusLabs/taffy/issues/28).
15
16This crate is a collaborative, cross-team project, and is designed to be used as a dependency for other UI and GUI libraries.
17Right now, it powers:
18
19- [Dioxus](https://dioxuslabs.com/): a React-like library for building fast, portable, and beautiful user interfaces with Rust
20- [Bevy](https://bevyengine.org/): an ergonomic, ECS-first Rust game engine
21
22## Usage
23
24```rust
25use taffy::prelude::*;
26
27// First create an instance of Taffy
28let mut taffy = Taffy::new();
29
30// Create a tree of nodes using `taffy.new_leaf` and `taffy.new_with_children`.
31// These functions both return a node id which can be used to refer to that node
32// The Style struct is used to specify styling information
33let header_node = taffy
34    .new_leaf(
35        Style {
36            size: Size { width: points(800.0), height: points(100.0) },
37            ..Default::default()
38        },
39    ).unwrap();
40
41let body_node = taffy
42    .new_leaf(
43        Style {
44            size: Size { width: points(800.0), height: auto() },
45            flex_grow: 1.0,
46            ..Default::default()
47        },
48    ).unwrap();
49
50let root_node = taffy
51    .new_with_children(
52        Style {
53            flex_direction: FlexDirection::Column,
54            size: Size { width: points(800.0), height: points(600.0) },
55            ..Default::default()
56        },
57        &[header_node, body_node],
58    )
59    .unwrap();
60
61// Call compute_layout on the root of your tree to run the layout algorithm
62taffy.compute_layout(root_node, Size::MAX_CONTENT).unwrap();
63
64// Inspect the computed layout using taffy.layout
65assert_eq!(taffy.layout(root_node).unwrap().size.width, 800.0);
66assert_eq!(taffy.layout(root_node).unwrap().size.height, 600.0);
67assert_eq!(taffy.layout(header_node).unwrap().size.width, 800.0);
68assert_eq!(taffy.layout(header_node).unwrap().size.height, 100.0);
69assert_eq!(taffy.layout(body_node).unwrap().size.width, 800.0);
70assert_eq!(taffy.layout(body_node).unwrap().size.height, 500.0); // This value was not set explicitly, but was computed by Taffy
71
72```
73
74## Benchmarks (vs. [Yoga](https://github.com/facebook/yoga))
75
76- Run on a 2021 MacBook Pro with M1 Pro processor.
77- Taffy benchmarks are using criterion (10 iterations).
78- Yoga benchmarks run via it's node.js bindings (the `yoga-layout-prebuilt` npm package), they were run a few times manually and it was verified that variance in the numbers of each run was minimal. It should be noted that this is using an old version of Yoga.
79
80(note that the table below contains multiple different units (milliseconds vs. microseconds vs. nanoseconds))
81
82| Benchmark | Yoga | Taffy 0.2 |
83| --- | --- | --- |
84| yoga/10 nodes (1-level hierarchy) | 45.1670 µs | 1.9857 µs |
85| yoga/100 nodes (2-level hierarchy) | 134.1250 µs | 41.810 µs |
86| yoga/1_000 nodes (3-level hierarchy) | 1.2221 ms | 357.48 µs |
87| yoga/10_000 nodes (4-level hierarchy) | 13.8672 ms | 3.7310 ms |
88| yoga/100_000 nodes (5-level hierarchy) | 141.5307 ms | 39.682 ms |
89
90Most 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
92## Contributions
93
94[Contributions welcome](https://github.com/DioxusLabs/taffy/blob/main/CONTRIBUTING.md):
95if 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).
96If 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.

Currently Taffy implements only CSS based layout algorithms:

Support for other paradigms is planned.

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: points(800.0), height: points(100.0) },
            ..Default::default()
        },
    ).unwrap();

let body_node = taffy
    .new_leaf(
        Style {
            size: Size { width: points(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: points(800.0), height: points(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

Benchmarks (vs. Yoga)

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

BenchmarkYogaTaffy 0.2
yoga/10 nodes (1-level hierarchy)45.1670 µs1.9857 µs
yoga/100 nodes (2-level hierarchy)134.1250 µs41.810 µs
yoga/1_000 nodes (3-level hierarchy)1.2221 ms357.48 µs
yoga/10_000 nodes (4-level hierarchy)13.8672 ms3.7310 ms
yoga/100_000 nodes (5-level hierarchy)141.5307 ms39.682 ms

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).

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.