wgui: Cherry-pick changes from `feat-monado-metrics` branch

This commit is contained in:
Aleksander 2026-03-27 22:11:23 +01:00
parent 00bc179655
commit 041754b02e
9 changed files with 51 additions and 25 deletions

View File

@ -1,16 +1,16 @@
<layout>
<include src="../t_group_box.xml" />
<include src_builtin="../t_group_box.xml" />
<!-- device_name, device_icon -->
<template name="DeviceSlider">
<rectangle macro="group_box">
<div width="100%" align_items="center" justify_content="center" gap="8">
<sprite src="${device_icon}" width="16" height="16" />
<sprite src_builtin="${device_icon}" width="16" height="16" />
<label text="${device_name}" margin_right="8" size="12" weight="bold" />
</div>
<div width="100%" align_items="center">
<CheckBox id="checkbox" />
<Button sprite_src="${volume_icon}" id="btn_mute" width="32" />
<Button sprite_src_builtin="${volume_icon}" id="btn_mute" width="32" />
<Slider id="slider" flex_grow="1" height="16" min_value="0" max_value="150" margin_left="8" />
</div>
</rectangle>
@ -41,7 +41,7 @@
flex_grow="1"
id="${id}"
translation="${translation}"
sprite_src="${src}">
sprite_src_builtin="${src}">
</Button>
</template>
@ -67,4 +67,4 @@
</div>
</div>
</elements>
</layout>
</layout>

View File

@ -286,7 +286,7 @@ impl AppList {
let mut params = HashMap::<Rc<str>, Rc<str>>::new();
params.insert("text".into(), category_name.into());
parser_state.parse_template(
parser_state.realize_template(
doc_params,
"CategoryText",
&mut frontend.layout,
@ -318,7 +318,7 @@ impl AppList {
);
params.insert("name".into(), entry.app_name.clone());
let data = parser_state.parse_template(
let data = parser_state.realize_template(
doc_params,
"AppEntry",
&mut frontend.layout,

View File

@ -117,7 +117,7 @@ impl<T> TabMonado<T> {
par.insert("flag_primary".into(), yesno(client.is_primary).into());
par.insert("flag_visible".into(), yesno(client.is_visible).into());
let state_cell = self.state.parse_template(
let state_cell = self.state.realize_template(
&doc_params(&self.globals),
"Cell",
&mut frontend.layout,

View File

@ -750,7 +750,7 @@ impl View {
let data = self
.state
.parse_template(&doc_params(&self.globals), "Card", params.layout, self.id_devices, par)?;
.realize_template(&doc_params(&self.globals), "Card", params.layout, self.id_devices, par)?;
let btn_card = data.fetch_component_as::<ComponentButton>("btn_card")?;
btn_card.on_click({
@ -764,7 +764,6 @@ impl View {
})
});
log::info!("mount card TODO: {}", params.card.name);
Ok(())
}
@ -794,7 +793,7 @@ impl View {
},
);
let data = self.state.parse_template(
let data = self.state.realize_template(
&doc_params(&self.globals),
"DeviceSlider",
params.layout,
@ -941,7 +940,7 @@ impl View {
layout.remove_children(self.id_devices);
{
let data = self.state.parse_template(
let data = self.state.realize_template(
&doc_params(&self.globals),
"SelectAudioProfileText",
layout,

View File

@ -127,7 +127,7 @@ impl View {
for game in games {
let game_name = View::extract_name_from_appid(&game.app_id, &self.installed_games);
let t = self.state.parse_template(
let t = self.state.realize_template(
&doc_params(layout.state.globals.clone()),
"RunningGameCell",
layout,

View File

@ -83,9 +83,7 @@ impl PlayspaceMover {
let offset = (space_transform.transform_vector3a(app.input_state.hmd.translation)
- app.input_state.hmd.translation)
* -1.0;
let mut overlay_transform = Affine3A::from_rotation_y(-rel_y);
overlay_transform.translation = offset;
space_transform.translation = offset;
data.pose *= space_transform;

View File

@ -377,7 +377,7 @@ impl Layout {
self.registered_components_to_refresh.insert(*node_id, component.weak());
}
/// Convenience function to avoid repeated `WidgetID` → `WidgetState` lookups.
/// Convenience function to avoid repeated `WidgetID` → `WidgetState` look-ups.
pub fn add_event_listener<U1: 'static, U2: 'static>(
&self,
widget_id: WidgetID,

View File

@ -201,7 +201,6 @@ impl Fetchable for ParserData {
let casted = widget
.get_as::<T>()
.ok_or_else(|| anyhow::anyhow!("fetch_widget_as({id}): failed to cast"))?;
Ok(casted)
}
}
@ -217,16 +216,40 @@ pub struct ParserState {
}
impl ParserState {
/// This function is suitable in cases if you don't want to pollute main parser state with dynamic IDs
/// Use `instantiate_template` instead unless you want to handle `components` results yourself.
/// Make sure not to drop them if you want to have your listener handles valid
pub fn parse_template(
/// Parse named <template> tag and process it.
/// Preferred method of parsing templates. Same as `parse_template_only`,
/// but it keeps components data in this `ParserState` object for you.
/// The result can be safely dropped, all required event listeners and components
/// will be kept intact in this `ParserState`.
/// Resulting ParserData::components Vec will be left empty (they are moved into this `ParserState::data`)
pub fn realize_template(
&mut self,
doc_params: &ParseDocumentParams,
template_name: &str,
layout: &mut Layout,
widget_id: WidgetID,
template_parameters: HashMap<Rc<str>, Rc<str>>,
) -> anyhow::Result<ParserData> {
let mut parser_data =
self.parse_template_only(doc_params, template_name, layout, widget_id, template_parameters)?;
// Collect components contained in this freshly-parsed template
self.data.components.append(&mut parser_data.components);
Ok(parser_data)
}
/// Parse named <template> tag and process it.
/// Semi-internal - This function is suitable in cases if you don't want to pollute
/// the main parser state state with dynamic IDs (this won't propagate components!)
/// Use `realize_template` (or in some rare cases: `instantiate_template`) instead unless you want to handle `components` results yourself.
/// Make sure not to drop resulting ParserData if you want to have your listener handles valid
/// (they are contained in components). Use `realize_template` instead if you don't want to think about it.
pub fn parse_template_only(
&self,
doc_params: &ParseDocumentParams,
template_name: &str,
layout: &mut Layout,
widget_id: WidgetID,
template_parameters: HashMap<Rc<str>, Rc<str>>,
) -> anyhow::Result<ParserData> {
let Some(template) = self.data.templates.get(template_name) else {
anyhow::bail!(
@ -252,7 +275,13 @@ impl ParserState {
Ok(ctx.data_local)
}
/// Instantinate template by saving all the results into the main `ParserState`
/// Parse named <template> tag and process it.
/// Instantiate template by saving all the results into the main `ParserState`.
/// Be aware you this function will save ALL parsed IDs and other metadata
/// into your main ParserState context (deep move).
/// You shouldn't instantiate the same template twice, to prevent ID name clash.
/// Consider using `parse_template_only` or `realize_template` instead if you want
/// to instantiate more than a single template of the same type.
pub fn instantiate_template(
&mut self,
doc_params: &ParseDocumentParams,
@ -261,7 +290,7 @@ impl ParserState {
widget_id: WidgetID,
template_parameters: HashMap<Rc<str>, Rc<str>>,
) -> anyhow::Result<()> {
let mut data_local = self.parse_template(doc_params, template_name, layout, widget_id, template_parameters)?;
let mut data_local = self.parse_template_only(doc_params, template_name, layout, widget_id, template_parameters)?;
self.data.take_results_from(&mut data_local);
Ok(())

View File

@ -117,7 +117,7 @@ impl ContextMenu {
par.insert(Rc::from("tooltip_str"), tooltip.generate(&mut globals.i18n()));
}
let mut data_cell = inner_parser.parse_template(&doc_params, "Cell", layout, id_buttons, par)?;
let mut data_cell = inner_parser.realize_template(&doc_params, "Cell", layout, id_buttons, par)?;
let button = data_cell.fetch_component_as::<ComponentButton>("button")?;
let button_id = button.base().get_id();
@ -136,7 +136,7 @@ impl ContextMenu {
}
if idx < cells.len() - 1 {
inner_parser.parse_template(&doc_params, "Separator", layout, id_buttons, Default::default())?;
inner_parser.realize_template(&doc_params, "Separator", layout, id_buttons, Default::default())?;
}
}
Ok(())