remove modules and have single event manager (atomics feature parity + preparation for local state)

This commit is contained in:
2025-12-12 01:46:24 -05:00
parent a708813ce7
commit 37b1987aa8
17 changed files with 390 additions and 432 deletions

View File

@@ -1,26 +1,172 @@
use crate::{Ui, UiModule, Widget, WidgetId, WidgetRef, util::HashMap};
use crate::{
ActiveData, IdLike, LayerId, Ui, Widget, WidgetData, WidgetId, WidgetRef, util::HashMap,
};
use std::{
hash::Hash,
any::{Any, TypeId},
ops::{Index, IndexMut},
rc::Rc,
};
pub trait Event: Sized {
type Module<Ctx: 'static>: EventModule<Self, Ctx>;
type Data: Clone;
pub trait Event: Sized + 'static + Clone {
type Data<'a> = ();
type State: Default = ();
#[allow(unused_variables)]
fn should_run(&self, data: &mut Self::Data<'_>) -> bool {
true
}
}
pub trait EventLike {
type Event: Event;
fn into_event(self) -> Self::Event;
}
impl<E: Event> EventLike for E {
type Event = Self;
fn into_event(self) -> Self::Event {
self
}
}
pub struct EventCtx<'a, Ctx, Data> {
pub ui: &'a mut Ui,
pub state: &'a mut Ctx,
pub data: Data,
pub data: &'a mut Data,
}
pub struct EventIdCtx<'a, Ctx, Data, W: ?Sized> {
pub widget: WidgetRef<W>,
pub ui: &'a mut Ui,
pub state: &'a mut Ctx,
pub data: Data,
pub data: &'a mut Data,
}
#[derive(Default)]
pub struct EventManager {
types: HashMap<TypeId, Box<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()
}
pub fn register<I: IdLike, E: EventLike, Ctx: 'static>(
&mut self,
id: I,
event: E,
f: impl for<'a> EventFn<Ctx, <E::Event as Event>::Data<'a>>,
) {
self.get_type::<E, Ctx>().register(id, event, f);
}
pub fn type_key<E: EventLike, Ctx: 'static>() -> TypeId {
TypeId::of::<TypeEventManager<E::Event, Ctx>>()
}
pub fn remove(&mut self, id: WidgetId) {
for m in self.types.values_mut() {
m.remove(id);
}
}
pub 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, active: &ActiveData) {
for t in &data.event_mgrs {
self.types.get_mut(t).unwrap().undraw(active);
}
}
}
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>>>);
pub struct TypeEventManager<E: Event, Ctx> {
// TODO: reduce visiblity!!
pub active: HashMap<LayerId, HashMap<WidgetId, E::State>>,
map: HashMap<WidgetId, Vec<EventData<E, Ctx>>>,
}
impl<E: Event, Ctx: 'static> EventManagerLike for TypeEventManager<E, Ctx> {
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);
}
}
fn as_any(&mut self) -> &mut dyn Any {
self
}
}
impl<E: Event, Ctx> Default for TypeEventManager<E, Ctx> {
fn default() -> Self {
Self {
active: Default::default(),
map: Default::default(),
}
}
}
impl<E: Event, Ctx: 'static> TypeEventManager<E, Ctx> {
fn register(
&mut self,
id: impl IdLike,
event: impl EventLike<Event = E>,
f: impl for<'a> EventFn<Ctx, E::Data<'a>>,
) {
let event = event.into_event();
self.map
.entry(id.id())
.or_default()
.push((event, Rc::new(f)));
}
fn run_fn<'a>(
&mut self,
id: impl IdLike,
) -> impl for<'b> FnOnce(EventCtx<Ctx, E::Data<'b>>) + 'a {
let fs = self.map.get(&id.id()).cloned().unwrap_or_default();
move |ctx| {
for (e, f) in fs {
if e.should_run(ctx.data) {
f(EventCtx {
ui: ctx.ui,
state: ctx.state,
data: ctx.data,
})
}
}
}
}
}
impl<'a, Ctx, Data, W2, W: Widget> Index<WidgetRef<W>> for EventIdCtx<'a, Ctx, Data, W2> {
@@ -52,120 +198,18 @@ impl<F: Fn(EventIdCtx<Ctx, Data, W>) + 'static, Ctx, Data, W: ?Sized> WidgetEven
{
}
pub trait DefaultEvent: Hash + Eq + 'static {
type Data: Clone = ();
}
impl<E: DefaultEvent> Event for E {
type Module<Ctx: 'static> = DefaultEventModule<E, Ctx>;
type Data = E::Data;
}
pub trait EventModule<E: Event, Ctx>: UiModule + Default {
fn register(&mut self, id: WidgetId, event: E, f: impl EventFn<Ctx, E::Data>);
fn run<'a>(
&self,
id: WidgetId,
event: E,
) -> Option<impl Fn(EventCtx<Ctx, E::Data>) + use<'a, Self, E, Ctx>>;
}
type EventFnMap<Ctx, Data> = HashMap<WidgetId, Vec<Rc<dyn EventFn<Ctx, Data>>>>;
pub struct DefaultEventModule<E: Event, Ctx> {
map: HashMap<E, EventFnMap<Ctx, <E as Event>::Data>>,
}
impl<E: Event + 'static, Ctx: 'static> UiModule for DefaultEventModule<E, Ctx> {
fn on_remove(&mut self, id: WidgetId) {
for map in self.map.values_mut() {
map.remove(&id);
}
}
}
pub trait HashableEvent: Event + Hash + Eq + 'static {}
impl<E: Event + Hash + Eq + 'static> HashableEvent for E {}
impl<E: HashableEvent, Ctx: 'static> EventModule<E, Ctx> for DefaultEventModule<E, Ctx> {
fn register(&mut self, id: WidgetId, event: E, f: impl EventFn<Ctx, <E as Event>::Data>) {
self.map
.entry(event)
.or_default()
.entry(id)
.or_default()
.push(Rc::new(f));
}
fn run<'a>(
&self,
id: WidgetId,
event: E,
) -> Option<impl Fn(EventCtx<Ctx, E::Data>) + use<'a, E, Ctx>> {
if let Some(map) = self.map.get(&event)
&& let Some(fs) = map.get(&id)
{
let fs = fs.clone();
Some(move |ctx: EventCtx<Ctx, <E as Event>::Data>| {
for f in &fs {
f(EventCtx {
ui: ctx.ui,
state: ctx.state,
data: ctx.data.clone(),
})
}
})
} else {
None
}
}
}
impl<E: HashableEvent, Ctx: 'static> DefaultEventModule<E, Ctx> {
pub fn run_all(&self, event: E, ctx: EventCtx<Ctx, E::Data>)
where
E::Data: Clone,
{
if let Some(map) = self.map.get(&event) {
for fs in map.values() {
for f in fs {
f(EventCtx {
ui: ctx.ui,
state: ctx.state,
data: ctx.data.clone(),
})
}
}
}
}
}
impl<E: Event + 'static, Ctx: 'static> Default for DefaultEventModule<E, Ctx> {
fn default() -> Self {
Self {
map: Default::default(),
}
}
}
impl Ui {
pub fn run_event<E: Event, Ctx: 'static, W>(
pub fn run_event<E: EventLike, Ctx: 'static>(
&mut self,
ctx: &mut Ctx,
id: WidgetRef<W>,
event: E,
data: E::Data,
id: impl IdLike,
data: &mut <E::Event as Event>::Data<'_>,
) {
if let Some(f) = self
.data
.modules
.get_mut::<E::Module<Ctx>>()
.run(id.id(), event)
{
f(EventCtx {
ui: self,
state: ctx,
data,
});
}
let f = self.data.events.get_type::<E, Ctx>().run_fn(id);
f(EventCtx {
ui: self,
state: ctx,
data,
})
}
}