157 lines
4.4 KiB
Rust
157 lines
4.4 KiB
Rust
use crate::{
|
|
ActiveData, Event, EventCtx, EventFn, EventIdCtx, EventLike, HasEvents, IdLike, LayerId,
|
|
Widget, WidgetEventFn, WidgetId, WidgetRef,
|
|
util::{HashMap, HashSet, TypeMap},
|
|
};
|
|
use std::{any::TypeId, rc::Rc};
|
|
|
|
pub struct EventManager<Rsc> {
|
|
widget_to_types: HashMap<WidgetId, HashSet<TypeId>>,
|
|
types: TypeMap<dyn EventManagerLike<Rsc>>,
|
|
}
|
|
|
|
impl<Rsc> Default for EventManager<Rsc> {
|
|
fn default() -> Self {
|
|
Self {
|
|
widget_to_types: Default::default(),
|
|
types: Default::default(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<Rsc: HasEvents + 'static> EventManager<Rsc> {
|
|
pub fn get_type<E: EventLike>(&mut self) -> &mut TypeEventManager<Rsc, E::Event> {
|
|
self.types.type_or_default()
|
|
}
|
|
|
|
pub fn register<W: Widget + ?Sized, E: EventLike>(
|
|
&mut self,
|
|
id: WidgetRef<W>,
|
|
event: E,
|
|
f: impl WidgetEventFn<Rsc, <E::Event as Event>::Data, W>,
|
|
) {
|
|
self.get_type::<E>().register(id, event, f);
|
|
self.widget_to_types
|
|
.entry(id.id())
|
|
.or_default()
|
|
.insert(Self::type_key::<E>());
|
|
}
|
|
|
|
pub fn type_key<E: EventLike>() -> TypeId {
|
|
TypeId::of::<TypeEventManager<Rsc, E::Event>>()
|
|
}
|
|
}
|
|
|
|
pub trait EventsLike {
|
|
fn remove(&mut self, id: WidgetId);
|
|
fn draw(&mut self, active: &ActiveData);
|
|
fn undraw(&mut self, active: &ActiveData);
|
|
}
|
|
|
|
impl<Rsc: HasEvents + 'static> EventsLike for EventManager<Rsc> {
|
|
fn remove(&mut self, id: WidgetId) {
|
|
for t in self.widget_to_types.get(&id).into_flat_iter() {
|
|
self.types.get_mut(t).unwrap().remove(id);
|
|
}
|
|
}
|
|
|
|
fn draw(&mut self, active: &ActiveData) {
|
|
for t in self.widget_to_types.get(&active.id).into_flat_iter() {
|
|
self.types.get_mut(t).unwrap().draw(active);
|
|
}
|
|
}
|
|
|
|
fn undraw(&mut self, active: &ActiveData) {
|
|
for t in self.widget_to_types.get(&active.id).into_flat_iter() {
|
|
self.types.get_mut(t).unwrap().undraw(active);
|
|
}
|
|
}
|
|
}
|
|
|
|
pub trait EventManagerLike<State> {
|
|
fn remove(&mut self, id: WidgetId);
|
|
fn draw(&mut self, data: &ActiveData);
|
|
fn undraw(&mut self, data: &ActiveData);
|
|
}
|
|
|
|
type EventData<Rsc, E> = (E, Rc<dyn EventFn<Rsc, <E as Event>::Data>>);
|
|
pub struct TypeEventManager<Rsc: HasEvents, E: Event> {
|
|
// TODO: reduce visiblity!!
|
|
pub active: HashMap<LayerId, HashMap<WidgetId, E::State>>,
|
|
map: HashMap<WidgetId, Vec<EventData<Rsc, E>>>,
|
|
}
|
|
|
|
impl<Rsc: HasEvents, E: Event> EventManagerLike<Rsc> for TypeEventManager<Rsc, E> {
|
|
fn remove(&mut self, id: WidgetId) {
|
|
self.map.remove(&id);
|
|
for layer in self.active.values_mut() {
|
|
layer.remove(&id);
|
|
}
|
|
}
|
|
fn draw(&mut self, data: &ActiveData) {
|
|
self.active
|
|
.entry(data.layer)
|
|
.or_default()
|
|
.entry(data.id)
|
|
.or_default();
|
|
}
|
|
fn undraw(&mut self, data: &ActiveData) {
|
|
if let Some(layer) = self.active.get_mut(&data.layer) {
|
|
layer.remove(&data.id);
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<Rsc: HasEvents, E: Event> Default for TypeEventManager<Rsc, E> {
|
|
fn default() -> Self {
|
|
Self {
|
|
active: Default::default(),
|
|
map: Default::default(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<Rsc: HasEvents + 'static, E: Event> TypeEventManager<Rsc, E> {
|
|
fn register<W: Widget + ?Sized>(
|
|
&mut self,
|
|
widget: WidgetRef<W>,
|
|
event: impl EventLike<Event = E>,
|
|
f: impl WidgetEventFn<Rsc, E::Data, W>,
|
|
) {
|
|
let event = event.into_event();
|
|
self.map.entry(widget.id()).or_default().push((
|
|
event,
|
|
Rc::new(move |ctx, rsc| {
|
|
f(
|
|
EventIdCtx {
|
|
widget,
|
|
state: ctx.state,
|
|
data: ctx.data,
|
|
},
|
|
rsc,
|
|
);
|
|
}),
|
|
));
|
|
}
|
|
|
|
pub fn run_fn<'a>(
|
|
&mut self,
|
|
id: impl IdLike,
|
|
) -> impl FnOnce(EventCtx<'_, Rsc, E::Data>, &mut Rsc) + 'a {
|
|
let fs = self.map.get(&id.id()).cloned().unwrap_or_default();
|
|
move |ctx, rsc| {
|
|
for (e, f) in fs {
|
|
if let Some(data) = e.should_run(&ctx.data) {
|
|
f(
|
|
EventCtx {
|
|
state: ctx.state,
|
|
data,
|
|
},
|
|
rsc,
|
|
)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|