Bläddra i källkod

update TUI to new native-core

Evan Almloff 2 år sedan
förälder
incheckning
c4845d5c8d

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

@@ -240,9 +240,9 @@ let get_child_view = {
             fn workload_system(type_id: std::any::TypeId, dependants: dioxus_native_core::exports::FxHashSet<std::any::TypeId>, pass_direction: dioxus_native_core::PassDirection) -> dioxus_native_core::exports::shipyard::WorkloadSystem {
                 use dioxus_native_core::exports::shipyard::{IntoWorkloadSystem, Get, AddComponent};
                 use dioxus_native_core::tree::TreeRef;
+                use dioxus_native_core::prelude::{NodeType, NodeView};
 
                 (move |data: #combined_dependencies_quote, run_view: dioxus_native_core::RunPassView #trait_generics| {
-                    println!("Running system for {:?}", type_id);
                     let (#(#split_views,)*) = data;
                     let (tree, types, _, _, _) = &run_view;
                     let tree = tree.clone();

+ 12 - 1
packages/native-core/src/real_dom.rs

@@ -311,6 +311,16 @@ impl<V: FromAnyValue + Send + Sync> RealDom<V> {
     pub fn add_node_watcher(&mut self, watcher: impl NodeWatcher<V> + 'static + Send + Sync) {
         self.node_watchers.write().unwrap().push(Box::new(watcher));
     }
+
+    /// Returns a reference to the underlying world. Any changes made to the world will not update the reactive system.
+    pub fn raw_world(&self) -> &World {
+        &self.world
+    }
+
+    /// Returns a mutable reference to the underlying world. Any changes made to the world will not update the reactive system.
+    pub fn raw_world_mut(&mut self) -> &mut World {
+        &mut self.world
+    }
 }
 
 pub struct ViewEntry<'a, V: Component + Send + Sync> {
@@ -371,7 +381,8 @@ pub trait NodeImmutable<V: FromAnyValue + Send + Sync>: Sized {
     fn get<'a, T: Component + Sync + Send>(&'a self) -> Option<ViewEntry<'a, T>> {
         // self.real_dom().tree.get(self.id())
         let view: View<'a, T> = self.real_dom().borrow_raw().ok()?;
-        Some(ViewEntry::new(view, self.id()))
+        view.contains(self.id())
+            .then(|| ViewEntry::new(view, self.id()))
     }
 
     #[inline]

+ 3 - 1
packages/tui/Cargo.toml

@@ -17,6 +17,7 @@ dioxus = { path = "../dioxus", version = "^0.3.0", optional = true }
 dioxus-core = { path = "../core", version = "^0.3.0", features = ["serialize"], optional = true }
 dioxus-html = { path = "../html", version = "^0.3.0" }
 dioxus-native-core = { path = "../native-core", version = "^0.2.0" }
+dioxus-native-core-macro = { path = "../native-core-macro", version = "^0.3.0" }
 dioxus-hot-reload = { path = "../hot-reload", optional = true }
 
 tui = "0.17.0"
@@ -29,6 +30,7 @@ smallvec = "1.6"
 rustc-hash = "1.1.0"
 anymap = "1.0.0-beta.2"
 futures-channel = "0.3.25"
+shipyard = "0.6.2"
 
 [dev-dependencies]
 dioxus = { path = "../dioxus" }
@@ -42,4 +44,4 @@ harness = false
 [features]
 default = ["hot-reload", "dioxus-bindings"]
 hot-reload = ["dioxus-hot-reload"]
-dioxus-bindings = ["dioxus", "dioxus-core", "dioxus-native-core/dioxus"]
+dioxus-bindings = ["dioxus", "dioxus-core", "dioxus-native-core/dioxus"]

+ 11 - 5
packages/tui/src/focus.rs

@@ -6,11 +6,15 @@ use dioxus_native_core::{
     utils::{ElementProduced, PersistantElementIter},
     Dependancy, NodeId, RealDom, SendAnyMap, State,
 };
+use dioxus_native_core_macro::partial_derive_state;
+use shipyard::Component;
+use shipyard::{Get, ViewMut};
 
 use std::{cmp::Ordering, num::NonZeroU16};
 
 use dioxus_native_core::node_ref::NodeView;
 
+#[derive(Component)]
 pub struct Focused(pub bool);
 
 #[derive(Clone, Copy, PartialEq, Eq, Debug)]
@@ -58,11 +62,12 @@ impl Default for FocusLevel {
     }
 }
 
-#[derive(Clone, PartialEq, Debug, Default)]
+#[derive(Clone, PartialEq, Debug, Default, Component)]
 pub(crate) struct Focus {
     pub level: FocusLevel,
 }
 
+#[partial_derive_state]
 impl State for Focus {
     const NODE_MASK: NodeMaskBuilder<'static> = NodeMaskBuilder::new()
         .with_attrs(AttributeMaskBuilder::Some(FOCUS_ATTRIBUTES))
@@ -77,7 +82,7 @@ impl State for Focus {
         node_view: NodeView,
         _: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
         _: Option<<Self::ParentDependencies as Dependancy>::ElementBorrowed<'a>>,
-        _: Option<Vec<<Self::ChildDependencies as Dependancy>::ElementBorrowed<'a>>>,
+        _: Vec<<Self::ChildDependencies as Dependancy>::ElementBorrowed<'a>>,
         _: &SendAnyMap,
     ) -> bool {
         let new = Focus {
@@ -126,7 +131,7 @@ impl State for Focus {
         node_view: NodeView<()>,
         node: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
         parent: Option<<Self::ParentDependencies as Dependancy>::ElementBorrowed<'a>>,
-        children: Option<Vec<<Self::ChildDependencies as Dependancy>::ElementBorrowed<'a>>>,
+        children: Vec<<Self::ChildDependencies as Dependancy>::ElementBorrowed<'a>>,
         context: &SendAnyMap,
     ) -> Self {
         let mut myself = Self::default();
@@ -159,7 +164,7 @@ impl FocusState {
     /// Returns true if the focus has changed.
     pub fn progress(&mut self, rdom: &mut RealDom, forward: bool) -> bool {
         if let Some(last) = self.last_focused_id {
-            if rdom.get(last).unwrap().get::<PreventDefault>().copied()
+            if rdom.get(last).unwrap().get::<PreventDefault>().map(|p| *p)
                 == Some(PreventDefault::KeyDown)
             {
                 return false;
@@ -257,7 +262,8 @@ impl FocusState {
             }
             node.insert(Focused(true));
             if let Some(old) = self.last_focused_id.replace(id) {
-                let focused = rdom.get_state_mut_raw::<Focused>(old).unwrap();
+                let mut focused_borrow: ViewMut<Focused> = rdom.raw_world().borrow().unwrap();
+                let focused = (&mut focused_borrow).get(old).unwrap();
                 focused.0 = false;
             }
             // reset the position to the currently focused element

+ 8 - 7
packages/tui/src/layout.rs

@@ -1,11 +1,13 @@
 use std::sync::{Arc, Mutex};
 
+use dioxus_native_core::exports::shipyard::Component;
 use dioxus_native_core::layout_attributes::{
     apply_layout_attributes_cfg, BorderWidths, LayoutConfigeration,
 };
 use dioxus_native_core::node::OwnedAttributeView;
 use dioxus_native_core::node_ref::{AttributeMaskBuilder, NodeMaskBuilder, NodeView};
 use dioxus_native_core::{Dependancy, SendAnyMap, State};
+use dioxus_native_core_macro::partial_derive_state;
 use taffy::prelude::*;
 
 use crate::{screen_to_layout_space, unit_to_layout_space};
@@ -36,12 +38,13 @@ impl<T> Default for PossiblyUninitalized<T> {
     }
 }
 
-#[derive(Clone, PartialEq, Default, Debug)]
+#[derive(Clone, PartialEq, Default, Debug, Component)]
 pub(crate) struct TaffyLayout {
     pub style: Style,
     pub node: PossiblyUninitalized<Node>,
 }
 
+#[partial_derive_state]
 impl State for TaffyLayout {
     type ChildDependencies = (Self,);
     type ParentDependencies = ();
@@ -56,7 +59,7 @@ impl State for TaffyLayout {
         node_view: NodeView,
         _: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
         _: Option<<Self::ParentDependencies as Dependancy>::ElementBorrowed<'a>>,
-        children: Option<Vec<<Self::ChildDependencies as Dependancy>::ElementBorrowed<'a>>>,
+        children: Vec<<Self::ChildDependencies as Dependancy>::ElementBorrowed<'a>>,
         ctx: &SendAnyMap,
     ) -> bool {
         let mut changed = false;
@@ -110,10 +113,8 @@ impl State for TaffyLayout {
 
             // Set all direct nodes as our children
             let mut child_layout = vec![];
-            if let Some(children) = children {
-                for (l,) in children {
-                    child_layout.push(l.node.unwrap());
-                }
+            for (l,) in children {
+                child_layout.push(l.node.unwrap());
             }
 
             fn scale_dimention(d: Dimension) -> Dimension {
@@ -194,7 +195,7 @@ impl State for TaffyLayout {
         node_view: NodeView<()>,
         node: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
         parent: Option<<Self::ParentDependencies as Dependancy>::ElementBorrowed<'a>>,
-        children: Option<Vec<<Self::ChildDependencies as Dependancy>::ElementBorrowed<'a>>>,
+        children: Vec<<Self::ChildDependencies as Dependancy>::ElementBorrowed<'a>>,
         context: &SendAnyMap,
     ) -> Self {
         let mut myself = Self::default();

+ 5 - 5
packages/tui/src/lib.rs

@@ -7,7 +7,7 @@ use crossterm::{
     terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
 };
 use dioxus_html::EventData;
-use dioxus_native_core::{node_ref::NodeMaskBuilder, real_dom::NodeImmutable, State};
+use dioxus_native_core::{node_ref::NodeMaskBuilder, prelude::AnyState, real_dom::NodeImmutable};
 use dioxus_native_core::{real_dom::RealDom, FxDashSet, NodeId, SendAnyMap};
 use focus::FocusState;
 use futures::{channel::mpsc::UnboundedSender, pin_mut, Future, StreamExt};
@@ -123,7 +123,7 @@ pub fn render<R: Renderer>(
         let mut any_map = SendAnyMap::new();
         any_map.insert(taffy.clone());
         let mut rdom = rdom.write().unwrap();
-        let _ = rdom.update_state(any_map, false);
+        let _ = rdom.update_state(any_map);
     }
 
     tokio::runtime::Builder::new_current_thread()
@@ -148,7 +148,7 @@ pub fn render<R: Renderer>(
             }
 
             let mut to_rerender = FxDashSet::default();
-            to_rerender.insert(NodeId(0));
+            to_rerender.insert(rdom.read().unwrap().root_id());
             let mut updated = true;
 
             loop {
@@ -167,7 +167,7 @@ pub fn render<R: Renderer>(
                         let width = screen_to_layout_space(dims.width);
                         let height = screen_to_layout_space(dims.height);
                         let root_node = rdom
-                            .get(NodeId(0))
+                            .get(rdom.root_id())
                             .unwrap()
                             .get::<TaffyLayout>()
                             .unwrap()
@@ -270,7 +270,7 @@ pub fn render<R: Renderer>(
                     let mut rdom = rdom.write().unwrap();
                     let mut any_map = SendAnyMap::new();
                     any_map.insert(taffy.clone());
-                    let (new_to_rerender, dirty) = rdom.update_state(any_map, false);
+                    let (new_to_rerender, dirty) = rdom.update_state(any_map);
                     to_rerender = new_to_rerender;
                     let text_mask = NodeMaskBuilder::new().with_text().build();
                     for (id, mask) in dirty {

+ 6 - 3
packages/tui/src/prevent_default.rs

@@ -1,6 +1,8 @@
 use dioxus_native_core::{node_ref::NodeView, Dependancy, SendAnyMap, State};
+use dioxus_native_core_macro::partial_derive_state;
+use shipyard::Component;
 
-#[derive(PartialEq, Debug, Clone, Copy)]
+#[derive(PartialEq, Debug, Clone, Copy, Component)]
 pub(crate) enum PreventDefault {
     Focus,
     KeyPress,
@@ -25,6 +27,7 @@ impl Default for PreventDefault {
     }
 }
 
+#[partial_derive_state]
 impl State for PreventDefault {
     type ParentDependencies = ();
     type ChildDependencies = ();
@@ -42,7 +45,7 @@ impl State for PreventDefault {
         node_view: NodeView,
         _: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
         _: Option<<Self::ParentDependencies as Dependancy>::ElementBorrowed<'a>>,
-        _: Option<Vec<<Self::ChildDependencies as Dependancy>::ElementBorrowed<'a>>>,
+        _: Vec<<Self::ChildDependencies as Dependancy>::ElementBorrowed<'a>>,
         _: &SendAnyMap,
     ) -> bool {
         let new = match node_view.attributes().and_then(|mut attrs| {
@@ -78,7 +81,7 @@ impl State for PreventDefault {
         node_view: NodeView<()>,
         node: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
         parent: Option<<Self::ParentDependencies as Dependancy>::ElementBorrowed<'a>>,
-        children: Option<Vec<<Self::ChildDependencies as Dependancy>::ElementBorrowed<'a>>>,
+        children: Vec<<Self::ChildDependencies as Dependancy>::ElementBorrowed<'a>>,
         context: &SendAnyMap,
     ) -> Self {
         let mut myself = Self::default();

+ 7 - 6
packages/tui/src/render.rs

@@ -28,7 +28,7 @@ pub(crate) fn render_vnode(
 ) {
     use dioxus_native_core::node::NodeType;
 
-    if let NodeType::Placeholder = &node.node_type() {
+    if let NodeType::Placeholder = &*node.node_type() {
         return;
     }
 
@@ -47,7 +47,7 @@ pub(crate) fn render_vnode(
     let width = layout_to_screen_space(fx + width).round() as u16 - x;
     let height = layout_to_screen_space(fy + height).round() as u16 - y;
 
-    match node.node_type() {
+    match &*node.node_type() {
         NodeType::Text(text) => {
             #[derive(Default)]
             struct Label<'a> {
@@ -272,10 +272,11 @@ impl RinkWidget for NodeRef<'_> {
                 if let Some(c) = self.get::<StyleModifier>().unwrap().core.bg {
                     new_cell.bg = c;
                 }
-                let focused = self.get::<Focused>();
-                if focused.is_some() && focused.unwrap().0 {
-                    new_cell.bg.alpha = 100;
-                    new_cell.bg.color = new_cell.bg.blend(Color::White);
+                if let Some(focused) = self.get::<Focused>() {
+                    if focused.0 {
+                        new_cell.bg.alpha = 100;
+                        new_cell.bg.color = new_cell.bg.blend(Color::White);
+                    }
                 }
                 buf.set(x, y, new_cell);
             }

+ 6 - 3
packages/tui/src/style_attributes.rs

@@ -35,16 +35,19 @@ use dioxus_native_core::{
     node_ref::{AttributeMaskBuilder, NodeMaskBuilder, NodeView},
     Dependancy, SendAnyMap, State,
 };
+use dioxus_native_core_macro::partial_derive_state;
+use shipyard::Component;
 use taffy::prelude::*;
 
 use crate::style::{RinkColor, RinkStyle};
 
-#[derive(Default, Clone, PartialEq, Debug)]
+#[derive(Default, Clone, PartialEq, Debug, Component)]
 pub struct StyleModifier {
     pub core: RinkStyle,
     pub modifier: TuiModifier,
 }
 
+#[partial_derive_state]
 impl State for StyleModifier {
     type ParentDependencies = (Self,);
     type ChildDependencies = ();
@@ -60,7 +63,7 @@ impl State for StyleModifier {
         node_view: NodeView,
         _: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
         parent: Option<<Self::ParentDependencies as Dependancy>::ElementBorrowed<'a>>,
-        _: Option<Vec<<Self::ChildDependencies as Dependancy>::ElementBorrowed<'a>>>,
+        _: Vec<<Self::ChildDependencies as Dependancy>::ElementBorrowed<'a>>,
         _: &SendAnyMap,
     ) -> bool {
         let mut new = StyleModifier::default();
@@ -117,7 +120,7 @@ impl State for StyleModifier {
         node_view: NodeView<()>,
         node: <Self::NodeDependencies as Dependancy>::ElementBorrowed<'a>,
         parent: Option<<Self::ParentDependencies as Dependancy>::ElementBorrowed<'a>>,
-        children: Option<Vec<<Self::ChildDependencies as Dependancy>::ElementBorrowed<'a>>>,
+        children: Vec<<Self::ChildDependencies as Dependancy>::ElementBorrowed<'a>>,
         context: &SendAnyMap,
     ) -> Self {
         let mut myself = Self::default();