Context children

This commit is contained in:
2026-01-24 17:22:59 +01:00
parent abe37686d8
commit 9d55dacde2
3 changed files with 34 additions and 13 deletions
+27 -1
View File
@@ -4,7 +4,7 @@ use std::{
sync::{Arc, Mutex},
};
use crate::View;
use crate::{render::DrawContext, View, ViewWrapper};
pub trait Event {
fn as_any(&self) -> &dyn Any;
@@ -23,6 +23,7 @@ pub struct Context {
component: Mutex<Component>,
event_handlers: Mutex<HashMap<TypeId, Vec<EventHandler>>>,
view: Mutex<View>,
children: Mutex<Vec<(Arc<Context>, Option<ViewWrapper>)>>,
}
impl Context {
@@ -31,6 +32,7 @@ impl Context {
component: Mutex::new(Arc::new(component)),
event_handlers: Mutex::new(HashMap::new()),
view: Mutex::new(Arc::new(|_| {})),
children: Mutex::new(Vec::new()),
})
}
@@ -108,4 +110,28 @@ impl Context {
}
}
}
pub fn child<F: Fn(&Arc<Context>) -> View + Send + Sync + 'static>(
self: &Arc<Context>,
child: F,
view_wrapper: Option<ViewWrapper>,
) {
let ctx = Context::new(child);
ctx.refresh();
self.children.lock().unwrap().push((ctx, view_wrapper));
}
pub fn draw_children(self: &Arc<Context>, ctx: &mut DrawContext) {
for (child, view_wrapper) in self.children.lock().unwrap().iter() {
let view = child.get_view();
if let Some(view_wrapper) = view_wrapper {
view_wrapper(ctx, view)
} else {
ctx.draw_view(ctx.allocated.clone(), view);
}
}
}
}
+1
View File
@@ -16,3 +16,4 @@ pub mod prelude {
}
pub type View = Arc<dyn Fn(&mut DrawContext) + Send + Sync>;
pub type ViewWrapper = Arc<dyn Fn(&mut DrawContext, View) + Send + Sync>;