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;
78#[cfg(feature = "taffy_tree")]
9use slotmap::{DefaultKey, Key, KeyData};
1011/// 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
19pub const fn new(val: u64) -> Self {
20Self(val)
21 }
22}
2324impl From<u64> for NodeId {
25#[inline]
26fn from(raw: u64) -> Self {
27Self(raw)
28 }
29}
30impl From<NodeId> for u64 {
31#[inline]
32fn from(id: NodeId) -> Self {
33 id.0
34}
35}
36impl From<usize> for NodeId {
37#[inline]
38fn from(raw: usize) -> Self {
39Self(raw as u64)
40 }
41}
42impl From<NodeId> for usize {
43#[inline]
44fn from(id: NodeId) -> Self {
45 id.0 as usize
46 }
47}
4849#[cfg(feature = "taffy_tree")]
50impl From<DefaultKey> for NodeId {
51#[inline]
52fn from(key: DefaultKey) -> Self {
53Self(key.data().as_ffi())
54 }
55}
5657#[cfg(feature = "taffy_tree")]
58impl From<NodeId> for DefaultKey {
59#[inline]
60fn from(key: NodeId) -> Self {
61 KeyData::from_ffi(key.0).into()
62 }
63}
6465/// 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
70pub(crate) style: Style,
7172/// 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>.
74pub(crate) unrounded_layout: Layout,
7576/// 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.
78pub(crate) final_layout: Layout,
7980/// Should we try and measure this node?
81pub(crate) needs_measure: bool,
8283/// The cached results of the layout computation
84pub(crate) cache: Cache,
85}
8687impl NodeData {
88/// Create the data for a new node
89#[must_use]
90pub const fn new(style: Style) -> Self {
91Self {
92 style,
93 cache: Cache::new(),
94 unrounded_layout: Layout::new(),
95 final_layout: Layout::new(),
96 needs_measure: false,
97 }
98 }
99100/// 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]
104pub fn mark_dirty(&mut self) {
105self.cache.clear()
106 }
107}