tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

Rust 1068HTML 999Markdown 12TOML 83188 Definitions

⎇ 837d03fde9d8a4efa0f4eaf1e8be2fd34a719a50 src/data.rs Rust · 53 lines · 1.7 KB History Who touched what raw

Fix rounding accumulation bug (#521) b2a38dafc · Nico Burns ·

1//! Node Data - important layout and styling data for nodes
2//!
3//! Used to compute layout for Taffy trees
4//!
5use crate::layout::{Cache, Layout};
6use crate::style::Style;
7
8/// The number of cache entries for each node in the tree
9pub(crate) const CACHE_SIZE: usize = 7;
10
11/// Layout information for a given [`Node`](crate::node::Node)
12///
13/// Stored in a [`Taffy`].
14pub(crate) struct NodeData {
15    /// The layout strategy used by this node
16    pub(crate) style: Style,
17
18    /// The always unrounded results of the layout computation. We must store this separately from the rounded
19    /// layout to avoid errors from rounding already-rounded values. See <https://github.com/DioxusLabs/taffy/issues/501>.
20    pub(crate) unrounded_layout: Layout,
21
22    /// The final results of the layout computation.
23    /// These may be rounded or unrounded depending on what the `use_rounding` config setting is set to.
24    pub(crate) final_layout: Layout,
25
26    /// Should we try and measure this node?
27    pub(crate) needs_measure: bool,
28
29    /// The primary cached results of the layout computation
30    pub(crate) size_cache: [Option<Cache>; CACHE_SIZE],
31}
32
33impl NodeData {
34    /// Create the data for a new node
35    #[must_use]
36    pub const fn new(style: Style) -> Self {
37        Self {
38            style,
39            size_cache: [None; CACHE_SIZE],
40            unrounded_layout: Layout::new(),
41            final_layout: Layout::new(),
42            needs_measure: false,
43        }
44    }
45
46    /// Marks a node and all of its parents (recursively) as dirty
47    ///
48    /// This clears any cached data and signals that the data must be recomputed.
49    #[inline]
50    pub fn mark_dirty(&mut self) {
51        self.size_cache = [None; CACHE_SIZE];
52    }
53}