55 lines
1.4 KiB
Rust
55 lines
1.4 KiB
Rust
use crate::prelude::*;
|
|
|
|
pub trait Sensable<W, Tag> {
|
|
fn on(self, sense: Senses, f: impl SenseFn) -> WidgetIdFnRet!(W);
|
|
fn id_on(
|
|
self,
|
|
senses: Senses,
|
|
f: impl FnMut(&WidgetId<W>, &mut Ui) + 'static + Clone,
|
|
) -> WidgetIdFnRet!(W)
|
|
where
|
|
W: Widget;
|
|
fn edit_on(self, senses: Senses, f: impl FnMut(&mut W) + 'static + Clone) -> WidgetIdFnRet!(W)
|
|
where
|
|
W: Widget;
|
|
}
|
|
|
|
impl<W: WidgetLike<Tag>, Tag> Sensable<W::Widget, Tag> for W {
|
|
fn on(self, senses: Senses, f: impl SenseFn) -> WidgetIdFnRet!(W::Widget) {
|
|
move |ui| {
|
|
let id = self.add(ui);
|
|
ui.add_sensor(
|
|
&id,
|
|
Sensor {
|
|
senses,
|
|
f: Box::new(f),
|
|
},
|
|
);
|
|
id
|
|
}
|
|
}
|
|
fn id_on(
|
|
self,
|
|
senses: Senses,
|
|
mut f: impl FnMut(&WidgetId<W::Widget>, &mut Ui) + 'static + Clone,
|
|
) -> WidgetIdFnRet!(W::Widget)
|
|
where
|
|
W::Widget: Widget,
|
|
{
|
|
self.with_id(move |ui, id| {
|
|
let id2 = id.clone();
|
|
ui.add(id.on(senses, move |ui| f(&id2, ui)))
|
|
})
|
|
}
|
|
fn edit_on(
|
|
self,
|
|
senses: Senses,
|
|
mut f: impl FnMut(&mut W::Widget) + 'static + Clone,
|
|
) -> WidgetIdFnRet!(W::Widget)
|
|
where
|
|
W::Widget: Widget,
|
|
{
|
|
self.id_on(senses, move |id, ui| f(&mut ui[id]))
|
|
}
|
|
}
|