move event data out of widgetdata

This commit is contained in:
2025-12-23 15:42:04 -05:00
parent 54534c4c34
commit 462c0e6416
7 changed files with 24 additions and 29 deletions

View File

@@ -1,17 +1,19 @@
use crate::{
ActiveData, Event, EventCtx, EventFn, EventIdCtx, EventLike, IdLike, LayerId, Widget,
WidgetData, WidgetEventFn, WidgetId, WidgetRef,
util::{HashMap, TypeMap},
WidgetEventFn, WidgetId, WidgetRef,
util::{HashMap, HashSet, TypeMap},
};
use std::{any::TypeId, rc::Rc};
pub struct EventManager<State> {
widget_to_types: HashMap<WidgetId, HashSet<TypeId>>,
types: TypeMap<dyn EventManagerLike<State>>,
}
impl<State> Default for EventManager<State> {
fn default() -> Self {
Self {
widget_to_types: Default::default(),
types: Default::default(),
}
}
@@ -29,6 +31,10 @@ impl<State: 'static> EventManager<State> {
f: impl for<'a> WidgetEventFn<State, <E::Event as Event>::Data<'a>, 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 {
@@ -38,25 +44,25 @@ impl<State: 'static> EventManager<State> {
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);
fn draw(&mut self, active: &ActiveData);
fn undraw(&mut self, 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);
for t in self.widget_to_types.get(&id).into_flat_iter() {
self.types.get_mut(t).unwrap().remove(id);
}
}
fn draw(&mut self, data: &WidgetData, active: &ActiveData) {
for t in &data.event_mgrs {
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, data: &WidgetData, active: &ActiveData) {
for t in &data.event_mgrs {
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);
}
}