add module system and move sensor into core with it
This commit is contained in:
1 parent
2adf7a43a1
commit
26c248dcba
13 files changed
+515
-342
No files matched your search
@@ -0,0 +1,85 @@
|
||||
use crate::{
|
||||
layout::{Ui, UiModule, Widget, WidgetId, WidgetIdFn, WidgetLike},
|
||||
util::Id,
|
||||
};
|
||||
|
||||
pub trait UiCtx {
|
||||
fn ui(&mut self) -> &mut Ui<Self>
|
||||
where
|
||||
Self: Sized;
|
||||
}
|
||||
|
||||
pub trait Event<Ctx>: Sized {
|
||||
type Module: UiModule + EventModule<Ctx, Self> + Default;
|
||||
type Data;
|
||||
}
|
||||
|
||||
pub trait EventModule<Ctx, E: Event<Ctx>> {
|
||||
fn register(&mut self, id: Id, event: E, f: impl EventFn<Ctx, E::Data>);
|
||||
}
|
||||
|
||||
pub trait EventFn<Ctx, Data>: FnMut(&mut Ctx, Data) + 'static {}
|
||||
impl<F: FnMut(&mut Ctx, Data) + 'static, Ctx, Data> EventFn<Ctx, Data> for F {}
|
||||
|
||||
pub trait Eventable<W, Ctx, Tag> {
|
||||
fn on<E: Event<Ctx>>(self, event: E, f: impl EventFn<Ctx, E::Data>) -> impl WidgetIdFn<W, Ctx>;
|
||||
|
||||
fn id_on<E: Event<Ctx>>(
|
||||
self,
|
||||
event: E,
|
||||
f: impl FnMut(&WidgetId<W>, &mut Ctx, E::Data) + 'static,
|
||||
) -> impl WidgetIdFn<W, Ctx>
|
||||
where
|
||||
W: Widget;
|
||||
|
||||
fn edit_on<E: Event<Ctx>>(
|
||||
self,
|
||||
event: E,
|
||||
f: impl FnMut(&mut W, E::Data) + 'static,
|
||||
) -> impl WidgetIdFn<W, Ctx>
|
||||
where
|
||||
W: Widget,
|
||||
Ctx: UiCtx;
|
||||
}
|
||||
|
||||
impl<W: WidgetLike<Ctx, Tag>, Ctx, Tag> Eventable<W::Widget, Ctx, Tag> for W {
|
||||
fn on<E: Event<Ctx>>(
|
||||
self,
|
||||
event: E,
|
||||
f: impl EventFn<Ctx, E::Data>,
|
||||
) -> impl WidgetIdFn<W::Widget, Ctx> {
|
||||
move |ui| {
|
||||
let id = self.add(ui);
|
||||
ui.modules
|
||||
.get_mut::<E::Module>()
|
||||
.register(id.id.duplicate(), event, f);
|
||||
id
|
||||
}
|
||||
}
|
||||
|
||||
fn id_on<E: Event<Ctx>>(
|
||||
self,
|
||||
event: E,
|
||||
mut f: impl FnMut(&WidgetId<W::Widget>, &mut Ctx, E::Data) + 'static,
|
||||
) -> impl WidgetIdFn<W::Widget, Ctx>
|
||||
where
|
||||
W::Widget: Widget,
|
||||
{
|
||||
self.with_id(move |ui, id| {
|
||||
let id2 = id.clone();
|
||||
id.on(event, move |ctx, pos| f(&id2, ctx, pos)).add(ui)
|
||||
})
|
||||
}
|
||||
|
||||
fn edit_on<E: Event<Ctx>>(
|
||||
self,
|
||||
event: E,
|
||||
mut f: impl FnMut(&mut W::Widget, E::Data) + 'static,
|
||||
) -> impl WidgetIdFn<W::Widget, Ctx>
|
||||
where
|
||||
W::Widget: Widget,
|
||||
Ctx: UiCtx,
|
||||
{
|
||||
self.id_on(event, move |id, ctx, pos| f(&mut ctx.ui()[id], pos))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user