handles (tuple)

This commit is contained in:
2025-12-11 07:30:59 -05:00
parent 36668c82f4
commit 2dad409300
5 changed files with 36 additions and 40 deletions

View File

@@ -26,6 +26,8 @@ pub struct WidgetView<W: ?Sized = dyn Widget> {
ty: *const W, ty: *const W,
} }
pub type WidgetHandles<W> = (WidgetHandle<W>, WidgetView<W>);
impl<W: Widget + ?Sized + Unsize<dyn Widget>> WidgetHandle<W> { impl<W: Widget + ?Sized + Unsize<dyn Widget>> WidgetHandle<W> {
pub fn any(self) -> WidgetHandle<dyn Widget> { pub fn any(self) -> WidgetHandle<dyn Widget> {
self self
@@ -60,7 +62,7 @@ impl<W: ?Sized> WidgetHandle<W> {
WidgetView { ty, id } WidgetView { ty, id }
} }
pub fn handles(self) -> (Self, WidgetView<W>) { pub fn handles(self) -> WidgetHandles<W> {
let weak = self.weak(); let weak = self.weak();
(self, weak) (self, weak)
} }

View File

@@ -1,7 +1,7 @@
use super::*; use super::*;
use std::marker::Unsize; use std::marker::Unsize;
pub trait WidgetLike<Tag> { pub trait WidgetLike<Tag>: Sized {
type Widget: Widget + ?Sized + Unsize<dyn Widget> + 'static; type Widget: Widget + ?Sized + Unsize<dyn Widget> + 'static;
fn add(self, ui: &mut Ui) -> WidgetHandle<Self::Widget>; fn add(self, ui: &mut Ui) -> WidgetHandle<Self::Widget>;
@@ -9,22 +9,20 @@ pub trait WidgetLike<Tag> {
fn with_id<W2>( fn with_id<W2>(
self, self,
f: impl FnOnce(&mut Ui, WidgetHandle<Self::Widget>) -> WidgetHandle<W2>, f: impl FnOnce(&mut Ui, WidgetHandle<Self::Widget>) -> WidgetHandle<W2>,
) -> impl WidgetIdFn<W2> ) -> impl WidgetIdFn<W2> {
where
Self: Sized,
{
move |ui| { move |ui| {
let id = self.add(ui); let id = self.add(ui);
f(ui, id) f(ui, id)
} }
} }
fn set_root(self, ui: &mut Ui) fn set_root(self, ui: &mut Ui) {
where
Self: Sized,
{
ui.set_root(self); ui.set_root(self);
} }
fn handles(self, ui: &mut Ui) -> WidgetHandles<Self::Widget> {
self.add(ui).handles()
}
} }
pub trait WidgetArrLike<const LEN: usize, Tag> { pub trait WidgetArrLike<const LEN: usize, Tag> {

View File

@@ -39,8 +39,7 @@ impl DefaultAppState for Client {
.width(rest(3)), .width(rest(3)),
) )
.span(Dir::RIGHT) .span(Dir::RIGHT)
.add(ui) .handles(ui);
.handles();
let span_test = ( let span_test = (
rrect.color(Color::GREEN).width(100), rrect.color(Color::GREEN).width(100),
@@ -53,15 +52,14 @@ impl DefaultAppState for Client {
.span(Dir::LEFT) .span(Dir::LEFT)
.add(ui); .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) let add_button = rect(Color::LIME)
.radius(30) .radius(30)
.on(CursorSense::click(), move |ctx| { .on(CursorSense::click(), move |ctx| {
let child = ctx let child = ctx
.ui .ui
.add(image(include_bytes!("assets/sungals.png")).center()) .add(image(include_bytes!("assets/sungals.png")).center());
.any();
ctx.ui[span_add.1].children.push(child); ctx.ui[span_add.1].children.push(child);
}) })
.sized((150, 150)) .sized((150, 150))
@@ -100,7 +98,7 @@ impl DefaultAppState for Client {
.span(Dir::DOWN) .span(Dir::DOWN)
.add(ui); .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 msg_area = texts.0.scroll().masked().background(rect(Color::SKY));
let add_text = wtext("add") let add_text = wtext("add")
.editable(false) .editable(false)
@@ -116,10 +114,9 @@ impl DefaultAppState for Client {
.wrap(true) .wrap(true)
.attr::<Selectable>(()); .attr::<Selectable>(());
let msg_box = text.background(rect(Color::WHITE.darker(0.5))).add(ctx.ui); 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(ui);
.handles();
let text_edit_scroll = ( let text_edit_scroll = (
msg_area.height(rest(1)), msg_area.height(rest(1)),
( (
@@ -143,7 +140,7 @@ impl DefaultAppState for Client {
.span(Dir::DOWN) .span(Dir::DOWN)
.add(ui); .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 vals = Rc::new(RefCell::new((0, Vec::new())));
let mut switch_button = |color, to: WidgetHandle, label| { let mut switch_button = |color, to: WidgetHandle, label| {
@@ -190,7 +187,7 @@ impl DefaultAppState for Client {
) )
.span(Dir::RIGHT); .span(Dir::RIGHT);
let info = wtext("").add(ui).handles(); let info = wtext("").handles(ui);
let info_sect = info.0.pad(10).align(Align::RIGHT); let info_sect = info.0.pad(10).align(Align::RIGHT);
((tabs.height(40), main.0.pad(10)).span(Dir::DOWN), info_sect) ((tabs.height(40), main.0.pad(10)).span(Dir::DOWN), info_sect)

View File

@@ -43,17 +43,16 @@ pub mod eventable {
f: impl WidgetEventFn<Ctx, E::Data, WL::Widget>, f: impl WidgetEventFn<Ctx, E::Data, WL::Widget>,
) -> impl WidgetIdFn<WL::Widget> { ) -> impl WidgetIdFn<WL::Widget> {
move |ui| { move |ui| {
let id = self.add(ui); let id = self.handles(ui);
let id_ = id.weak(); ui.register_event(&id.1, event, move |ctx| {
ui.register_event(&id, event, move |ctx| {
f(EventIdCtx { f(EventIdCtx {
id: id_, id: id.1,
state: ctx.state, state: ctx.state,
data: ctx.data, data: ctx.data,
ui: ctx.ui, ui: ctx.ui,
}); });
}); });
id id.0
} }
} }
} }

View File

@@ -10,13 +10,13 @@ widget_trait! {
fn pad(self, padding: impl Into<Padding>) -> impl WidgetFn<Pad> { fn pad(self, padding: impl Into<Padding>) -> impl WidgetFn<Pad> {
|ui| Pad { |ui| Pad {
padding: padding.into(), padding: padding.into(),
inner: self.add(ui).any(), inner: self.add(ui),
} }
} }
fn align(self, align: impl Into<Align>) -> impl WidgetFn<Aligned> { fn align(self, align: impl Into<Align>) -> impl WidgetFn<Aligned> {
move |ui| Aligned { move |ui| Aligned {
inner: self.add(ui).any(), inner: self.add(ui),
align: align.into(), align: align.into(),
} }
} }
@@ -36,7 +36,7 @@ widget_trait! {
fn sized(self, size: impl Into<Size>) -> impl WidgetFn<Sized> { fn sized(self, size: impl Into<Size>) -> impl WidgetFn<Sized> {
let size = size.into(); let size = size.into();
move |ui| Sized { move |ui| Sized {
inner: self.add(ui).any(), inner: self.add(ui),
x: Some(size.x), x: Some(size.x),
y: Some(size.y), y: Some(size.y),
} }
@@ -45,7 +45,7 @@ widget_trait! {
fn max_width(self, len: impl Into<Len>) -> impl WidgetFn<MaxSize> { fn max_width(self, len: impl Into<Len>) -> impl WidgetFn<MaxSize> {
let len = len.into(); let len = len.into();
move |ui| MaxSize { move |ui| MaxSize {
inner: self.add(ui).any(), inner: self.add(ui),
x: Some(len), x: Some(len),
y: None, y: None,
} }
@@ -54,7 +54,7 @@ widget_trait! {
fn max_height(self, len: impl Into<Len>) -> impl WidgetFn<MaxSize> { fn max_height(self, len: impl Into<Len>) -> impl WidgetFn<MaxSize> {
let len = len.into(); let len = len.into();
move |ui| MaxSize { move |ui| MaxSize {
inner: self.add(ui).any(), inner: self.add(ui),
x: None, x: None,
y: Some(len), y: Some(len),
} }
@@ -63,7 +63,7 @@ widget_trait! {
fn width(self, len: impl Into<Len>) -> impl WidgetFn<Sized> { fn width(self, len: impl Into<Len>) -> impl WidgetFn<Sized> {
let len = len.into(); let len = len.into();
move |ui| Sized { move |ui| Sized {
inner: self.add(ui).any(), inner: self.add(ui),
x: Some(len), x: Some(len),
y: None, y: None,
} }
@@ -72,7 +72,7 @@ widget_trait! {
fn height(self, len: impl Into<Len>) -> impl WidgetFn<Sized> { fn height(self, len: impl Into<Len>) -> impl WidgetFn<Sized> {
let len = len.into(); let len = len.into();
move |ui| Sized { move |ui| Sized {
inner: self.add(ui).any(), inner: self.add(ui),
x: None, x: None,
y: Some(len), y: Some(len),
} }
@@ -80,14 +80,14 @@ widget_trait! {
fn offset(self, amt: impl Into<UiVec2>) -> impl WidgetFn<Offset> { fn offset(self, amt: impl Into<UiVec2>) -> impl WidgetFn<Offset> {
move |ui| Offset { move |ui| Offset {
inner: self.add(ui).any(), inner: self.add(ui),
amt: amt.into(), amt: amt.into(),
} }
} }
fn scroll(self) -> impl WidgetIdFn<Scroll> { fn scroll(self) -> impl WidgetIdFn<Scroll> {
move |ui| { move |ui| {
Scroll::new(self.add(ui).any(), Axis::Y) Scroll::new(self.add(ui), Axis::Y)
.on(CursorSense::Scroll, |ctx| { .on(CursorSense::Scroll, |ctx| {
let s = &mut ctx.ui[ctx.id]; let s = &mut ctx.ui[ctx.id];
s.scroll(ctx.data.scroll_delta.y * 50.0); s.scroll(ctx.data.scroll_delta.y * 50.0);
@@ -98,33 +98,33 @@ widget_trait! {
fn masked(self) -> impl WidgetFn<Masked> { fn masked(self) -> impl WidgetFn<Masked> {
move |ui| Masked { move |ui| Masked {
inner: self.add(ui).any(), inner: self.add(ui),
} }
} }
fn background<T>(self, w: impl WidgetLike<T>) -> impl WidgetFn<Stack> { fn background<T>(self, w: impl WidgetLike<T>) -> impl WidgetFn<Stack> {
move |ui| Stack { 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), size: StackSize::Child(1),
} }
} }
fn foreground<T>(self, w: impl WidgetLike<T>) -> impl WidgetFn<Stack> { fn foreground<T>(self, w: impl WidgetLike<T>) -> impl WidgetFn<Stack> {
move |ui| Stack { 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), size: StackSize::Child(0),
} }
} }
fn layer_offset(self, offset: usize) -> impl WidgetFn<LayerOffset> { fn layer_offset(self, offset: usize) -> impl WidgetFn<LayerOffset> {
move |ui| LayerOffset { move |ui| LayerOffset {
inner: self.add(ui).any(), inner: self.add(ui),
offset, offset,
} }
} }
fn to_any(self) -> impl WidgetIdFn { fn to_any(self) -> impl WidgetIdFn {
|ui| self.add(ui).any() |ui| self.add(ui)
} }
} }