From 04a69b138a8112ff6fd761484fc7721cd453eeb8 Mon Sep 17 00:00:00 2001 From: Leo dev Date: Sun, 25 Jan 2026 20:12:47 +0100 Subject: [PATCH] RSX For loop --- examples/hello_world.rs | 48 ++++------------------------------------- src/macros.rs | 19 +++++++++++++++- 2 files changed, 22 insertions(+), 45 deletions(-) diff --git a/examples/hello_world.rs b/examples/hello_world.rs index 9f99450..b00799d 100644 --- a/examples/hello_world.rs +++ b/examples/hello_world.rs @@ -40,57 +40,17 @@ fn app(cx: &Arc) -> 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! { if %count (*count.get() > 0) { my_component (ctx, view) { - let area = ctx.allocate(0, 1, 10, 10); + let area = ctx.allocate(10, 0, 10, 10); ctx.draw_view(area, view); } } - %count "{count}" + for %count (i in 0..count.get_dl()) { + "{i}" + } } .view(cx.clone()) } diff --git a/src/macros.rs b/src/macros.rs index 7dd86a0..3724013 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -15,6 +15,23 @@ macro_rules! rsx { #[macro_export] macro_rules! rsx_scope { + ($rsx:expr, for $(%$($dep:ident)+)? ($p:pat in $v:expr) {$($inner:tt)*} $($rest:tt)*) => { + { + $($(let $dep = $dep.clone();)+)? + + $rsx.dynamic_scope({ + $($(let $dep = $dep.clone();)+)? + move |scope| { + for $p in $v { + $crate::rsx_child!(scope, $($inner)*); + } + } + }, vec![$($(Box::new($dep)),+)?]); + } + + $crate::rsx_scope!($rsx, $($rest)*); + }; + ($rsx:expr, if $(%$($dep:ident)+)? ($st:expr) {$($inner:tt)*} $($rest:tt)*) => { { $($(let $dep = $dep.clone();)+)? @@ -66,7 +83,7 @@ macro_rules! rsx_scope { { let scope = $crate::component::Scope::new(); - $crate::rsx_child!(scope, $component $body); + $crate::rsx_child!(scope, $component); $rsx.static_scope(scope); }