rework toast to use xml

This commit is contained in:
galister
2025-12-10 20:49:39 +09:00
parent 6294ccae1c
commit 7c41a01122
3 changed files with 52 additions and 86 deletions

View File

@@ -0,0 +1,16 @@
<layout>
<include src="theme.xml" />
<elements>
<div interactable="0" flex_direction="column">
<rectangle height="100%" padding="4" box_sizing="content_box" flex_wrap="wrap" flex_direction="row" gap="4" color="#000000c0" border_color="~color_accent" border="2" round="8" justify_content="space_between">
<div flex_direction="row" padding="4" align_items="center" justify_content="center">
<label text="" id="toast_title" weight="bold" />
</div>
<div flex_direction="row" flex_wrap="wrap" padding="4" align_items="center" justify_content="center" id="toolbox">
<label text="" id="toast_body" />
</div>
</rectangle>
</div>
</elements>
</layout>

View File

@@ -18,7 +18,7 @@ use crate::{
}, },
config::load_config_with_conf_d, config::load_config_with_conf_d,
config_io, config_io,
overlays::wayvr::{WayVRData, executable_exists_in_path}, overlays::wayvr::{executable_exists_in_path, WayVRData},
}; };
// Flat version of RelativeTo // Flat version of RelativeTo

View File

@@ -5,21 +5,9 @@ use std::{
time::Instant, time::Instant,
}; };
use anyhow::Context;
use glam::{vec3, Affine3A, Quat, Vec3}; use glam::{vec3, Affine3A, Quat, Vec3};
use wgui::{ use wgui::{i18n::Translation, widget::label::WidgetLabel};
i18n::Translation,
parser::parse_color_hex,
renderer_vk::text::{FontWeight, TextStyle},
taffy::{
self,
prelude::{auto, length, percent},
},
widget::{
label::{WidgetLabel, WidgetLabelParams},
rectangle::{WidgetRectangle, WidgetRectangleParams},
util::WLength,
},
};
use wlx_common::{ use wlx_common::{
common::LeftRight, common::LeftRight,
overlays::{ToastDisplayMethod, ToastTopic}, overlays::{ToastDisplayMethod, ToastTopic},
@@ -28,7 +16,7 @@ use wlx_common::{
use crate::{ use crate::{
backend::task::{OverlayTask, TaskType}, backend::task::{OverlayTask, TaskType},
gui::panel::GuiPanel, gui::panel::{GuiPanel, NewGuiPanelParams, OnCustomIdFunc},
state::AppState, state::AppState,
windowing::{window::OverlayWindowConfig, OverlaySelector, Z_ORDER_TOAST}, windowing::{window::OverlayWindowConfig, OverlaySelector, Z_ORDER_TOAST},
}; };
@@ -152,83 +140,45 @@ fn new_toast(toast: Toast, app: &mut AppState) -> Option<OverlayWindowConfig> {
}; };
let title = if toast.title.is_empty() { let title = if toast.title.is_empty() {
"Notification".into() Translation::from_translation_key("TOAST.DEFAULT_TITLE")
} else { } else {
toast.title Translation::from_raw_text(&toast.title)
}; };
let mut panel = GuiPanel::new_blank(app, (), Default::default()).ok()?; let on_custom_id: OnCustomIdFunc<()> =
Box::new(move |id, widget, _doc_params, layout, _parser_state, ()| {
if &*id == "toast_title" {
let mut label = layout
.state
.widgets
.get_as::<WidgetLabel>(widget)
.context("toast.xml: missing element with id: toast_title")?;
let mut globals = layout.state.globals.get();
label.set_text_simple(&mut globals, title.clone());
}
if &*id == "toast_body" {
let mut label = layout
.state
.widgets
.get_as::<WidgetLabel>(widget)
.context("toast.xml: missing element with id: toast_body")?;
let mut globals = layout.state.globals.get();
label.set_text_simple(&mut globals, Translation::from_raw_text(&toast.body));
}
Ok(())
});
let globals = panel.layout.state.globals.clone(); let panel = GuiPanel::new_from_template(
app,
let (rect, _) = panel "gui/toast.xml",
.layout (),
.add_child( NewGuiPanelParams {
panel.layout.content_root_widget, on_custom_id: Some(on_custom_id),
WidgetRectangle::create(WidgetRectangleParams {
color: parse_color_hex("#1e2030").unwrap(),
border_color: parse_color_hex("#5e7090").unwrap(),
border: 1.0,
round: WLength::Units(4.0),
..Default::default()
}),
taffy::Style {
align_items: Some(taffy::AlignItems::Center),
justify_content: Some(taffy::JustifyContent::Center),
flex_direction: taffy::FlexDirection::Column,
padding: length(4.0),
..Default::default() ..Default::default()
}, },
) )
.ok()?; .ok()?;
let _ = panel.layout.add_child(
rect.id,
WidgetLabel::create(
&mut globals.get(),
WidgetLabelParams {
content: Translation::from_raw_text(&title),
style: TextStyle {
color: parse_color_hex("#ffffff"),
..Default::default()
},
},
),
taffy::Style {
size: taffy::Size {
width: percent(1.0),
height: auto(),
},
padding: length(8.0),
..Default::default()
},
);
let _ = panel.layout.add_child(
rect.id,
WidgetLabel::create(
&mut globals.get(),
WidgetLabelParams {
content: Translation::from_raw_text(&toast.body),
style: TextStyle {
weight: Some(FontWeight::Bold),
color: parse_color_hex("#eeeeee"),
..Default::default()
},
},
),
taffy::Style {
size: taffy::Size {
width: percent(1.0),
height: auto(),
},
padding: length(8.0),
..Default::default()
},
);
panel.update_layout().ok()?;
Some(OverlayWindowConfig { Some(OverlayWindowConfig {
name: TOAST_NAME.clone(), name: TOAST_NAME.clone(),
default_state: OverlayWindowState { default_state: OverlayWindowState {