From abe37686d8cf8e539c485d9ebb7100de80894e91 Mon Sep 17 00:00:00 2001 From: Leo dev Date: Sat, 24 Jan 2026 16:40:03 +0100 Subject: [PATCH] Refresh on children --- examples/hello_world.rs | 26 ++++++++++++++++++-------- src/component.rs | 28 +++++++++++++++++++++++++--- 2 files changed, 43 insertions(+), 11 deletions(-) diff --git a/examples/hello_world.rs b/examples/hello_world.rs index d50f1cf..cd7dcd5 100644 --- a/examples/hello_world.rs +++ b/examples/hello_world.rs @@ -23,6 +23,24 @@ fn main() { } fn app(cx: &Arc) -> View { + let mc_cx = Context::new(my_component); + + cx.on_event({ + let mc_cx = mc_cx.clone(); + move |_, &KeyPress| { + mc_cx.emit_event(&KeyPress); + } + }); + + mc_cx.refresh(); + + Arc::new(move |ctx| { + let area = ctx.allocate(0, 0, 10, 10); + ctx.draw_view(area, mc_cx.get_view()); + }) +} + +fn my_component(cx: &Arc) -> View { let count = use_state(0); cx.on_event({ @@ -39,13 +57,5 @@ fn app(cx: &Arc) -> View { Arc::new(move |ctx| { ctx.draw_text(Point { x: 0, y: 0 }, &format!("Count: {count}")); - let area = ctx.allocate(0, 0, 10, 10); - ctx.draw_view(area, Arc::new(my_view)); }) } - -fn my_view(ctx: &mut DrawContext) { - let count = 67; - - ctx.draw_text(Point { x: 0, y: 0 }, &format!("Count: {count}")); -} diff --git a/src/component.rs b/src/component.rs index 6da4dc2..9404930 100644 --- a/src/component.rs +++ b/src/component.rs @@ -35,10 +35,32 @@ impl Context { } pub fn refresh(self: &Arc) { - self.event_handlers.lock().unwrap().clear(); + let s = self.clone(); - let c = self.component.lock().unwrap().clone(); - *self.view.lock().unwrap() = (c)(self); + std::thread::spawn({ + move || { + s.event_handlers.lock().unwrap().clear(); + let c = s.component.lock().unwrap().clone(); + *s.view.lock().unwrap() = (c)(&s); + } + }); + } + + pub fn refresh_atomic(self: &Arc) -> Arc> { + let s = self.clone(); + let done = Arc::new(Mutex::new(false)); + + std::thread::spawn({ + let done = done.clone(); + move || { + s.event_handlers.lock().unwrap().clear(); + let c = s.component.lock().unwrap().clone(); + *s.view.lock().unwrap() = (c)(&s); + *done.lock().unwrap() = true; + } + }); + + done } pub fn get_view(self: &Arc) -> View {