Scope children with AccessCell

This commit is contained in:
2026-01-31 14:44:28 +01:00
parent 8685cdde2a
commit d8173b8ab3
2 changed files with 32 additions and 20 deletions
+24 -15
View File
@@ -113,9 +113,12 @@ impl Context {
self.scopes.access(move |scopes| { self.scopes.access(move |scopes| {
for scope in scopes { for scope in scopes {
for (child, _) in scope.children.lock().unwrap().iter() { let event = event.clone();
child.emit_event(event.clone()); scope.children.access(move |children| {
} for (child, _) in children {
child.emit_event(event.clone());
}
});
} }
}); });
} }
@@ -139,9 +142,12 @@ impl Context {
let event = event.clone(); let event = event.clone();
self.scopes.access(move |scopes| { self.scopes.access(move |scopes| {
for scope in scopes { for scope in scopes {
for (child, _) in scope.children.lock().unwrap().iter() { let event = event.clone();
child.emit_event_threaded(&event); scope.children.access(move |children| {
} for (child, _) in children {
child.emit_event_threaded(&event);
}
});
} }
}); });
} }
@@ -189,23 +195,26 @@ impl Context {
} }
pub fn draw_children(self: &Arc<Self>, ctx: &mut DrawContext) { pub fn draw_children(self: &Arc<Self>, ctx: &mut DrawContext) {
let mut c = ctx.clone(); let c = ctx.clone();
let (tx, rx) = std::sync::mpsc::channel::<DrawContext>(); let (tx, rx) = std::sync::mpsc::channel::<DrawContext>();
self.scopes.access(move |scopes| { self.scopes.access(move |scopes| {
for scope in scopes { for scope in scopes {
for (child, view_wrapper) in scope.children.lock().unwrap().iter() { let mut c = c.clone();
let view = child.get_view(); let tx = tx.clone();
scope.children.access(move |children| {
for (child, view_wrapper) in children {
let view = child.get_view();
if let Some(view_wrapper) = view_wrapper { if let Some(view_wrapper) = view_wrapper {
view_wrapper(&mut c, view) view_wrapper(&mut c, view)
} else { } else {
c.draw_view(c.area.clone(), view); c.draw_view(c.area.clone(), view);
}
} }
tx.send(c.clone()).expect("Failed transmitting DrawContext"); tx.send(c.clone()).expect("Failed transmitting DrawContext");
} });
} }
}); });
+8 -5
View File
@@ -1,18 +1,20 @@
use std::sync::{Arc, Mutex}; use std::sync::Arc;
use access_cell::AccessCell;
use crate::{engine::CommandExecutor, View, ViewWrapper}; use crate::{engine::CommandExecutor, View, ViewWrapper};
use super::{context::Context, ComponentImpl}; use super::{context::Context, ComponentImpl};
pub struct Scope { pub struct Scope {
pub children: Mutex<Vec<(Arc<Context>, Option<ViewWrapper>)>>, pub children: AccessCell<Vec<(Arc<Context>, Option<ViewWrapper>)>>,
executor: Arc<dyn CommandExecutor>, executor: Arc<dyn CommandExecutor>,
} }
impl Scope { impl Scope {
pub fn new(executor: Arc<dyn CommandExecutor>) -> Arc<Self> { pub fn new(executor: Arc<dyn CommandExecutor>) -> Arc<Self> {
Arc::new(Self { Arc::new(Self {
children: Mutex::new(Vec::new()), children: AccessCell::new(Vec::new()),
executor, executor,
}) })
} }
@@ -26,7 +28,8 @@ impl Scope {
ctx.refresh(); ctx.refresh();
self.children.lock().unwrap().push((ctx, view_wrapper)); self.children
.access(|children| children.push((ctx, view_wrapper)));
} }
pub fn view(self: &Arc<Self>, view: View) { pub fn view(self: &Arc<Self>, view: View) {
@@ -34,6 +37,6 @@ impl Scope {
ctx.refresh(); ctx.refresh();
self.children.lock().unwrap().push((ctx, None)); self.children.access(|children| children.push((ctx, None)));
} }
} }