Dynamic scoping
This commit is contained in:
+18
-14
@@ -23,21 +23,25 @@ fn main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn app(cx: &Arc<Context>) -> View {
|
fn app(cx: &Arc<Context>) -> View {
|
||||||
cx.child(
|
{
|
||||||
my_component,
|
let scope = cx.scope();
|
||||||
Some(Arc::new(|ctx, view| {
|
|
||||||
let area = ctx.allocate(0, 0, 0, 0);
|
|
||||||
ctx.draw_view(area, 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, 0, 0, 0);
|
||||||
ctx.draw_view(area, view)
|
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({
|
Arc::new({
|
||||||
let cx = cx.clone();
|
let cx = cx.clone();
|
||||||
|
|||||||
+51
-28
@@ -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() {
|
||||||
child.emit_event(event);
|
for (child, _) in scope.children.lock().unwrap().iter() {
|
||||||
|
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,13 +121,41 @@ impl Context {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (child, _) in self.children.lock().unwrap().iter() {
|
for scope in self.scopes.lock().unwrap().iter() {
|
||||||
child.emit_event_threaded(event);
|
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>(
|
pub fn child<F: Fn(&Arc<Context>) -> View + Send + Sync + 'static>(
|
||||||
self: &Arc<Context>,
|
self: &Arc<Self>,
|
||||||
child: F,
|
child: F,
|
||||||
view_wrapper: Option<ViewWrapper>,
|
view_wrapper: Option<ViewWrapper>,
|
||||||
) {
|
) {
|
||||||
@@ -130,16 +165,4 @@ impl Context {
|
|||||||
|
|
||||||
self.children.lock().unwrap().push((ctx, view_wrapper));
|
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user