36 lines
923 B
Rust
36 lines
923 B
Rust
use std::marker::PhantomData;
|
|
|
|
use crate::{Painter, Widget, WidgetFn, WidgetId, WidgetLike};
|
|
|
|
pub struct Sensor<F: SenseFn<Ctx>, Ctx> {
|
|
inner: WidgetId,
|
|
f: F,
|
|
_pd: PhantomData<Ctx>,
|
|
}
|
|
|
|
impl<F: SenseFn<Ctx>, Ctx: 'static> Widget<Ctx> for Sensor<F, Ctx> {
|
|
fn draw(&self, painter: &mut Painter<Ctx>) {
|
|
(self.f)(painter.ctx_mut());
|
|
painter.draw(&self.inner);
|
|
}
|
|
}
|
|
|
|
pub trait SenseFn<Ctx> = Fn(&mut Ctx) + 'static;
|
|
|
|
pub trait Sensable<Ctx: 'static, Tag> {
|
|
fn sense<F: SenseFn<Ctx>>(self, f: F) -> impl WidgetFn<Sensor<F, Ctx>, Ctx>;
|
|
}
|
|
|
|
impl<W: WidgetLike<Ctx, Tag>, Ctx: 'static, Tag> Sensable<Ctx, Tag> for W {
|
|
fn sense<F: SenseFn<Ctx>>(self, f: F) -> impl WidgetFn<Sensor<F, Ctx>, Ctx> {
|
|
|ui| {
|
|
let inner = self.add(ui).erase_type();
|
|
Sensor {
|
|
inner,
|
|
f,
|
|
_pd: PhantomData,
|
|
}
|
|
}
|
|
}
|
|
}
|