diff --git a/TODO b/TODO index d945cde..9e3d6a4 100644 --- a/TODO +++ b/TODO @@ -1,5 +1,4 @@ images text -ui ctx = ui + app ctx for sensors abstract sensors to work with any event, maybe associate data as well? make current senses orable so you can select multiple diff --git a/src/core/sense.rs b/src/core/sense.rs index 4338c9e..7ca8817 100644 --- a/src/core/sense.rs +++ b/src/core/sense.rs @@ -1,11 +1,11 @@ -use crate::{Sense, SenseFn, Sensor, Ui, Widget, WidgetId, WidgetIdFnRet, WidgetLike}; +use crate::{Sense, SenseCtx, SenseFn, Sensor, Widget, WidgetId, WidgetIdFnRet, WidgetLike}; pub trait Sensable { fn on(self, sense: Sense, f: impl SenseFn) -> WidgetIdFnRet!(W, Ctx); fn id_on( self, sense: Sense, - f: impl FnMut(&WidgetId, &mut Ui, &mut Ctx) + 'static + Clone, + f: impl FnMut(&WidgetId, SenseCtx) + 'static + Clone, ) -> WidgetIdFnRet!(W, Ctx) where W: Widget; @@ -35,26 +35,24 @@ impl, Ctx, Tag> Sensable for W { fn id_on( self, sense: Sense, - // trait copied here bc rust analyzer skill issue - mut f: impl FnMut(&WidgetId, &mut Ui, &mut Ctx) + 'static + Clone, + mut f: impl FnMut(&WidgetId, SenseCtx) + 'static + Clone, ) -> WidgetIdFnRet!(W::Widget, Ctx) where W::Widget: Widget, { self.with_id(move |ui, id| { let id2 = id.clone(); - ui.add(id.on(sense, move |ui, ctx| f(&id2, ui, ctx))) + ui.add(id.on(sense, move |ctx| f(&id2, 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, ) -> WidgetIdFnRet!(W::Widget, Ctx) where W::Widget: Widget, { - self.id_on(sense, move |id, ui, ctx| f(&mut ui[id], ctx)) + self.id_on(sense, move |id, ctx| f(&mut ctx.ui[id], ctx.app)) } } diff --git a/src/layout/sense.rs b/src/layout/sense.rs index d8af829..9df7752 100644 --- a/src/layout/sense.rs +++ b/src/layout/sense.rs @@ -44,10 +44,14 @@ pub struct SensorGroup { pub cursor: ActivationState, pub sensors: Vec>, } -pub trait SenseFn: FnMut(&mut Ui, &mut Ctx) + 'static { +pub struct SenseCtx<'a, Ctx> { + pub ui: &'a mut Ui, + pub app: &'a mut Ctx, +} +pub trait SenseFn: FnMut(SenseCtx) + 'static { fn box_clone(&self) -> Box>; } -impl, &mut Ctx) + 'static + Clone, Ctx> SenseFn for F { +impl) + 'static + Clone, Ctx> SenseFn for F { fn box_clone(&self) -> Box> { Box::new(self.clone()) } @@ -77,7 +81,10 @@ impl Ui { for sensor in &mut group.sensors { if should_run(sensor.sense, group.cursor, group.hover) { - (sensor.f.box_clone())(self, ctx); + (sensor.f.box_clone())(SenseCtx { + ui: self, + app: ctx, + }); } } } diff --git a/src/testing/mod.rs b/src/testing/mod.rs index a451058..cf288c9 100644 --- a/src/testing/mod.rs +++ b/src/testing/mod.rs @@ -84,10 +84,9 @@ impl Client { let main = main.clone(); let to = to.clone().erase_type(); Rect::new(color) - .on(Sense::PressEnd, |_, _| {}) - .id_on(Sense::PressStart, move |id, ui, _| { - ui[&main].inner = to.clone(); - ui[id].color = color.add_rgb(-0.2); + .id_on(Sense::PressStart, move |id, ctx| { + ctx.ui[&main].inner = to.clone(); + ctx.ui[id].color = color.add_rgb(-0.2); }) .edit_on(Sense::HoverStart, move |r, _| { r.color = color.add_rgb(0.1); @@ -110,8 +109,8 @@ impl Client { ); let test_button = Rect::new(Color::PURPLE) .radius(30) - .on(Sense::PressStart, move |ui, _| { - println!("{}", ui.num_widgets()); + .on(Sense::PressStart, move |ctx| { + println!("{}", ctx.ui.num_widgets()); }) .region( UiRegion::corner(Corner::BotRight) @@ -122,8 +121,8 @@ impl Client { let s = span_add.clone(); let del_button = Rect::new(Color::RED) .radius(30) - .on(Sense::PressStart, move |ui, _| { - ui[&s].children.pop(); + .on(Sense::PressStart, move |ctx| { + ctx.ui[&s].children.pop(); }) .region( UiRegion::corner(Corner::BotLeft)