taffy/tree/
node.rs

1//! UI node types and related data structures.
2//!
3//! Layouts are composed of multiple nodes, which live in a tree-like data structure.
4use crate::style::Style;
5use crate::tree::Cache;
6use crate::tree::Layout;
7
8#[cfg(feature = "taffy_tree")]
9use slotmap::{DefaultKey, Key, KeyData};
10
11/// A type representing the id of a single node in a tree of nodes
12///
13/// Internally it is a wrapper around a u64 and a `NodeId` can be converted to and from
14/// and u64 if needed.
15#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
16pub struct NodeId(u64);
17impl NodeId {
18    /// Create a new NodeId from a u64 value
19    pub const fn new(val: u64) -> Self {
20        Self(val)
21    }
22}
23
24impl From<u64> for NodeId {
25    #[inline]
26    fn from(raw: u64) -> Self {
27        Self(raw)
28    }
29}
30impl From<NodeId> for u64 {
31    #[inline]
32    fn from(id: NodeId) -> Self {
33        id.0
34    }
35}
36impl From<usize> for NodeId {
37    #[inline]
38    fn from(raw: usize) -> Self {
39        Self(raw as u64)
40    }
41}
42impl From<NodeId> for usize {
43    #[inline]
44    fn from(id: NodeId) -> Self {
45        id.0 as usize
46    }
47}
48
49#[cfg(feature = "taffy_tree")]
50impl From<DefaultKey> for NodeId {
51    #[inline]
52    fn from(key: DefaultKey) -> Self {
53        Self(key.data().as_ffi())
54    }
55}
56
57#[cfg(feature = "taffy_tree")]
58impl From<NodeId> for DefaultKey {
59    #[inline]
60    fn from(key: NodeId) -> Self {
61        KeyData::from_ffi(key.0).into()
62    }
63}
64
65/// Layout information for a given [`Node`](crate::node::Node)
66///
67/// Stored in a [`TaffyTree`].
68pub(crate) struct NodeData {
69    /// The layout strategy used by this node
70    pub(crate) style: Style,
71
72    /// The always unrounded results of the layout computation. We must store this separately from the rounded
73    /// layout to avoid errors from rounding already-rounded values. See <https://github.com/DioxusLabs/taffy/issues/501>.
74    pub(crate) unrounded_layout: Layout,
75
76    /// The final results of the layout computation.
77    /// These may be rounded or unrounded depending on what the `use_rounding` config setting is set to.
78    pub(crate) final_layout: Layout,
79
80    /// Should we try and measure this node?
81    pub(crate) needs_measure: bool,
82
83    /// The cached results of the layout computation
84    pub(crate) cache: Cache,
85}
86
87impl NodeData {
88    /// Create the data for a new node
89    #[must_use]
90    pub const fn new(style: Style) -> Self {
91        Self {
92            style,
93            cache: Cache::new(),
94            unrounded_layout: Layout::new(),
95            final_layout: Layout::new(),
96            needs_measure: false,
97        }
98    }
99
100    /// Marks a node and all of its parents (recursively) as dirty
101    ///
102    /// This clears any cached data and signals that the data must be recomputed.
103    #[inline]
104    pub fn mark_dirty(&mut self) {
105        self.cache.clear()
106    }
107}