diff --git a/packages/native-core-macro/tests/parse.rs b/packages/native-core-macro/tests/parse.rs deleted file mode 100644 index 6103015e7..000000000 --- a/packages/native-core-macro/tests/parse.rs +++ /dev/null @@ -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, - _: &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!() - } -} diff --git a/packages/native-core-macro/tests/test.rs b/packages/native-core-macro/tests/test.rs deleted file mode 100644 index ea0231cd7..000000000 --- a/packages/native-core-macro/tests/test.rs +++ /dev/null @@ -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, Vec>); -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, - 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, Option>); -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, 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, -} diff --git a/packages/native-core-macro/tests/update_state.rs b/packages/native-core-macro/tests/update_state.rs index d6e5e22b7..611fa752e 100644 --- a/packages/native-core-macro/tests/update_state.rs +++ b/packages/native-core-macro/tests/update_state.rs @@ -72,7 +72,7 @@ struct BubbledUpStateTester(Option, Vec>); 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, Option>) 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, 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( diff --git a/packages/native-core/src/node_ref.rs b/packages/native-core/src/node_ref.rs index bed0dc9ff..bf06368d2 100644 --- a/packages/native-core/src/node_ref.rs +++ b/packages/native-core/src/node_ref.rs @@ -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 + } } diff --git a/packages/native-core/src/real_dom.rs b/packages/native-core/src/real_dom.rs index 496af5797..2bfdaa21b 100644 --- a/packages/native-core/src/real_dom.rs +++ b/packages/native-core/src/real_dom.rs @@ -169,10 +169,7 @@ impl RealDom { 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 RealDom { 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 RealDom { } => { nodes_updated.push(( root as usize, - NodeMask::new(AttributeMask::single(field), false, false, false), + NodeMask::new_with_attrs(AttributeMask::single(field)), )); } PopRoot {} => { diff --git a/packages/tui/src/layout.rs b/packages/tui/src/layout.rs index 45e279f45..4eb209905 100644 --- a/packages/tui/src/layout.rs +++ b/packages/tui/src/layout.rs @@ -37,12 +37,8 @@ impl ChildDepState for StretchLayout { type Ctx = Rc>; 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>( diff --git a/packages/tui/src/style_attributes.rs b/packages/tui/src/style_attributes.rs index 7bd228f8b..87353bbe0 100644 --- a/packages/tui/src/style_attributes.rs +++ b/packages/tui/src/style_attributes.rs @@ -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();