tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

HTML 5544Rust 88Markdown 11TOML 91926 Definiciones

Move `NodeData` struct into `taffy_tree` module (#584)

This struct is only used in the taffy_tree module, and can be made
private if it also defined within this module.

a6e60e2dcebcf6fae8fa06d85a8db05bb26c23f8 · por Nico Burns · (hace 2 años) · padre 7781c7024 · ver el árbol en este commit

Delta semántico · 8

src/compute/mod.rs

src/tree/node.rs

src/tree/taffy_tree.rs

4 archivos · +60 −63

diff --git a/src/compute/mod.rs b/src/compute/mod.rs
index 267fdf4e0..b125bba8a 100644
--- a/src/compute/mod.rs
+++ b/src/compute/mod.rs
@@ -216,11 +216,11 @@ mod tests {
 
         // Whatever size and display-mode the nodes had previously,
         // all layouts should resolve to ZERO due to the root's DISPLAY::NONE
-        for (node, _) in taffy.nodes.iter().filter(|(node, _)| *node != root.into()) {
-            if let Ok(layout) = taffy.layout(node.into()) {
-                assert_eq!(layout.size, Size::zero());
-                assert_eq!(layout.location, Point::zero());
-            }
+
+        for node in [root, child_00, child_01, grandchild_00, grandchild_01, grandchild_02] {
+            let layout = taffy.layout(node.into()).unwrap();
+            assert_eq!(layout.size, Size::zero());
+            assert_eq!(layout.location, Point::zero());
         }
     }
 }
diff --git a/src/tree/mod.rs b/src/tree/mod.rs
index dd2e37613..3939c5fcb 100644
--- a/src/tree/mod.rs
+++ b/src/tree/mod.rs
@@ -5,17 +5,17 @@ use crate::style::{AvailableSpace, Style};
 
 // Submodules
 mod cache;
-pub use cache::Cache;
+mod layout;
 mod node;
-#[cfg(feature = "taffy_tree")]
-use node::NodeData;
+
+pub use cache::Cache;
+pub use layout::{CollapsibleMarginSet, Layout, LayoutInput, LayoutOutput, RequestedAxis, RunMode, SizingMode};
 pub use node::NodeId;
+
 #[cfg(feature = "taffy_tree")]
 mod taffy_tree;
 #[cfg(feature = "taffy_tree")]
 pub use taffy_tree::{TaffyError, TaffyResult, TaffyTree};
-mod layout;
-pub use layout::{CollapsibleMarginSet, Layout, LayoutInput, LayoutOutput, RequestedAxis, RunMode, SizingMode};
 
 /// This if the core abstraction in Taffy. Any type that *correctly* implements `PartialLayoutTree` can be laid out using Taffy's algorithms.
 ///
diff --git a/src/tree/node.rs b/src/tree/node.rs
index 78e785e97..9285c22e2 100644
--- a/src/tree/node.rs
+++ b/src/tree/node.rs
@@ -1,9 +1,6 @@
 //! UI node types and related data structures.
 //!
 //! Layouts are composed of multiple nodes, which live in a tree-like data structure.
-use crate::style::Style;
-use crate::tree::Cache;
-use crate::tree::Layout;
 
 #[cfg(feature = "taffy_tree")]
 use slotmap::{DefaultKey, Key, KeyData};
@@ -61,47 +58,3 @@ impl From<NodeId> for DefaultKey {
         KeyData::from_ffi(key.0).into()
     }
 }
-
-/// Layout information for a given [`Node`](crate::node::Node)
-///
-/// Stored in a [`TaffyTree`].
-pub(crate) struct NodeData {
-    /// The layout strategy used by this node
-    pub(crate) style: Style,
-
-    /// The always unrounded results of the layout computation. We must store this separately from the rounded
-    /// layout to avoid errors from rounding already-rounded values. See <https://github.com/DioxusLabs/taffy/issues/501>.
-    pub(crate) unrounded_layout: Layout,
-
-    /// The final results of the layout computation.
-    /// These may be rounded or unrounded depending on what the `use_rounding` config setting is set to.
-    pub(crate) final_layout: Layout,
-
-    /// Should we try and measure this node?
-    pub(crate) needs_measure: bool,
-
-    /// The cached results of the layout computation
-    pub(crate) cache: Cache,
-}
-
-impl NodeData {
-    /// Create the data for a new node
-    #[must_use]
-    pub const fn new(style: Style) -> Self {
-        Self {
-            style,
-            cache: Cache::new(),
-            unrounded_layout: Layout::new(),
-            final_layout: Layout::new(),
-            needs_measure: false,
-        }
-    }
-
-    /// Marks a node and all of its parents (recursively) as dirty
-    ///
-    /// This clears any cached data and signals that the data must be recomputed.
-    #[inline]
-    pub fn mark_dirty(&mut self) {
-        self.cache.clear()
-    }
-}
diff --git a/src/tree/taffy_tree.rs b/src/tree/taffy_tree.rs
index 2764cd9ac..371e3f53a 100644
--- a/src/tree/taffy_tree.rs
+++ b/src/tree/taffy_tree.rs
@@ -3,7 +3,7 @@ use slotmap::{DefaultKey, SlotMap, SparseSecondaryMap};
 
 use crate::geometry::Size;
 use crate::style::{AvailableSpace, Display, Style};
-use crate::tree::{Cache, Layout, LayoutInput, LayoutOutput, LayoutTree, NodeData, NodeId, PartialLayoutTree, RunMode};
+use crate::tree::{Cache, Layout, LayoutInput, LayoutOutput, LayoutTree, NodeId, PartialLayoutTree, RunMode};
 use crate::util::debug::{debug_log, debug_log_node};
 use crate::util::sys::{new_vec_with_capacity, ChildrenVec, Vec};
 
@@ -68,28 +68,72 @@ impl Default for TaffyConfig {
     }
 }
 
+/// Layout information for a given [`Node`](crate::node::Node)
+///
+/// Stored in a [`TaffyTree`].
+struct NodeData {
+    /// The layout strategy used by this node
+    pub(crate) style: Style,
+
+    /// The always unrounded results of the layout computation. We must store this separately from the rounded
+    /// layout to avoid errors from rounding already-rounded values. See <https://github.com/DioxusLabs/taffy/issues/501>.
+    pub(crate) unrounded_layout: Layout,
+
+    /// The final results of the layout computation.
+    /// These may be rounded or unrounded depending on what the `use_rounding` config setting is set to.
+    pub(crate) final_layout: Layout,
+
+    /// Should we try and measure this node?
+    pub(crate) needs_measure: bool,
+
+    /// The cached results of the layout computation
+    pub(crate) cache: Cache,
+}
+
+impl NodeData {
+    /// Create the data for a new node
+    #[must_use]
+    pub const fn new(style: Style) -> Self {
+        Self {
+            style,
+            cache: Cache::new(),
+            unrounded_layout: Layout::new(),
+            final_layout: Layout::new(),
+            needs_measure: false,
+        }
+    }
+
+    /// Marks a node and all of its parents (recursively) as dirty
+    ///
+    /// This clears any cached data and signals that the data must be recomputed.
+    #[inline]
+    pub fn mark_dirty(&mut self) {
+        self.cache.clear()
+    }
+}
+
 /// An entire tree of UI nodes. The entry point to Taffy's high-level API.
 ///
 /// Allows you to build a tree of UI nodes, run Taffy's layout algorithms over that tree, and then access the resultant layout.
 pub struct TaffyTree<NodeContext = ()> {
     /// The [`NodeData`] for each node stored in this tree
-    pub(crate) nodes: SlotMap<DefaultKey, NodeData>,
+    nodes: SlotMap<DefaultKey, NodeData>,
 
     /// Functions/closures that compute the intrinsic size of leaf nodes
-    pub(crate) node_context_data: SparseSecondaryMap<DefaultKey, NodeContext>,
+    node_context_data: SparseSecondaryMap<DefaultKey, NodeContext>,
 
     /// The children of each node
     ///
     /// The indexes in the outer vector correspond to the position of the parent [`NodeData`]
-    pub(crate) children: SlotMap<DefaultKey, ChildrenVec<NodeId>>,
+    children: SlotMap<DefaultKey, ChildrenVec<NodeId>>,
 
     /// The parents of each node
     ///
     /// The indexes in the outer vector correspond to the position of the child [`NodeData`]
-    pub(crate) parents: SlotMap<DefaultKey, Option<NodeId>>,
+    parents: SlotMap<DefaultKey, Option<NodeId>>,
 
     /// Layout mode configuration
-    pub(crate) config: TaffyConfig,
+    config: TaffyConfig,
 }
 
 impl Default for TaffyTree {