From 0a026c4ee7997902058ac09dc07f1a11a0b6f878 Mon Sep 17 00:00:00 2001 From: Leo dev Date: Sat, 24 Jan 2026 00:28:43 +0100 Subject: [PATCH] View --- examples/hello_world.rs | 19 +++++-------------- src/component.rs | 18 ++++++++---------- src/lib.rs | 21 +++------------------ 3 files changed, 16 insertions(+), 42 deletions(-) diff --git a/examples/hello_world.rs b/examples/hello_world.rs index 89445cb..2a279b6 100644 --- a/examples/hello_world.rs +++ b/examples/hello_world.rs @@ -24,24 +24,13 @@ fn main() { }); loop { - println!("{:?}", cx.get_nodes()); + cx.get_view()(&mut DrawContext {}); std::thread::sleep(std::time::Duration::from_millis(16)); } } -fn app(cx: &Arc) -> Vec { +fn app(cx: &Arc) -> View { let count = use_state(0); - let count2 = use_state(0); - - use_effect( - { - let count2 = count2.clone(); - move || { - *count2.get() += 1; - } - }, - &[&count], - ); cx.on_event({ let count = count.clone(); @@ -52,5 +41,7 @@ fn app(cx: &Arc) -> Vec { } }); - vec![Node::String(Arc::new(move || format!("{count} {count2}")))] + Arc::new(move |_| { + println!("Count: {count}"); + }) } diff --git a/src/component.rs b/src/component.rs index 6cf344b..2d8bccf 100644 --- a/src/component.rs +++ b/src/component.rs @@ -4,7 +4,7 @@ use std::{ sync::{Arc, Mutex}, }; -use crate::Node; +use crate::View; pub trait Event { fn as_any(&self) -> &dyn Any; @@ -16,33 +16,31 @@ impl<'a> dyn Event + 'a { } } -pub type Component = Arc) -> Vec + Send + Sync>; +pub type Component = Arc) -> View + Send + Sync>; pub type EventHandler = Arc, &dyn Event) + Send + Sync>>; pub struct Context { component: Mutex, - nodes: Mutex>, event_handlers: Mutex>>, + view: Mutex, } impl Context { - pub fn new) -> Vec + Send + Sync + 'static>( - component: F, - ) -> Arc { + pub fn new) -> View + Send + Sync + 'static>(component: F) -> Arc { Arc::new(Self { component: Mutex::new(Arc::new(component)), - nodes: Mutex::new(Vec::new()), event_handlers: Mutex::new(HashMap::new()), + view: Mutex::new(Arc::new(|_| {})), }) } pub fn refresh(self: &Arc) { let c = self.component.lock().unwrap().clone(); - *self.nodes.lock().unwrap() = (c)(self); + *self.view.lock().unwrap() = (c)(self); } - pub fn get_nodes(self: &Arc) -> Vec { - self.nodes.lock().unwrap().clone() + pub fn get_view(self: &Arc) -> View { + self.view.lock().unwrap().clone() } pub fn on_event, &T) + Send + Sync + 'static>( diff --git a/src/lib.rs b/src/lib.rs index 357c3ac..6cb4c55 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,24 +6,9 @@ pub mod state; pub mod prelude { pub use crate::component::*; pub use crate::state::*; - pub use crate::Node; + pub use crate::{DrawContext, View}; } -#[derive(Clone)] -pub enum Node { - String(Arc String + Send + Sync>), - Component(component::Component), -} +pub type View = Arc; -impl std::fmt::Debug for Node { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!( - f, - "{}", - match self { - Self::Component(_) => format!("Component"), - Self::String(v) => format!("String({})", v()), - } - ) - } -} +pub struct DrawContext {}