gioser

tawasuyu/taffy

A high performance rust-powered UI layout library

HTML 5364Rust 86Markdown 12TOML 91841 Definitions

⎇ fda1f88edd2bb6de8a7eb2611be94fc81485726d src/tree/node.rs Rust · 95 lines · 2.2 KB History Who touched what raw

Add `taffy_tree` feature to opt-out of default Taffy tree (#448) fda1f88ed · Martijn Arts ·

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(Copy, Clone, Debug, PartialEq, Eq)]
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 [`Taffy`].
68pub(crate) struct NodeData {
69    /// The layout strategy used by this node
70    pub(crate) style: Style,
71    /// The results of the layout computation
72    pub(crate) layout: Layout,
73
74    /// Should we try and measure this node?
75    pub(crate) needs_measure: bool,
76
77    /// The cached results of the layout computation
78    pub(crate) cache: Cache,
79}
80
81impl NodeData {
82    /// Create the data for a new node
83    #[must_use]
84    pub const fn new(style: Style) -> Self {
85        Self { style, cache: Cache::new(), layout: Layout::new(), needs_measure: false }
86    }
87
88    /// Marks a node and all of its parents (recursively) as dirty
89    ///
90    /// This clears any cached data and signals that the data must be recomputed.
91    #[inline]
92    pub fn mark_dirty(&mut self) {
93        self.cache.clear()
94    }
95}