wgui: checkbox component

This commit is contained in:
Aleksander
2025-08-13 19:42:48 +02:00
parent faec8866c5
commit df35dba24f
8 changed files with 503 additions and 35 deletions

View File

@@ -10,14 +10,20 @@
<div margin_left="16" gap="8" flex_direction="column">
<label id="label_current_option" text="Click any of these buttons" size="20" weight="bold" />
<div>
<div gap="4">
<button id="button_red" text="Red button" width="220" height="32" color="#FF0000" />
<button id="button_aqua" text="Aqua button" width="220" height="32" color="#00FFFF" />
<button id="button_yellow" text="Yellow button" width="220" height="32" color="#FFFF00" />
</div>
<button id="button_click_me" text="Click me" width="128" height="24" color="#FFFFFF" />
</div>
<div gap="8" align_items="center">
<check_box id="cb_first" text="I'm a checkbox!" />
<check_box text="and me too!" />
<check_box text="i'm tall" box_size="32" />
<check_box text="i'm checked by default" checked="1" />
</div>
</div>
<div flex_direction="row" gap="16">
<div flex_direction="column" gap="8">

View File

@@ -6,6 +6,7 @@ use wgui::{
components::{
Component,
button::{ButtonClickCallback, ComponentButton},
checkbox::ComponentCheckbox,
},
event::EventListenerCollection,
globals::WguiGlobals,
@@ -60,8 +61,7 @@ impl TestbedGeneric {
let (layout, state) =
wgui::parser::new_layout_from_assets(globals, listeners, XML_PATH, false)?;
let label_cur_option =
state.fetch_widget::<WidgetLabel>(&layout.state, "label_current_option")?;
let label_cur_option = state.fetch_widget(&layout.state, "label_current_option")?;
let button_click_me = state.fetch_component_as::<ComponentButton>("button_click_me")?;
let button = button_click_me.clone();
@@ -82,6 +82,17 @@ impl TestbedGeneric {
handle_button_click(button_aqua, label_cur_option.clone(), "Clicked aqua");
handle_button_click(button_yellow, label_cur_option.clone(), "Clicked yellow");
let cb_first = state.fetch_component_as::<ComponentCheckbox>("cb_first")?;
let label = label_cur_option.clone();
cb_first.on_toggle(Box::new(move |e| {
let mut widget = label.get_as_mut::<WidgetLabel>();
widget.set_text(
&mut e.state.globals.i18n(),
Translation::from_raw_text(&format!("checkbox toggle: {}", e.checked)),
);
Ok(())
}));
Ok(Self { layout, state })
}
}