From 05a390b10feaf8427d6fb7caa9e8970d7f44c85c Mon Sep 17 00:00:00 2001 From: Leo dev Date: Sun, 25 Jan 2026 00:30:53 +0100 Subject: [PATCH] Dynamic scoping --- examples/hello_world.rs | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/examples/hello_world.rs b/examples/hello_world.rs index 1d45b1a..23aeeab 100644 --- a/examples/hello_world.rs +++ b/examples/hello_world.rs @@ -23,6 +23,8 @@ fn main() { } fn app(cx: &Arc) -> View { + let count = use_state(0); + { let scope = cx.scope(); @@ -34,13 +36,28 @@ fn app(cx: &Arc) -> View { })), ); - scope.child( - my_component, - Some(Arc::new(|ctx, view| { - let area = ctx.allocate(0, 1, 0, 0); - ctx.draw_view(area, view) - })), - ); + cx.on_event({ + let count = count.clone(); + move |_, _: &KeyPress| { + let mut count = count.get(); + + if *count > 5 { + if scope.children.lock().unwrap().len() == 1 { + scope.child( + my_component, + Some(Arc::new(|ctx, view| { + let area = ctx.allocate(0, 1, 0, 0); + ctx.draw_view(area, view) + })), + ); + } + + *count = 0; + } + + *count += 1; + } + }); } Arc::new({ @@ -53,7 +70,7 @@ fn app(cx: &Arc) -> View { } fn my_component(cx: &Arc) -> View { - let count = use_state(0); + let count = use_state(1); cx.on_event({ let count = count.clone();