Context children
This commit is contained in:
+6
-12
@@ -23,20 +23,14 @@ fn main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn app(cx: &Arc<Context>) -> View {
|
fn app(cx: &Arc<Context>) -> View {
|
||||||
let mc_cx = Context::new(my_component);
|
cx.child(my_component, None);
|
||||||
|
|
||||||
cx.on_event({
|
Arc::new({
|
||||||
let mc_cx = mc_cx.clone();
|
let cx = cx.clone();
|
||||||
move |_, &KeyPress| {
|
|
||||||
mc_cx.emit_event(&KeyPress);
|
move |ctx| {
|
||||||
|
cx.draw_children(ctx);
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
mc_cx.refresh();
|
|
||||||
|
|
||||||
Arc::new(move |ctx| {
|
|
||||||
let area = ctx.allocate(0, 0, 10, 10);
|
|
||||||
ctx.draw_view(area, mc_cx.get_view());
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+27
-1
@@ -4,7 +4,7 @@ use std::{
|
|||||||
sync::{Arc, Mutex},
|
sync::{Arc, Mutex},
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::View;
|
use crate::{render::DrawContext, View, ViewWrapper};
|
||||||
|
|
||||||
pub trait Event {
|
pub trait Event {
|
||||||
fn as_any(&self) -> &dyn Any;
|
fn as_any(&self) -> &dyn Any;
|
||||||
@@ -23,6 +23,7 @@ pub struct Context {
|
|||||||
component: Mutex<Component>,
|
component: Mutex<Component>,
|
||||||
event_handlers: Mutex<HashMap<TypeId, Vec<EventHandler>>>,
|
event_handlers: Mutex<HashMap<TypeId, Vec<EventHandler>>>,
|
||||||
view: Mutex<View>,
|
view: Mutex<View>,
|
||||||
|
children: Mutex<Vec<(Arc<Context>, Option<ViewWrapper>)>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Context {
|
impl Context {
|
||||||
@@ -31,6 +32,7 @@ impl Context {
|
|||||||
component: Mutex::new(Arc::new(component)),
|
component: Mutex::new(Arc::new(component)),
|
||||||
event_handlers: Mutex::new(HashMap::new()),
|
event_handlers: Mutex::new(HashMap::new()),
|
||||||
view: Mutex::new(Arc::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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,3 +16,4 @@ pub mod prelude {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub type View = Arc<dyn Fn(&mut DrawContext) + Send + Sync>;
|
pub type View = Arc<dyn Fn(&mut DrawContext) + Send + Sync>;
|
||||||
|
pub type ViewWrapper = Arc<dyn Fn(&mut DrawContext, View) + Send + Sync>;
|
||||||
|
|||||||
Reference in New Issue
Block a user