better global state structure?
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
use crate::{
|
||||
ActiveData, Event, EventCtx, EventFn, EventLike, HasRsc, HasUi, IdLike, LayerId, WidgetData,
|
||||
WidgetId,
|
||||
ActiveData, Event, EventCtx, EventFn, EventIdCtx, EventLike, IdLike, LayerId, Widget,
|
||||
WidgetData, WidgetEventFn, WidgetId, WidgetRef,
|
||||
util::{HashMap, TypeMap},
|
||||
};
|
||||
use std::{any::TypeId, rc::Rc};
|
||||
@@ -22,11 +22,11 @@ impl<State: 'static> EventManager<State> {
|
||||
self.types.type_or_default()
|
||||
}
|
||||
|
||||
pub fn register<I: IdLike, E: EventLike>(
|
||||
pub fn register<W: Widget + ?Sized, E: EventLike>(
|
||||
&mut self,
|
||||
id: I,
|
||||
id: WidgetRef<W>,
|
||||
event: E,
|
||||
f: impl for<'a> EventFn<State, <E::Event as Event>::Data<'a>>,
|
||||
f: impl for<'a> WidgetEventFn<State, <E::Event as Event>::Data<'a>, W>,
|
||||
) {
|
||||
self.get_type::<E>().register(id, event, f);
|
||||
}
|
||||
@@ -106,17 +106,23 @@ impl<State, E: Event> Default for TypeEventManager<State, E> {
|
||||
}
|
||||
|
||||
impl<State: 'static, E: Event> TypeEventManager<State, E> {
|
||||
fn register(
|
||||
fn register<W: Widget + ?Sized>(
|
||||
&mut self,
|
||||
id: impl IdLike,
|
||||
widget: WidgetRef<W>,
|
||||
event: impl EventLike<Event = E>,
|
||||
f: impl for<'a> EventFn<State, E::Data<'a>>,
|
||||
f: impl for<'a> WidgetEventFn<State, E::Data<'a>, W>,
|
||||
) {
|
||||
let event = event.into_event();
|
||||
self.map
|
||||
.entry(id.id())
|
||||
.or_default()
|
||||
.push((event, Rc::new(f)));
|
||||
self.map.entry(widget.id()).or_default().push((
|
||||
event,
|
||||
Rc::new(move |ctx| {
|
||||
f(&mut EventIdCtx {
|
||||
widget,
|
||||
state: ctx.state,
|
||||
data: ctx.data,
|
||||
});
|
||||
}),
|
||||
));
|
||||
}
|
||||
|
||||
pub fn run_fn<'a>(
|
||||
@@ -127,7 +133,7 @@ impl<State: 'static, E: Event> TypeEventManager<State, E> {
|
||||
move |ctx| {
|
||||
for (e, f) in fs {
|
||||
if e.should_run(ctx.data) {
|
||||
f(EventCtx {
|
||||
f(&mut EventCtx {
|
||||
state: ctx.state,
|
||||
data: ctx.data,
|
||||
})
|
||||
@@ -136,27 +142,3 @@ impl<State: 'static, E: Event> TypeEventManager<State, E> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait HasEvents: Sized {
|
||||
type State: HasRsc<Rsc = Self>;
|
||||
|
||||
fn events(&mut self) -> &mut EventManager<Self::State>;
|
||||
|
||||
fn register_event<I: IdLike, E: EventLike>(
|
||||
&mut self,
|
||||
id: I,
|
||||
event: E,
|
||||
f: impl for<'a> EventFn<Self::State, <E::Event as Event>::Data<'a>>,
|
||||
) where
|
||||
Self: HasUi,
|
||||
{
|
||||
let id = id.id();
|
||||
self.events().register(id, event, f);
|
||||
self.ui()
|
||||
.widgets
|
||||
.data_mut(id)
|
||||
.unwrap()
|
||||
.event_mgrs
|
||||
.insert(EventManager::<Self::State>::type_key::<E>());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user