refactor events

This commit is contained in:
2025-12-12 02:02:54 -05:00
parent a2a32b4322
commit 9d8ca8fa72
4 changed files with 75 additions and 94 deletions

View File

@@ -1,27 +0,0 @@
use std::any::{Any, TypeId};
use crate::util::{HashMap, Id};
#[derive(Default)]
pub struct UiData {
map: HashMap<TypeId, Box<dyn Any>>,
}
impl UiData {
pub fn get<T: 'static>(&self) -> Option<&T> {
self.map
.get(&TypeId::of::<T>())
.map(|d| d.downcast_ref().unwrap())
}
pub fn get_mut<T: 'static>(&mut self) -> Option<&mut T> {
self.map
.get_mut(&TypeId::of::<T>())
.map(|d| d.downcast_mut().unwrap())
}
pub fn emit_remove(&mut self, id: &Id) {
for (tid, f) in &mut self.on_remove {
let data = self.map.get_mut(tid).unwrap().downcast_ref().unwrap();
}
}
}

36
core/src/event/ctx.rs Normal file
View File

@@ -0,0 +1,36 @@
use std::ops::{Index, IndexMut};
use crate::{Ui, Widget, WidgetRef};
pub struct EventCtx<'a, Ctx, Data> {
pub ui: &'a mut Ui,
pub state: &'a mut Ctx,
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: &'a mut Data,
}
impl<'a, Ctx, Data, W2, W: Widget> Index<WidgetRef<W>> for EventIdCtx<'a, Ctx, Data, W2> {
type Output = W;
fn index(&self, index: WidgetRef<W>) -> &Self::Output {
&self.ui[index]
}
}
impl<'a, Ctx, Data, W2, W: Widget> IndexMut<WidgetRef<W>> for EventIdCtx<'a, Ctx, Data, W2> {
fn index_mut(&mut self, index: WidgetRef<W>) -> &mut Self::Output {
&mut self.ui[index]
}
}
impl<'a, Ctx, Data, W: Widget> EventIdCtx<'a, Ctx, Data, W> {
pub fn widget(&mut self) -> &mut W {
&mut self.ui[self.widget]
}
}

View File

@@ -1,47 +1,12 @@
use crate::{
ActiveData, IdLike, LayerId, Ui, Widget, WidgetData, WidgetId, WidgetRef, util::HashMap,
ActiveData, Event, EventCtx, EventFn, EventLike, IdLike, LayerId, Ui, WidgetData, WidgetId,
util::HashMap,
};
use std::{
any::{Any, TypeId},
ops::{Index, IndexMut},
rc::Rc,
};
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: &'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: &'a mut Data,
}
#[derive(Default)]
pub struct EventManager {
types: HashMap<TypeId, Box<dyn EventManagerLike>>,
@@ -150,7 +115,7 @@ impl<E: Event, Ctx: 'static> TypeEventManager<E, Ctx> {
.push((event, Rc::new(f)));
}
fn run_fn<'a>(
pub fn run_fn<'a>(
&mut self,
id: impl IdLike,
) -> impl for<'b> FnOnce(EventCtx<Ctx, E::Data<'b>>) + 'a {
@@ -169,35 +134,6 @@ impl<E: Event, Ctx: 'static> TypeEventManager<E, Ctx> {
}
}
impl<'a, Ctx, Data, W2, W: Widget> Index<WidgetRef<W>> for EventIdCtx<'a, Ctx, Data, W2> {
type Output = W;
fn index(&self, index: WidgetRef<W>) -> &Self::Output {
&self.ui[index]
}
}
impl<'a, Ctx, Data, W2, W: Widget> IndexMut<WidgetRef<W>> for EventIdCtx<'a, Ctx, Data, W2> {
fn index_mut(&mut self, index: WidgetRef<W>) -> &mut Self::Output {
&mut self.ui[index]
}
}
impl<'a, Ctx, Data, W: Widget> EventIdCtx<'a, Ctx, Data, W> {
pub fn widget(&mut self) -> &mut W {
&mut self.ui[self.widget]
}
}
pub trait EventFn<Ctx, Data>: Fn(EventCtx<Ctx, Data>) + 'static {}
impl<F: Fn(EventCtx<Ctx, Data>) + 'static, Ctx, Data> EventFn<Ctx, Data> for F {}
pub trait WidgetEventFn<Ctx, Data, W: ?Sized>: Fn(EventIdCtx<Ctx, Data, W>) + 'static {}
impl<F: Fn(EventIdCtx<Ctx, Data, W>) + 'static, Ctx, Data, W: ?Sized> WidgetEventFn<Ctx, Data, W>
for F
{
}
impl Ui {
pub fn run_event<E: EventLike, Ctx: 'static>(
&mut self,

36
core/src/event/mod.rs Normal file
View File

@@ -0,0 +1,36 @@
mod ctx;
mod manager;
pub use ctx::*;
pub use manager::*;
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 trait EventFn<Ctx, Data>: Fn(EventCtx<Ctx, Data>) + 'static {}
impl<F: Fn(EventCtx<Ctx, Data>) + 'static, Ctx, Data> EventFn<Ctx, Data> for F {}
pub trait WidgetEventFn<Ctx, Data, W: ?Sized>: Fn(EventIdCtx<Ctx, Data, W>) + 'static {}
impl<F: Fn(EventIdCtx<Ctx, Data, W>) + 'static, Ctx, Data, W: ?Sized> WidgetEventFn<Ctx, Data, W>
for F
{
}