From 0f6eb4fca25bc4f78a8981a3c8083f76e25a2382 Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Wed, 8 Feb 2023 14:59:47 -0600 Subject: [PATCH] clean up node api --- packages/native-core/src/lib.rs | 2 +- packages/native-core/src/real_dom.rs | 32 +++++++++++-------- .../src/utils/persistant_iterator.rs | 18 ++++------- packages/tui/src/render.rs | 2 +- 4 files changed, 27 insertions(+), 27 deletions(-) diff --git a/packages/native-core/src/lib.rs b/packages/native-core/src/lib.rs index 9e8dbe7a1..2e2ad14d8 100644 --- a/packages/native-core/src/lib.rs +++ b/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}; diff --git a/packages/native-core/src/real_dom.rs b/packages/native-core/src/real_dom.rs index 976c5c708..b14461e10 100644 --- a/packages/native-core/src/real_dom.rs +++ b/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 NodesDirty { } } -type NodeWatchers = Rc>>>>; +type NodeWatchers = Arc + 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 RealDom { self.tree.insert_slab::(); } - pub fn add_node_watcher(&mut self, watcher: impl NodeWatcher + 'static) { + pub fn add_node_watcher(&mut self, watcher: impl NodeWatcher + 'static + Send + Sync) { self.node_watchers.write().unwrap().push(Box::new(watcher)); } } @@ -302,15 +301,17 @@ pub trait NodeImmutable: Sized { } #[inline] - fn children(&self) -> Option>> { - self.child_ids().map(|ids| { - ids.iter() - .map(|id| NodeRef { - id: *id, - dom: self.real_dom(), - }) - .collect() - }) + fn children(&self) -> Vec> { + 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: Sized { None } } + + #[inline] + fn height(&self) -> u16 { + self.real_dom().tree.height(self.id()).unwrap() + } } pub struct NodeRef<'a, V: FromAnyValue + Send + Sync = ()> { diff --git a/packages/native-core/src/utils/persistant_iterator.rs b/packages/native-core/src/utils/persistant_iterator.rs index d290caaa4..0e87c03f2 100644 --- a/packages/native-core/src/utils/persistant_iterator.rs +++ b/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, ) -> 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() } diff --git a/packages/tui/src/render.rs b/packages/tui/src/render.rs index c10c52252..a739d2621 100644 --- a/packages/tui/src/render.rs +++ b/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); } }