refactor out typemap
This commit is contained in:
@@ -1,25 +1,17 @@
|
||||
use crate::{
|
||||
ActiveData, Event, EventCtx, EventFn, EventLike, IdLike, LayerId, Ui, WidgetData, WidgetId,
|
||||
util::HashMap,
|
||||
};
|
||||
use std::{
|
||||
any::{Any, TypeId},
|
||||
rc::Rc,
|
||||
util::{HashMap, TypeMap},
|
||||
};
|
||||
use std::{any::TypeId, rc::Rc};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct EventManager {
|
||||
types: HashMap<TypeId, Box<dyn EventManagerLike>>,
|
||||
types: TypeMap<dyn EventManagerLike>,
|
||||
}
|
||||
|
||||
impl EventManager {
|
||||
pub fn get_type<E: EventLike, Ctx: 'static>(&mut self) -> &mut TypeEventManager<E::Event, Ctx> {
|
||||
self.types
|
||||
.entry(Self::type_key::<E, Ctx>())
|
||||
.or_insert(Box::new(TypeEventManager::<E::Event, Ctx>::default()))
|
||||
.as_any()
|
||||
.downcast_mut()
|
||||
.unwrap()
|
||||
self.types.type_or_default()
|
||||
}
|
||||
|
||||
pub fn register<I: IdLike, E: EventLike, Ctx: 'static>(
|
||||
@@ -58,7 +50,6 @@ pub trait EventManagerLike {
|
||||
fn remove(&mut self, id: WidgetId);
|
||||
fn draw(&mut self, data: &ActiveData);
|
||||
fn undraw(&mut self, data: &ActiveData);
|
||||
fn as_any(&mut self) -> &mut dyn Any;
|
||||
}
|
||||
|
||||
type EventData<E, Ctx> = (E, Rc<dyn for<'a> EventFn<Ctx, <E as Event>::Data<'a>>>);
|
||||
@@ -87,9 +78,6 @@ impl<E: Event, Ctx: 'static> EventManagerLike for TypeEventManager<E, Ctx> {
|
||||
layer.remove(&data.id);
|
||||
}
|
||||
}
|
||||
fn as_any(&mut self) -> &mut dyn Any {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Event, Ctx> Default for TypeEventManager<E, Ctx> {
|
||||
|
||||
Reference in New Issue
Block a user