proper widgetid + slot vec instead of map

This commit is contained in:
2025-12-11 16:23:14 -05:00
parent 2dad409300
commit 966b6a2ac2
12 changed files with 606 additions and 152 deletions

View File

@@ -1,7 +1,4 @@
use crate::{
Ui, UiModule, WidgetView,
util::{HashMap, Id},
};
use crate::{Ui, UiModule, WidgetId, WidgetView, util::HashMap};
use std::{hash::Hash, rc::Rc};
pub trait Event: Sized {
@@ -41,23 +38,23 @@ impl<E: DefaultEvent> Event for E {
}
pub trait EventModule<E: Event, Ctx>: UiModule + Default {
fn register(&mut self, id: Id, event: E, f: impl EventFn<Ctx, E::Data>);
fn register(&mut self, id: WidgetId, event: E, f: impl EventFn<Ctx, E::Data>);
fn run<'a>(
&self,
id: &Id,
id: WidgetId,
event: E,
) -> Option<impl Fn(EventCtx<Ctx, E::Data>) + use<'a, Self, E, Ctx>>;
}
type EventFnMap<Ctx, Data> = HashMap<Id, Vec<Rc<dyn EventFn<Ctx, Data>>>>;
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: &Id) {
fn on_remove(&mut self, id: WidgetId) {
for map in self.map.values_mut() {
map.remove(id);
map.remove(&id);
}
}
}
@@ -66,7 +63,7 @@ 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: Id, event: E, f: impl EventFn<Ctx, <E as Event>::Data>) {
fn register(&mut self, id: WidgetId, event: E, f: impl EventFn<Ctx, <E as Event>::Data>) {
self.map
.entry(event)
.or_default()
@@ -77,11 +74,11 @@ impl<E: HashableEvent, Ctx: 'static> EventModule<E, Ctx> for DefaultEventModule<
fn run<'a>(
&self,
id: &Id,
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 Some(fs) = map.get(&id)
{
let fs = fs.clone();
Some(move |ctx: EventCtx<Ctx, <E as Event>::Data>| {
@@ -138,7 +135,7 @@ impl Ui {
.data
.modules
.get_mut::<E::Module<Ctx>>()
.run(&id.id(), event)
.run(id.id(), event)
{
f(EventCtx {
ui: self,