static ids

This commit is contained in:
2025-08-25 16:12:49 -04:00
parent 41103f2732
commit 7b21b0714d
4 changed files with 172 additions and 33 deletions

View File

@@ -29,14 +29,13 @@ pub struct ClientUi {
impl Client {
pub fn create_ui() -> (Ui<Self>, ClientUi) {
let mut ui = Ui::new();
let span_add = ui.id();
let rect = Rect {
color: UiColor::WHITE,
radius: 20.0,
thickness: 0.0,
inner_radius: 0.0,
};
let pad_test = ui.add(
let pad_test = ui.add_static(
(
rect.color(UiColor::BLUE),
(
@@ -53,7 +52,7 @@ impl Client {
)
.span(Dir::RIGHT, [1, 3]),
);
let span_test = ui.add(
let span_test = ui.add_static(
(
rect.color(UiColor::GREEN),
rect.color(UiColor::ORANGE),
@@ -74,20 +73,13 @@ impl Client {
],
),
);
let span_add_test = ui.add(Span::empty(Dir::RIGHT).id(&span_add));
let main: WidgetId<Regioned> = ui.id();
let span_add = ui.add_static(Span::empty(Dir::RIGHT));
let main: StaticWidgetId<Regioned> = ui.id_static();
fn switch_button<To>(
color: UiColor,
main: &WidgetId<Regioned>,
to: &WidgetId<To>,
label: impl Into<String>,
) -> impl WidgetLike<Client, FnTag> {
let main = main.clone();
let to = to.clone().erase_type();
let switch_button = |color, to, label| {
let rect = Rect::new(color)
.id_on(Sense::PressStart, move |id, ctx| {
ctx.ui[&main].inner = to.clone();
ctx.ui[main].inner.set_static(to);
ctx.ui[id].color = color.add_rgb(-0.2);
})
.edit_on(Sense::HoverStart, move |r, _| {
@@ -100,17 +92,16 @@ impl Client {
r.color = color;
});
(rect, text(label).size(30)).stack()
}
};
let tabs = ui.add(
(
switch_button(UiColor::RED, &main, &pad_test, "pad test"),
switch_button(UiColor::GREEN, &main, &span_test, "span test"),
switch_button(UiColor::BLUE, &main, &span_add_test, "span add test"),
switch_button(UiColor::RED, pad_test, "pad test"),
switch_button(UiColor::GREEN, span_test, "span test"),
switch_button(UiColor::BLUE, span_add, "span add test"),
)
.span(Dir::RIGHT, [1, 1, 1]),
);
let s = span_add.clone();
let add_button = Rect::new(Color::LIME)
.radius(30)
.on(Sense::PressStart, move |ctx| {
@@ -118,7 +109,7 @@ impl Client {
.ui
.add(image(include_bytes!("assets/sungals.png")))
.erase_type();
ctx.ui[&s].children.push((child, ratio(1)));
ctx.ui[span_add].children.push((child, ratio(1)));
})
.region(
UiPos::corner(Corner::BotRight)
@@ -126,11 +117,10 @@ impl Client {
.shifted((-75, -75)),
);
let s = span_add.clone();
let del_button = Rect::new(Color::RED)
.radius(30)
.on(Sense::PressStart, move |ctx| {
ctx.ui[&s].children.pop();
ctx.ui[span_add].children.pop();
})
.region(
UiPos::corner(Corner::BotLeft)
@@ -148,7 +138,7 @@ impl Client {
(
tabs.label("tabs"),
(
pad_test.pad(10).id(&main),
pad_test.pad(10).id_static(main),
add_button.label("add button"),
del_button.label("del button"),
info_sect.label("info sect"),