use gooey::value::Dynamic;
use gooey::widget::{MakeWidget, MakeWidgetWithTag, Widget, WidgetInstance, WidgetTag, HANDLED};
use gooey::widgets::Custom;
use gooey::Run;
use kludgine::figures::units::{Lp, UPx};
use kludgine::figures::{ScreenScale, Size};
use kludgine::Color;
fn main() -> gooey::Result {
"Inline Widgets"
.and(callback_widget())
.into_rows()
.and(
"impl MakeWidget"
.and(ToggleMakeWidget::default())
.into_rows(),
)
.and("impl Widget".and(impl_widget()).into_rows())
.into_columns()
.centered()
.run()
}
fn callback_widget() -> impl MakeWidgetWithTag {
let toggle = Toggle::default();
Custom::empty()
.background_color(toggle.color)
.on_hit_test(|_, _| true)
.on_mouse_down(move |_, _, _, _| {
toggle.value.toggle();
HANDLED
})
.height(Lp::inches(1))
}
#[derive(Default)]
struct ToggleMakeWidget(Toggle);
impl MakeWidgetWithTag for ToggleMakeWidget {
fn make_with_tag(self, id: WidgetTag) -> WidgetInstance {
callback_widget().make_with_tag(id)
}
}
fn impl_widget() -> impl MakeWidgetWithTag {
Toggle::default()
}
#[derive(Debug)]
struct Toggle {
value: Dynamic<bool>,
color: Dynamic<Color>,
}
impl Default for Toggle {
fn default() -> Self {
let value = Dynamic::default();
let color = value.map_each(|on| if *on { Color::RED } else { Color::BLUE });
Self { value, color }
}
}
impl Widget for Toggle {
fn redraw(&mut self, context: &mut gooey::context::GraphicsContext<'_, '_, '_, '_, '_>) {
context.fill(self.color.get_tracking_redraw(context));
}
fn layout(
&mut self,
available_space: Size<gooey::ConstraintLimit>,
context: &mut gooey::context::LayoutContext<'_, '_, '_, '_, '_>,
) -> Size<UPx> {
Size::new(
available_space.width.min(),
Lp::inches(1).into_upx(context.gfx.scale()),
)
}
fn hit_test(
&mut self,
_location: kludgine::figures::Point<kludgine::figures::units::Px>,
_context: &mut gooey::context::EventContext<'_, '_>,
) -> bool {
true
}
fn mouse_down(
&mut self,
_location: kludgine::figures::Point<kludgine::figures::units::Px>,
_device_id: kludgine::app::winit::event::DeviceId,
_button: kludgine::app::winit::event::MouseButton,
_context: &mut gooey::context::EventContext<'_, '_>,
) -> gooey::widget::EventHandling {
self.value.toggle();
HANDLED
}
}