Improve documentation for the native core and native core macro crates (#528)

* improve documentation for the native core and native core macro crates

* fix spelling of depenency
This commit is contained in:
Demonthos authored and GitHub committed 2022-09-06 17:25:10 -07:00
1 parent 286cfa43b7
commit 2d08532bad
7 files changed
+312 -79

No files matched your search

+46 -12
View File
@@ -2,6 +2,7 @@ use dioxus_core::*;
use crate::state::union_ordered_iter;
/// A view into a [VNode] with limited access.
#[derive(Debug)]
pub struct NodeView<'a> {
inner: &'a VNode<'a>,
@@ -9,6 +10,7 @@ pub struct NodeView<'a> {
}
impl<'a> NodeView<'a> {
/// Create a new NodeView from a VNode, and mask.
pub fn new(mut vnode: &'a VNode<'a>, view: NodeMask, vdom: &'a VirtualDom) -> Self {
if let VNode::Component(sc) = vnode {
let scope = vdom.get_scope(sc.scope.get().unwrap()).unwrap();
@@ -20,41 +22,53 @@ impl<'a> NodeView<'a> {
}
}
/// Get the id of the node
pub fn id(&self) -> ElementId {
self.inner.mounted_id()
}
/// Get the tag of the node if the tag is enabled in the mask
pub fn tag(&self) -> Option<&'a str> {
self.mask.tag.then(|| self.el().map(|el| el.tag)).flatten()
}
pub fn namespace(&self) -> Option<&'a str> {
self.mask
.namespace
.then(|| self.el().and_then(|el| el.namespace))
.tag
.then(|| self.try_element().map(|el| el.tag))
.flatten()
}
/// Get the tag of the node if the namespace is enabled in the mask
pub fn namespace(&self) -> Option<&'a str> {
self.mask
.namespace
.then(|| self.try_element().and_then(|el| el.namespace))
.flatten()
}
/// Get any attributes that are enabled in the mask
pub fn attributes(&self) -> impl Iterator<Item = &Attribute<'a>> {
self.el()
self.try_element()
.map(|el| el.attributes)
.unwrap_or_default()
.iter()
.filter(|a| self.mask.attritutes.contains_attribute(a.name))
}
/// Get the text if it is enabled in the mask
pub fn text(&self) -> Option<&str> {
self.mask
.text
.then(|| self.txt().map(|txt| txt.text))
.then(|| self.try_text().map(|txt| txt.text))
.flatten()
}
/// Get the listeners if it is enabled in the mask
pub fn listeners(&self) -> &'a [Listener<'a>] {
self.el().map(|el| el.listeners).unwrap_or_default()
self.try_element()
.map(|el| el.listeners)
.unwrap_or_default()
}
fn el(&self) -> Option<&'a VElement<'a>> {
/// Try to get the underlying element.
fn try_element(&self) -> Option<&'a VElement<'a>> {
if let VNode::Element(el) = &self.inner {
Some(el)
} else {
@@ -62,7 +76,8 @@ impl<'a> NodeView<'a> {
}
}
fn txt(&self) -> Option<&'a VText<'a>> {
/// Try to get the underlying text node.
fn try_text(&self) -> Option<&'a VText<'a>> {
if let VNode::Text(txt) = &self.inner {
Some(txt)
} else {
@@ -71,14 +86,18 @@ impl<'a> NodeView<'a> {
}
}
/// A mask that contains a list of attributes that are visible.
#[derive(PartialEq, Eq, Clone, Debug)]
pub enum AttributeMask {
All,
/// A list of attribute names that are visible, this list must be sorted
Dynamic(Vec<&'static str>),
/// A list of attribute names that are visible, this list must be sorted
Static(&'static [&'static str]),
}
impl AttributeMask {
/// A empty attribute mask
pub const NONE: Self = Self::Static(&[]);
fn contains_attribute(&self, attr: &'static str) -> bool {
@@ -89,10 +108,12 @@ impl AttributeMask {
}
}
/// Create a new dynamic attribute mask with a single attribute
pub fn single(new: &'static str) -> Self {
Self::Dynamic(vec![new])
}
/// Ensure the attribute list is sorted.
pub fn verify(&self) {
match &self {
AttributeMask::Static(attrs) => debug_assert!(
@@ -107,6 +128,7 @@ impl AttributeMask {
}
}
/// Combine two attribute masks
pub fn union(&self, other: &Self) -> Self {
let new = match (self, other) {
(AttributeMask::Dynamic(s), AttributeMask::Dynamic(o)) => AttributeMask::Dynamic(
@@ -127,6 +149,7 @@ impl AttributeMask {
new
}
/// Check if two attribute masks overlap
fn overlaps(&self, other: &Self) -> bool {
fn overlaps_iter(
self_iter: impl Iterator<Item = &'static str>,
@@ -176,9 +199,9 @@ impl Default for AttributeMask {
}
}
/// A mask that limits what parts of a node a dependency can see.
#[derive(Default, PartialEq, Eq, Clone, Debug)]
pub struct NodeMask {
// must be sorted
attritutes: AttributeMask,
tag: bool,
namespace: bool,
@@ -187,11 +210,14 @@ pub struct NodeMask {
}
impl NodeMask {
/// A node mask with no parts visible.
pub const NONE: Self = Self::new();
/// A node mask with every part visible.
pub const ALL: Self = Self::new_with_attrs(AttributeMask::All)
.with_text()
.with_element();
/// Check if two masks overlap
pub fn overlaps(&self, other: &Self) -> bool {
(self.tag && other.tag)
|| (self.namespace && other.namespace)
@@ -200,6 +226,7 @@ impl NodeMask {
|| (self.listeners && other.listeners)
}
/// Combine two node masks
pub fn union(&self, other: &Self) -> Self {
Self {
attritutes: self.attritutes.union(&other.attritutes),
@@ -210,6 +237,7 @@ impl NodeMask {
}
}
/// Create a new node mask with the given attributes
pub const fn new_with_attrs(attritutes: AttributeMask) -> Self {
Self {
attritutes,
@@ -220,29 +248,35 @@ impl NodeMask {
}
}
/// Create a empty node mask
pub const fn new() -> Self {
Self::new_with_attrs(AttributeMask::NONE)
}
/// Allow the mask to view the tag
pub const fn with_tag(mut self) -> Self {
self.tag = true;
self
}
/// Allow the mask to view the namespace
pub const fn with_namespace(mut self) -> Self {
self.namespace = true;
self
}
/// Allow the mask to view the namespace and tag
pub const fn with_element(self) -> Self {
self.with_namespace().with_tag()
}
/// Allow the mask to view the text
pub const fn with_text(mut self) -> Self {
self.text = true;
self
}
/// Allow the mask to view the listeners
pub const fn with_listeners(mut self) -> Self {
self.listeners = true;
self
+11 -2
View File
@@ -45,7 +45,7 @@ impl<S: State> RealDom<S> {
}
}
/// Updates the dom, up and down state and return a set of nodes that were updated pass this to update_state.
/// Updates the dom with some mutations and return a set of nodes that were updated. Pass the dirty nodes to update_state.
pub fn apply_mutations(&mut self, mutations_vec: Vec<Mutations>) -> Vec<(ElementId, NodeMask)> {
let mut nodes_updated = Vec::new();
for mutations in mutations_vec {
@@ -203,6 +203,7 @@ impl<S: State> RealDom<S> {
nodes_updated
}
/// Update the state of the dom, after appling some mutations. This will keep the nodes in the dom up to date with their VNode counterparts.
pub fn update_state(
&mut self,
vdom: &VirtualDom,
@@ -217,6 +218,7 @@ impl<S: State> RealDom<S> {
)
}
/// Link a child and parent together
fn link_child(&mut self, child_id: ElementId, parent_id: ElementId) -> Option<()> {
debug_assert_ne!(child_id, parent_id);
let parent = &mut self[parent_id];
@@ -227,6 +229,7 @@ impl<S: State> RealDom<S> {
Some(())
}
/// Recursively increase the height of a node and its children
fn increase_height(&mut self, id: ElementId, amount: u16) {
let n = &mut self[id];
n.height += amount;
@@ -262,16 +265,18 @@ impl<S: State> RealDom<S> {
Some(node)
}
/// Create a node
fn insert(&mut self, node: Node<S>) {
let current_len = self.nodes.len();
let id = node.id.0;
if current_len - 1 < node.id.0 {
// self.nodes.reserve(1 + id - current_len);
self.nodes.extend((0..1 + id - current_len).map(|_| None));
}
self.nodes[id] = Some(node);
}
/// Find all nodes that are listening for an event, sorted by there height in the dom progressing starting at the bottom and progressing up.
/// This can be useful to avoid creating duplicate events.
pub fn get_listening_sorted(&self, event: &'static str) -> Vec<&Node<S>> {
if let Some(nodes) = self.nodes_listening.get(event) {
let mut listening: Vec<_> = nodes.iter().map(|id| &self[*id]).collect();
@@ -409,6 +414,7 @@ pub struct Node<S: State> {
pub height: u16,
}
/// A type of node with data specific to the node type. The types are a subset of the [VNode] types.
#[derive(Debug, Clone)]
pub enum NodeType {
Text {
@@ -438,18 +444,21 @@ impl<S: State> Node<S> {
vdom.get_element(self.id).unwrap()
}
/// link a child node
fn add_child(&mut self, child: ElementId) {
if let NodeType::Element { children, .. } = &mut self.node_type {
children.push(child);
}
}
/// remove a child node
fn remove_child(&mut self, child: ElementId) {
if let NodeType::Element { children, .. } = &mut self.node_type {
children.retain(|c| c != &child);
}
}
/// link the parent node
fn set_parent(&mut self, parent: ElementId) {
self.parent = Some(parent);
}
+108 -39
View File
@@ -7,6 +7,7 @@ use fxhash::FxHashSet;
use crate::node_ref::{NodeMask, NodeView};
use crate::traversable::Traversable;
/// Join two sorted iterators
pub(crate) fn union_ordered_iter<T: Ord + Debug>(
s_iter: impl Iterator<Item = T>,
o_iter: impl Iterator<Item = T>,
@@ -44,13 +45,45 @@ pub(crate) fn union_ordered_iter<T: Ord + Debug>(
/// This state is derived from children. For example a node's size could be derived from the size of children.
/// Called when the current node's node properties are modified, a child's [ChildDepState] is modified or a child is removed.
/// Called at most once per update.
/// ```rust
/// #[derive(Clone, Copy, PartialEq, Default)]
/// struct Layout {
/// width: u32,
/// height: u32,
/// }
///
/// impl ChildDepState for Layout {
/// type Ctx = ();
/// // The layout depends on the layout of the children.
/// type DepState = Layout;
/// fn reduce<'a>(
/// &mut self,
/// _node: NodeView,
/// children: impl Iterator<Item = &'a Self::DepState>,
/// _ctx: &Self::Ctx,
/// ) -> bool
/// where
/// Self::DepState: 'a{
/// /// Children are layed out form left to right. The width of the parent is the sum of the widths and the max of the heights.
/// let new = children.copied().reduce(|c1, c2| Layout{
/// width: c1.width + c2.width,
/// height: c1.height.max(c2.height)
/// }).unwrap_or_default();
/// let changed = new != self.combined;
/// self = new;
/// changed
/// }
/// }
/// ```
pub trait ChildDepState {
/// The context is passed to the [ChildDepState::reduce] when it is pushed down.
/// This is sometimes nessisary for lifetime purposes.
/// The context is passed to the [ChildDepState::reduce] when it is resolved.
type Ctx;
/// This must be either a [ChildDepState], or [NodeDepState]
/// A state from each child node that this node depends on. Typically this is Self, but it could be any state that is within the state tree.
/// This must be either a [ChildDepState], or [NodeDepState].
type DepState;
/// The part of a node that this state cares about. This is used to determine if the state should be updated when a node is updated.
const NODE_MASK: NodeMask = NodeMask::NONE;
/// Resolve the state current node's state from the state of the children, the state of the node, and some external context.
fn reduce<'a>(
&mut self,
node: NodeView,
@@ -64,13 +97,55 @@ pub trait ChildDepState {
/// This state that is passed down to children. For example text properties (`<b>` `<i>` `<u>`) would be passed to children.
/// Called when the current node's node properties are modified or a parrent's [ParentDepState] is modified.
/// Called at most once per update.
/// ```rust
/// use dioxus_native_core::node_ref::{NodeMask, AttributeMask, NodeView};
/// use dioxus_native_core::state::*;
///
/// #[derive(Clone, Copy, PartialEq)]
/// struct FontSize(usize);
///
/// impl ParentDepState for FontSize {
/// type Ctx = ();
/// // The font size depends on the font size of the parent element.
/// type DepState = Self;
/// const NODE_MASK: NodeMask =
/// NodeMask::new_with_attrs(AttributeMask::Static(&[
/// "font-size"
/// ]));
/// fn reduce<'a>(
/// &mut self,
/// node: NodeView,
/// parent: Option<&'a Self::DepState>,
/// ctx: &Self::Ctx,
/// ) -> bool{
/// let old = *self;
/// // If the font size was set on the parent, it is passed down to the current element
/// if let Some(parent) = parent{
/// *self = parent;
/// }
/// // If the current node overrides the font size, use that size insead.
/// for attr in node.attributes() {
/// match attr.name {
/// "font-size" => {
/// self.0 = attr.value.as_text().unwrap().parse().unwrap();
/// }
/// // font-size is the only attribute we specified in the mask, so it is the only one we can see
/// _ => unreachable!(),
/// }
/// }
/// old != *self
/// }
/// }
/// ```
pub trait ParentDepState {
/// The context is passed to the [ParentDepState::reduce] when it is pushed down.
/// This is sometimes nessisary for lifetime purposes.
/// The context is passed to the [ParentDepState::reduce] when it is resolved.
type Ctx;
/// A state from from the parent node that this node depends on. Typically this is Self, but it could be any state that is within the state tree.
/// This must be either a [ParentDepState] or [NodeDepState]
type DepState;
/// The part of a node that this state cares about. This is used to determine if the state should be updated when a node is updated.
const NODE_MASK: NodeMask = NodeMask::NONE;
/// Resolve the state current node's state from the state of the parent node, the state of the node, and some external context.
fn reduce<'a>(
&mut self,
node: NodeView,
@@ -80,56 +155,51 @@ pub trait ParentDepState {
}
/// This state that is upadated lazily. For example any propertys that do not effect other parts of the dom like bg-color.
/// Called when the current node's node properties are modified or a sibling's [NodeDepState] is modified.
/// Called when the current node's node properties are modified or a one of its dependanices are modified.
/// Called at most once per update.
/// NodeDepState is the only state that can accept multiple dependancies, but only from the current node.
/// ```rust, ignore
/// impl<'a, 'b> NodeDepState<(&'a TextWrap, &'b ChildLayout)> for Layout {
/// type Ctx = LayoutCache;
/// ```rust
/// use dioxus_native_core::node_ref::{NodeMask, AttributeMask, NodeView};
/// use dioxus_native_core::state::*;
///
/// #[derive(Clone, Copy, PartialEq)]
/// struct TabIndex(usize);
///
/// impl NodeDepState for TabIndex {
/// type Ctx = ();
/// const NODE_MASK: NodeMask =
/// NodeMask::new_with_attrs(AttributeMask::Static(&sorted_str_slice!([
/// "width", "height"
/// ])))
/// .with_text();
/// fn reduce<'a>(
/// NodeMask::new_with_attrs(AttributeMask::Static(&[
/// "tabindex"
/// ]));
/// fn reduce(
/// &mut self,
/// node: NodeView,
/// siblings: (&'a TextWrap, &'b ChildLayout),
/// ctx: &Self::Ctx,
/// siblings: (),
/// ctx: &(),
/// ) -> bool {
/// let old = self.clone();
/// let (text_wrap, child_layout) = siblings;
/// if TextWrap::Wrap == text_wrap {
/// if let Some(text) = node.text() {
/// let lines = text_wrap.get_lines(text);
/// self.width = lines.max_by(|l| l.len());
/// self.height = lines.len();
/// return old != self;
/// }
/// }
/// let mut width = child_layout.width;
/// let mut height = child_layout.width;
/// let old = self;
/// for attr in node.attributes() {
/// match attr.name {
/// "width" => {
/// width = attr.value.as_text().unwrap().parse().unwrap();
/// }
/// "height" => {
/// height = attr.value.as_text().unwrap().parse().unwrap();
/// "tabindex" => {
/// self.0 = attr.value.as_text().unwrap().parse().unwrap();
/// }
/// // tabindex is the only attribute we specified in the mask, so it is the only one we can see
/// _ => unreachable!(),
/// }
/// }
/// self.width = width;
/// self.height = height;
/// old != self
/// old != *self
/// }
/// }
/// ```
/// The generic argument (Depstate) must be a tuple containing any number of borrowed elments that are either a [ChildDepState], [ParentDepState] or [NodeDepState].
pub trait NodeDepState<DepState> {
/// The generic argument (Depstate) must be a tuple containing any number of borrowed elements that are either a [ChildDepState], [ParentDepState] or [NodeDepState].
// Todo: once GATs land we can model multable dependencies better
pub trait NodeDepState<DepState = ()> {
/// The state passed to [NodeDepState::reduce] when it is resolved.
type Ctx;
/// The part of a node that this state cares about. This is used to determine if the state should be updated when a node is updated.
const NODE_MASK: NodeMask = NodeMask::NONE;
/// Resolve the state current node's state from the state of the sibling states, the state of the node, and some external context.
fn reduce(&mut self, node: NodeView, siblings: DepState, ctx: &Self::Ctx) -> bool;
}
@@ -144,7 +214,6 @@ pub trait State: Default + Clone {
) -> FxHashSet<ElementId>;
}
// Todo: once GATs land we can model multable dependencies
impl ChildDepState for () {
type Ctx = ();
type DepState = ();
+2
View File
@@ -1,3 +1,4 @@
/// This is a interface for a tree with the ability to jump to a specific node
pub trait Traversable {
type Id: Copy;
type Node;
@@ -55,6 +56,7 @@ pub trait Traversable {
}
}
/// Maps one type of tree to another. Similar to [std::iter::Map].
pub struct Map<
'a,
T: Traversable,
+5 -2
View File
@@ -5,7 +5,9 @@ use crate::{
use dioxus_core::{DomEdit, ElementId, Mutations};
pub enum ElementProduced {
/// The iterator produced an element by progressing to the next node in a depth first order.
Progressed(ElementId),
/// The iterator reached the end of the tree and looped back to the root
Looped(ElementId),
}
impl ElementProduced {
@@ -42,8 +44,9 @@ impl NodePosition {
}
}
/// The focus system needs a iterator that can persist through changes in the [VirtualDom].
/// Iterate through it with [ElementIter::next] [ElementIter::prev], and update it with [ElementIter::update] (with data from [`VirtualDom::work_with_deadline`]).
/// Focus systems need a iterator that can persist through changes in the [dioxus_core::VirtualDom].
/// This iterator traverses the tree depth first.
/// Iterate through it with [PersistantElementIter::next] [PersistantElementIter::prev], and update it with [PersistantElementIter::prune] (with data from [`dioxus_core::prelude::VirtualDom::work_with_deadline`]).
/// The iterator loops around when it reaches the end or the beginning.
pub struct PersistantElementIter {
// stack of elements and fragments