clean up buttons
This commit is contained in:
@@ -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> {
|
||||
// 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,
|
||||
sense: Sense,
|
||||
f: F,
|
||||
|
||||
@@ -35,51 +35,68 @@ impl Client {
|
||||
thickness: 0.0,
|
||||
inner_radius: 0.0,
|
||||
};
|
||||
ui.set_base(
|
||||
let pad_test = ui.add(
|
||||
(
|
||||
rect.color(UiColor::BLUE),
|
||||
(
|
||||
rect.color(UiColor::BLUE)
|
||||
.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::RED).center((100.0, 100.0)),
|
||||
(
|
||||
rect.color(UiColor::ORANGE),
|
||||
rect.color(UiColor::LIME).pad(10.0),
|
||||
)
|
||||
.span(Dir::RIGHT, [1, 1]),
|
||||
rect.color(UiColor::YELLOW),
|
||||
rect.color(UiColor::ORANGE),
|
||||
rect.color(UiColor::LIME).pad(10.0),
|
||||
)
|
||||
.span(Dir::RIGHT, [2, 2, 1])
|
||||
.pad(10),
|
||||
.span(Dir::RIGHT, [1, 1]),
|
||||
rect.color(UiColor::YELLOW),
|
||||
)
|
||||
.span(Dir::RIGHT, [1, 3]),
|
||||
Span::empty(Dir::RIGHT).id(&test),
|
||||
(
|
||||
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::RIGHT, [2, 2, 1])
|
||||
.pad(10),
|
||||
)
|
||||
.span(Dir::DOWN, [3, 1, 1])
|
||||
.pad(10),
|
||||
.span(Dir::RIGHT, [1, 3]),
|
||||
);
|
||||
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 })
|
||||
}
|
||||
|
||||
@@ -103,6 +120,13 @@ impl Client {
|
||||
self.renderer.draw()
|
||||
}
|
||||
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() {
|
||||
|
||||
Reference in New Issue
Block a user