fix event stuff
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use std::hash::Hash;
|
||||
use std::{cell::RefCell, hash::Hash, rc::Rc};
|
||||
|
||||
use crate::{
|
||||
layout::{Ui, UiModule, WidgetId, WidgetRef},
|
||||
@@ -77,10 +77,14 @@ impl<E: DefaultEvent> Event for E {
|
||||
|
||||
pub trait EventModule<E: Event, Ctx>: UiModule + Default {
|
||||
fn register(&mut self, id: WidgetId, event: E, f: impl EventFn<Ctx, E::Data>);
|
||||
fn run(&mut self, id: WidgetId, event: E) -> Option<impl FnOnce(EventCtx<Ctx, E::Data>)>;
|
||||
fn run<'a>(
|
||||
&mut self,
|
||||
id: WidgetId,
|
||||
event: E,
|
||||
) -> Option<impl FnOnce(EventCtx<Ctx, E::Data>) + use<'a, Self, Ctx, E>>;
|
||||
}
|
||||
|
||||
type EventFnMap<Ctx, Data> = HashMap<WidgetId, Vec<Box<dyn EventFn<Ctx, Data>>>>;
|
||||
type EventFnMap<Ctx, Data> = HashMap<WidgetId, Vec<Rc<RefCell<dyn EventFn<Ctx, Data>>>>>;
|
||||
pub struct DefaultEventModule<E: Event, Ctx> {
|
||||
map: HashMap<E, EventFnMap<Ctx, <E as Event>::Data>>,
|
||||
}
|
||||
@@ -103,16 +107,21 @@ impl<E: HashableEvent, Ctx: 'static> EventModule<E, Ctx> for DefaultEventModule<
|
||||
.or_default()
|
||||
.entry(id)
|
||||
.or_default()
|
||||
.push(Box::new(f));
|
||||
.push(Rc::new(RefCell::new(f)));
|
||||
}
|
||||
|
||||
fn run(&mut self, id: WidgetId, event: E) -> Option<impl FnOnce(EventCtx<Ctx, E::Data>)> {
|
||||
if let Some(map) = self.map.get_mut(&event)
|
||||
&& let Some(fs) = map.get_mut(&id)
|
||||
fn run<'a>(
|
||||
&mut self,
|
||||
id: WidgetId,
|
||||
event: E,
|
||||
) -> Option<impl FnOnce(EventCtx<Ctx, E::Data>) + use<'a, Ctx, E>> {
|
||||
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 {
|
||||
f.borrow_mut()(EventCtx {
|
||||
ui: ctx.ui,
|
||||
state: ctx.state,
|
||||
data: ctx.data.clone(),
|
||||
@@ -130,10 +139,11 @@ impl<E: HashableEvent, Ctx: 'static> DefaultEventModule<E, Ctx> {
|
||||
where
|
||||
E::Data: Clone,
|
||||
{
|
||||
if let Some(map) = self.map.get_mut(&event) {
|
||||
for fs in map.values_mut() {
|
||||
if let Some(map) = self.map.get(&event) {
|
||||
for fs in map.values() {
|
||||
let fs = fs.clone();
|
||||
for f in fs {
|
||||
f(EventCtx {
|
||||
f.borrow_mut()(EventCtx {
|
||||
ui: ctx.ui,
|
||||
state: ctx.state,
|
||||
data: ctx.data.clone(),
|
||||
@@ -168,7 +178,7 @@ impl Ui {
|
||||
{
|
||||
eprintln!("FIXME");
|
||||
f(EventCtx {
|
||||
ui: &mut Ui::new(),
|
||||
ui: self,
|
||||
state: ctx,
|
||||
data,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user