81 lines
2.2 KiB
Rust
81 lines
2.2 KiB
Rust
use iris_core::*;
|
|
use iris_macro::*;
|
|
use std::sync::Arc;
|
|
|
|
use crate::default::{TaskCtx, TaskUpdate, Tasks};
|
|
|
|
pub trait Eventable<Rsc: HasEvents, Tag>: WidgetLike<Rsc, Tag> {
|
|
fn on<E: EventLike>(
|
|
self,
|
|
event: E,
|
|
f: impl for<'a> WidgetEventFn<Rsc, <E::Event as Event>::Data<'a>, Self::Widget>,
|
|
) -> impl WidgetIdFn<Rsc, Self::Widget> {
|
|
move |rsc| {
|
|
let id = self.add(rsc);
|
|
rsc.register_event(id, event.into_event(), move |ctx, rsc| {
|
|
f(
|
|
EventIdCtx {
|
|
widget: id,
|
|
state: ctx.state,
|
|
data: ctx.data,
|
|
},
|
|
rsc,
|
|
);
|
|
});
|
|
id
|
|
}
|
|
}
|
|
}
|
|
impl<WL: WidgetLike<Rsc, Tag>, Rsc: HasEvents, Tag> Eventable<Rsc, Tag> for WL {}
|
|
|
|
widget_trait! {
|
|
pub trait TaskEventable<Rsc: HasEvents + HasTasks>;
|
|
fn task_on<'a, E: EventLike, F: AsyncWidgetEventFn<Rsc, WL::Widget>>(
|
|
self,
|
|
event: E,
|
|
f: F,
|
|
) -> impl WidgetIdFn<Rsc, WL::Widget>
|
|
where <E::Event as Event>::Data<'a>: Send,
|
|
for<'b> F::CallRefFuture<'b>: Send,
|
|
{
|
|
let f = Arc::new(f);
|
|
move |rsc| {
|
|
let id = self.add(rsc);
|
|
rsc.register_event(id, event.into_event(), move |_, rsc| {
|
|
let f = f.clone();
|
|
rsc.tasks_mut().spawn(async move |task| {
|
|
f(AsyncEventIdCtx {
|
|
widget: id,
|
|
task,
|
|
}).await;
|
|
});
|
|
});
|
|
id
|
|
}
|
|
}
|
|
}
|
|
|
|
pub trait HasTasks: Sized + HasState + HasEvents {
|
|
fn tasks_mut(&mut self) -> &mut Tasks<Self>;
|
|
}
|
|
|
|
pub trait AsyncWidgetEventFn<Rsc: HasEvents, W: ?Sized>:
|
|
AsyncFn(AsyncEventIdCtx<Rsc, W>) + Send + Sync + 'static
|
|
{
|
|
}
|
|
impl<Rsc: HasEvents, F: AsyncFn(AsyncEventIdCtx<Rsc, W>) + Send + Sync + 'static, W: ?Sized>
|
|
AsyncWidgetEventFn<Rsc, W> for F
|
|
{
|
|
}
|
|
|
|
pub struct AsyncEventIdCtx<Rsc: HasEvents, W: ?Sized> {
|
|
pub widget: WeakWidget<W>,
|
|
task: TaskCtx<Rsc>,
|
|
}
|
|
|
|
impl<Rsc: HasEvents, W: ?Sized> AsyncEventIdCtx<Rsc, W> {
|
|
pub fn update(&mut self, f: impl TaskUpdate<Rsc> + 'static) {
|
|
self.task.update(f);
|
|
}
|
|
}
|