fix reactivity 😭 + visual widget counter

This commit is contained in:
2025-08-24 22:02:50 -04:00
parent 6bb6db32a6
commit 74d01d14d4
9 changed files with 119 additions and 47 deletions

View File

@@ -18,15 +18,16 @@ pub fn main() {
pub struct Client {
renderer: Renderer,
input: Input,
ui: UiIds,
ui: ClientUi,
}
pub struct UiIds {
span_add: WidgetId<Span>,
pub struct ClientUi {
info: WidgetId<Text>,
old_num: usize,
}
impl Client {
pub fn create_ui() -> (Ui<Self>, UiIds) {
pub fn create_ui() -> (Ui<Self>, ClientUi) {
let mut ui = Ui::new();
let span_add = ui.id();
let rect = Rect {
@@ -109,10 +110,15 @@ impl Client {
)
.span(Dir::RIGHT, [1, 1, 1]),
);
let test_button = Rect::new(Color::PURPLE)
let s = span_add.clone();
let add_button = Rect::new(Color::LIME)
.radius(30)
.on(Sense::PressStart, move |ctx| {
println!("{}", ctx.ui.num_widgets());
let child = ctx
.ui
.add(image(include_bytes!("assets/sungals.png")))
.erase_type();
ctx.ui[&s].children.push((child, ratio(1)));
})
.region(
UiPos::corner(Corner::BotRight)
@@ -131,22 +137,35 @@ impl Client {
.expand((150, 150))
.shifted((75, -75)),
);
let info = ui.add(text(""));
let info_sect = info.clone().region(
UiPos::corner(Corner::TopRight)
.expand((150, 150))
.shifted((-75, 0)),
);
ui.set_base(
(
tabs,
(pad_test.pad(10).id(&main), test_button, del_button).stack(),
tabs.label("tabs"),
(
pad_test.pad(10).id(&main),
add_button.label("add button"),
del_button.label("del button"),
info_sect.label("info sect"),
)
.stack().label("main stack"),
)
.span(Dir::DOWN, [fixed(40), ratio(1)]),
.span(Dir::DOWN, [fixed(40), ratio(1)]).label("root"),
);
(ui, UiIds { span_add })
(ui, ClientUi { info, old_num: 0 })
}
pub fn new(window: Arc<Window>, ui: UiIds) -> Self {
pub fn new(window: Arc<Window>, ui: ClientUi) -> Self {
let renderer = Renderer::new(window);
Self {
renderer,
ui,
input: Input::default(),
ui,
}
}
@@ -166,17 +185,13 @@ impl Client {
ui.resize((size.width, size.height));
self.renderer.resize(&size)
}
WindowEvent::KeyboardInput { event, .. } => {
if event.state.is_pressed() {
let child = ui
.add(image(include_bytes!("assets/sungals.png")))
.erase_type();
ui[&self.ui.span_add].children.push((child, ratio(1)));
self.renderer.window().request_redraw();
}
}
_ => (),
}
let num = ui.num_widgets();
if num != self.ui.old_num {
ui[&self.ui.info].content = format!("widgets: {}", num);
self.ui.old_num = num;
}
if ui.needs_redraw() {
self.renderer.window().request_redraw();
}