add builder methods to NodeMask

This commit is contained in:
Evan Almloff committed 2022-04-16 17:22:03 -05:00
1 parent 436c6a02f7
commit b9c2664089
7 files changed
+45 -172

No files matched your search

-74
View File
@@ -1,74 +0,0 @@
use dioxus_native_core::node_ref::*;
use dioxus_native_core::state::*;
use dioxus_native_core_macro::*;
#[derive(State, Default, Clone)]
#[allow(dead_code)]
struct Z {
// depends on text, the C component of it's parent and a u16 context
#[parent_dep_state(c, u16)]
d: D,
// depends on just attributes and no context
#[node_dep_state()]
a: A,
// depends on the B component of children and i32 context
#[child_dep_state(b, i32)]
b: B,
// depends on the C component of it's parent and a u8 context
#[parent_dep_state(c, u8)]
c: C,
// this will remain uneffected on updates
n: i32,
}
use dioxus_native_core::state::NodeDepState;
#[derive(Default, Clone, Debug)]
struct A;
impl NodeDepState for A {
type Ctx = ();
type DepState = ();
const NODE_MASK: NodeMask = NodeMask::new(AttributeMask::All, false, false, false);
fn reduce(&mut self, _: NodeView, _: &Self::DepState, _: &()) -> bool {
todo!()
}
}
#[derive(Default, Clone, Debug)]
struct B;
impl ChildDepState for B {
type Ctx = i32;
type DepState = Self;
fn reduce<'a>(
&mut self,
_: NodeView,
_: impl Iterator<Item = &'a Self::DepState>,
_: &i32,
) -> bool
where
Self::DepState: 'a,
{
todo!()
}
}
#[derive(Default, Clone, Debug)]
struct C;
impl ParentDepState for C {
type Ctx = u8;
type DepState = Self;
fn reduce(&mut self, _: NodeView, _: Option<&Self::DepState>, _: &u8) -> bool {
todo!()
}
}
#[derive(Default, Clone, Debug)]
struct D;
impl ParentDepState for D {
type Ctx = u16;
type DepState = C;
const NODE_MASK: NodeMask = NodeMask::new(AttributeMask::NONE, false, false, true);
fn reduce(&mut self, _: NodeView, _: Option<&Self::DepState>, _: &u16) -> bool {
todo!()
}
}
-70
View File
@@ -1,70 +0,0 @@
use dioxus_native_core::node_ref::*;
use dioxus_native_core::state::{ChildDepState, NodeDepState, ParentDepState, State};
use dioxus_native_core_macro::State;
#[derive(Debug, Clone, PartialEq, Default)]
struct BubbledUpStateTester(Option<String>, Vec<Box<BubbledUpStateTester>>);
impl ChildDepState for BubbledUpStateTester {
type Ctx = u32;
type DepState = Self;
const NODE_MASK: NodeMask = NodeMask::new(AttributeMask::NONE, true, false, false);
fn reduce<'a>(
&mut self,
node: NodeView,
children: impl Iterator<Item = &'a Self::DepState>,
ctx: &Self::Ctx,
) -> bool
where
Self::DepState: 'a,
{
assert_eq!(*ctx, 42);
*self = BubbledUpStateTester(
node.tag().map(|s| s.to_string()),
children.into_iter().map(|c| Box::new(c.clone())).collect(),
);
true
}
}
#[derive(Debug, Clone, PartialEq, Default)]
struct PushedDownStateTester(Option<String>, Option<Box<PushedDownStateTester>>);
impl ParentDepState for PushedDownStateTester {
type Ctx = u32;
type DepState = Self;
const NODE_MASK: NodeMask = NodeMask::new(AttributeMask::NONE, true, false, false);
fn reduce(&mut self, node: NodeView, parent: Option<&Self::DepState>, ctx: &Self::Ctx) -> bool {
assert_eq!(*ctx, 42);
*self = PushedDownStateTester(
node.tag().map(|s| s.to_string()),
parent.map(|c| Box::new(c.clone())),
);
true
}
}
#[derive(Debug, Clone, PartialEq, Default)]
struct NodeStateTester(Option<String>, Vec<(String, String)>);
impl NodeDepState for NodeStateTester {
type Ctx = u32;
type DepState = ();
const NODE_MASK: NodeMask = NodeMask::new(AttributeMask::All, true, false, false);
fn reduce(&mut self, node: NodeView, _sibling: &Self::DepState, ctx: &Self::Ctx) -> bool {
assert_eq!(*ctx, 42);
*self = NodeStateTester(
node.tag().map(|s| s.to_string()),
node.attributes()
.map(|a| (a.name.to_string(), a.value.to_string()))
.collect(),
);
true
}
}
#[derive(State, Clone, Default, Debug)]
struct StateTester {
#[child_dep_state(bubbled, u32)]
bubbled: BubbledUpStateTester,
#[parent_dep_state(pushed, u32)]
pushed: PushedDownStateTester,
#[node_dep_state(NONE, u32)]
node: NodeStateTester,
}
@@ -72,7 +72,7 @@ struct BubbledUpStateTester(Option<String>, Vec<Box<BubbledUpStateTester>>);
impl ChildDepState for BubbledUpStateTester {
type Ctx = u32;
type DepState = Self;
const NODE_MASK: NodeMask = NodeMask::new(AttributeMask::NONE, true, false, false);
const NODE_MASK: NodeMask = NodeMask::new().with_tag();
fn reduce<'a>(
&mut self,
node: NodeView,
@@ -96,7 +96,7 @@ struct PushedDownStateTester(Option<String>, Option<Box<PushedDownStateTester>>)
impl ParentDepState for PushedDownStateTester {
type Ctx = u32;
type DepState = Self;
const NODE_MASK: NodeMask = NodeMask::new(AttributeMask::NONE, true, false, false);
const NODE_MASK: NodeMask = NodeMask::new().with_tag();
fn reduce(&mut self, node: NodeView, parent: Option<&Self::DepState>, ctx: &Self::Ctx) -> bool {
assert_eq!(*ctx, 42);
*self = PushedDownStateTester(
@@ -112,7 +112,7 @@ struct NodeStateTester(Option<String>, Vec<(String, String)>);
impl NodeDepState for NodeStateTester {
type Ctx = u32;
type DepState = ();
const NODE_MASK: NodeMask = NodeMask::new(AttributeMask::All, true, false, false);
const NODE_MASK: NodeMask = NodeMask::new_with_attrs(AttributeMask::All).with_tag();
fn reduce(&mut self, node: NodeView, _sibling: &Self::DepState, ctx: &Self::Ctx) -> bool {
assert_eq!(*ctx, 42);
*self = NodeStateTester(
+36 -12
View File
@@ -181,18 +181,10 @@ pub struct NodeMask {
}
impl NodeMask {
pub const NONE: Self = Self::new(AttributeMask::Static(&[]), false, false, false);
pub const ALL: Self = Self::new(AttributeMask::All, true, true, true);
/// attritutes must be sorted!
pub const fn new(attritutes: AttributeMask, tag: bool, namespace: bool, text: bool) -> Self {
Self {
attritutes,
tag,
namespace,
text,
}
}
pub const NONE: Self = Self::new();
pub const ALL: Self = Self::new_with_attrs(AttributeMask::All)
.with_text()
.with_element();
pub fn overlaps(&self, other: &Self) -> bool {
(self.tag && other.tag)
@@ -209,4 +201,36 @@ impl NodeMask {
text: self.text | other.text,
}
}
pub const fn new_with_attrs(attritutes: AttributeMask) -> Self {
Self {
attritutes,
tag: false,
namespace: false,
text: false,
}
}
pub const fn new() -> Self {
Self::new_with_attrs(AttributeMask::NONE)
}
pub const fn with_tag(mut self) -> Self {
self.tag = true;
self
}
pub const fn with_namespace(mut self) -> Self {
self.namespace = true;
self
}
pub const fn with_element(self) -> Self {
self.with_namespace().with_tag()
}
pub const fn with_text(mut self) -> Self {
self.text = true;
self
}
}
+3 -6
View File
@@ -169,10 +169,7 @@ impl<S: State> RealDom<S> {
text: new_text,
} => {
let target = &mut self[root as usize];
nodes_updated.push((
root as usize,
NodeMask::new(AttributeMask::NONE, false, false, true),
));
nodes_updated.push((root as usize, NodeMask::new().with_text()));
match &mut target.node_type {
NodeType::Text { text } => {
*text = new_text.to_string();
@@ -183,7 +180,7 @@ impl<S: State> RealDom<S> {
SetAttribute { root, field, .. } => {
nodes_updated.push((
root as usize,
NodeMask::new(AttributeMask::single(field), false, false, false),
NodeMask::new_with_attrs(AttributeMask::single(field)),
));
}
RemoveAttribute {
@@ -191,7 +188,7 @@ impl<S: State> RealDom<S> {
} => {
nodes_updated.push((
root as usize,
NodeMask::new(AttributeMask::single(field), false, false, false),
NodeMask::new_with_attrs(AttributeMask::single(field)),
));
}
PopRoot {} => {
+2 -6
View File
@@ -37,12 +37,8 @@ impl ChildDepState for StretchLayout {
type Ctx = Rc<RefCell<Stretch>>;
type DepState = Self;
// todo: update mask
const NODE_MASK: NodeMask = NodeMask::new(
AttributeMask::Static(SORTED_LAYOUT_ATTRS),
false,
false,
true,
);
const NODE_MASK: NodeMask =
NodeMask::new_with_attrs(AttributeMask::Static(SORTED_LAYOUT_ATTRS)).with_text();
/// Setup the layout
fn reduce<'a>(
+1 -1
View File
@@ -50,7 +50,7 @@ impl ParentDepState for StyleModifier {
type DepState = Self;
// todo: seperate each attribute into it's own class
const NODE_MASK: NodeMask =
NodeMask::new(AttributeMask::Static(SORTED_STYLE_ATTRS), true, true, false);
NodeMask::new_with_attrs(AttributeMask::Static(SORTED_STYLE_ATTRS)).with_element();
fn reduce(&mut self, node: NodeView, parent: Option<&Self::DepState>, _: &Self::Ctx) -> bool {
let mut new = StyleModifier::default();