wgui: components refactoring, id handles

This commit is contained in:
Aleksander
2025-08-09 13:23:49 +02:00
parent 17b4eaa1e4
commit 91e584383f
13 changed files with 243 additions and 79 deletions

View File

@@ -10,7 +10,6 @@
<div flex_direction="column" padding="16">
<label weight="bold" text="Try these environment variables:" />
<label text="TESTBED=bar" />
<label text="TESTBED=various_widgets" />
<label text="TESTBED=watch" />
<label text="TESTBED=dashboard" />
</div>

View File

@@ -8,9 +8,12 @@
<label text="Raw text" color="#FFFFFF" />
<label translation="TESTBED.HELLO_WORLD" color="#FFFFFF" />
<button text="Red button" width="128" height="32" color="#FF0000" />
<button text="Aqua button" width="128" height="32" color="#00FFFF" />
<button text="Yellow button" width="128" height="32" color="#FFFF00" />
<label id="label_current_option" />
<button margin_left="16" id="button_red" text="Red button" width="128" height="32" color="#FF0000" />
<button margin_left="16" id="button_aqua" text="Aqua button" width="128" height="32" color="#00FFFF" />
<button margin_left="16" id="button_yellow" text="Yellow button" width="128" height="32" color="#FFFF00" />
<div flex_direction="row" gap="16">
<div flex_direction="column" gap="8">

View File

@@ -1,7 +1,8 @@
use crate::{assets, testbed::Testbed};
use glam::Vec2;
use wgui::{
event::EventListenerCollection, globals::WguiGlobals, layout::Layout, parser::ParserState,
components::button::ComponentButton, event::EventListenerCollection, globals::WguiGlobals,
i18n::Translation, layout::Layout, parser::ParserState,
};
pub struct TestbedGeneric {
@@ -13,13 +14,23 @@ pub struct TestbedGeneric {
impl TestbedGeneric {
pub fn new(listeners: &mut EventListenerCollection<(), ()>) -> anyhow::Result<Self> {
const XML_PATH: &str = "gui/testbed.xml";
const XML_PATH: &str = "gui/various_widgets.xml";
let globals = WguiGlobals::new(Box::new(assets::Asset {}))?;
let (layout, state) =
let (mut layout, state) =
wgui::parser::new_layout_from_assets(globals, listeners, XML_PATH, false)?;
let label_current_option = state.fetch_widget("label_current_option")?;
let b1 = state.fetch_component_as::<ComponentButton>("button_red")?;
let b2 = state.fetch_component_as::<ComponentButton>("button_aqua")?;
let b3 = state.fetch_component_as::<ComponentButton>("button_yellow")?;
b1.set_text(
&mut layout.state,
Translation::from_raw_text("hello, world!"),
);
Ok(Self { layout, state })
}
}