diff --git a/core/src/widget/handle.rs b/core/src/widget/handle.rs index 96cb936..832ffff 100644 --- a/core/src/widget/handle.rs +++ b/core/src/widget/handle.rs @@ -26,6 +26,8 @@ pub struct WidgetView { ty: *const W, } +pub type WidgetHandles = (WidgetHandle, WidgetView); + impl> WidgetHandle { pub fn any(self) -> WidgetHandle { self @@ -60,7 +62,7 @@ impl WidgetHandle { WidgetView { ty, id } } - pub fn handles(self) -> (Self, WidgetView) { + pub fn handles(self) -> WidgetHandles { let weak = self.weak(); (self, weak) } diff --git a/core/src/widget/like.rs b/core/src/widget/like.rs index 1bfa5bc..ada9bf7 100644 --- a/core/src/widget/like.rs +++ b/core/src/widget/like.rs @@ -1,7 +1,7 @@ use super::*; use std::marker::Unsize; -pub trait WidgetLike { +pub trait WidgetLike: Sized { type Widget: Widget + ?Sized + Unsize + 'static; fn add(self, ui: &mut Ui) -> WidgetHandle; @@ -9,22 +9,20 @@ pub trait WidgetLike { fn with_id( self, f: impl FnOnce(&mut Ui, WidgetHandle) -> WidgetHandle, - ) -> impl WidgetIdFn - where - Self: Sized, - { + ) -> impl WidgetIdFn { move |ui| { let id = self.add(ui); f(ui, id) } } - fn set_root(self, ui: &mut Ui) - where - Self: Sized, - { + fn set_root(self, ui: &mut Ui) { ui.set_root(self); } + + fn handles(self, ui: &mut Ui) -> WidgetHandles { + self.add(ui).handles() + } } pub trait WidgetArrLike { diff --git a/src/bin/test/main.rs b/src/bin/test/main.rs index ada9dc8..ce72f30 100644 --- a/src/bin/test/main.rs +++ b/src/bin/test/main.rs @@ -39,8 +39,7 @@ impl DefaultAppState for Client { .width(rest(3)), ) .span(Dir::RIGHT) - .add(ui) - .handles(); + .handles(ui); let span_test = ( rrect.color(Color::GREEN).width(100), @@ -53,15 +52,14 @@ impl DefaultAppState for Client { .span(Dir::LEFT) .add(ui); - let span_add = Span::empty(Dir::RIGHT).add(ui).handles(); + let span_add = Span::empty(Dir::RIGHT).handles(ui); let add_button = rect(Color::LIME) .radius(30) .on(CursorSense::click(), move |ctx| { let child = ctx .ui - .add(image(include_bytes!("assets/sungals.png")).center()) - .any(); + .add(image(include_bytes!("assets/sungals.png")).center()); ctx.ui[span_add.1].children.push(child); }) .sized((150, 150)) @@ -100,7 +98,7 @@ impl DefaultAppState for Client { .span(Dir::DOWN) .add(ui); - let texts = Span::empty(Dir::DOWN).gap(10).add(ui).handles(); + let texts = Span::empty(Dir::DOWN).gap(10).handles(ui); let msg_area = texts.0.scroll().masked().background(rect(Color::SKY)); let add_text = wtext("add") .editable(false) @@ -116,10 +114,9 @@ impl DefaultAppState for Client { .wrap(true) .attr::(()); let msg_box = text.background(rect(Color::WHITE.darker(0.5))).add(ctx.ui); - ctx.ui[texts.1].children.push(msg_box.any()); + ctx.ui[texts.1].children.push(msg_box); }) - .add(ui) - .handles(); + .handles(ui); let text_edit_scroll = ( msg_area.height(rest(1)), ( @@ -143,7 +140,7 @@ impl DefaultAppState for Client { .span(Dir::DOWN) .add(ui); - let main = WidgetPtr::new().add(ui).handles(); + let main = WidgetPtr::new().handles(ui); let vals = Rc::new(RefCell::new((0, Vec::new()))); let mut switch_button = |color, to: WidgetHandle, label| { @@ -190,7 +187,7 @@ impl DefaultAppState for Client { ) .span(Dir::RIGHT); - let info = wtext("").add(ui).handles(); + let info = wtext("").handles(ui); let info_sect = info.0.pad(10).align(Align::RIGHT); ((tabs.height(40), main.0.pad(10)).span(Dir::DOWN), info_sect) diff --git a/src/event.rs b/src/event.rs index eacc093..9bb3847 100644 --- a/src/event.rs +++ b/src/event.rs @@ -43,17 +43,16 @@ pub mod eventable { f: impl WidgetEventFn, ) -> impl WidgetIdFn { move |ui| { - let id = self.add(ui); - let id_ = id.weak(); - ui.register_event(&id, event, move |ctx| { + let id = self.handles(ui); + ui.register_event(&id.1, event, move |ctx| { f(EventIdCtx { - id: id_, + id: id.1, state: ctx.state, data: ctx.data, ui: ctx.ui, }); }); - id + id.0 } } } diff --git a/src/widget/trait_fns.rs b/src/widget/trait_fns.rs index 5c6a061..32a4492 100644 --- a/src/widget/trait_fns.rs +++ b/src/widget/trait_fns.rs @@ -10,13 +10,13 @@ widget_trait! { fn pad(self, padding: impl Into) -> impl WidgetFn { |ui| Pad { padding: padding.into(), - inner: self.add(ui).any(), + inner: self.add(ui), } } fn align(self, align: impl Into) -> impl WidgetFn { move |ui| Aligned { - inner: self.add(ui).any(), + inner: self.add(ui), align: align.into(), } } @@ -36,7 +36,7 @@ widget_trait! { fn sized(self, size: impl Into) -> impl WidgetFn { let size = size.into(); move |ui| Sized { - inner: self.add(ui).any(), + inner: self.add(ui), x: Some(size.x), y: Some(size.y), } @@ -45,7 +45,7 @@ widget_trait! { fn max_width(self, len: impl Into) -> impl WidgetFn { let len = len.into(); move |ui| MaxSize { - inner: self.add(ui).any(), + inner: self.add(ui), x: Some(len), y: None, } @@ -54,7 +54,7 @@ widget_trait! { fn max_height(self, len: impl Into) -> impl WidgetFn { let len = len.into(); move |ui| MaxSize { - inner: self.add(ui).any(), + inner: self.add(ui), x: None, y: Some(len), } @@ -63,7 +63,7 @@ widget_trait! { fn width(self, len: impl Into) -> impl WidgetFn { let len = len.into(); move |ui| Sized { - inner: self.add(ui).any(), + inner: self.add(ui), x: Some(len), y: None, } @@ -72,7 +72,7 @@ widget_trait! { fn height(self, len: impl Into) -> impl WidgetFn { let len = len.into(); move |ui| Sized { - inner: self.add(ui).any(), + inner: self.add(ui), x: None, y: Some(len), } @@ -80,14 +80,14 @@ widget_trait! { fn offset(self, amt: impl Into) -> impl WidgetFn { move |ui| Offset { - inner: self.add(ui).any(), + inner: self.add(ui), amt: amt.into(), } } fn scroll(self) -> impl WidgetIdFn { move |ui| { - Scroll::new(self.add(ui).any(), Axis::Y) + Scroll::new(self.add(ui), Axis::Y) .on(CursorSense::Scroll, |ctx| { let s = &mut ctx.ui[ctx.id]; s.scroll(ctx.data.scroll_delta.y * 50.0); @@ -98,33 +98,33 @@ widget_trait! { fn masked(self) -> impl WidgetFn { move |ui| Masked { - inner: self.add(ui).any(), + inner: self.add(ui), } } fn background(self, w: impl WidgetLike) -> impl WidgetFn { move |ui| Stack { - children: vec![w.add(ui).any(), self.add(ui).any()], + children: vec![w.add(ui), self.add(ui)], size: StackSize::Child(1), } } fn foreground(self, w: impl WidgetLike) -> impl WidgetFn { move |ui| Stack { - children: vec![self.add(ui).any(), w.add(ui).any()], + children: vec![self.add(ui), w.add(ui)], size: StackSize::Child(0), } } fn layer_offset(self, offset: usize) -> impl WidgetFn { move |ui| LayerOffset { - inner: self.add(ui).any(), + inner: self.add(ui), offset, } } fn to_any(self) -> impl WidgetIdFn { - |ui| self.add(ui).any() + |ui| self.add(ui) } }