Selaa lähdekoodia

clean up node api

Evan Almloff 2 vuotta sitten
vanhempi
commit
0f6eb4fca2

+ 1 - 1
packages/native-core/src/lib.rs

@@ -23,7 +23,7 @@ pub mod tree;
 pub mod utils;
 
 pub mod prelude {
-    pub use crate::node::{ElementNode, FromAnyValue, NodeType, OwnedAttributeView};
+    pub use crate::node::{ElementNode, FromAnyValue, NodeType, OwnedAttributeView, TextNode};
     pub use crate::node_ref::{AttributeMaskBuilder, NodeMaskBuilder, NodeView};
     pub use crate::passes::{Dependancy, Pass};
     pub use crate::real_dom::{NodeImmutable, NodeMut, NodeRef, RealDom};

+ 19 - 13
packages/native-core/src/real_dom.rs

@@ -1,8 +1,7 @@
 use rustc_hash::{FxHashMap, FxHashSet};
 use std::any::{Any, TypeId};
 use std::collections::VecDeque;
-use std::rc::Rc;
-use std::sync::RwLock;
+use std::sync::{Arc, RwLock};
 
 use crate::node::{
     ElementNode, FromAnyValue, NodeType, OwnedAttributeDiscription, OwnedAttributeValue, TextNode,
@@ -54,7 +53,7 @@ impl<V: FromAnyValue + Send + Sync> NodesDirty<V> {
     }
 }
 
-type NodeWatchers<V> = Rc<RwLock<Vec<Box<dyn NodeWatcher<V>>>>>;
+type NodeWatchers<V> = Arc<RwLock<Vec<Box<dyn NodeWatcher<V> + Send + Sync>>>>;
 
 /// A Dom that can sync with the VirtualDom mutations intended for use in lazy renderers.
 /// The render state passes from parent to children and or accumulates state from children to parents.
@@ -276,7 +275,7 @@ impl<V: FromAnyValue + Send + Sync> RealDom<V> {
         self.tree.insert_slab::<T>();
     }
 
-    pub fn add_node_watcher(&mut self, watcher: impl NodeWatcher<V> + 'static) {
+    pub fn add_node_watcher(&mut self, watcher: impl NodeWatcher<V> + 'static + Send + Sync) {
         self.node_watchers.write().unwrap().push(Box::new(watcher));
     }
 }
@@ -302,15 +301,17 @@ pub trait NodeImmutable<V: FromAnyValue + Send + Sync>: Sized {
     }
 
     #[inline]
-    fn children(&self) -> Option<Vec<NodeRef<V>>> {
-        self.child_ids().map(|ids| {
-            ids.iter()
-                .map(|id| NodeRef {
-                    id: *id,
-                    dom: self.real_dom(),
-                })
-                .collect()
-        })
+    fn children(&self) -> Vec<NodeRef<V>> {
+        self.child_ids()
+            .map(|ids| {
+                ids.iter()
+                    .map(|id| NodeRef {
+                        id: *id,
+                        dom: self.real_dom(),
+                    })
+                    .collect()
+            })
+            .unwrap_or_default()
     }
 
     #[inline]
@@ -355,6 +356,11 @@ pub trait NodeImmutable<V: FromAnyValue + Send + Sync>: Sized {
             None
         }
     }
+
+    #[inline]
+    fn height(&self) -> u16 {
+        self.real_dom().tree.height(self.id()).unwrap()
+    }
 }
 
 pub struct NodeRef<'a, V: FromAnyValue + Send + Sync = ()> {

+ 6 - 12
packages/native-core/src/utils/persistant_iterator.rs

@@ -97,12 +97,10 @@ impl PersistantElementIter {
                 if let Some(current) = stack.last().and_then(|last| rdom.get(*last)) {
                     // if the current element has children, add the first child to the stack and return it
                     if look_in_children {
-                        if let Some(children) = current.children() {
-                            if let Some(first) = children.first() {
-                                let new = first.id();
-                                stack.push(new);
-                                return ElementProduced::Progressed(new);
-                            }
+                        if let Some(first) = current.children().first() {
+                            let new = first.id();
+                            stack.push(new);
+                            return ElementProduced::Progressed(new);
                         }
                     }
                     stack.pop();
@@ -133,12 +131,8 @@ impl PersistantElementIter {
             node: NodeRef<V>,
         ) -> NodeId {
             stack.push(node.id());
-            if let Some(children) = node.children() {
-                if let Some(last) = children.last() {
-                    push_back(stack, *last)
-                } else {
-                    node.id()
-                }
+            if let Some(last) = node.children().last() {
+                push_back(stack, *last)
             } else {
                 node.id()
             }

+ 1 - 1
packages/tui/src/render.rs

@@ -85,7 +85,7 @@ pub(crate) fn render_vnode(
                 frame.render_widget(WidgetWithContext::new(node, cfg), area);
             }
 
-            for c in node.children().unwrap() {
+            for c in node.children() {
                 render_vnode(frame, layout, c, cfg, location);
             }
         }