Make library more usable in a `const` context (#203)
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::node::MeasureFunc;
7use crate::style::FlexboxLayout;
8
9/// Layout information for a given [`Node`](crate::node::Node)
10///
11/// Stored in a [`Taffy`].
12pub(crate) struct NodeData {
13 /// The layout strategy used by this node
14 pub(crate) style: FlexboxLayout,
15 /// The mapping from the Size<Option<f32>> (in real units) to Size<f32> (in points) for this node
16 pub(crate) measure: Option<MeasureFunc>,
17 /// The results of the layout computation
18 pub(crate) layout: Layout,
19 /// The primary cached results of the layout computation
20 pub(crate) main_size_layout_cache: Option<Cache>,
21 /// Secondary cached results of the layout computation
22 pub(crate) other_layout_cache: Option<Cache>,
23 /// Does this node's layout need to be recomputed?
24 pub(crate) is_dirty: bool,
25}
26
27impl NodeData {
28 /// Create the data for a new node with a [`MeasureFunc`]
29 #[must_use]
30 pub const fn new_with_measure(style: FlexboxLayout, measure: MeasureFunc) -> Self {
31 Self {
32 style,
33 measure: Some(measure),
34 main_size_layout_cache: None,
35 other_layout_cache: None,
36 layout: Layout::new(),
37 is_dirty: true,
38 }
39 }
40
41 /// Create the data for a new node
42 #[must_use]
43 pub const fn new(style: FlexboxLayout) -> Self {
44 Self {
45 style,
46 measure: None,
47 main_size_layout_cache: None,
48 other_layout_cache: None,
49 layout: Layout::new(),
50 is_dirty: true,
51 }
52 }
53
54 /// Marks a node and all of its parents (recursively) as dirty
55 ///
56 /// This clears any cached data and signals that the data must be recomputed.
57 #[inline]
58 pub fn mark_dirty(&mut self) {
59 self.main_size_layout_cache = None;
60 self.other_layout_cache = None;
61 self.is_dirty = true;
62 }
63}