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,
}
pub type WidgetHandles<W> = (WidgetHandle<W>, WidgetView<W>);
impl<W: Widget + ?Sized + Unsize<dyn Widget>> WidgetHandle<W> {
pub fn any(self) -> WidgetHandle<dyn Widget> {
self
@@ -60,7 +62,7 @@ impl<W: ?Sized> WidgetHandle<W> {
WidgetView { ty, id }
}
pub fn handles(self) -> (Self, WidgetView<W>) {
pub fn handles(self) -> WidgetHandles<W> {
let weak = self.weak();
(self, weak)
}

View File

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

View File

@@ -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::<Selectable>(());
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)

View File

@@ -43,17 +43,16 @@ pub mod eventable {
f: impl WidgetEventFn<Ctx, E::Data, WL::Widget>,
) -> impl WidgetIdFn<WL::Widget> {
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
}
}
}

View File

@@ -10,13 +10,13 @@ widget_trait! {
fn pad(self, padding: impl Into<Padding>) -> impl WidgetFn<Pad> {
|ui| Pad {
padding: padding.into(),
inner: self.add(ui).any(),
inner: self.add(ui),
}
}
fn align(self, align: impl Into<Align>) -> impl WidgetFn<Aligned> {
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<Size>) -> impl WidgetFn<Sized> {
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<Len>) -> impl WidgetFn<MaxSize> {
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<Len>) -> impl WidgetFn<MaxSize> {
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<Len>) -> impl WidgetFn<Sized> {
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<Len>) -> impl WidgetFn<Sized> {
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<UiVec2>) -> impl WidgetFn<Offset> {
move |ui| Offset {
inner: self.add(ui).any(),
inner: self.add(ui),
amt: amt.into(),
}
}
fn scroll(self) -> impl WidgetIdFn<Scroll> {
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<Masked> {
move |ui| Masked {
inner: self.add(ui).any(),
inner: self.add(ui),
}
}
fn background<T>(self, w: impl WidgetLike<T>) -> impl WidgetFn<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),
}
}
fn foreground<T>(self, w: impl WidgetLike<T>) -> impl WidgetFn<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),
}
}
fn layer_offset(self, offset: usize) -> impl WidgetFn<LayerOffset> {
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)
}
}