REAL SENSORS
This commit is contained in:
@@ -1,58 +1,52 @@
|
||||
use crate::{
|
||||
Painter, Sense, SenseFn, SenseShape, SenseTrigger, Ui, Widget, WidgetFn, WidgetId, WidgetLike,
|
||||
};
|
||||
|
||||
pub struct Sensor<Ctx> {
|
||||
inner: WidgetId,
|
||||
sense: Sense,
|
||||
f: Box<dyn SenseFn<Ctx>>,
|
||||
}
|
||||
|
||||
impl<Ctx: 'static> Widget<Ctx> for Sensor<Ctx> {
|
||||
fn draw(&self, painter: &mut Painter<Ctx>) {
|
||||
painter.sense(
|
||||
SenseTrigger {
|
||||
shape: painter.region,
|
||||
sense: self.sense,
|
||||
},
|
||||
self.f.box_clone(),
|
||||
);
|
||||
painter.draw(self.inner.as_any());
|
||||
}
|
||||
}
|
||||
use crate::{Sense, SenseFn, SenseTrigger, Sensor, Ui, Widget, WidgetIdFn, WidgetLike};
|
||||
|
||||
pub trait SenseCtx: 'static {
|
||||
fn active(&mut self, trigger: &SenseTrigger) -> bool;
|
||||
}
|
||||
|
||||
pub trait WidgetSenseFn<W: Widget<Ctx>, Ctx> = Fn(&WidgetId<W>, &mut Ui<Ctx>, &mut Ctx) + 'static;
|
||||
|
||||
pub trait Sensable<W: Widget<Ctx>, Ctx: 'static, Tag> {
|
||||
// copied here so LSP can at least get the UI and id
|
||||
fn sense<F: WidgetSenseFn<W, Ctx> + Clone>(
|
||||
pub trait Sensable<W, Ctx: SenseCtx, Tag> {
|
||||
fn sense(
|
||||
self,
|
||||
sense: Sense,
|
||||
f: F,
|
||||
) -> impl WidgetFn<Sensor<Ctx>, Ctx>;
|
||||
// trait copied here bc rust analyzer skill issue
|
||||
f: impl Fn(&mut Ui<Ctx>, &mut Ctx) + 'static + Clone,
|
||||
) -> impl WidgetIdFn<W, Ctx>;
|
||||
fn sense_and_edit(
|
||||
self,
|
||||
sense: Sense,
|
||||
// trait copied here bc rust analyzer skill issue
|
||||
f: impl Fn(&mut W, &mut Ctx) + 'static + Clone,
|
||||
) -> impl WidgetIdFn<W, Ctx>
|
||||
where
|
||||
W: Widget<Ctx>;
|
||||
}
|
||||
|
||||
impl<W: WidgetLike<Ctx, Tag>, Ctx: SenseCtx, Tag> Sensable<W::Widget, Ctx, Tag> for W
|
||||
where
|
||||
W::Widget: Widget<Ctx>,
|
||||
{
|
||||
fn sense<F: WidgetSenseFn<W::Widget, Ctx> + Clone>(
|
||||
self,
|
||||
sense: Sense,
|
||||
f: F,
|
||||
) -> impl WidgetFn<Sensor<Ctx>, Ctx> {
|
||||
impl<W: WidgetLike<Ctx, Tag>, Ctx: SenseCtx, Tag> Sensable<W::Widget, Ctx, Tag> for W {
|
||||
fn sense(self, sense: Sense, f: impl SenseFn<Ctx> + Clone) -> impl WidgetIdFn<W::Widget, Ctx> {
|
||||
move |ui| {
|
||||
let inner_arg = self.add(ui);
|
||||
let inner = inner_arg.clone().erase_type();
|
||||
Sensor {
|
||||
inner,
|
||||
sense,
|
||||
f: Box::new(move |ui, ctx| (f)(&inner_arg, ui, ctx)),
|
||||
}
|
||||
let id = self.add(ui);
|
||||
ui.add_sensor(
|
||||
&id,
|
||||
Sensor {
|
||||
sense,
|
||||
f: Box::new(f),
|
||||
},
|
||||
);
|
||||
id
|
||||
}
|
||||
}
|
||||
fn sense_and_edit(
|
||||
self,
|
||||
sense: Sense,
|
||||
// trait copied here bc rust analyzer skill issue
|
||||
f: impl Fn(&mut W::Widget, &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)))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user