TAG TECHNOLOGY
This commit is contained in:
35
src/core/sense.rs
Normal file
35
src/core/sense.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
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,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user