Files
iris/core/src/event.rs

216 lines
5.6 KiB
Rust

use crate::{
ActiveData, IdLike, LayerId, Ui, Widget, WidgetData, WidgetId, WidgetRef, 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>>,
}
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> {
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,
ctx: &mut Ctx,
id: impl IdLike,
data: &mut <E::Event as Event>::Data<'_>,
) {
let f = self.data.events.get_type::<E, Ctx>().run_fn(id);
f(EventCtx {
ui: self,
state: ctx,
data,
})
}
}