Taffy:一个灵活、高性能、跨平台的 UI 布局库
Taffy: A flexible, high-performance, cross-platform UI layout library

原始链接: https://github.com/DioxusLabs/taffy

Taffy 是一个用 Rust 编写的高性能、跨平台 UI 布局库。它实现了 CSS Block、Flexbox 和 CSS Grid 算法,是其他 GUI 和 UI 库的理想引擎。目前,它为多个知名项目提供了支持,包括 Bevy 游戏引擎、Zed 和 Lapce 文本编辑器、Servo 网络浏览器以及 Slint 工具包。 作为一款依赖库,Taffy 允许开发人员以编程方式构建布局树并计算精确的几何图形。由于它严格遵循 CSS 标准规范,开发人员可以放心地参考 MDN 或 CSS Tricks 等常见的 Web 资源获取指南。在性能基准测试中,Taffy 与 Yoga 等现有替代方案相比极具竞争力,即使在处理复杂且深度嵌套的布局树时,也能提供低延迟的计算能力。作为开源协作项目,Taffy 积极欢迎社区贡献、反馈问题并参与讨论,以支持其持续的开发和路线图规划。

Hacker News 最新 | 过往 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 Taffy:一个灵活、高性能、跨平台的 UI 布局库 (github.com/dioxuslabs) 7 分,由 robin_reala 发布于 1 小时前 | 隐藏 | 过往 | 收藏 | 2 条评论 帮助 troupo 2 分钟前 | 下一条 [-] 我的观点是,没人应该重新实现 CSS 布局原语和算法。由于历史的必然性和各种怪癖,这些东西在实现方式上相当独特、笨拙、奇怪且生硬。应该去探索其他的布局思路。例如:https://stackoverflow.com/questions/53911631/gui-layout-algo... 回复 LoganDark 6 分钟前 | 上一条 [-] 我对类似苹果的约束布局系统(Constraint Layout)很感兴趣。嵌套的 Flexbox 和网格布局固然能做很多事情,但在接触到约束布局后,我就喜欢上了它。甚至连窗口缩放如何表现为一种具有特定优先级的约束,也令我着迷! 回复 准则 | 常见问题 | 列表 | API | 安全 | 法律 | 加入 YC | 联系 搜索:
相关文章

原文

Taffy

GitHub CI crates.io docs.rs Crates.io MSRV

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:

  • Servo: an alternative web browser
  • Blitz: a radically modular web engine
  • Bevy: an ergonomic, ECS-first Rust game engine
  • Takumi: Renders your React components to images
  • iocraft: crafting beautiful interfaces for the terminal
  • Slint: a declarative GUI toolkit for building native user interfaces
  • The Lapce text editor via the Floem UI framework
  • The Zed text editor via the GPUI UI framework
use taffy::prelude::*;

// First create an instance of TaffyTree
let mut tree : TaffyTree<()> = TaffyTree::new();

// Create a tree of nodes using `TaffyTree.new_leaf` and `TaffyTree.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 = tree
    .new_leaf(
        Style {
            size: Size { width: length(800.0), height: length(100.0) },
            ..Default::default()
        },
    ).unwrap();

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

let root_node = tree
    .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
tree.compute_layout(root_node, Size::MAX_CONTENT).unwrap();

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

Bindings to other languages

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 Froggy. This is an interactive tutorial/game that allows you to learn the essential parts of Flexbox in a fun engaging way.
  • A Complete Guide To Flexbox by CSS Tricks. This is detailed guide with illustrations and comprehensive written explanation of the different Flexbox properties and how they work.
  • CSS Grid Garden. This is an interactive tutorial/game that allows you to learn the essential parts of CSS Grid in a fun engaging way.
  • A Complete Guide To CSS Grid by CSS Tricks. This is detailed guide with illustrations and comprehensive written explanation of the different CSS Grid properties and how they work.
  • Run on a 2021 MacBook Pro with M1 Pro processor using criterion
  • The benchmarks measure layout computation only. They do not measure tree creation.
  • Yoga benchmarks were run via the yoga crate (Rust bindings)
  • 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).

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

Benchmark Node Count Depth Yoga (ba27f9d) Taffy (71027a8)
yoga 'huge nested' 1,000 3 364.60 µs 329.04 µs
yoga 'huge nested' 10,000 4 4.1988 ms 4.3486 ms
yoga 'huge nested' 100,000 5 45.804 ms 38.559 ms
big trees (wide) 1,000 1 737.77 µs 505.99 µs
big trees (wide) 10,000 1 7.1007 ms 8.3395 ms
big trees (wide) 100,000 1 135.78 ms 247.42 ms
big trees (deep) 4,000 12 2.2333 ms 1.7400 ms
big trees (deep) 10,000 14 5.9477 ms 4.4445 ms
big trees (deep) 100,000 17 76.755 ms 63.778 ms
super deep 1,000 1,000 555.32 µs 472.85 µs

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.

联系我们 contact @ memedata.com