tooltip raw text, inline translation fallback support

This commit is contained in:
Aleksander
2026-01-05 01:05:56 +01:00
parent 4dd7c85e79
commit b86525d65d
4 changed files with 35 additions and 15 deletions

View File

@@ -198,17 +198,22 @@ impl AppList {
tasks: &Tasks<Task>,
parser_state: &mut ParserState,
) -> anyhow::Result<()> {
if let Some(entry) = self.entries_to_mount.pop_front() {
let globals = frontend.layout.state.globals.clone();
let button = self.mount_entry(frontend, parser_state, &doc_params(globals.clone()), &entry)?;
// load 4 entries for a single frame at most
for _ in 0..4 {
if let Some(entry) = self.entries_to_mount.pop_front() {
let globals = frontend.layout.state.globals.clone();
let button = self.mount_entry(frontend, parser_state, &doc_params(globals.clone()), &entry)?;
button.on_click(on_app_click(
frontend.tasks.clone(),
globals.clone(),
entry.clone(),
state.clone(),
tasks.clone(),
));
button.on_click(on_app_click(
frontend.tasks.clone(),
globals.clone(),
entry.clone(),
state.clone(),
tasks.clone(),
));
} else {
break;
}
}
Ok(())

View File

@@ -264,6 +264,10 @@ _Translated by key_
_Tooltip text on hover, translated by key_
`tooltip_str`: **string**
_Tooltip text on hover, raw text (not translated)_
`tooltip_side`: "top" | "bottom" | "left" | "right" (default: top)
`sticky`: "1" | "0" (default: "0")

View File

@@ -109,7 +109,11 @@ impl I18n {
})
}
pub fn translate(&mut self, translation_key: &str) -> Rc<str> {
pub fn translate(&mut self, translation_key_full: &str) -> Rc<str> {
let translation_key = translation_key_full
.split_once(';')
.map_or(translation_key_full, |(a, _)| a);
if let Some(translated) = find_translation(translation_key, &self.json_root_translated) {
return Rc::from(translated);
}
@@ -119,6 +123,11 @@ impl I18n {
return Rc::from(translated_fallback);
}
// not even found in fallback, check if the translation contains ";" (to be used as "MY_TRANSLATION_KEY;A fallback text")
if let Some((idx, _)) = translation_key_full.match_indices(';').next() {
return Rc::from(&translation_key_full[idx + 1..]);
}
log::error!("missing translation for key \"{translation_key}\"");
Rc::from(translation_key) // show translation key as a fallback
}

View File

@@ -12,6 +12,7 @@ use crate::{
widget::util::WLength,
};
#[allow(clippy::too_many_lines)]
pub fn parse_component_button<'a>(
file: &'a ParserFile,
ctx: &mut ParserContext,
@@ -25,7 +26,7 @@ pub fn parse_component_button<'a>(
let mut hover_color: Option<Color> = None;
let mut hover_border_color: Option<Color> = None;
let mut round = WLength::Units(4.0);
let mut tooltip: Option<String> = None;
let mut tooltip: Option<Translation> = None;
let mut tooltip_side: Option<tooltip::TooltipSide> = None;
let mut sticky: bool = false;
let mut long_press_time = 0.0;
@@ -76,7 +77,8 @@ pub fn parse_component_button<'a>(
sprite_src = Some(asset_path);
}
}
"tooltip" => tooltip = Some(String::from(value)),
"tooltip" => tooltip = Some(Translation::from_translation_key(value)),
"tooltip_str" => tooltip = Some(Translation::from_raw_text(value)),
"tooltip_side" => {
tooltip_side = match value {
"left" => Some(tooltip::TooltipSide::Left),
@@ -112,9 +114,9 @@ pub fn parse_component_button<'a>(
style,
text_style,
round,
tooltip: tooltip.map(|t| tooltip::TooltipInfo {
tooltip: tooltip.map(|text| tooltip::TooltipInfo {
side: tooltip_side.map_or(tooltip::TooltipSide::Top, |f| f),
text: Translation::from_translation_key(&t),
text,
}),
sticky,
long_press_time,