remove state generic from a lot of things
This commit is contained in:
1 parent
7e6369029f
commit
30bc55c78e
45 files changed
+740
-856
No files matched your search
@@ -8,13 +8,13 @@ pub struct EventCtx<'a, State, Data> {
|
||||
}
|
||||
|
||||
pub struct EventIdCtx<'a, State, Data, W: ?Sized> {
|
||||
pub widget: WidgetRef<State, W>,
|
||||
pub widget: WidgetRef<W>,
|
||||
pub state: &'a mut State,
|
||||
pub data: &'a mut Data,
|
||||
}
|
||||
|
||||
impl<State: HasUi, Data, W: ?Sized> Deref for EventIdCtx<'_, State, Data, W> {
|
||||
type Target = Ui<State>;
|
||||
type Target = Ui;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
self.state.ui_ref()
|
||||
@@ -27,7 +27,7 @@ impl<State: HasUi, Data, W: ?Sized> DerefMut for EventIdCtx<'_, State, Data, W>
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, State: HasUi + 'static, Data, W: Widget<State>> EventIdCtx<'a, State, Data, W> {
|
||||
impl<'a, State: HasUi + 'static, Data, W: Widget> EventIdCtx<'a, State, Data, W> {
|
||||
pub fn widget(&mut self) -> &mut W {
|
||||
&mut self.state.ui()[self.widget]
|
||||
}
|
||||
|
||||
+35
-21
@@ -1,5 +1,6 @@
|
||||
use crate::{
|
||||
ActiveData, Event, EventCtx, EventFn, EventLike, HasUi, IdLike, LayerId, WidgetData, WidgetId,
|
||||
ActiveData, Event, EventCtx, EventFn, EventLike, HasRsc, HasUi, IdLike, LayerId, WidgetData,
|
||||
WidgetId,
|
||||
util::{HashMap, TypeMap},
|
||||
};
|
||||
use std::{any::TypeId, rc::Rc};
|
||||
@@ -21,7 +22,7 @@ impl<State: 'static> EventManager<State> {
|
||||
self.types.type_or_default()
|
||||
}
|
||||
|
||||
pub fn register<I: IdLike<State>, E: EventLike>(
|
||||
pub fn register<I: IdLike, E: EventLike>(
|
||||
&mut self,
|
||||
id: I,
|
||||
event: E,
|
||||
@@ -33,20 +34,28 @@ impl<State: 'static> EventManager<State> {
|
||||
pub fn type_key<E: EventLike>() -> TypeId {
|
||||
TypeId::of::<TypeEventManager<State, E::Event>>()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn remove(&mut self, id: WidgetId) {
|
||||
pub trait EventsLike {
|
||||
fn remove(&mut self, id: WidgetId);
|
||||
fn draw(&mut self, data: &WidgetData, active: &ActiveData);
|
||||
fn undraw(&mut self, data: &WidgetData, active: &ActiveData);
|
||||
}
|
||||
|
||||
impl<State: 'static> EventsLike for EventManager<State> {
|
||||
fn remove(&mut self, id: WidgetId) {
|
||||
for m in self.types.values_mut() {
|
||||
m.remove(id);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn draw(&mut self, data: &WidgetData<State>, active: &ActiveData) {
|
||||
fn draw(&mut self, data: &WidgetData, active: &ActiveData) {
|
||||
for t in &data.event_mgrs {
|
||||
self.types.get_mut(t).unwrap().draw(active);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn undraw(&mut self, data: &WidgetData<State>, active: &ActiveData) {
|
||||
fn undraw(&mut self, data: &WidgetData, active: &ActiveData) {
|
||||
for t in &data.event_mgrs {
|
||||
self.types.get_mut(t).unwrap().undraw(active);
|
||||
}
|
||||
@@ -99,7 +108,7 @@ impl<State, E: Event> Default for TypeEventManager<State, E> {
|
||||
impl<State: 'static, E: Event> TypeEventManager<State, E> {
|
||||
fn register(
|
||||
&mut self,
|
||||
id: impl IdLike<State>,
|
||||
id: impl IdLike,
|
||||
event: impl EventLike<Event = E>,
|
||||
f: impl for<'a> EventFn<State, E::Data<'a>>,
|
||||
) {
|
||||
@@ -112,7 +121,7 @@ impl<State: 'static, E: Event> TypeEventManager<State, E> {
|
||||
|
||||
pub fn run_fn<'a>(
|
||||
&mut self,
|
||||
id: impl IdLike<State>,
|
||||
id: impl IdLike,
|
||||
) -> impl for<'b> FnOnce(EventCtx<State, E::Data<'b>>) + 'a {
|
||||
let fs = self.map.get(&id.id()).cloned().unwrap_or_default();
|
||||
move |ctx| {
|
||||
@@ -128,21 +137,26 @@ impl<State: 'static, E: Event> TypeEventManager<State, E> {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait HasEvents: Sized + HasUi {
|
||||
fn run_event<E: EventLike>(
|
||||
&mut self,
|
||||
id: impl IdLike<Self>,
|
||||
data: &mut <E::Event as Event>::Data<'_>,
|
||||
);
|
||||
}
|
||||
pub trait HasEvents: Sized {
|
||||
type State: HasRsc<Rsc = Self>;
|
||||
|
||||
impl<State: HasUi + 'static> HasEvents for State {
|
||||
fn run_event<E: EventLike>(
|
||||
fn events(&mut self) -> &mut EventManager<Self::State>;
|
||||
|
||||
fn register_event<I: IdLike, E: EventLike>(
|
||||
&mut self,
|
||||
id: impl IdLike<Self>,
|
||||
data: &mut <E::Event as Event>::Data<'_>,
|
||||
) {
|
||||
let f = self.ui().events.get_type::<E>().run_fn(id);
|
||||
f(EventCtx { state: self, data })
|
||||
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>());
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
mod ctx;
|
||||
mod manager;
|
||||
mod rsc;
|
||||
|
||||
pub use ctx::*;
|
||||
pub use manager::*;
|
||||
pub use rsc::*;
|
||||
|
||||
pub trait Event: Sized + 'static + Clone {
|
||||
type Data<'a> = ();
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
use crate::{Event, EventCtx, EventLike, EventManager, HasEvents, HasUi, IdLike};
|
||||
|
||||
pub trait HasRsc: Sized + 'static {
|
||||
type Rsc: HasEvents<State = Self> + HasUi;
|
||||
fn rsc(&self) -> &Self::Rsc;
|
||||
fn rsc_mut(&mut self) -> &mut Self::Rsc;
|
||||
|
||||
fn run_event<E: EventLike>(&mut self, id: impl IdLike, data: &mut <E::Event as Event>::Data<'_>)
|
||||
where
|
||||
Self: 'static,
|
||||
{
|
||||
let f = self.rsc_mut().events().get_type::<E>().run_fn(id);
|
||||
f(EventCtx { state: self, data })
|
||||
}
|
||||
|
||||
fn events(&mut self) -> &mut EventManager<Self> {
|
||||
self.rsc_mut().events()
|
||||
}
|
||||
}
|
||||
|
||||
impl<R: HasRsc> HasUi for R {
|
||||
fn ui_ref(&self) -> &crate::Ui {
|
||||
self.rsc().ui_ref()
|
||||
}
|
||||
|
||||
fn ui(&mut self) -> &mut crate::Ui {
|
||||
self.rsc_mut().ui()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user