actually sane sensor handling

This commit is contained in:
2025-08-16 00:56:00 -04:00
parent f4aef3a983
commit 11188f2951
9 changed files with 306 additions and 117 deletions

View File

@@ -1,28 +1,32 @@
use crate::{Sense, SenseFn, SenseTrigger, Sensor, Ui, Widget, WidgetIdFn, WidgetLike};
pub trait SenseCtx: 'static {
fn active(&mut self, trigger: &SenseTrigger) -> bool;
}
use crate::{Sense, SenseFn, Sensor, Ui, Widget, WidgetId, WidgetIdFn, WidgetLike};
pub trait Sensable<W, Ctx, Tag> {
fn sense(
fn on(
self,
sense: Sense,
// trait copied here bc rust analyzer skill issue
f: impl Fn(&mut Ui<Ctx>, &mut Ctx) + 'static + Clone,
f: impl FnMut(&mut Ui<Ctx>, &mut Ctx) + 'static + Clone,
) -> impl WidgetIdFn<W, Ctx>;
fn sense_and_edit(
fn id_on(
self,
sense: Sense,
// trait copied here bc rust analyzer skill issue
f: impl Fn(&mut W, &mut Ctx) + 'static + Clone,
f: impl FnMut(&WidgetId<W>, &mut Ui<Ctx>, &mut Ctx) + 'static + Clone,
) -> impl WidgetIdFn<W, Ctx>
where
W: Widget<Ctx>;
fn edit_on(
self,
sense: Sense,
// trait copied here bc rust analyzer skill issue
f: impl FnMut(&mut W, &mut Ctx) + 'static + Clone,
) -> impl WidgetIdFn<W, Ctx>
where
W: Widget<Ctx>;
}
impl<W: WidgetLike<Ctx, Tag>, Ctx, Tag> Sensable<W::Widget, Ctx, Tag> for W {
fn sense(self, sense: Sense, f: impl SenseFn<Ctx> + Clone) -> impl WidgetIdFn<W::Widget, Ctx> {
fn on(self, sense: Sense, f: impl SenseFn<Ctx> + Clone) -> impl WidgetIdFn<W::Widget, Ctx> {
move |ui| {
let id = self.add(ui);
ui.add_sensor(
@@ -35,18 +39,29 @@ impl<W: WidgetLike<Ctx, Tag>, Ctx, Tag> Sensable<W::Widget, Ctx, Tag> for W {
id
}
}
fn sense_and_edit(
fn id_on(
self,
sense: Sense,
// trait copied here bc rust analyzer skill issue
f: impl Fn(&mut W::Widget, &mut Ctx) + 'static + Clone,
mut f: impl FnMut(&WidgetId<W::Widget>, &mut Ui<Ctx>, &mut Ctx) + 'static + Clone,
) -> impl WidgetIdFn<W::Widget, Ctx>
where
W::Widget: Widget<Ctx>,
{
self.with_id(move |ui, id| {
let id2 = id.clone();
ui.add(id.sense(sense, move |ui, ctx| f(&mut ui[&id2], ctx)))
ui.add(id.on(sense, move |ui, ctx| f(&id2, ui, ctx)))
})
}
fn edit_on(
self,
sense: Sense,
// trait copied here bc rust analyzer skill issue
mut f: impl FnMut(&mut W::Widget, &mut Ctx) + 'static + Clone,
) -> impl WidgetIdFn<W::Widget, Ctx>
where
W::Widget: Widget<Ctx>,
{
self.id_on(sense, move |id, ui, ctx| f(&mut ui[id], ctx))
}
}