From a9ecc8da8b1c26ac79b87c34c21a7e7822be5afb Mon Sep 17 00:00:00 2001 From: Leo dev Date: Sun, 25 Jan 2026 18:58:24 +0100 Subject: [PATCH] working Rsx --- examples/hello_world.rs | 107 +++++++++++++++++++--------------------- src/component.rs | 6 +++ src/macros.rs | 35 ++++++++++++- 3 files changed, 91 insertions(+), 57 deletions(-) diff --git a/examples/hello_world.rs b/examples/hello_world.rs index 948c67c..dfbca25 100644 --- a/examples/hello_world.rs +++ b/examples/hello_world.rs @@ -40,61 +40,55 @@ fn app(cx: &Arc) -> View { } }); - { - let scope = cx.scope(); + // { + // cx.dyn_scope( + // { + // let count = count.clone(); + // move |scope| { + // if *count.get() != 0 { + // if scope.children.lock().unwrap().len() == 0 { + // scope.child( + // my_component, + // Some(Arc::new(|ctx, view| { + // let area = ctx.allocate(5, 2, 0, 0); + // ctx.draw_view(area, view) + // })), + // ); + // } + // } else { + // scope.children.lock().unwrap().clear(); + // } + // } + // }, + // &[&count], + // ); + // } - scope.child( - my_component, - Some(Arc::new(|ctx, view| { - let area = ctx.allocate(5, 0, 0, 0); - ctx.draw_view(area, view) - })), - ); + // { + // cx.dyn_scope( + // { + // let count = count.clone(); + // move |scope| { + // scope.children.lock().unwrap().clear(); + + // for i in 0..count.get_dl() { + // scope.view(Arc::new(move |ctx| { + // ctx.draw_text(Point { x: 0, y: i }, &format!("{i}")); + // })); + // } + // } + // }, + // &[&count], + // ); + // } + + rsx! { + my_component (ctx, view) { + let area = ctx.allocate(0, 0, 10, 10); + ctx.draw_view(area, view); + } } - - { - cx.dyn_scope( - { - let count = count.clone(); - move |scope| { - if *count.get() != 0 { - if scope.children.lock().unwrap().len() == 0 { - scope.child( - my_component, - Some(Arc::new(|ctx, view| { - let area = ctx.allocate(5, 2, 0, 0); - ctx.draw_view(area, view) - })), - ); - } - } else { - scope.children.lock().unwrap().clear(); - } - } - }, - &[&count], - ); - } - - { - cx.dyn_scope( - { - let count = count.clone(); - move |scope| { - scope.children.lock().unwrap().clear(); - - for i in 0..count.get_dl() { - scope.view(Arc::new(move |ctx| { - ctx.draw_text(Point { x: 0, y: i }, &format!("{i}")); - })); - } - } - }, - &[&count], - ); - } - - rsx! {}.view(cx.clone()) + .view(cx.clone()) } fn my_component(cx: &Arc) -> View { @@ -112,7 +106,8 @@ fn my_component(cx: &Arc) -> View { } }); - Arc::new(move |ctx| { - ctx.draw_text(Point { x: 0, y: 0 }, &format!("Count: {count}")); - }) + rsx! { + "Count: {count}" + } + .view(cx.clone()) } diff --git a/src/component.rs b/src/component.rs index 27f6c14..0d43ed2 100644 --- a/src/component.rs +++ b/src/component.rs @@ -179,6 +179,12 @@ impl Context { } impl Scope { + pub fn new() -> Arc { + Arc::new(Self { + children: Mutex::new(Vec::new()), + }) + } + pub fn child) -> View + Send + Sync + 'static>( self: &Arc, child: F, diff --git a/src/macros.rs b/src/macros.rs index 331ef5c..fbb33c4 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -3,9 +3,42 @@ macro_rules! rsx { () => { $crate::frontend::Rsx::new() }; + + ($($rsx:tt)+) => {{ + let mut r = $crate::frontend::Rsx::new(); + $crate::rsx_scope!(r, $($rsx)+); + r + }}; } #[macro_export] macro_rules! rsx_scope { - () => {}; + ($rsx:expr, $text:literal $($rest:tt)*) => { + let scope = $crate::component::Scope::new(); + + scope.view(Arc::new(move |ctx| ctx.draw_text(Point { x: 0, y: 0 }, &format!($text)))); + + $rsx.static_scope(scope); + $crate::rsx_scope!($rsx, $($rest)*); + }; + + ($rsx:expr, $component:ident ($ctx:ident, $view:ident) $body:block $($rest:tt)*) => { + let scope = $crate::component::Scope::new(); + + scope.child($component, Some(Arc::new(|$ctx, $view| $body))); + + $rsx.static_scope(scope); + $crate::rsx_scope!($rsx, $($rest)*); + }; + + ($rsx:expr, $component:ident $($rest:tt)*) => { + let scope = $crate::component::Scope::new(); + + scope.child($component, None); + + $rsx.static_scope(scope); + $crate::rsx_scope!($rsx, $($rest)*); + }; + + ($rsx:expr,) => {}; }