Refresh on children

This commit is contained in:
2026-01-24 16:40:03 +01:00
parent 02f3aea4e3
commit abe37686d8
2 changed files with 43 additions and 11 deletions
+18 -8
View File
@@ -23,6 +23,24 @@ fn main() {
}
fn app(cx: &Arc<Context>) -> 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<Context>) -> View {
let count = use_state(0);
cx.on_event({
@@ -39,13 +57,5 @@ fn app(cx: &Arc<Context>) -> 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}"));
}
+25 -3
View File
@@ -35,10 +35,32 @@ impl Context {
}
pub fn refresh(self: &Arc<Context>) {
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<Context>) -> Arc<Mutex<bool>> {
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<Context>) -> View {