wgui: basic i18n support, refactoring: use LayoutState, translation framework (LLM-based generator)
This commit is contained in:
@@ -16,7 +16,7 @@ use wgui::{
|
||||
|
||||
use crate::{
|
||||
backend::overlay::{OverlayData, OverlayState, Positioning},
|
||||
gui::{self, panel::GuiPanel},
|
||||
gui::panel::GuiPanel,
|
||||
state::AppState,
|
||||
subsystem::hid::{ALT, CTRL, META, SHIFT, SUPER, XkbKeymap},
|
||||
};
|
||||
@@ -76,7 +76,7 @@ where
|
||||
}
|
||||
|
||||
let (_, mut gui_state_key) = wgui::parser::new_layout_from_assets(
|
||||
Box::new(gui::asset::GuiAsset {}),
|
||||
app.wgui_globals.clone(),
|
||||
&mut panel.listeners,
|
||||
"gui/keyboard.xml",
|
||||
)?;
|
||||
@@ -169,7 +169,8 @@ where
|
||||
let key_state = {
|
||||
let rect = panel
|
||||
.layout
|
||||
.widget_map
|
||||
.state
|
||||
.widgets
|
||||
.get_as::<Rectangle>(*widget_id)
|
||||
.unwrap(); // want panic
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ use glam::{Quat, vec3a};
|
||||
use idmap_derive::IntegerId;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use wgui::{
|
||||
i18n::Translation,
|
||||
parser::parse_color_hex,
|
||||
renderer_vk::text::{FontWeight, TextStyle},
|
||||
taffy::{
|
||||
@@ -168,6 +169,9 @@ fn new_toast(toast: Toast, app: &mut AppState) -> Option<(OverlayState, Box<dyn
|
||||
|
||||
let mut panel = GuiPanel::new_blank(app, ()).ok()?;
|
||||
|
||||
let globals = panel.layout.state.globals.clone();
|
||||
let mut i18n = globals.i18n();
|
||||
|
||||
let (rect, _) = panel
|
||||
.layout
|
||||
.add_child(
|
||||
@@ -192,13 +196,16 @@ fn new_toast(toast: Toast, app: &mut AppState) -> Option<(OverlayState, Box<dyn
|
||||
|
||||
let _ = panel.layout.add_child(
|
||||
rect,
|
||||
TextLabel::create(TextParams {
|
||||
content: title,
|
||||
style: TextStyle {
|
||||
color: parse_color_hex("#ffffff"),
|
||||
..Default::default()
|
||||
TextLabel::create(
|
||||
&mut i18n,
|
||||
TextParams {
|
||||
content: Translation::from_raw_text(&title),
|
||||
style: TextStyle {
|
||||
color: parse_color_hex("#ffffff"),
|
||||
..Default::default()
|
||||
},
|
||||
},
|
||||
})
|
||||
)
|
||||
.unwrap(),
|
||||
taffy::Style {
|
||||
size: taffy::Size {
|
||||
@@ -212,14 +219,17 @@ fn new_toast(toast: Toast, app: &mut AppState) -> Option<(OverlayState, Box<dyn
|
||||
|
||||
let _ = panel.layout.add_child(
|
||||
rect,
|
||||
TextLabel::create(TextParams {
|
||||
content: toast.body,
|
||||
style: TextStyle {
|
||||
weight: Some(FontWeight::Bold),
|
||||
color: parse_color_hex("#eeeeee"),
|
||||
..Default::default()
|
||||
TextLabel::create(
|
||||
&mut i18n,
|
||||
TextParams {
|
||||
content: Translation::from_raw_text(&toast.body),
|
||||
style: TextStyle {
|
||||
weight: Some(FontWeight::Bold),
|
||||
color: parse_color_hex("#eeeeee"),
|
||||
..Default::default()
|
||||
},
|
||||
},
|
||||
})
|
||||
)
|
||||
.unwrap(),
|
||||
taffy::Style {
|
||||
size: taffy::Size {
|
||||
|
||||
@@ -6,6 +6,7 @@ use glam::Vec3A;
|
||||
use regex::Regex;
|
||||
use wgui::{
|
||||
event::{self, EventListenerKind},
|
||||
i18n::Translation,
|
||||
widget::text::TextLabel,
|
||||
};
|
||||
|
||||
@@ -43,18 +44,20 @@ where
|
||||
|
||||
let mut label = panel
|
||||
.layout
|
||||
.widget_map
|
||||
.state
|
||||
.widgets
|
||||
.get_as::<TextLabel>(*widget_id)
|
||||
.unwrap();
|
||||
|
||||
let format = match role {
|
||||
"tz" => {
|
||||
let mut i18n = panel.layout.state.globals.i18n();
|
||||
if let Some(s) =
|
||||
tz_str.and_then(|tz| tz.split('/').next_back().map(|x| x.replace('_', " ")))
|
||||
{
|
||||
label.set_text(&s);
|
||||
label.set_text(&mut i18n, Translation::from_raw_text(&s));
|
||||
} else {
|
||||
label.set_text("Local");
|
||||
label.set_text(&mut i18n, Translation::from_raw_text("Local"));
|
||||
}
|
||||
|
||||
continue;
|
||||
@@ -69,7 +72,8 @@ where
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
label.set_text("ERR");
|
||||
let mut i18n = panel.layout.state.globals.i18n();
|
||||
label.set_text(&mut i18n, Translation::from_raw_text("ERR"));
|
||||
continue;
|
||||
}
|
||||
};
|
||||
@@ -87,8 +91,8 @@ where
|
||||
&mut panel.listener_handles,
|
||||
*widget_id,
|
||||
EventListenerKind::InternalStateChange,
|
||||
Box::new(move |_common, data, _, _| {
|
||||
clock_on_tick(&clock, data);
|
||||
Box::new(move |common, data, _, _| {
|
||||
clock_on_tick(&clock, common, data);
|
||||
}),
|
||||
);
|
||||
}
|
||||
@@ -151,12 +155,16 @@ struct ClockState {
|
||||
format: Rc<str>,
|
||||
}
|
||||
|
||||
fn clock_on_tick(clock: &ClockState, data: &mut event::CallbackData) {
|
||||
fn clock_on_tick(
|
||||
clock: &ClockState,
|
||||
common: &event::CallbackDataCommon,
|
||||
data: &mut event::CallbackData,
|
||||
) {
|
||||
let date_time = clock.timezone.as_ref().map_or_else(
|
||||
|| format!("{}", Local::now().format(&clock.format)),
|
||||
|tz| format!("{}", Local::now().with_timezone(tz).format(&clock.format)),
|
||||
);
|
||||
|
||||
let label = data.obj.get_as_mut::<TextLabel>();
|
||||
label.set_text(&date_time);
|
||||
label.set_text(&mut common.i18n(), Translation::from_raw_text(&date_time));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user