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
+15 -6
View File
@@ -113,9 +113,12 @@ impl Context {
self.scopes.access(move |scopes| {
for scope in scopes {
for (child, _) in scope.children.lock().unwrap().iter() {
let 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();
self.scopes.access(move |scopes| {
for scope in scopes {
for (child, _) in scope.children.lock().unwrap().iter() {
let event = event.clone();
scope.children.access(move |children| {
for (child, _) in children {
child.emit_event_threaded(&event);
}
});
}
});
}
@@ -189,13 +195,16 @@ impl Context {
}
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>();
self.scopes.access(move |scopes| {
for scope in scopes {
for (child, view_wrapper) in scope.children.lock().unwrap().iter() {
let mut c = c.clone();
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 {
@@ -203,9 +212,9 @@ impl Context {
} else {
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 super::{context::Context, ComponentImpl};
pub struct Scope {
pub children: Mutex<Vec<(Arc<Context>, Option<ViewWrapper>)>>,
pub children: AccessCell<Vec<(Arc<Context>, Option<ViewWrapper>)>>,
executor: Arc<dyn CommandExecutor>,
}
impl Scope {
pub fn new(executor: Arc<dyn CommandExecutor>) -> Arc<Self> {
Arc::new(Self {
children: Mutex::new(Vec::new()),
children: AccessCell::new(Vec::new()),
executor,
})
}
@@ -26,7 +28,8 @@ impl Scope {
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) {
@@ -34,6 +37,6 @@ impl Scope {
ctx.refresh();
self.children.lock().unwrap().push((ctx, None));
self.children.access(|children| children.push((ctx, None)));
}
}