separate state from rsc
This commit is contained in:
@@ -2,40 +2,31 @@ use crate::{
|
||||
Event, EventCtx, EventLike, EventManager, HasUi, IdLike, Widget, WidgetEventFn, WidgetRef,
|
||||
};
|
||||
|
||||
pub trait HasState {
|
||||
type State: HasUi;
|
||||
}
|
||||
pub trait HasEvents: Sized + HasUi + 'static {
|
||||
type State;
|
||||
|
||||
pub trait HasEvents: Sized + HasState + HasUi {
|
||||
fn get(&self) -> &EventManager<Self::State>;
|
||||
fn get_mut(&mut self) -> &mut EventManager<Self::State>;
|
||||
fn events(&self) -> &EventManager<Self::State> {
|
||||
HasEvents::get(self)
|
||||
}
|
||||
fn events_mut(&mut self) -> &mut EventManager<Self::State> {
|
||||
HasEvents::get_mut(self)
|
||||
}
|
||||
fn events(&self) -> &EventManager<Self>;
|
||||
fn events_mut(&mut self) -> &mut EventManager<Self>;
|
||||
|
||||
fn register_event<W: Widget + ?Sized, E: EventLike>(
|
||||
&mut self,
|
||||
id: WidgetRef<W>,
|
||||
event: E,
|
||||
f: impl for<'a> WidgetEventFn<Self::State, <E::Event as Event>::Data<'a>, W>,
|
||||
) where
|
||||
Self::State: 'static,
|
||||
{
|
||||
f: impl for<'a> WidgetEventFn<Self, <E::Event as Event>::Data<'a>, W>,
|
||||
) {
|
||||
self.events_mut().register(id, event, f);
|
||||
}
|
||||
}
|
||||
|
||||
pub trait RunEvents: HasEvents + HasState<State = Self> + 'static {
|
||||
pub trait RunEvents: HasEvents + 'static {
|
||||
fn run_event<E: EventLike>(
|
||||
&mut self,
|
||||
id: impl IdLike,
|
||||
data: &mut <E::Event as Event>::Data<'_>,
|
||||
state: &mut Self::State,
|
||||
) {
|
||||
let f = self.events_mut().get_type::<E>().run_fn(id);
|
||||
f(EventCtx { state: self, data })
|
||||
f(EventCtx { state, data }, self)
|
||||
}
|
||||
}
|
||||
impl<T: HasEvents + HasState<State = Self> + 'static> RunEvents for T {}
|
||||
impl<T: HasEvents + 'static> RunEvents for T {}
|
||||
|
||||
Reference in New Issue
Block a user