diff --git a/core/src/data.rs b/core/src/data.rs deleted file mode 100644 index 3c85abc..0000000 --- a/core/src/data.rs +++ /dev/null @@ -1,27 +0,0 @@ -use std::any::{Any, TypeId}; - -use crate::util::{HashMap, Id}; - -#[derive(Default)] -pub struct UiData { - map: HashMap>, -} - -impl UiData { - pub fn get(&self) -> Option<&T> { - self.map - .get(&TypeId::of::()) - .map(|d| d.downcast_ref().unwrap()) - } - pub fn get_mut(&mut self) -> Option<&mut T> { - self.map - .get_mut(&TypeId::of::()) - .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(); - } - } -} diff --git a/core/src/event/ctx.rs b/core/src/event/ctx.rs new file mode 100644 index 0000000..fb10503 --- /dev/null +++ b/core/src/event/ctx.rs @@ -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, + pub ui: &'a mut Ui, + pub state: &'a mut Ctx, + pub data: &'a mut Data, +} + +impl<'a, Ctx, Data, W2, W: Widget> Index> for EventIdCtx<'a, Ctx, Data, W2> { + type Output = W; + + fn index(&self, index: WidgetRef) -> &Self::Output { + &self.ui[index] + } +} + +impl<'a, Ctx, Data, W2, W: Widget> IndexMut> for EventIdCtx<'a, Ctx, Data, W2> { + fn index_mut(&mut self, index: WidgetRef) -> &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] + } +} diff --git a/core/src/event.rs b/core/src/event/manager.rs similarity index 69% rename from core/src/event.rs rename to core/src/event/manager.rs index a3b38cc..aa98b23 100644 --- a/core/src/event.rs +++ b/core/src/event/manager.rs @@ -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 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, - pub ui: &'a mut Ui, - pub state: &'a mut Ctx, - pub data: &'a mut Data, -} - #[derive(Default)] pub struct EventManager { types: HashMap>, @@ -150,7 +115,7 @@ impl TypeEventManager { .push((event, Rc::new(f))); } - fn run_fn<'a>( + pub fn run_fn<'a>( &mut self, id: impl IdLike, ) -> impl for<'b> FnOnce(EventCtx>) + 'a { @@ -169,35 +134,6 @@ impl TypeEventManager { } } -impl<'a, Ctx, Data, W2, W: Widget> Index> for EventIdCtx<'a, Ctx, Data, W2> { - type Output = W; - - fn index(&self, index: WidgetRef) -> &Self::Output { - &self.ui[index] - } -} - -impl<'a, Ctx, Data, W2, W: Widget> IndexMut> for EventIdCtx<'a, Ctx, Data, W2> { - fn index_mut(&mut self, index: WidgetRef) -> &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: Fn(EventCtx) + 'static {} -impl) + 'static, Ctx, Data> EventFn for F {} - -pub trait WidgetEventFn: Fn(EventIdCtx) + 'static {} -impl) + 'static, Ctx, Data, W: ?Sized> WidgetEventFn - for F -{ -} - impl Ui { pub fn run_event( &mut self, diff --git a/core/src/event/mod.rs b/core/src/event/mod.rs new file mode 100644 index 0000000..c673159 --- /dev/null +++ b/core/src/event/mod.rs @@ -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 EventLike for E { + type Event = Self; + + fn into_event(self) -> Self::Event { + self + } +} + +pub trait EventFn: Fn(EventCtx) + 'static {} +impl) + 'static, Ctx, Data> EventFn for F {} + +pub trait WidgetEventFn: Fn(EventIdCtx) + 'static {} +impl) + 'static, Ctx, Data, W: ?Sized> WidgetEventFn + for F +{ +}