switch to Rc<RefCell<...>> for widget storage

This commit is contained in:
2025-12-07 14:36:38 -05:00
parent 38266debb6
commit c99d466b75
27 changed files with 685 additions and 784 deletions

View File

@@ -11,7 +11,7 @@ fn main() {
}
pub struct Client {
info: WidgetId<Text>,
info: WidgetRef<Text>,
}
event_ctx!(Client);
@@ -63,7 +63,7 @@ impl DefaultAppState for Client {
.ui
.add(image(include_bytes!("assets/sungals.png")).center())
.any();
ctx.ui[&span_add_].children.push(child);
span_add_.get_mut().children.push(child);
})
.sized((150, 150))
.align(Align::BOT_RIGHT);
@@ -71,8 +71,8 @@ impl DefaultAppState for Client {
let span_add_ = span_add.clone();
let del_button = rect(Color::RED)
.radius(30)
.on(CursorSense::click(), move |ctx| {
ctx.ui[&span_add_].children.pop();
.on(CursorSense::click(), move |_| {
span_add_.get_mut().children.pop();
})
.sized((150, 150))
.align(Align::BOT_LEFT);
@@ -110,7 +110,7 @@ impl DefaultAppState for Client {
.size(30)
.attr::<Selectable>(())
.on(Submit, move |ctx| {
let content = ctx.ui.text(ctx.id).take();
let content = ctx.widget.get_mut().take();
let text = wtext(content)
.editable(false)
.size(30)
@@ -118,7 +118,7 @@ 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].children.push(msg_box.any());
texts.get_mut().children.push(msg_box.any());
})
.add(ui);
let text_edit_scroll = (
@@ -146,21 +146,21 @@ impl DefaultAppState for Client {
let main = pad_test.clone().pad(10).add(ui);
let switch_button = |color, to: WidgetId, label| {
let switch_button = |color, to: WidgetRef, label| {
let main_ = main.clone();
let rect = rect(color)
.on(CursorSense::click(), move |ctx| {
ctx.ui[&main_.clone()].inner = to.clone();
ctx.ui[ctx.id].color = color.darker(0.3);
main_.get_mut().inner = to.clone();
ctx.widget.get_mut().color = color.darker(0.3);
})
.on(
CursorSense::HoverStart | CursorSense::unclick(),
move |ctx| {
ctx.ui[ctx.id].color = color.brighter(0.2);
ctx.widget.get_mut().color = color.brighter(0.2);
},
)
.on(CursorSense::HoverEnd, move |ctx| {
ctx.ui[ctx.id].color = color;
ctx.widget.get_mut().color = color;
});
(rect, wtext(label).size(30).text_align(Align::CENTER)).stack()
};
@@ -195,11 +195,8 @@ impl DefaultAppState for Client {
ui.active_widgets(),
state.renderer.ui.view_count()
);
if new != *ui[&self.info].content {
*ui[&self.info].content = new;
}
if ui.needs_redraw() {
state.window.request_redraw();
if new != *self.info.get().content {
*self.info.get_mut().content = new;
}
}
}