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
+7 -3
View File
@@ -83,10 +83,14 @@ fn app(cx: &Arc<Context>) -> 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())
}
+54 -14
View File
@@ -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)*);
};