"nvmnd" (event FnMut)

This commit is contained in:
2025-12-10 13:56:38 -05:00
parent 76f75192d5
commit 2537284372
4 changed files with 17 additions and 18 deletions

View File

@@ -1,4 +1,4 @@
use std::{cell::RefCell, hash::Hash, rc::Rc};
use std::{hash::Hash, rc::Rc};
use crate::{
layout::{Ui, UiModule, WidgetId, WidgetRef},
@@ -24,11 +24,11 @@ pub struct EventIdCtx<'a, Ctx, Data, W: ?Sized> {
pub data: Data,
}
pub trait EventFn<Ctx, Data>: FnMut(EventCtx<Ctx, Data>) + 'static {}
impl<F: FnMut(EventCtx<Ctx, Data>) + 'static, Ctx, Data> EventFn<Ctx, Data> for F {}
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>: FnMut(EventIdCtx<Ctx, Data, W>) + 'static {}
impl<F: FnMut(EventIdCtx<Ctx, Data, W>) + 'static, Ctx, Data, W: ?Sized> WidgetEventFn<Ctx, Data, W>
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
{
}
@@ -50,13 +50,13 @@ impl Ui {
&mut self,
id: WidgetRef<W>,
event: E,
mut f: impl WidgetEventFn<Ctx, E::Data, W>,
f: impl WidgetEventFn<Ctx, E::Data, W>,
) {
self.data
.modules
.get_mut::<E::Module<Ctx>>()
.register(id.id(), event, move |ctx| {
(&mut f)(EventIdCtx {
f(EventIdCtx {
widget: id,
ui: ctx.ui,
state: ctx.state,
@@ -84,7 +84,7 @@ pub trait EventModule<E: Event, Ctx>: UiModule + Default {
) -> Option<impl FnOnce(EventCtx<Ctx, E::Data>) + use<'a, Self, Ctx, E>>;
}
type EventFnMap<Ctx, Data> = HashMap<WidgetId, Vec<Rc<RefCell<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>>,
}
@@ -107,7 +107,7 @@ impl<E: HashableEvent, Ctx: 'static> EventModule<E, Ctx> for DefaultEventModule<
.or_default()
.entry(id)
.or_default()
.push(Rc::new(RefCell::new(f)));
.push(Rc::new(f));
}
fn run<'a>(
@@ -121,7 +121,7 @@ impl<E: HashableEvent, Ctx: 'static> EventModule<E, Ctx> for DefaultEventModule<
let fs = fs.clone();
Some(move |ctx: EventCtx<Ctx, <E as Event>::Data>| {
for f in fs {
f.borrow_mut()(EventCtx {
f(EventCtx {
ui: ctx.ui,
state: ctx.state,
data: ctx.data.clone(),
@@ -143,7 +143,7 @@ impl<E: HashableEvent, Ctx: 'static> DefaultEventModule<E, Ctx> {
for fs in map.values() {
let fs = fs.clone();
for f in fs {
f.borrow_mut()(EventCtx {
f(EventCtx {
ui: ctx.ui,
state: ctx.state,
data: ctx.data.clone(),

View File

@@ -2,7 +2,7 @@ use image::DynamicImage;
use crate::{
layout::{
IdLike, PainterData, PixelRegion, TextureHandle, Vec2, Widget, WidgetHandle, WidgetId,
IdLike, PainterData, PixelRegion, TextureHandle, Vec2, WidgetHandle, WidgetId,
WidgetInstance, WidgetLike, WidgetUpdate,
},
util::HashSet,