From 9f1802f4979a2eaf21b70450727a2d03b9336119 Mon Sep 17 00:00:00 2001 From: Shadow Cat Date: Fri, 15 Aug 2025 16:22:28 -0400 Subject: [PATCH] clean up buttons --- src/core/sense.rs | 2 +- src/testing/mod.rs | 100 ++++++++++++++++++++++++++++----------------- 2 files changed, 63 insertions(+), 39 deletions(-) diff --git a/src/core/sense.rs b/src/core/sense.rs index d1789f9..09379da 100644 --- a/src/core/sense.rs +++ b/src/core/sense.rs @@ -29,7 +29,7 @@ pub trait WidgetSenseFn, Ctx> = Fn(&WidgetId, &mut Ui, &m pub trait Sensable, Ctx: 'static, Tag> { // copied here so LSP can at least get the UI and id - fn sense, &mut Ui, &mut Ctx) + 'static + Clone>( + fn sense + Clone>( self, sense: Sense, f: F, diff --git a/src/testing/mod.rs b/src/testing/mod.rs index 759d832..62fcb1c 100644 --- a/src/testing/mod.rs +++ b/src/testing/mod.rs @@ -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 = ui.id(); + + fn switch_button( + color: UiColor, + main: &WidgetId, + to: &WidgetId, + ) -> impl WidgetLike { + let main = main.clone(); + let to = to.clone().erase_type(); + Rect::new(color).sense(Sense::Click, move |_, ui: &mut Ui, _| { + 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() {