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
+18 -14
View File
@@ -23,21 +23,25 @@ fn main() {
}
fn app(cx: &Arc<Context>) -> View {
cx.child(
my_component,
Some(Arc::new(|ctx, view| {
let area = ctx.allocate(0, 0, 0, 0);
ctx.draw_view(area, view)
})),
);
{
let scope = cx.scope();
cx.child(
my_component,
Some(Arc::new(|ctx, view| {
let area = ctx.allocate(0, 1, 0, 0);
ctx.draw_view(area, view)
})),
);
scope.child(
my_component,
Some(Arc::new(|ctx, view| {
let area = ctx.allocate(0, 0, 0, 0);
ctx.draw_view(area, view)
})),
);
scope.child(
my_component,
Some(Arc::new(|ctx, view| {
let area = ctx.allocate(0, 1, 0, 0);
ctx.draw_view(area, view)
})),
);
}
Arc::new({
let cx = cx.clone();
+51 -28
View File
@@ -19,24 +19,29 @@ impl<'a> dyn Event + 'a {
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 struct Scope {
pub children: Mutex<Vec<(Arc<Context>, Option<ViewWrapper>)>>,
pub scope: Mutex<usize>,
}
pub struct Context {
component: Mutex<Component>,
event_handlers: Mutex<HashMap<TypeId, Vec<EventHandler>>>,
view: Mutex<View>,
children: Mutex<Vec<(Arc<Context>, Option<ViewWrapper>)>>,
scopes: Mutex<Vec<Arc<Scope>>>,
}
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 {
component: Mutex::new(Arc::new(component)),
event_handlers: Mutex::new(HashMap::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();
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 done = Arc::new(Mutex::new(false));
@@ -65,12 +70,12 @@ impl Context {
done
}
pub fn get_view(self: &Arc<Context>) -> View {
pub fn get_view(self: &Arc<Self>) -> View {
self.view.lock().unwrap().clone()
}
pub fn on_event<T: Event + 'static, F: Fn(&Arc<Context>, &T) + Send + Sync + 'static>(
self: &Arc<Context>,
pub fn on_event<T: Event + 'static, F: Fn(&Arc<Self>, &T) + Send + Sync + 'static>(
self: &Arc<Self>,
handler: F,
) {
self.event_handlers
@@ -79,7 +84,7 @@ impl Context {
.entry(TypeId::of::<T>())
.or_insert_with(|| Vec::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>() {
(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>()) {
for i in v {
(i.lock().unwrap())(self, event);
}
}
for (child, _) in self.children.lock().unwrap().iter() {
child.emit_event(event);
for scope in self.scopes.lock().unwrap().iter() {
for (child, _) in scope.children.lock().unwrap().iter() {
child.emit_event(event);
}
}
}
pub fn emit_event_threaded<E: Event + Send + Sync + Clone + 'static>(
self: &Arc<Context>,
self: &Arc<Self>,
event: &E,
) {
if let Some(v) = self.event_handlers.lock().unwrap().get(&TypeId::of::<E>()) {
@@ -114,13 +121,41 @@ impl Context {
}
}
for (child, _) in self.children.lock().unwrap().iter() {
child.emit_event_threaded(event);
for scope in self.scopes.lock().unwrap().iter() {
for (child, _) in scope.children.lock().unwrap().iter() {
child.emit_event_threaded(event);
}
}
}
pub fn scope(self: &Arc<Self>) -> Arc<Scope> {
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();
if let Some(view_wrapper) = view_wrapper {
view_wrapper(ctx, view)
} else {
ctx.draw_view(ctx.area.clone(), view);
}
}
}
}
}
impl Scope {
pub fn child<F: Fn(&Arc<Context>) -> View + Send + Sync + 'static>(
self: &Arc<Context>,
self: &Arc<Self>,
child: F,
view_wrapper: Option<ViewWrapper>,
) {
@@ -130,16 +165,4 @@ impl Context {
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.area.clone(), view);
}
}
}
}