From 993a790da72ab0cea6557964679679244f89b464 Mon Sep 17 00:00:00 2001 From: Leo dev Date: Sun, 25 Jan 2026 20:00:21 +0100 Subject: [PATCH] If statement rsx --- examples/hello_world.rs | 10 ++++-- src/macros.rs | 68 ++++++++++++++++++++++++++++++++--------- 2 files changed, 61 insertions(+), 17 deletions(-) diff --git a/examples/hello_world.rs b/examples/hello_world.rs index dfbca25..9f99450 100644 --- a/examples/hello_world.rs +++ b/examples/hello_world.rs @@ -83,10 +83,14 @@ fn app(cx: &Arc) -> View { // } rsx! { - my_component (ctx, view) { - let area = ctx.allocate(0, 0, 10, 10); - ctx.draw_view(area, view); + if %count (*count.get() > 0) { + my_component (ctx, view) { + let area = ctx.allocate(0, 1, 10, 10); + ctx.draw_view(area, view); + } } + + %count "{count}" } .view(cx.clone()) } diff --git a/src/macros.rs b/src/macros.rs index 28afe93..7dd86a0 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -6,39 +6,71 @@ macro_rules! rsx { ($($rsx:tt)+) => {{ let mut r = $crate::frontend::Rsx::new(); - $crate::rsx_scope!(r, $($rsx)+); + { + $crate::rsx_scope!(r, $($rsx)+); + } r }}; } #[macro_export] macro_rules! rsx_scope { + ($rsx:expr, if $(%$($dep:ident)+)? ($st:expr) {$($inner:tt)*} $($rest:tt)*) => { + { + $($(let $dep = $dep.clone();)+)? + + $rsx.dynamic_scope({ + $($(let $dep = $dep.clone();)+)? + move |scope| { + if $st { + if scope.children.lock().unwrap().len() == 0 { + $crate::rsx_child!(scope, $($inner)*); + } + } else { + scope.children.lock().unwrap().clear(); + } + } + }, vec![$($(Box::new($dep)),+)?]); + } + + $crate::rsx_scope!($rsx, $($rest)*); + }; + ($rsx:expr, $(%$($dep:ident)+)? $text:literal $($rest:tt)*) => { - let scope = $crate::component::Scope::new(); + { + let scope = $crate::component::Scope::new(); - $($(let $dep = $dep.clone();)+)? + $($(let $dep = $dep.clone();)+)? - $crate::rsx_child!(scope, $text); + $crate::rsx_child!(scope, $text); + + $rsx.static_scope(scope); + } - $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(); + { + let scope = $crate::component::Scope::new(); - $crate::rsx_child!(scope, $component ($ctx, $view) $body); + $crate::rsx_child!(scope, $component ($ctx, $view) $body); + + $rsx.static_scope(scope); + } - $rsx.static_scope(scope); $crate::rsx_scope!($rsx, $($rest)*); }; ($rsx:expr, $component:ident $($rest:tt)*) => { - let scope = $crate::component::Scope::new(); + { + let scope = $crate::component::Scope::new(); - $crate::rsx_child!(scope, $component $body); + $crate::rsx_child!(scope, $component $body); + + $rsx.static_scope(scope); + } - $rsx.static_scope(scope); $crate::rsx_scope!($rsx, $($rest)*); }; @@ -48,17 +80,25 @@ macro_rules! rsx_scope { #[macro_export] macro_rules! rsx_child { ($scope:expr, $text:literal $($rest:tt)*) => { - $scope.view(Arc::new(move |ctx| ctx.draw_text(Point { x: 0, y: 0 }, &format!($text)))); + { + $scope.view(Arc::new(move |ctx| ctx.draw_text(Point { x: 0, y: 0 }, &format!($text)))); + } + $crate::rsx_child!($scope, $($rest)*); }; ($scope:expr, $component:ident ($ctx:ident, $view:ident) $body:block $($rest:tt)*) => { - $scope.child($component, Some(Arc::new(|$ctx, $view| $body))); + { + $scope.child($component, Some(Arc::new(|$ctx, $view| $body))); + } + $crate::rsx_child!($scope, $($rest)*); }; ($scope:expr, $component:ident $($rest:tt)*) => { - $scope.child($component, None); + { + $scope.child($component, None); + } $crate::rsx_child!($scope, $($rest)*); };