remove context from ui (again) and create weird trait for it
This commit is contained in:
1 parent
26c248dcba
commit
719bee4b31
12 files changed
+107
-92
No files matched your search
+24
-9
@@ -1,10 +1,10 @@
|
||||
use crate::{
|
||||
layout::{Ui, UiModule, Widget, WidgetId, WidgetIdFn, WidgetLike},
|
||||
layout::{IdFnTag, Ui, UiModule, Widget, WidgetId, WidgetIdFn, WidgetLike},
|
||||
util::Id,
|
||||
};
|
||||
|
||||
pub trait UiCtx {
|
||||
fn ui(&mut self) -> &mut Ui<Self>
|
||||
fn ui(&mut self) -> &mut Ui
|
||||
where
|
||||
Self: Sized;
|
||||
}
|
||||
@@ -22,13 +22,17 @@ 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 on<E: Event<Ctx>>(
|
||||
self,
|
||||
event: E,
|
||||
f: impl EventFn<Ctx, E::Data>,
|
||||
) -> impl WidgetIdFn<W> + Eventable<W, Ctx, IdFnTag>;
|
||||
|
||||
fn id_on<E: Event<Ctx>>(
|
||||
self,
|
||||
event: E,
|
||||
f: impl FnMut(&WidgetId<W>, &mut Ctx, E::Data) + 'static,
|
||||
) -> impl WidgetIdFn<W, Ctx>
|
||||
) -> impl WidgetIdFn<W> + Eventable<W, Ctx, IdFnTag>
|
||||
where
|
||||
W: Widget;
|
||||
|
||||
@@ -36,18 +40,29 @@ pub trait Eventable<W, Ctx, Tag> {
|
||||
self,
|
||||
event: E,
|
||||
f: impl FnMut(&mut W, E::Data) + 'static,
|
||||
) -> impl WidgetIdFn<W, Ctx>
|
||||
) -> impl WidgetIdFn<W> + Eventable<W, Ctx, IdFnTag>
|
||||
where
|
||||
W: Widget,
|
||||
Ctx: UiCtx;
|
||||
}
|
||||
|
||||
impl<W: WidgetLike<Ctx, Tag>, Ctx, Tag> Eventable<W::Widget, Ctx, Tag> for W {
|
||||
pub trait Ctxable<W, Tag> {
|
||||
/// sets context which lets event functions work without needing to specify generics
|
||||
fn ctx<Ctx>(self) -> impl WidgetLike<Tag> + Eventable<W, Ctx, Tag>;
|
||||
}
|
||||
|
||||
impl<W: WidgetLike<Tag>, Tag> Ctxable<W::Widget, Tag> for W {
|
||||
fn ctx<Ctx>(self) -> impl WidgetLike<Tag> + Eventable<W::Widget, Ctx, Tag> {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: WidgetLike<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> {
|
||||
) -> impl WidgetIdFn<W::Widget> + Eventable<W::Widget, Ctx, IdFnTag> {
|
||||
move |ui| {
|
||||
let id = self.add(ui);
|
||||
ui.modules
|
||||
@@ -61,7 +76,7 @@ impl<W: WidgetLike<Ctx, Tag>, Ctx, Tag> Eventable<W::Widget, Ctx, Tag> for W {
|
||||
self,
|
||||
event: E,
|
||||
mut f: impl FnMut(&WidgetId<W::Widget>, &mut Ctx, E::Data) + 'static,
|
||||
) -> impl WidgetIdFn<W::Widget, Ctx>
|
||||
) -> impl WidgetIdFn<W::Widget> + Eventable<W::Widget, Ctx, IdFnTag>
|
||||
where
|
||||
W::Widget: Widget,
|
||||
{
|
||||
@@ -75,7 +90,7 @@ impl<W: WidgetLike<Ctx, Tag>, Ctx, Tag> Eventable<W::Widget, Ctx, Tag> for W {
|
||||
self,
|
||||
event: E,
|
||||
mut f: impl FnMut(&mut W::Widget, E::Data) + 'static,
|
||||
) -> impl WidgetIdFn<W::Widget, Ctx>
|
||||
) -> impl WidgetIdFn<W::Widget> + Eventable<W::Widget, Ctx, IdFnTag>
|
||||
where
|
||||
W::Widget: Widget,
|
||||
Ctx: UiCtx,
|
||||
|
||||
Reference in new issue
Block a user