switch away from handles to refs that must be upgraded once

This commit is contained in:
2025-12-20 01:36:07 -05:00
parent 32ca4ec5a6
commit c00ded78c0
11 changed files with 157 additions and 85 deletions

View File

@@ -18,7 +18,7 @@ impl<State: HasUi + StateLike<State>, WL: WidgetLike<State, Tag>, Tag>
) -> impl WidgetIdFn<State, WL::Widget> { ) -> impl WidgetIdFn<State, WL::Widget> {
|state| { |state| {
let id = self.add(state); let id = self.add(state);
A::run(state, id.weak(), input); A::run(state, id, input);
id id
} }
} }

View File

@@ -7,7 +7,7 @@ use crate::{
use image::DynamicImage; use image::DynamicImage;
use std::{ use std::{
ops::{Index, IndexMut}, ops::{Index, IndexMut},
sync::mpsc::{Receiver, Sender, channel}, sync::mpsc::{Receiver, channel},
}; };
mod active; mod active;
@@ -38,7 +38,6 @@ pub struct Ui {
pub root: Option<WidgetHandle>, pub root: Option<WidgetHandle>,
old_root: Option<WidgetId>, old_root: Option<WidgetId>,
recv: Receiver<WidgetId>, recv: Receiver<WidgetId>,
send: Sender<WidgetId>,
resized: bool, resized: bool,
} }
@@ -73,24 +72,20 @@ impl Ui {
&self.widgets.data(id.id()).unwrap().label &self.widgets.data(id.id()).unwrap().label
} }
pub fn add_widget<W: Widget>(&mut self, w: W) -> WidgetHandle<W> {
WidgetHandle::new(self.widgets.add(w), self.send.clone())
}
pub fn new() -> Self { pub fn new() -> Self {
Self::default() Self::default()
} }
pub fn get<I: IdLike>(&self, id: &I) -> Option<&I::Widget> pub fn get<I: IdLike>(&self, id: &I) -> Option<&I::Widget>
where where
I::Widget: Sized, I::Widget: Sized + Widget,
{ {
self.widgets.get(id) self.widgets.get(id)
} }
pub fn get_mut<I: IdLike>(&mut self, id: &I) -> Option<&mut I::Widget> pub fn get_mut<I: IdLike>(&mut self, id: &I) -> Option<&mut I::Widget>
where where
I::Widget: Sized, I::Widget: Sized + Widget,
{ {
self.widgets.get_mut(id) self.widgets.get_mut(id)
} }
@@ -105,6 +100,20 @@ impl Ui {
} }
pub fn update(&mut self, events: &mut dyn EventsLike) { pub fn update(&mut self, events: &mut dyn EventsLike) {
if !self.widgets.waiting.is_empty() {
let len = self.widgets.waiting.len();
let all: Vec<_> = self
.widgets
.waiting
.iter()
.map(|&w| format!("'{}' ({w:?})", self.label(w)))
.collect();
panic!(
"{len} widget(s) were never upgraded\n\
this is likely a memory leak; consider upgrading to strong if you plan on using it later\n\
weak widgets: {all:#?}"
);
}
if self.root_changed() { if self.root_changed() {
DrawState::new(self, events).redraw_all(); DrawState::new(self, events).redraw_all();
self.old_root = self.root.as_ref().map(|r| r.id()); self.old_root = self.root.as_ref().map(|r| r.id());
@@ -169,7 +178,7 @@ impl Ui {
impl<I: IdLike> Index<I> for Ui impl<I: IdLike> Index<I> for Ui
where where
I::Widget: Sized, I::Widget: Sized + Widget,
{ {
type Output = I::Widget; type Output = I::Widget;
@@ -180,7 +189,7 @@ where
impl<I: IdLike> IndexMut<I> for Ui impl<I: IdLike> IndexMut<I> for Ui
where where
I::Widget: Sized, I::Widget: Sized + Widget,
{ {
fn index_mut(&mut self, id: I) -> &mut Self::Output { fn index_mut(&mut self, id: I) -> &mut Self::Output {
self.get_mut(&id).unwrap() self.get_mut(&id).unwrap()
@@ -191,7 +200,7 @@ impl Default for Ui {
fn default() -> Self { fn default() -> Self {
let (send, recv) = channel(); let (send, recv) = channel();
Self { Self {
widgets: Default::default(), widgets: Widgets::new(send),
active: Default::default(), active: Default::default(),
layers: Default::default(), layers: Default::default(),
masks: Default::default(), masks: Default::default(),
@@ -201,7 +210,6 @@ impl Default for Ui {
output_size: Vec2::ZERO, output_size: Vec2::ZERO,
root: None, root: None,
old_root: None, old_root: None,
send,
recv, recv,
resized: false, resized: false,
} }

View File

@@ -68,9 +68,21 @@ impl<W: ?Sized> WidgetHandle<W> {
} }
impl<W: ?Sized> WidgetRef<W> { impl<W: ?Sized> WidgetRef<W> {
pub(crate) fn new(id: WidgetId) -> Self {
Self {
id,
ty: unsafe { MaybeUninit::zeroed().assume_init() },
}
}
pub fn id(&self) -> WidgetId { pub fn id(&self) -> WidgetId {
self.id self.id
} }
#[track_caller]
pub fn upgrade(self, ui: &mut impl HasUi) -> WidgetHandle<W> {
ui.ui_mut().widgets.upgrade(self)
}
} }
impl<W: ?Sized> Drop for WidgetHandle<W> { impl<W: ?Sized> Drop for WidgetHandle<W> {
@@ -81,29 +93,29 @@ impl<W: ?Sized> Drop for WidgetHandle<W> {
} }
} }
pub trait WidgetIdFn<State, W: ?Sized = dyn Widget>: FnOnce(&mut State) -> WidgetHandle<W> {} pub trait WidgetIdFn<State, W: ?Sized = dyn Widget>: FnOnce(&mut State) -> WidgetRef<W> {}
impl<State, W: ?Sized, F: FnOnce(&mut State) -> WidgetHandle<W>> WidgetIdFn<State, W> for F {} impl<State, W: ?Sized, F: FnOnce(&mut State) -> WidgetRef<W>> WidgetIdFn<State, W> for F {}
pub trait IdLike { pub trait IdLike {
type Widget: Widget + ?Sized + 'static; type Widget: ?Sized;
fn id(&self) -> WidgetId; fn id(&self) -> WidgetId;
} }
impl<W: Widget + ?Sized> IdLike for &WidgetHandle<W> { impl<W: ?Sized> IdLike for &WidgetHandle<W> {
type Widget = W; type Widget = W;
fn id(&self) -> WidgetId { fn id(&self) -> WidgetId {
self.id self.id
} }
} }
impl<W: Widget + ?Sized> IdLike for WidgetHandle<W> { impl<W: ?Sized> IdLike for WidgetHandle<W> {
type Widget = W; type Widget = W;
fn id(&self) -> WidgetId { fn id(&self) -> WidgetId {
self.id self.id
} }
} }
impl<W: Widget + ?Sized> IdLike for WidgetRef<W> { impl<W: ?Sized> IdLike for WidgetRef<W> {
type Widget = W; type Widget = W;
fn id(&self) -> WidgetId { fn id(&self) -> WidgetId {
self.id self.id

View File

@@ -16,11 +16,15 @@ impl StateLike<Ui> for Ui {
pub trait WidgetLike<State: HasUi + StateLike<State>, Tag>: Sized { pub trait WidgetLike<State: HasUi + StateLike<State>, Tag>: Sized {
type Widget: Widget + ?Sized + Unsize<dyn Widget>; type Widget: Widget + ?Sized + Unsize<dyn Widget>;
fn add(self, state: &mut impl StateLike<State>) -> WidgetHandle<Self::Widget>; fn add(self, state: &mut impl StateLike<State>) -> WidgetRef<Self::Widget>;
fn add_strong(self, state: &mut impl StateLike<State>) -> WidgetHandle<Self::Widget> {
self.add(state).upgrade(state.as_state().ui_mut())
}
fn with_id<W2>( fn with_id<W2>(
self, self,
f: impl FnOnce(&mut State, WidgetHandle<Self::Widget>) -> WidgetHandle<W2>, f: impl FnOnce(&mut State, WidgetRef<Self::Widget>) -> WidgetRef<W2>,
) -> impl WidgetIdFn<State, W2> { ) -> impl WidgetIdFn<State, W2> {
move |state| { move |state| {
let id = self.add(state); let id = self.add(state);
@@ -29,15 +33,18 @@ pub trait WidgetLike<State: HasUi + StateLike<State>, Tag>: Sized {
} }
fn set_root(self, state: &mut impl StateLike<State>) { fn set_root(self, state: &mut impl StateLike<State>) {
state.as_state().get_mut().root = Some(self.add(state)); let id = self.add(state);
let ui = state.as_state().ui_mut();
ui.root = Some(id.upgrade(ui));
} }
fn handles(self, state: &mut impl StateLike<State>) -> WidgetHandles<Self::Widget> { fn handles(self, state: &mut impl StateLike<State>) -> WidgetHandles<Self::Widget> {
self.add(state).handles() self.add(state).upgrade(state.as_state().ui_mut()).handles()
} }
} }
pub trait WidgetArrLike<State, const LEN: usize, Tag> { pub trait WidgetArrLike<State, const LEN: usize, Tag> {
#[track_caller]
fn add(self, state: &mut impl StateLike<State>) -> WidgetArr<LEN>; fn add(self, state: &mut impl StateLike<State>) -> WidgetArr<LEN>;
} }
@@ -58,7 +65,7 @@ macro_rules! impl_widget_arr {
#[allow(non_snake_case)] #[allow(non_snake_case)]
let ($($W,)*) = self; let ($($W,)*) = self;
WidgetArr::new( WidgetArr::new(
[$($W.add(state),)*], [$($W.add(state).upgrade(state.as_state().ui_mut()),)*],
) )
} }
} }

View File

@@ -5,8 +5,8 @@ use std::marker::Unsize;
pub struct WidgetTag; pub struct WidgetTag;
impl<State: HasUi + StateLike<State>, W: Widget> WidgetLike<State, WidgetTag> for W { impl<State: HasUi + StateLike<State>, W: Widget> WidgetLike<State, WidgetTag> for W {
type Widget = W; type Widget = W;
fn add(self, state: &mut impl StateLike<State>) -> WidgetHandle<W> { fn add(self, state: &mut impl StateLike<State>) -> WidgetRef<W> {
state.as_state().get_mut().add_widget(self) state.as_state().get_mut().widgets.add_weak(self)
} }
} }
@@ -15,17 +15,30 @@ impl<State: HasUi + StateLike<State>, W: Widget, F: FnOnce(&mut State) -> W>
WidgetLike<State, FnTag> for F WidgetLike<State, FnTag> for F
{ {
type Widget = W; type Widget = W;
fn add(self, state: &mut impl StateLike<State>) -> WidgetHandle<W> { fn add(self, state: &mut impl StateLike<State>) -> WidgetRef<W> {
self(state.as_state()).add(state) self(state.as_state()).add(state)
} }
} }
pub trait WidgetFnTrait<State> {
type Widget: Widget;
fn run(self, state: &mut State) -> Self::Widget;
}
pub struct FnTraitTag;
impl<State: HasUi + StateLike<State>, T: WidgetFnTrait<State>> WidgetLike<State, FnTraitTag> for T {
type Widget = T::Widget;
#[track_caller]
fn add(self, state: &mut impl StateLike<State>) -> WidgetRef<T::Widget> {
self.run(state.as_state()).add(state)
}
}
pub struct IdTag; pub struct IdTag;
impl<State: HasUi + StateLike<State>, W: ?Sized + Widget + Unsize<dyn Widget>> impl<State: HasUi + StateLike<State>, W: ?Sized + Widget + Unsize<dyn Widget>>
WidgetLike<State, IdTag> for WidgetHandle<W> WidgetLike<State, IdTag> for WidgetRef<W>
{ {
type Widget = W; type Widget = W;
fn add(self, _: &mut impl StateLike<State>) -> WidgetHandle<W> { fn add(self, _: &mut impl StateLike<State>) -> WidgetRef<W> {
self self
} }
} }
@@ -34,11 +47,11 @@ pub struct IdFnTag;
impl< impl<
State: HasUi + StateLike<State>, State: HasUi + StateLike<State>,
W: ?Sized + Widget + Unsize<dyn Widget>, W: ?Sized + Widget + Unsize<dyn Widget>,
F: FnOnce(&mut State) -> WidgetHandle<W>, F: FnOnce(&mut State) -> WidgetRef<W>,
> WidgetLike<State, IdFnTag> for F > WidgetLike<State, IdFnTag> for F
{ {
type Widget = W; type Widget = W;
fn add(self, state: &mut impl StateLike<State>) -> WidgetHandle<W> { fn add(self, state: &mut impl StateLike<State>) -> WidgetRef<W> {
self(state.as_state()) self(state.as_state())
} }
} }

View File

@@ -1,15 +1,27 @@
use std::sync::mpsc::Sender;
use crate::{ use crate::{
IdLike, Widget, WidgetData, WidgetId, IdLike, Widget, WidgetData, WidgetHandle, WidgetId, WidgetRef,
util::{DynBorrower, HashSet, SlotVec, forget_mut, to_mut}, util::{DynBorrower, HashSet, SlotVec, forget_mut, to_mut},
}; };
#[derive(Default)]
pub struct Widgets { pub struct Widgets {
pub needs_redraw: HashSet<WidgetId>, pub needs_redraw: HashSet<WidgetId>,
vec: SlotVec<WidgetData>, vec: SlotVec<WidgetData>,
send: Sender<WidgetId>,
pub(crate) waiting: HashSet<WidgetId>,
} }
impl Widgets { impl Widgets {
pub fn new(send: Sender<WidgetId>) -> Self {
Self {
needs_redraw: Default::default(),
vec: Default::default(),
waiting: Default::default(),
send,
}
}
pub fn has_updates(&self) -> bool { pub fn has_updates(&self) -> bool {
!self.needs_redraw.is_empty() !self.needs_redraw.is_empty()
} }
@@ -37,20 +49,37 @@ impl Widgets {
pub fn get<I: IdLike>(&self, id: &I) -> Option<&I::Widget> pub fn get<I: IdLike>(&self, id: &I) -> Option<&I::Widget>
where where
I::Widget: Sized, I::Widget: Sized + Widget,
{ {
self.get_dyn(id.id())?.as_any().downcast_ref() self.get_dyn(id.id())?.as_any().downcast_ref()
} }
pub fn get_mut<I: IdLike>(&mut self, id: &I) -> Option<&mut I::Widget> pub fn get_mut<I: IdLike>(&mut self, id: &I) -> Option<&mut I::Widget>
where where
I::Widget: Sized, I::Widget: Sized + Widget,
{ {
self.get_dyn_mut(id.id())?.as_any_mut().downcast_mut() self.get_dyn_mut(id.id())?.as_any_mut().downcast_mut()
} }
pub fn add<W: Widget>(&mut self, widget: W) -> WidgetId { pub fn add_strong<W: Widget>(&mut self, widget: W) -> WidgetHandle<W> {
self.vec.add(WidgetData::new(widget)) let id = self.vec.add(WidgetData::new(widget));
WidgetHandle::new(id, self.send.clone())
}
pub fn add_weak<W: Widget>(&mut self, widget: W) -> WidgetRef<W> {
let id = self.vec.add(WidgetData::new(widget));
self.waiting.insert(id);
WidgetRef::new(id)
}
#[track_caller]
pub fn upgrade<W: ?Sized>(&mut self, rf: WidgetRef<W>) -> WidgetHandle<W> {
if !self.waiting.remove(&rf.id()) {
let label = self.label(rf);
let id = rf.id();
panic!("widget '{label}' ({id:?}) was already added\ncannot add a widget twice; consider creating two")
}
WidgetHandle::new(rf.id(), self.send.clone())
} }
pub fn data(&self, id: impl IdLike) -> Option<&WidgetData> { pub fn data(&self, id: impl IdLike) -> Option<&WidgetData> {

View File

@@ -21,7 +21,6 @@ impl DefaultAppState for Client {
events: EventManager::default(), events: EventManager::default(),
}; };
let info;
let rrect = rect(Color::WHITE).radius(20); let rrect = rect(Color::WHITE).radius(20);
let pad_test = ( let pad_test = (
rrect.color(Color::BLUE), rrect.color(Color::BLUE),
@@ -44,7 +43,7 @@ impl DefaultAppState for Client {
.width(rest(3)), .width(rest(3)),
) )
.span(Dir::RIGHT) .span(Dir::RIGHT)
.handles(&mut rsc); .add(&mut rsc);
let span_test = ( let span_test = (
rrect.color(Color::GREEN).width(100), rrect.color(Color::GREEN).width(100),
@@ -57,15 +56,15 @@ impl DefaultAppState for Client {
.span(Dir::LEFT) .span(Dir::LEFT)
.add(&mut rsc); .add(&mut rsc);
let span_add = Span::empty(Dir::RIGHT).handles(&mut rsc); let span_add = Span::empty(Dir::RIGHT).add(&mut rsc);
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 = image(include_bytes!("assets/sungals.png")) let child = image(include_bytes!("assets/sungals.png"))
.center() .center()
.add(ctx); .add_strong(ctx);
(span_add.r)(ctx).children.push(child); span_add(ctx).children.push(child);
}) })
.sized((150, 150)) .sized((150, 150))
.align(Align::BOT_RIGHT); .align(Align::BOT_RIGHT);
@@ -73,12 +72,12 @@ impl DefaultAppState for Client {
let del_button = rect(Color::RED) let del_button = rect(Color::RED)
.radius(30) .radius(30)
.on(CursorSense::click(), move |ctx| { .on(CursorSense::click(), move |ctx| {
(span_add.r)(ctx).children.pop(); span_add(ctx).children.pop();
}) })
.sized((150, 150)) .sized((150, 150))
.align(Align::BOT_LEFT); .align(Align::BOT_LEFT);
let span_add_test = (span_add.h, add_button, del_button).stack().add(&mut rsc); let span_add_test = (span_add, add_button, del_button).stack().add(&mut rsc);
let btext = |content| wtext(content).size(30); let btext = |content| wtext(content).size(30);
@@ -103,8 +102,8 @@ impl DefaultAppState for Client {
.span(Dir::DOWN) .span(Dir::DOWN)
.add(&mut rsc); .add(&mut rsc);
let texts = Span::empty(Dir::DOWN).gap(10).handles(&mut rsc); let texts = Span::empty(Dir::DOWN).gap(10).add(&mut rsc);
let msg_area = texts.h.scrollable().masked().background(rect(Color::SKY)); let msg_area = texts.scrollable().masked().background(rect(Color::SKY));
let add_text = wtext("add") let add_text = wtext("add")
.editable(EditMode::MultiLine) .editable(EditMode::MultiLine)
.text_align(Align::LEFT) .text_align(Align::LEFT)
@@ -119,19 +118,21 @@ impl DefaultAppState for Client {
.text_align(Align::LEFT) .text_align(Align::LEFT)
.wrap(true) .wrap(true)
.attr::<Selectable>(()); .attr::<Selectable>(());
let msg_box = text.background(rect(Color::WHITE.darker(0.5))).add(ctx); let msg_box = text
(texts.r)(ctx).children.push(msg_box); .background(rect(Color::WHITE.darker(0.5)))
.add_strong(ctx);
texts(ctx).children.push(msg_box);
}) })
.handles(&mut rsc); .add(&mut rsc);
let text_edit_scroll = ( let text_edit_scroll = (
msg_area.height(rest(1)), msg_area.height(rest(1)),
( (
Rect::new(Color::WHITE.darker(0.9)), Rect::new(Color::WHITE.darker(0.9)),
( (
add_text.h.width(rest(1)), add_text.width(rest(1)),
Rect::new(Color::GREEN) Rect::new(Color::GREEN)
.on(CursorSense::click(), move |ctx| { .on(CursorSense::click(), move |ctx| {
ctx.state.run_event::<Submit>(add_text.r, &mut ()); ctx.state.run_event::<Submit>(add_text, &mut ());
}) })
.sized((40, 40)), .sized((40, 40)),
) )
@@ -146,15 +147,16 @@ impl DefaultAppState for Client {
.span(Dir::DOWN) .span(Dir::DOWN)
.add(&mut rsc); .add(&mut rsc);
let main = WidgetPtr::new().handles(&mut rsc); let main = WidgetPtr::new().add(&mut rsc);
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: WidgetRef, label| {
let to = to.upgrade(&mut rsc);
let vec = &mut vals.borrow_mut().1; let vec = &mut vals.borrow_mut().1;
let i = vec.len(); let i = vec.len();
if vec.is_empty() { if vec.is_empty() {
vec.push(None); vec.push(None);
rsc.ui[main.r].set(to); rsc.ui[main].set(to);
} else { } else {
vec.push(Some(to)); vec.push(Some(to));
} }
@@ -163,7 +165,7 @@ impl DefaultAppState for Client {
.on(CursorSense::click(), move |ctx| { .on(CursorSense::click(), move |ctx| {
let (prev, vec) = &mut *vals.borrow_mut(); let (prev, vec) = &mut *vals.borrow_mut();
if let Some(h) = vec[i].take() { if let Some(h) = vec[i].take() {
vec[*prev] = (main.r)(ctx).replace(h); vec[*prev] = main(ctx).replace(h);
*prev = i; *prev = i;
} }
ctx.widget().color = color.darker(0.3); ctx.widget().color = color.darker(0.3);
@@ -181,7 +183,7 @@ impl DefaultAppState for Client {
}; };
let tabs = ( let tabs = (
switch_button(Color::RED, pad_test.h, "pad"), switch_button(Color::RED, pad_test, "pad"),
switch_button(Color::GREEN, span_test, "span"), switch_button(Color::GREEN, span_test, "span"),
switch_button(Color::BLUE, span_add_test, "image span"), switch_button(Color::BLUE, span_add_test, "image span"),
switch_button(Color::MAGENTA, text_test, "text layout"), switch_button(Color::MAGENTA, text_test, "text layout"),
@@ -193,10 +195,10 @@ impl DefaultAppState for Client {
) )
.span(Dir::RIGHT); .span(Dir::RIGHT);
info = wtext("").handles(&mut rsc); let info = wtext("").add(&mut rsc);
let info_sect = info.h.pad(10).align(Align::RIGHT); let info_sect = info.pad(10).align(Align::RIGHT);
((tabs.height(40), main.h.pad(10)).span(Dir::DOWN), info_sect) ((tabs.height(40), main.pad(10)).span(Dir::DOWN), info_sect)
.stack() .stack()
.set_root(&mut rsc); .set_root(&mut rsc);
@@ -204,7 +206,7 @@ impl DefaultAppState for Client {
ui: rsc.ui, ui: rsc.ui,
ui_state: rsc.ui_state, ui_state: rsc.ui_state,
events: rsc.events, events: rsc.events,
info: info.r, info,
} }
} }

View File

@@ -10,15 +10,15 @@ pub mod eventable {
f: impl for<'a> WidgetEventFn<State::State, <E::Event as Event>::Data<'a>, WL::Widget>, f: impl for<'a> WidgetEventFn<State::State, <E::Event as Event>::Data<'a>, WL::Widget>,
) -> impl WidgetIdFn<State, WL::Widget> { ) -> impl WidgetIdFn<State, WL::Widget> {
move |state| { move |state| {
let id = self.handles(state); let id = self.add(state);
state.register_event(id.r, event.into_event(), move |ctx| { state.register_event(id, event.into_event(), move |ctx| {
f(&mut EventIdCtx { f(&mut EventIdCtx {
widget: id.r, widget: id,
state: ctx.state, state: ctx.state,
data: ctx.data, data: ctx.data,
}); });
}); });
id.h id
} }
} }
} }

View File

@@ -151,14 +151,15 @@ pub struct SpanBuilder<State, const LEN: usize, Wa: WidgetArrLike<State, LEN, Ta
_pd: PhantomData<(State, Tag)>, _pd: PhantomData<(State, Tag)>,
} }
impl<State: StateLike<State>, const LEN: usize, Wa: WidgetArrLike<State, LEN, Tag>, Tag> FnOnce<(&mut State,)> impl<State: StateLike<State>, const LEN: usize, Wa: WidgetArrLike<State, LEN, Tag>, Tag>
for SpanBuilder<State, LEN, Wa, Tag> WidgetFnTrait<State> for SpanBuilder<State, LEN, Wa, Tag>
{ {
type Output = Span; type Widget = Span;
extern "rust-call" fn call_once(self, args: (&mut State,)) -> Self::Output { #[track_caller]
fn run(self, state: &mut State) -> Self::Widget {
Span { Span {
children: self.children.add(args.0).arr.into_iter().collect(), children: self.children.add(state).arr.into_iter().collect(),
dir: self.dir, dir: self.dir,
gap: self.gap, gap: self.gap,
} }

View File

@@ -59,7 +59,7 @@ impl<State: HasUi + StateLike<State>, O> TextBuilder<State, O> {
TextBuilder { TextBuilder {
content: self.content, content: self.content,
attrs: self.attrs, attrs: self.attrs,
hint: move |ui: &mut State| Some(hint.add(ui).any()), hint: move |ui: &mut State| Some(hint.add_strong(ui).any()),
output: self.output, output: self.output,
state: PhantomData, state: PhantomData,
} }

View File

@@ -8,13 +8,13 @@ widget_trait! {
fn pad(self, padding: impl Into<Padding>) -> impl WidgetFn<State, Pad> { fn pad(self, padding: impl Into<Padding>) -> impl WidgetFn<State, Pad> {
|state| Pad { |state| Pad {
padding: padding.into(), padding: padding.into(),
inner: self.add(state), inner: self.add_strong(state),
} }
} }
fn align(self, align: impl Into<Align>) -> impl WidgetFn<State, Aligned> { fn align(self, align: impl Into<Align>) -> impl WidgetFn<State, Aligned> {
move |state| Aligned { move |state| Aligned {
inner: self.add(state), inner: self.add_strong(state),
align: align.into(), align: align.into(),
} }
} }
@@ -26,7 +26,7 @@ widget_trait! {
fn label(self, label: impl Into<String>) -> impl WidgetIdFn<State, WL::Widget> { fn label(self, label: impl Into<String>) -> impl WidgetIdFn<State, WL::Widget> {
|state| { |state| {
let id = self.add(state); let id = self.add(state);
state.get_mut().set_label(&id, label.into()); state.get_mut().set_label(id, label.into());
id id
} }
} }
@@ -34,7 +34,7 @@ widget_trait! {
fn sized(self, size: impl Into<Size>) -> impl WidgetFn<State, Sized> { fn sized(self, size: impl Into<Size>) -> impl WidgetFn<State, Sized> {
let size = size.into(); let size = size.into();
move |state| Sized { move |state| Sized {
inner: self.add(state), inner: self.add_strong(state),
x: Some(size.x), x: Some(size.x),
y: Some(size.y), y: Some(size.y),
} }
@@ -43,7 +43,7 @@ widget_trait! {
fn max_width(self, len: impl Into<Len>) -> impl WidgetFn<State, MaxSize> { fn max_width(self, len: impl Into<Len>) -> impl WidgetFn<State, MaxSize> {
let len = len.into(); let len = len.into();
move |state| MaxSize { move |state| MaxSize {
inner: self.add(state), inner: self.add_strong(state),
x: Some(len), x: Some(len),
y: None, y: None,
} }
@@ -52,7 +52,7 @@ widget_trait! {
fn max_height(self, len: impl Into<Len>) -> impl WidgetFn<State, MaxSize> { fn max_height(self, len: impl Into<Len>) -> impl WidgetFn<State, MaxSize> {
let len = len.into(); let len = len.into();
move |state| MaxSize { move |state| MaxSize {
inner: self.add(state), inner: self.add_strong(state),
x: None, x: None,
y: Some(len), y: Some(len),
} }
@@ -61,7 +61,7 @@ widget_trait! {
fn width(self, len: impl Into<Len>) -> impl WidgetFn<State, Sized> { fn width(self, len: impl Into<Len>) -> impl WidgetFn<State, Sized> {
let len = len.into(); let len = len.into();
move |state| Sized { move |state| Sized {
inner: self.add(state), inner: self.add_strong(state),
x: Some(len), x: Some(len),
y: None, y: None,
} }
@@ -70,7 +70,7 @@ widget_trait! {
fn height(self, len: impl Into<Len>) -> impl WidgetFn<State, Sized> { fn height(self, len: impl Into<Len>) -> impl WidgetFn<State, Sized> {
let len = len.into(); let len = len.into();
move |state| Sized { move |state| Sized {
inner: self.add(state), inner: self.add_strong(state),
x: None, x: None,
y: Some(len), y: Some(len),
} }
@@ -78,7 +78,7 @@ widget_trait! {
fn offset(self, amt: impl Into<UiVec2>) -> impl WidgetFn<State, Offset> { fn offset(self, amt: impl Into<UiVec2>) -> impl WidgetFn<State, Offset> {
move |state| Offset { move |state| Offset {
inner: self.add(state), inner: self.add_strong(state),
amt: amt.into(), amt: amt.into(),
} }
} }
@@ -86,7 +86,7 @@ widget_trait! {
fn scrollable(self) -> impl WidgetIdFn<State, Scroll> where State: HasEvents { fn scrollable(self) -> impl WidgetIdFn<State, Scroll> where State: HasEvents {
use eventable::*; use eventable::*;
move |state| { move |state| {
Scroll::new(self.add(state), Axis::Y) Scroll::new(self.add_strong(state), Axis::Y)
.on(CursorSense::Scroll, |ctx: &mut EventIdCtx<'_, State::State, CursorData<'_>, Scroll>| { .on(CursorSense::Scroll, |ctx: &mut EventIdCtx<'_, State::State, CursorData<'_>, Scroll>| {
let delta = ctx.data.scroll_delta.y * 50.0; let delta = ctx.data.scroll_delta.y * 50.0;
ctx.widget().scroll(delta); ctx.widget().scroll(delta);
@@ -97,27 +97,27 @@ widget_trait! {
fn masked(self) -> impl WidgetFn<State, Masked> { fn masked(self) -> impl WidgetFn<State, Masked> {
move |state| Masked { move |state| Masked {
inner: self.add(state), inner: self.add_strong(state),
} }
} }
fn background<T>(self, w: impl WidgetLike<State, T>) -> impl WidgetFn<State, Stack> { fn background<T>(self, w: impl WidgetLike<State, T>) -> impl WidgetFn<State, Stack> {
move |state| Stack { move |state| Stack {
children: vec![w.add(state), self.add(state)], children: vec![w.add_strong(state), self.add_strong(state)],
size: StackSize::Child(1), size: StackSize::Child(1),
} }
} }
fn foreground<T>(self, w: impl WidgetLike<State, T>) -> impl WidgetFn<State, Stack> { fn foreground<T>(self, w: impl WidgetLike<State, T>) -> impl WidgetFn<State, Stack> {
move |state| Stack { move |state| Stack {
children: vec![self.add(state), w.add(state)], children: vec![self.add_strong(state), w.add_strong(state)],
size: StackSize::Child(0), size: StackSize::Child(0),
} }
} }
fn layer_offset(self, offset: usize) -> impl WidgetFn<State, LayerOffset> { fn layer_offset(self, offset: usize) -> impl WidgetFn<State, LayerOffset> {
move |state| LayerOffset { move |state| LayerOffset {
inner: self.add(state), inner: self.add_strong(state),
offset, offset,
} }
} }
@@ -127,7 +127,7 @@ widget_trait! {
} }
fn set_ptr(self, ptr: WidgetRef<WidgetPtr>, state: &mut State) { fn set_ptr(self, ptr: WidgetRef<WidgetPtr>, state: &mut State) {
let id = self.add(state); let id = self.add_strong(state);
state.get_mut()[ptr].inner = Some(id); state.get_mut()[ptr].inner = Some(id);
} }
} }