diff --git a/src/core/frame.rs b/src/core/frame.rs index 122b13d..cf91df4 100644 --- a/src/core/frame.rs +++ b/src/core/frame.rs @@ -5,8 +5,8 @@ pub struct Regioned { pub inner: WidgetId, } -impl Widget for Regioned { - fn draw(&self, painter: &mut Painter) { +impl Widget for Regioned { + fn draw(&self, painter: &mut Painter) { painter.draw_within(&self.inner, self.region); } } diff --git a/src/core/image.rs b/src/core/image.rs index b752657..b14a4aa 100644 --- a/src/core/image.rs +++ b/src/core/image.rs @@ -5,8 +5,8 @@ pub struct Image { handle: Option, } -impl Widget for Image { - fn draw(&self, painter: &mut Painter) { +impl Widget for Image { + fn draw(&self, painter: &mut Painter) { if let Some(handle) = &self.handle { painter.draw_texture(handle); } else { @@ -20,7 +20,7 @@ impl Widget for Image { } } -pub fn image(image: impl LoadableImage) -> WidgetFnRet!(Image, Ctx) { +pub fn image(image: impl LoadableImage) -> WidgetFnRet!(Image) { let image = match image.get_image() { Ok(image) => Some(image), Err(e) => { diff --git a/src/core/rect.rs b/src/core/rect.rs index 56f3bc0..3c8b769 100644 --- a/src/core/rect.rs +++ b/src/core/rect.rs @@ -27,8 +27,8 @@ impl Rect { } } -impl Widget for Rect { - fn draw(&self, painter: &mut Painter) { +impl Widget for Rect { + fn draw(&self, painter: &mut Painter) { painter.write(RectPrimitive { color: self.color, radius: self.radius, diff --git a/src/core/sense.rs b/src/core/sense.rs index 5fadd3a..cb3ad20 100644 --- a/src/core/sense.rs +++ b/src/core/sense.rs @@ -1,25 +1,21 @@ use crate::prelude::*; -pub trait Sensable { - fn on(self, sense: Sense, f: impl SenseFn) -> WidgetIdFnRet!(W, Ctx); +pub trait Sensable { + fn on(self, sense: Sense, f: impl SenseFn) -> WidgetIdFnRet!(W); fn id_on( self, sense: Sense, - f: impl FnMut(&WidgetId, SenseCtx) + 'static + Clone, - ) -> WidgetIdFnRet!(W, Ctx) + f: impl FnMut(&WidgetId, &mut Ui) + 'static + Clone, + ) -> WidgetIdFnRet!(W) where - W: Widget; - fn edit_on( - self, - sense: Sense, - f: impl FnMut(&mut W, &mut Ctx) + 'static + Clone, - ) -> WidgetIdFnRet!(W, Ctx) + W: Widget; + fn edit_on(self, sense: Sense, f: impl FnMut(&mut W) + 'static + Clone) -> WidgetIdFnRet!(W) where - W: Widget; + W: Widget; } -impl, Ctx, Tag> Sensable for W { - fn on(self, sense: Sense, f: impl SenseFn) -> WidgetIdFnRet!(W::Widget, Ctx) { +impl, Tag> Sensable for W { + fn on(self, sense: Sense, f: impl SenseFn) -> WidgetIdFnRet!(W::Widget) { move |ui| { let id = self.add(ui); ui.add_sensor( @@ -35,10 +31,10 @@ impl, Ctx, Tag> Sensable for W { fn id_on( self, sense: Sense, - mut f: impl FnMut(&WidgetId, SenseCtx) + 'static + Clone, - ) -> WidgetIdFnRet!(W::Widget, Ctx) + mut f: impl FnMut(&WidgetId, &mut Ui) + 'static + Clone, + ) -> WidgetIdFnRet!(W::Widget) where - W::Widget: Widget, + W::Widget: Widget, { self.with_id(move |ui, id| { let id2 = id.clone(); @@ -48,11 +44,11 @@ impl, Ctx, Tag> Sensable for W { fn edit_on( self, sense: Sense, - mut f: impl FnMut(&mut W::Widget, &mut Ctx) + 'static + Clone, - ) -> WidgetIdFnRet!(W::Widget, Ctx) + mut f: impl FnMut(&mut W::Widget) + 'static + Clone, + ) -> WidgetIdFnRet!(W::Widget) where - W::Widget: Widget, + W::Widget: Widget, { - self.id_on(sense, move |id, ctx| f(&mut ctx.ui[id], ctx.app)) + self.id_on(sense, move |id, ui| f(&mut ui[id])) } } diff --git a/src/core/span.rs b/src/core/span.rs index f09af63..bff560a 100644 --- a/src/core/span.rs +++ b/src/core/span.rs @@ -5,8 +5,8 @@ pub struct Span { pub dir: Dir, } -impl Widget for Span { - fn draw(&self, painter: &mut Painter) { +impl Widget for Span { + fn draw(&self, painter: &mut Painter) { let total = self.sums(); let mut start = UIScalar::min(); for (child, length) in &self.children { diff --git a/src/core/stack.rs b/src/core/stack.rs index 73bbc5b..dcdbab6 100644 --- a/src/core/stack.rs +++ b/src/core/stack.rs @@ -4,8 +4,8 @@ pub struct Stack { pub children: Vec, } -impl Widget for Stack { - fn draw(&self, painter: &mut Painter) { +impl Widget for Stack { + fn draw(&self, painter: &mut Painter) { for child in &self.children { painter.draw(child); } diff --git a/src/core/text.rs b/src/core/text.rs index 7741069..09a6ec3 100644 --- a/src/core/text.rs +++ b/src/core/text.rs @@ -20,8 +20,8 @@ impl Text { } } -impl Widget for Text { - fn draw(&self, painter: &mut Painter) { +impl Widget for Text { + fn draw(&self, painter: &mut Painter) { // TODO: when on_update is added to painter, // return & store TextureHandle to reuse painter.draw_text(&self.content, &self.attrs); diff --git a/src/core/trait_fns.rs b/src/core/trait_fns.rs index 9b0a851..984f907 100644 --- a/src/core/trait_fns.rs +++ b/src/core/trait_fns.rs @@ -3,36 +3,36 @@ use crate::layout::{ Dir, UiPos, UiRegion, Vec2, WidgetArrLike, WidgetFnRet, WidgetIdFnRet, WidgetLike, }; -pub trait CoreWidget { - fn pad(self, padding: impl Into) -> WidgetFnRet!(Regioned, Ctx); - fn center(self, size: impl Into) -> WidgetFnRet!(Regioned, Ctx); - fn region(self, region: UiRegion) -> WidgetFnRet!(Regioned, Ctx); - fn label(self, label: impl Into) -> WidgetIdFnRet!(W, Ctx); +pub trait CoreWidget { + fn pad(self, padding: impl Into) -> WidgetFnRet!(Regioned); + fn center(self, size: impl Into) -> WidgetFnRet!(Regioned); + fn region(self, region: UiRegion) -> WidgetFnRet!(Regioned); + fn label(self, label: impl Into) -> WidgetIdFnRet!(W); } -impl, Ctx: 'static, Tag> CoreWidget for W { - fn pad(self, padding: impl Into) -> WidgetFnRet!(Regioned, Ctx) { +impl, Tag> CoreWidget for W { + fn pad(self, padding: impl Into) -> WidgetFnRet!(Regioned) { |ui| Regioned { region: padding.into().region(), inner: self.add(ui).erase_type(), } } - fn center(self, size: impl Into) -> WidgetFnRet!(Regioned, Ctx) { + fn center(self, size: impl Into) -> WidgetFnRet!(Regioned) { |ui| Regioned { region: UiPos::center().expand(size.into()), inner: self.add(ui).erase_type(), } } - fn region(self, region: UiRegion) -> WidgetFnRet!(Regioned, Ctx) { + fn region(self, region: UiRegion) -> WidgetFnRet!(Regioned) { move |ui| Regioned { region, inner: self.add(ui).erase_type(), } } - fn label(self, label: impl Into) -> WidgetIdFnRet!(W::Widget, Ctx) { + fn label(self, label: impl Into) -> WidgetIdFnRet!(W::Widget) { |ui| { let id = self.add(ui); ui.set_label(&id, label.into()); @@ -41,22 +41,20 @@ impl, Ctx: 'static, Tag> CoreWidget } } -pub trait CoreWidgetArr { - fn span(self, dir: Dir, lengths: [impl Into; LEN]) -> WidgetFnRet!(Span, Ctx); - fn stack(self) -> WidgetFnRet!(Stack, Ctx); +pub trait CoreWidgetArr { + fn span(self, dir: Dir, lengths: [impl Into; LEN]) -> WidgetFnRet!(Span); + fn stack(self) -> WidgetFnRet!(Stack); } -impl, Ctx: 'static, Tag> - CoreWidgetArr for Wa -{ - fn span(self, dir: Dir, lengths: [impl Into; LEN]) -> WidgetFnRet!(Span, Ctx) { +impl, Tag> CoreWidgetArr for Wa { + fn span(self, dir: Dir, lengths: [impl Into; LEN]) -> WidgetFnRet!(Span) { let lengths = lengths.map(Into::into); move |ui| Span { dir, children: self.ui(ui).arr.into_iter().zip(lengths).collect(), } } - fn stack(self) -> WidgetFnRet!(Stack, Ctx) { + fn stack(self) -> WidgetFnRet!(Stack) { move |ui| Stack { children: self.ui(ui).arr.to_vec(), } diff --git a/src/layout/id.rs b/src/layout/id.rs index 6d01c7d..e7ac7cd 100644 --- a/src/layout/id.rs +++ b/src/layout/id.rs @@ -119,24 +119,24 @@ impl Drop for WidgetId { pub struct IdTag; pub struct IdFnTag; -// pub trait WidgetIdFn = FnOnce(&mut Ui) -> WidgetId; +// pub trait WidgetIdFn = FnOnce(&mut Ui) -> WidgetId; macro_rules! WidgetIdFnRet { - ($W:ty, $Ctx:ty) => { - impl FnOnce(&mut $crate::layout::Ui<$Ctx>) -> $crate::layout::WidgetId<$W> + ($W:ty) => { + impl FnOnce(&mut $crate::layout::Ui) -> $crate::layout::WidgetId<$W> }; - ($W:ty, $Ctx:ty, $($use:tt)*) => { - impl FnOnce(&mut $crate::layout::Ui<$Ctx>) -> $crate::layout::WidgetId<$W> + use<$($use)*> + ($W:ty, $($use:tt)*) => { + impl FnOnce(&mut $crate::layout::Ui) -> $crate::layout::WidgetId<$W> + use<$($use)*> }; } pub(crate) use WidgetIdFnRet; -pub trait Idable { - type Widget: Widget; - fn set(self, ui: &mut Ui, id: &WidgetId); +pub trait Idable { + type Widget: Widget; + fn set(self, ui: &mut Ui, id: &WidgetId); fn id<'a>( self, id: &WidgetId, - ) -> WidgetIdFnRet!(Self::Widget, Ctx, 'a, Self, Ctx, Tag) + ) -> WidgetIdFnRet!(Self::Widget, 'a, Self, Tag) where Self: Sized, { @@ -149,7 +149,7 @@ pub trait Idable { fn id_static<'a>( self, id: StaticWidgetId, - ) -> WidgetIdFnRet!(Self::Widget, Ctx, 'a, Self, Ctx, Tag) + ) -> WidgetIdFnRet!(Self::Widget, 'a, Self, Tag) where Self: Sized, { @@ -161,33 +161,33 @@ pub trait Idable { } } -impl, Ctx> Idable for W { +impl Idable for W { type Widget = W; - fn set(self, ui: &mut Ui, id: &WidgetId) { + fn set(self, ui: &mut Ui, id: &WidgetId) { ui.set(id, self); } } -impl) -> W, W: Widget, Ctx> Idable for F { +impl W, W: Widget> Idable for F { type Widget = W; - fn set(self, ui: &mut Ui, id: &WidgetId) { + fn set(self, ui: &mut Ui, id: &WidgetId) { let w = self(ui); ui.set(id, w); } } -impl WidgetLike for WidgetId { +impl WidgetLike for WidgetId { type Widget = W; - fn add(self, _: &mut Ui) -> WidgetId { + fn add(self, _: &mut Ui) -> WidgetId { self } } -impl) -> WidgetId, Ctx> WidgetLike for F { +impl WidgetId> WidgetLike for F { type Widget = W; - fn add(self, ui: &mut Ui) -> WidgetId { + fn add(self, ui: &mut Ui) -> WidgetId { self(ui) } } @@ -198,9 +198,9 @@ impl StaticWidgetId { } } -impl WidgetLike for StaticWidgetId { +impl WidgetLike for StaticWidgetId { type Widget = W; - fn add(self, ui: &mut Ui) -> WidgetId { + fn add(self, ui: &mut Ui) -> WidgetId { self.id(&ui.send) } } diff --git a/src/layout/painter.rs b/src/layout/painter.rs index 8e71b11..82839db 100644 --- a/src/layout/painter.rs +++ b/src/layout/painter.rs @@ -18,10 +18,9 @@ struct State { id: Option, } -pub struct Painter<'a, Ctx: 'static> { - widgets: &'a Widgets, - ctx: &'a mut Ctx, - sensors_map: &'a SensorMap, +pub struct Painter<'a> { + widgets: &'a Widgets, + sensors_map: &'a SensorMap, pub(super) active: &'a mut Active, pub(super) primitives: &'a mut Primitives, textures: &'a mut Textures, @@ -31,13 +30,12 @@ pub struct Painter<'a, Ctx: 'static> { state: State, } -impl<'a, Ctx> Painter<'a, Ctx> { +impl<'a> Painter<'a> { #[allow(clippy::too_many_arguments)] pub(super) fn new( - nodes: &'a Widgets, + nodes: &'a Widgets, primitives: &'a mut Primitives, - ctx: &'a mut Ctx, - sensors_map: &'a SensorMap, + sensors_map: &'a SensorMap, active: &'a mut Active, text: &'a mut TextData, textures: &'a mut Textures, @@ -45,7 +43,6 @@ impl<'a, Ctx> Painter<'a, Ctx> { ) -> Self { Self { widgets: nodes, - ctx, active, sensors_map, primitives, @@ -68,34 +65,22 @@ impl<'a, Ctx> Painter<'a, Ctx> { } /// Draws a widget within this widget's region. - pub fn draw(&mut self, id: &WidgetId) - where - Ctx: 'static, - { + pub fn draw(&mut self, id: &WidgetId) { self.draw_at(id, self.state.region); } /// Draws a widget somewhere within this one. /// Useful for drawing child widgets in select areas. - pub fn draw_within(&mut self, id: &WidgetId, region: UiRegion) - where - Ctx: 'static, - { + pub fn draw_within(&mut self, id: &WidgetId, region: UiRegion) { self.draw_at(id, region.within(&self.state.region)); } /// Draws a widget in an arbitrary region. - pub fn draw_at(&mut self, id: &WidgetId, region: UiRegion) - where - Ctx: 'static, - { + pub fn draw_at(&mut self, id: &WidgetId, region: UiRegion) { self.draw_raw_at(&id.id, region); } - fn draw_raw_at(&mut self, id: &Id, region: UiRegion) - where - Ctx: 'static, - { + fn draw_raw_at(&mut self, id: &Id, region: UiRegion) { if self.active.widgets.contains_key(id) { panic!("widget drawn twice!"); } @@ -158,10 +143,6 @@ impl<'a, Ctx> Painter<'a, Ctx> { self.state.region } - pub fn ctx(&mut self) -> &mut Ctx { - self.ctx - } - pub fn region_size(&self) -> Vec2 { self.state.region.in_size(self.screen_size) } diff --git a/src/layout/sense.rs b/src/layout/sense.rs index 2999cfb..4e44660 100644 --- a/src/layout/sense.rs +++ b/src/layout/sense.rs @@ -30,12 +30,12 @@ pub enum ActivationState { Off, } -pub struct Sensor { +pub struct Sensor { pub sense: Sense, - pub f: Box>, + pub f: Box, } -pub type SensorMap = HashMap>; +pub type SensorMap = HashMap; pub type ActiveSensors = HashMap; pub type SenseShape = UiRegion; #[derive(Clone)] @@ -43,26 +43,22 @@ pub struct SenseTrigger { pub shape: SenseShape, pub sense: Sense, } -pub struct SensorGroup { +pub struct SensorGroup { pub hover: ActivationState, pub cursor: ActivationState, - pub sensors: Vec>, + pub sensors: Vec, } -pub struct SenseCtx<'a, Ctx> { - pub ui: &'a mut Ui, - pub app: &'a mut Ctx, +pub trait SenseFn: FnMut(&mut Ui) + 'static { + fn box_clone(&self) -> Box; } -pub trait SenseFn: FnMut(SenseCtx) + 'static { - fn box_clone(&self) -> Box>; -} -impl) + 'static + Clone, Ctx> SenseFn for F { - fn box_clone(&self) -> Box> { +impl SenseFn for F { + fn box_clone(&self) -> Box { Box::new(self.clone()) } } -impl Ui { - pub fn add_sensor(&mut self, id: &WidgetId, f: Sensor) { +impl Ui { + pub fn add_sensor(&mut self, id: &WidgetId, f: Sensor) { self.sensor_map .entry(id.id.duplicate()) .or_default() @@ -70,10 +66,7 @@ impl Ui { .push(f); } - pub fn run_sensors(&mut self, ctx: &mut Ctx, cursor: &CursorState, window_size: Vec2) - where - Ctx: 'static, - { + pub fn run_sensors(&mut self, cursor: &CursorState, window_size: Vec2) { let active = std::mem::take(&mut self.active.sensors); let mut map = std::mem::take(&mut self.sensor_map); for (id, shape) in active.iter() { @@ -85,7 +78,7 @@ impl Ui { for sensor in &mut group.sensors { if should_run(sensor.sense, group.cursor, group.hover) { - (sensor.f.box_clone())(SenseCtx { ui: self, app: ctx }); + (sensor.f.box_clone())(self); } } } @@ -141,7 +134,7 @@ impl ActivationState { } } -impl Default for SensorGroup { +impl Default for SensorGroup { fn default() -> Self { Self { hover: Default::default(), @@ -150,7 +143,7 @@ impl Default for SensorGroup { } } } -impl Clone for SensorGroup { +impl Clone for SensorGroup { fn clone(&self) -> Self { Self { hover: self.hover, @@ -159,7 +152,7 @@ impl Clone for SensorGroup { } } } -impl Clone for Sensor { +impl Clone for Sensor { fn clone(&self) -> Self { Self { sense: self.sense, diff --git a/src/layout/ui.rs b/src/layout/ui.rs index 98cac51..ca9500f 100644 --- a/src/layout/ui.rs +++ b/src/layout/ui.rs @@ -14,9 +14,9 @@ use std::{ sync::mpsc::{Receiver, Sender, channel}, }; -pub struct Ui { +pub struct Ui { base: Option, - widgets: Widgets, + widgets: Widgets, labels: HashMap, updates: Vec, recv: Receiver, @@ -29,26 +29,23 @@ pub struct Ui { full_redraw: bool, pub(super) active: Active, - pub(super) sensor_map: SensorMap, + pub(super) sensor_map: SensorMap, } #[derive(Default)] -pub struct Widgets { +pub struct Widgets { ids: IdTracker, - map: HashMap>>, + map: HashMap>, } -impl Ui { - pub fn add, Tag>( - &mut self, - w: impl WidgetLike, - ) -> WidgetId { +impl Ui { + pub fn add(&mut self, w: impl WidgetLike) -> WidgetId { w.add(self) } - pub fn add_static, Tag>( + pub fn add_static( &mut self, - w: impl WidgetLike, + w: impl WidgetLike, ) -> StaticWidgetId { let id = w.add(self); id.into_static() @@ -59,11 +56,11 @@ impl Ui { self.labels.insert(id.id.duplicate(), label); } - pub fn add_widget>(&mut self, w: W) -> WidgetId { + pub fn add_widget(&mut self, w: W) -> WidgetId { self.push(w) } - pub fn push>(&mut self, w: W) -> WidgetId { + pub fn push(&mut self, w: W) -> WidgetId { let id = self.id(); self.labels .insert(id.id.duplicate(), std::any::type_name::().to_string()); @@ -71,31 +68,28 @@ impl Ui { id } - pub fn set>(&mut self, id: &WidgetId, w: W) { + pub fn set(&mut self, id: &WidgetId, w: W) { self.widgets.insert(id.id.duplicate(), w); } - pub fn set_base(&mut self, w: impl WidgetLike) { + pub fn set_base(&mut self, w: impl WidgetLike) { self.base = Some(w.add(self).erase_type()); self.full_redraw = true; } - pub fn new() -> Self - where - Ctx: 'static, - { + pub fn new() -> Self { Self::default() } - pub fn get>(&self, id: &WidgetId) -> Option<&W> { + pub fn get(&self, id: &WidgetId) -> Option<&W> { self.widgets.get(id) } - pub fn get_mut>(&mut self, id: &WidgetId) -> Option<&mut W> { + pub fn get_mut(&mut self, id: &WidgetId) -> Option<&mut W> { self.widgets.get_mut(id) } - pub fn id>(&mut self) -> WidgetId { + pub fn id(&mut self) -> WidgetId { WidgetId::new( self.widgets.reserve(), TypeId::of::(), @@ -104,7 +98,7 @@ impl Ui { ) } - pub fn id_static>(&mut self) -> StaticWidgetId { + pub fn id_static(&mut self) -> StaticWidgetId { let id = self.id(); id.into_static() } @@ -117,10 +111,7 @@ impl Ui { self.size = size.into(); } - pub fn redraw_all(&mut self, ctx: &mut Ctx) - where - Ctx: 'static, - { + pub fn redraw_all(&mut self) { self.active.clear(); self.primitives.clear(); // free before bc nothing should exist @@ -128,7 +119,6 @@ impl Ui { let mut painter = Painter::new( &self.widgets, &mut self.primitives, - ctx, &self.sensor_map, &mut self.active, &mut self.text, @@ -141,26 +131,19 @@ impl Ui { self.primitives.replace(); } - pub fn update(&mut self, ctx: &mut Ctx) - where - Ctx: 'static, - { + pub fn update(&mut self) { if self.full_redraw { - self.redraw_all(ctx); + self.redraw_all(); self.full_redraw = false; } else if !self.updates.is_empty() { - self.redraw_updates(ctx); + self.redraw_updates(); } } - fn redraw_updates(&mut self, ctx: &mut Ctx) - where - Ctx: 'static, - { + fn redraw_updates(&mut self) { let mut painter = Painter::new( &self.widgets, &mut self.primitives, - ctx, &self.sensor_map, &mut self.active, &mut self.text, @@ -191,7 +174,7 @@ impl Ui { } } -impl, Ctx> Index<&WidgetId> for Ui { +impl Index<&WidgetId> for Ui { type Output = W; fn index(&self, id: &WidgetId) -> &Self::Output { @@ -199,14 +182,14 @@ impl, Ctx> Index<&WidgetId> for Ui { } } -impl, Ctx> IndexMut<&WidgetId> for Ui { +impl IndexMut<&WidgetId> for Ui { fn index_mut(&mut self, id: &WidgetId) -> &mut Self::Output { self.updates.push(id.id.duplicate()); self.get_mut(id).unwrap() } } -impl, Ctx> Index> for Ui { +impl Index> for Ui { type Output = W; fn index(&self, id: StaticWidgetId) -> &Self::Output { @@ -214,14 +197,14 @@ impl, Ctx> Index> for Ui { } } -impl, Ctx> IndexMut> for Ui { +impl IndexMut> for Ui { fn index_mut(&mut self, id: StaticWidgetId) -> &mut Self::Output { self.updates.push(id.id.id()); self.widgets.get_static_mut(&id).unwrap() } } -impl Widgets { +impl Widgets { pub fn new() -> Self { Self { ids: IdTracker::default(), @@ -229,15 +212,15 @@ impl Widgets { } } - pub fn get_dyn(&self, id: &Id) -> &dyn Widget { + pub fn get_dyn(&self, id: &Id) -> &dyn Widget { self.map.get(id).unwrap().as_ref() } - pub fn get_static>(&self, id: &StaticWidgetId) -> Option<&W> { + pub fn get_static(&self, id: &StaticWidgetId) -> Option<&W> { self.map.get(&id.id.id()).unwrap().as_any().downcast_ref() } - pub fn get_static_mut>(&mut self, id: &StaticWidgetId) -> Option<&mut W> { + pub fn get_static_mut(&mut self, id: &StaticWidgetId) -> Option<&mut W> { self.map .get_mut(&id.id.id()) .unwrap() @@ -245,11 +228,11 @@ impl Widgets { .downcast_mut() } - pub fn get>(&self, id: &WidgetId) -> Option<&W> { + pub fn get(&self, id: &WidgetId) -> Option<&W> { self.map.get(&id.id).unwrap().as_any().downcast_ref() } - pub fn get_mut>(&mut self, id: &WidgetId) -> Option<&mut W> { + pub fn get_mut(&mut self, id: &WidgetId) -> Option<&mut W> { self.map .get_mut(&id.id) .unwrap() @@ -257,11 +240,11 @@ impl Widgets { .downcast_mut() } - pub fn insert(&mut self, id: Id, widget: impl Widget) { + pub fn insert(&mut self, id: Id, widget: impl Widget) { self.map.insert(id, Box::new(widget)); } - pub fn insert_any(&mut self, id: Id, widget: Box>) { + pub fn insert_any(&mut self, id: Id, widget: Box) { self.map.insert(id, widget); } @@ -283,7 +266,7 @@ impl Widgets { } } -impl dyn Widget { +impl dyn Widget { pub fn as_any(&self) -> &dyn Any { self } @@ -293,7 +276,7 @@ impl dyn Widget { } } -impl Default for Ui { +impl Default for Ui { fn default() -> Self { let (send, recv) = channel(); Self { @@ -341,7 +324,7 @@ impl Active { instance } - pub fn add(&mut self, id: &Id, instance: WidgetInstance, sensors_map: &SensorMap) { + pub fn add(&mut self, id: &Id, instance: WidgetInstance, sensors_map: &SensorMap) { if sensors_map.get(id).is_some() { self.sensors.insert(id.duplicate(), instance.region); } diff --git a/src/layout/widget.rs b/src/layout/widget.rs index 863ae3f..cc5049c 100644 --- a/src/layout/widget.rs +++ b/src/layout/widget.rs @@ -2,20 +2,20 @@ use crate::layout::{Painter, Ui, WidgetId, WidgetIdFnRet}; use std::{any::Any, marker::PhantomData}; -pub trait Widget: Any { - fn draw(&self, painter: &mut Painter); +pub trait Widget: Any { + fn draw(&self, painter: &mut Painter); } pub struct WidgetTag; pub struct FnTag; -pub trait WidgetLike { +pub trait WidgetLike { type Widget: 'static; - fn add(self, ui: &mut Ui) -> WidgetId; + fn add(self, ui: &mut Ui) -> WidgetId; fn with_id( self, - f: impl FnOnce(&mut Ui, WidgetId) -> WidgetId, - ) -> WidgetIdFnRet!(W2, Ctx) + f: impl FnOnce(&mut Ui, WidgetId) -> WidgetId, + ) -> WidgetIdFnRet!(W2) where Self: Sized, { @@ -33,22 +33,22 @@ pub trait WidgetLike { /// don't need to be IDs yet /// currently a macro for rust analyzer (doesn't support trait aliases atm) macro_rules! WidgetFnRet { - ($W:ty, $Ctx:ty) => { - impl FnOnce(&mut $crate::layout::Ui<$Ctx>) -> $W + ($W:ty) => { + impl FnOnce(&mut $crate::layout::Ui) -> $W }; } pub(crate) use WidgetFnRet; -impl, Ctx, F: FnOnce(&mut Ui) -> W> WidgetLike for F { +impl W> WidgetLike for F { type Widget = W; - fn add(self, ui: &mut Ui) -> WidgetId { + fn add(self, ui: &mut Ui) -> WidgetId { self(ui).add(ui) } } -impl, Ctx> WidgetLike for W { +impl WidgetLike for W { type Widget = W; - fn add(self, ui: &mut Ui) -> WidgetId { + fn add(self, ui: &mut Ui) -> WidgetId { ui.add_widget(self) } } @@ -68,21 +68,21 @@ impl WidgetArr { } pub struct ArrTag; -pub trait WidgetArrLike { +pub trait WidgetArrLike { type Ws; - fn ui(self, ui: &mut Ui) -> WidgetArr; + fn ui(self, ui: &mut Ui) -> WidgetArr; } -impl WidgetArrLike for WidgetArr { +impl WidgetArrLike for WidgetArr { type Ws = Ws; - fn ui(self, _: &mut Ui) -> WidgetArr { + fn ui(self, _: &mut Ui) -> WidgetArr { self } } -impl, Ctx> WidgetArrLike<1, Ctx, WidgetTag> for W { +impl> WidgetArrLike<1, WidgetTag> for W { type Ws = (W::Widget,); - fn ui(self, ui: &mut Ui) -> WidgetArr<1, (W::Widget,)> { + fn ui(self, ui: &mut Ui) -> WidgetArr<1, (W::Widget,)> { WidgetArr::new([self.add(ui).erase_type()]) } } @@ -93,9 +93,9 @@ macro_rules! impl_widget_arr { impl_widget_arr!($n;$($W)*;$(${concat($W,Tag)})*); }; ($n:expr;$($W:ident)*;$($Tag:ident)*) => { - impl<$($W: WidgetLike,$Tag,)* Ctx> WidgetArrLike<$n, Ctx, ($($Tag,)*)> for ($($W,)*) { + impl<$($W: WidgetLike<$Tag>,$Tag,)*> WidgetArrLike<$n, ($($Tag,)*)> for ($($W,)*) { type Ws = ($($W::Widget,)*); - fn ui(self, ui: &mut Ui) -> WidgetArr<$n, ($($W::Widget,)*)> { + fn ui(self, ui: &mut Ui) -> WidgetArr<$n, ($($W::Widget,)*)> { #[allow(non_snake_case)] let ($($W,)*) = self; WidgetArr::new( diff --git a/src/render/mod.rs b/src/render/mod.rs index 9061b70..651cdd0 100644 --- a/src/render/mod.rs +++ b/src/render/mod.rs @@ -48,7 +48,7 @@ impl UiRenderer { pass.draw(0..4, 0..self.instance.len() as u32); } - pub fn update(&mut self, device: &Device, queue: &Queue, ui: &mut Ui) { + pub fn update(&mut self, device: &Device, queue: &Queue, ui: &mut Ui) { if ui.primitives.updated { self.instance .update(device, queue, &ui.primitives.instances); diff --git a/src/testing/app.rs b/src/testing/app.rs index b766298..36f1d7d 100644 --- a/src/testing/app.rs +++ b/src/testing/app.rs @@ -10,7 +10,7 @@ use super::Client; #[derive(Default)] pub struct App { - client: Option<(Client, Ui)>, + client: Option<(Client, Ui)>, } impl App { diff --git a/src/testing/mod.rs b/src/testing/mod.rs index 18c2960..02452b8 100644 --- a/src/testing/mod.rs +++ b/src/testing/mod.rs @@ -27,7 +27,7 @@ pub struct ClientUi { } impl Client { - pub fn create_ui() -> (Ui, ClientUi) { + pub fn create_ui() -> (Ui, ClientUi) { let mut ui = Ui::new(); let rect = Rect { color: UiColor::WHITE, @@ -78,17 +78,17 @@ impl Client { let switch_button = |color, to, label| { let rect = Rect::new(color) - .id_on(Sense::PressStart, move |id, ctx| { - ctx.ui[main].inner.set_static(to); - ctx.ui[id].color = color.add_rgb(-0.2); + .id_on(Sense::PressStart, move |id, ui| { + ui[main].inner.set_static(to); + ui[id].color = color.add_rgb(-0.2); }) - .edit_on(Sense::HoverStart, move |r, _| { + .edit_on(Sense::HoverStart, move |r| { r.color = color.add_rgb(0.4); }) - .edit_on(Sense::PressEnd, move |r, _| { + .edit_on(Sense::PressEnd, move |r| { r.color = color.add_rgb(0.4); }) - .edit_on(Sense::HoverEnd, move |r, _| { + .edit_on(Sense::HoverEnd, move |r| { r.color = color; }); (rect, text(label).size(30)).stack() @@ -104,12 +104,11 @@ impl Client { ); let add_button = Rect::new(Color::LIME) .radius(30) - .on(Sense::PressStart, move |ctx| { - let child = ctx - .ui + .on(Sense::PressStart, move |ui| { + let child = ui .add(image(include_bytes!("assets/sungals.png"))) .erase_type(); - ctx.ui[span_add].children.push((child, ratio(1))); + ui[span_add].children.push((child, ratio(1))); }) .region( UiPos::corner(Corner::BotRight) @@ -119,8 +118,8 @@ impl Client { let del_button = Rect::new(Color::RED) .radius(30) - .on(Sense::PressStart, move |ctx| { - ctx.ui[span_add].children.pop(); + .on(Sense::PressStart, move |ui| { + ui[span_add].children.pop(); }) .region( UiPos::corner(Corner::BotLeft) @@ -167,15 +166,15 @@ impl Client { } } - pub fn event(&mut self, event: WindowEvent, event_loop: &ActiveEventLoop, ui: &mut Ui) { + pub fn event(&mut self, event: WindowEvent, event_loop: &ActiveEventLoop, ui: &mut Ui) { self.input.event(&event); let cursor_state = self.cursor_state(); let window_size = self.window_size(); - ui.run_sensors(self, &cursor_state, window_size); + ui.run_sensors(&cursor_state, window_size); match event { WindowEvent::CloseRequested => event_loop.exit(), WindowEvent::RedrawRequested => { - ui.update(self); + ui.update(); self.renderer.update(ui); self.renderer.draw() } diff --git a/src/testing/render/mod.rs b/src/testing/render/mod.rs index adb4353..d57b4ad 100644 --- a/src/testing/render/mod.rs +++ b/src/testing/render/mod.rs @@ -21,7 +21,7 @@ pub struct Renderer { } impl Renderer { - pub fn update(&mut self, updates: &mut Ui) { + pub fn update(&mut self, updates: &mut Ui) { self.ui.update(&self.device, &self.queue, updates); }