clean up buttons

This commit is contained in:
2025-08-15 16:22:28 -04:00
parent 78ea738b8e
commit 9f1802f497
2 changed files with 63 additions and 39 deletions

View File

@@ -29,7 +29,7 @@ pub trait WidgetSenseFn<W: Widget<Ctx>, Ctx> = Fn(&WidgetId<W>, &mut Ui<Ctx>, &m
pub trait Sensable<W: Widget<Ctx>, Ctx: 'static, Tag> { pub trait Sensable<W: Widget<Ctx>, Ctx: 'static, Tag> {
// copied here so LSP can at least get the UI and id // copied here so LSP can at least get the UI and id
fn sense<F: Fn(&WidgetId<W>, &mut Ui<Ctx>, &mut Ctx) + 'static + Clone>( fn sense<F: WidgetSenseFn<W, Ctx> + Clone>(
self, self,
sense: Sense, sense: Sense,
f: F, f: F,

View File

@@ -35,51 +35,68 @@ impl Client {
thickness: 0.0, thickness: 0.0,
inner_radius: 0.0, inner_radius: 0.0,
}; };
ui.set_base( let pad_test = ui.add(
( (
rect.color(UiColor::BLUE),
( (
rect.color(UiColor::BLUE) rect.color(UiColor::RED).center((100.0, 100.0)),
.sense(Sense::Click, |id, ui, client| {
println!("hello!");
ui[id].color.a -= 1;
}),
( (
rect.color(UiColor::RED).center((100.0, 100.0)), rect.color(UiColor::ORANGE),
( rect.color(UiColor::LIME).pad(10.0),
rect.color(UiColor::ORANGE),
rect.color(UiColor::LIME).pad(10.0),
)
.span(Dir::RIGHT, [1, 1]),
rect.color(UiColor::YELLOW),
) )
.span(Dir::RIGHT, [2, 2, 1]) .span(Dir::RIGHT, [1, 1]),
.pad(10), rect.color(UiColor::YELLOW),
) )
.span(Dir::RIGHT, [1, 3]), .span(Dir::RIGHT, [2, 2, 1])
Span::empty(Dir::RIGHT).id(&test), .pad(10),
(
rect.color(UiColor::GREEN),
rect.color(UiColor::ORANGE),
rect.color(UiColor::CYAN),
rect.color(UiColor::BLUE),
rect.color(UiColor::MAGENTA),
rect.color(UiColor::RED),
)
.span(
Dir::LEFT,
[
fixed(100),
ratio(1),
ratio(1),
rel(0.5),
fixed(100),
fixed(100),
],
),
) )
.span(Dir::DOWN, [3, 1, 1]) .span(Dir::RIGHT, [1, 3]),
.pad(10),
); );
let span_test = ui.add(
(
rect.color(UiColor::GREEN),
rect.color(UiColor::ORANGE),
rect.color(UiColor::CYAN),
rect.color(UiColor::BLUE),
rect.color(UiColor::MAGENTA),
rect.color(UiColor::RED),
)
.span(
Dir::LEFT,
[
fixed(100),
ratio(1),
ratio(1),
rel(0.5),
fixed(100),
fixed(100),
],
),
);
let span_add_test = ui.add(Span::empty(Dir::RIGHT).id(&test));
let main: WidgetId<Regioned> = ui.id();
fn switch_button<To>(
color: UiColor,
main: &WidgetId<Regioned>,
to: &WidgetId<To>,
) -> impl WidgetLike<Client, FnTag> {
let main = main.clone();
let to = to.clone().erase_type();
Rect::new(color).sense(Sense::Click, move |_, ui: &mut Ui<Client>, _| {
ui[&main].inner = to.clone();
})
}
let buttons = ui.add(
(
switch_button(UiColor::RED, &main, &pad_test),
switch_button(UiColor::GREEN, &main, &span_test),
switch_button(UiColor::BLUE, &main, &span_add_test),
)
.span(Dir::RIGHT, [1, 1, 1]),
);
ui.set_base((buttons, pad_test.pad(10).id(&main)).span(Dir::DOWN, [fixed(40), ratio(1)]));
(ui, UiIds { test }) (ui, UiIds { test })
} }
@@ -103,6 +120,13 @@ impl Client {
self.renderer.draw() self.renderer.draw()
} }
WindowEvent::Resized(size) => self.renderer.resize(&size), WindowEvent::Resized(size) => self.renderer.resize(&size),
WindowEvent::KeyboardInput { event, .. } => {
if event.state.is_pressed() {
let child = ui.add(Rect::new(Color::YELLOW)).erase_type();
ui[&self.ui.test].children.push((child, fixed(20.0)));
self.renderer.window().request_redraw();
}
}
_ => (), _ => (),
} }
if ui.needs_redraw() { if ui.needs_redraw() {