Refresh on children
This commit is contained in:
+18
-8
@@ -23,6 +23,24 @@ fn main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn app(cx: &Arc<Context>) -> View {
|
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);
|
let count = use_state(0);
|
||||||
|
|
||||||
cx.on_event({
|
cx.on_event({
|
||||||
@@ -39,13 +57,5 @@ fn app(cx: &Arc<Context>) -> View {
|
|||||||
|
|
||||||
Arc::new(move |ctx| {
|
Arc::new(move |ctx| {
|
||||||
ctx.draw_text(Point { x: 0, y: 0 }, &format!("Count: {count}"));
|
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
@@ -35,10 +35,32 @@ impl Context {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn refresh(self: &Arc<Context>) {
|
pub fn refresh(self: &Arc<Context>) {
|
||||||
self.event_handlers.lock().unwrap().clear();
|
let s = self.clone();
|
||||||
|
|
||||||
let c = self.component.lock().unwrap().clone();
|
std::thread::spawn({
|
||||||
*self.view.lock().unwrap() = (c)(self);
|
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 {
|
pub fn get_view(self: &Arc<Context>) -> View {
|
||||||
|
|||||||
Reference in New Issue
Block a user