If statement rsx

This commit is contained in:
2026-01-25 20:00:21 +01:00
parent ecc1172737
commit 993a790da7
2 changed files with 61 additions and 17 deletions
+5 -1
View File
@@ -83,11 +83,15 @@ fn app(cx: &Arc<Context>) -> View {
// } // }
rsx! { rsx! {
if %count (*count.get() > 0) {
my_component (ctx, view) { my_component (ctx, view) {
let area = ctx.allocate(0, 0, 10, 10); let area = ctx.allocate(0, 1, 10, 10);
ctx.draw_view(area, view); ctx.draw_view(area, view);
} }
} }
%count "{count}"
}
.view(cx.clone()) .view(cx.clone())
} }
+40
View File
@@ -6,14 +6,38 @@ macro_rules! rsx {
($($rsx:tt)+) => {{ ($($rsx:tt)+) => {{
let mut r = $crate::frontend::Rsx::new(); let mut r = $crate::frontend::Rsx::new();
{
$crate::rsx_scope!(r, $($rsx)+); $crate::rsx_scope!(r, $($rsx)+);
}
r r
}}; }};
} }
#[macro_export] #[macro_export]
macro_rules! rsx_scope { 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)*) => { ($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();)+)?
@@ -21,24 +45,32 @@ macro_rules! rsx_scope {
$crate::rsx_child!(scope, $text); $crate::rsx_child!(scope, $text);
$rsx.static_scope(scope); $rsx.static_scope(scope);
}
$crate::rsx_scope!($rsx, $($rest)*); $crate::rsx_scope!($rsx, $($rest)*);
}; };
($rsx:expr, $component:ident ($ctx:ident, $view:ident) $body:block $($rest:tt)*) => { ($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)*); $crate::rsx_scope!($rsx, $($rest)*);
}; };
($rsx:expr, $component:ident $($rest:tt)*) => { ($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)*); $crate::rsx_scope!($rsx, $($rest)*);
}; };
@@ -48,17 +80,25 @@ macro_rules! rsx_scope {
#[macro_export] #[macro_export]
macro_rules! rsx_child { macro_rules! rsx_child {
($scope:expr, $text:literal $($rest:tt)*) => { ($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)*); $crate::rsx_child!($scope, $($rest)*);
}; };
($scope:expr, $component:ident ($ctx:ident, $view:ident) $body:block $($rest:tt)*) => { ($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)*); $crate::rsx_child!($scope, $($rest)*);
}; };
($scope:expr, $component:ident $($rest:tt)*) => { ($scope:expr, $component:ident $($rest:tt)*) => {
{
$scope.child($component, None); $scope.child($component, None);
}
$crate::rsx_child!($scope, $($rest)*); $crate::rsx_child!($scope, $($rest)*);
}; };