Dynamic scoping

This commit is contained in:
2026-01-24 21:38:45 +01:00
parent 9953e4a0bc
commit 926405a863
2 changed files with 69 additions and 42 deletions
+6 -2
View File
@@ -23,7 +23,10 @@ fn main() {
} }
fn app(cx: &Arc<Context>) -> View { fn app(cx: &Arc<Context>) -> View {
cx.child( {
let scope = cx.scope();
scope.child(
my_component, my_component,
Some(Arc::new(|ctx, view| { Some(Arc::new(|ctx, view| {
let area = ctx.allocate(0, 0, 0, 0); let area = ctx.allocate(0, 0, 0, 0);
@@ -31,13 +34,14 @@ fn app(cx: &Arc<Context>) -> View {
})), })),
); );
cx.child( scope.child(
my_component, my_component,
Some(Arc::new(|ctx, view| { Some(Arc::new(|ctx, view| {
let area = ctx.allocate(0, 1, 0, 0); let area = ctx.allocate(0, 1, 0, 0);
ctx.draw_view(area, view) ctx.draw_view(area, view)
})), })),
); );
}
Arc::new({ Arc::new({
let cx = cx.clone(); let cx = cx.clone();
+49 -26
View File
@@ -19,24 +19,29 @@ impl<'a> dyn Event + 'a {
pub type Component = Arc<dyn Fn(&Arc<Context>) -> View + Send + Sync>; pub type Component = Arc<dyn Fn(&Arc<Context>) -> View + Send + Sync>;
pub type EventHandler = Arc<Mutex<dyn FnMut(&Arc<Context>, &dyn Event) + Send + Sync>>; pub type EventHandler = Arc<Mutex<dyn FnMut(&Arc<Context>, &dyn Event) + Send + Sync>>;
pub struct Scope {
pub children: Mutex<Vec<(Arc<Context>, Option<ViewWrapper>)>>,
pub scope: Mutex<usize>,
}
pub struct Context { 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>)>>, scopes: Mutex<Vec<Arc<Scope>>>,
} }
impl Context { impl Context {
pub fn new<F: Fn(&Arc<Context>) -> View + Send + Sync + 'static>(component: F) -> Arc<Self> { pub fn new<F: Fn(&Arc<Self>) -> View + Send + Sync + 'static>(component: F) -> Arc<Self> {
Arc::new(Self { Arc::new(Self {
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()), scopes: Mutex::new(Vec::new()),
}) })
} }
pub fn refresh(self: &Arc<Context>) { pub fn refresh(self: &Arc<Self>) {
let s = self.clone(); let s = self.clone();
std::thread::spawn({ std::thread::spawn({
@@ -48,7 +53,7 @@ impl Context {
}); });
} }
pub fn refresh_atomic(self: &Arc<Context>) -> Arc<Mutex<bool>> { pub fn refresh_atomic(self: &Arc<Self>) -> Arc<Mutex<bool>> {
let s = self.clone(); let s = self.clone();
let done = Arc::new(Mutex::new(false)); let done = Arc::new(Mutex::new(false));
@@ -65,12 +70,12 @@ impl Context {
done done
} }
pub fn get_view(self: &Arc<Context>) -> View { pub fn get_view(self: &Arc<Self>) -> View {
self.view.lock().unwrap().clone() self.view.lock().unwrap().clone()
} }
pub fn on_event<T: Event + 'static, F: Fn(&Arc<Context>, &T) + Send + Sync + 'static>( pub fn on_event<T: Event + 'static, F: Fn(&Arc<Self>, &T) + Send + Sync + 'static>(
self: &Arc<Context>, self: &Arc<Self>,
handler: F, handler: F,
) { ) {
self.event_handlers self.event_handlers
@@ -79,7 +84,7 @@ impl Context {
.entry(TypeId::of::<T>()) .entry(TypeId::of::<T>())
.or_insert_with(|| Vec::new()) .or_insert_with(|| Vec::new())
.push(Arc::new(Mutex::new( .push(Arc::new(Mutex::new(
move |ctx: &Arc<Context>, event: &dyn Event| { move |ctx: &Arc<Self>, event: &dyn Event| {
if let Some(e) = event.get::<T>() { if let Some(e) = event.get::<T>() {
(handler)(ctx, e); (handler)(ctx, e);
} }
@@ -87,20 +92,22 @@ impl Context {
))); )));
} }
pub fn emit_event<E: Event + 'static>(self: &Arc<Context>, event: &E) { pub fn emit_event<E: Event + 'static>(self: &Arc<Self>, event: &E) {
if let Some(v) = self.event_handlers.lock().unwrap().get(&TypeId::of::<E>()) { if let Some(v) = self.event_handlers.lock().unwrap().get(&TypeId::of::<E>()) {
for i in v { for i in v {
(i.lock().unwrap())(self, event); (i.lock().unwrap())(self, event);
} }
} }
for (child, _) in self.children.lock().unwrap().iter() { for scope in self.scopes.lock().unwrap().iter() {
for (child, _) in scope.children.lock().unwrap().iter() {
child.emit_event(event); child.emit_event(event);
} }
} }
}
pub fn emit_event_threaded<E: Event + Send + Sync + Clone + 'static>( pub fn emit_event_threaded<E: Event + Send + Sync + Clone + 'static>(
self: &Arc<Context>, self: &Arc<Self>,
event: &E, event: &E,
) { ) {
if let Some(v) = self.event_handlers.lock().unwrap().get(&TypeId::of::<E>()) { if let Some(v) = self.event_handlers.lock().unwrap().get(&TypeId::of::<E>()) {
@@ -114,25 +121,26 @@ impl Context {
} }
} }
for (child, _) in self.children.lock().unwrap().iter() { for scope in self.scopes.lock().unwrap().iter() {
for (child, _) in scope.children.lock().unwrap().iter() {
child.emit_event_threaded(event); child.emit_event_threaded(event);
} }
} }
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) { pub fn scope(self: &Arc<Self>) -> Arc<Scope> {
for (child, view_wrapper) in self.children.lock().unwrap().iter() { let scope = Arc::new(Scope {
children: Mutex::new(Vec::new()),
scope: Mutex::new(0),
});
self.scopes.lock().unwrap().push(scope.clone());
scope
}
pub fn draw_children(self: &Arc<Self>, ctx: &mut DrawContext) {
for scope in self.scopes.lock().unwrap().iter() {
for (child, view_wrapper) in scope.children.lock().unwrap().iter() {
let view = child.get_view(); let view = child.get_view();
if let Some(view_wrapper) = view_wrapper { if let Some(view_wrapper) = view_wrapper {
@@ -142,4 +150,19 @@ impl Context {
} }
} }
} }
}
}
impl Scope {
pub fn child<F: Fn(&Arc<Context>) -> View + Send + Sync + 'static>(
self: &Arc<Self>,
child: F,
view_wrapper: Option<ViewWrapper>,
) {
let ctx = Context::new(child);
ctx.refresh();
self.children.lock().unwrap().push((ctx, view_wrapper));
}
} }