This commit is contained in:
JMARyA 2025-01-17 16:47:14 +01:00
parent 302daacc82
commit 28fa0f21dc
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263

View file

@ -1,5 +1,5 @@
use crate::ui::{UIWidget, color::UIColor}; use crate::ui::{UIWidget, color::UIColor};
use maud::{Markup, Render, html}; use maud::{Markup, PreEscaped, Render};
use super::{Nothing, space::ScreenValue}; use super::{Nothing, space::ScreenValue};
@ -11,6 +11,7 @@ pub fn Text(txt: &str) -> TextWidget {
inner: None, inner: None,
txt: txt.to_string(), txt: txt.to_string(),
family: String::new(), family: String::new(),
title: None,
font: String::new(), font: String::new(),
color: String::new(), color: String::new(),
style: Vec::new(), style: Vec::new(),
@ -30,15 +31,22 @@ pub fn Text(txt: &str) -> TextWidget {
align: None, align: None,
vert_align: None, vert_align: None,
list_style: None, list_style: None,
span: false, kind: TextKind::Paragraph,
} }
} }
enum TextKind {
Paragraph,
Span,
Pre,
}
#[allow(non_snake_case)] #[allow(non_snake_case)]
/// HTML `<p>` Paragraph /// HTML `<p>` Paragraph
pub fn Paragraph<T: UIWidget + 'static>(inner: T) -> TextWidget { pub fn Paragraph<T: UIWidget + 'static>(inner: T) -> TextWidget {
TextWidget { TextWidget {
inner: Some(Box::new(inner)), inner: Some(Box::new(inner)),
title: None,
font: String::new(), font: String::new(),
family: String::new(), family: String::new(),
color: String::new(), color: String::new(),
@ -60,7 +68,7 @@ pub fn Paragraph<T: UIWidget + 'static>(inner: T) -> TextWidget {
list_style: None, list_style: None,
clamp: None, clamp: None,
align: None, align: None,
span: false, kind: TextKind::Paragraph,
} }
} }
@ -70,6 +78,7 @@ pub fn Paragraph<T: UIWidget + 'static>(inner: T) -> TextWidget {
pub fn Span(txt: &str) -> TextWidget { pub fn Span(txt: &str) -> TextWidget {
TextWidget { TextWidget {
inner: None, inner: None,
title: None,
txt: txt.to_string(), txt: txt.to_string(),
family: String::new(), family: String::new(),
font: String::new(), font: String::new(),
@ -91,7 +100,39 @@ pub fn Span(txt: &str) -> TextWidget {
clamp: None, clamp: None,
align: None, align: None,
pseudo: None, pseudo: None,
span: true, kind: TextKind::Span,
}
}
#[allow(non_snake_case)]
/// `<pre>` element
#[must_use]
pub fn Code(txt: &str) -> TextWidget {
TextWidget {
inner: None,
title: None,
txt: txt.to_string(),
family: String::new(),
font: String::new(),
style: Vec::new(),
color: String::new(),
list_style: None,
size: String::new(),
indent: None,
overflow: None,
whitespace: None,
wordbreak: None,
hyphens: None,
decoration: None,
transform: None,
wrap: None,
vert_align: None,
spacing: None,
line_height: None,
clamp: None,
align: None,
pseudo: None,
kind: TextKind::Pre,
} }
} }
@ -114,11 +155,12 @@ pub struct TextWidget {
wordbreak: Option<TextWordBreak>, wordbreak: Option<TextWordBreak>,
hyphens: Option<TextHyphens>, hyphens: Option<TextHyphens>,
size: String, size: String,
span: bool, kind: TextKind,
spacing: Option<LetterSpacing>, spacing: Option<LetterSpacing>,
pseudo: Option<TextContent>, pseudo: Option<TextContent>,
align: Option<TextAlignment>, align: Option<TextAlignment>,
clamp: Option<LineClamp>, clamp: Option<LineClamp>,
title: Option<String>,
} }
impl TextWidget { impl TextWidget {
@ -289,6 +331,12 @@ impl TextWidget {
self self
} }
#[must_use]
pub fn title(mut self, title: &str) -> Self {
self.title = Some(title.to_string());
self
}
// Sizes // Sizes
#[must_use] #[must_use]
@ -490,25 +538,37 @@ impl UIWidget for TextWidget {
} }
fn render_with_class(&self, class: &str) -> Markup { fn render_with_class(&self, class: &str) -> Markup {
if let Some(inner) = &self.inner { let element = match self.kind {
if self.span { TextKind::Paragraph => "p",
html! { TextKind::Span => "span",
span class=(format!("{} {}", class, self.base_class().join(" "))) { (inner) } TextKind::Pre => "pre",
} };
} else {
html! { let mut ret = "<".to_string();
p class=(format!("{} {}", class, self.base_class().join(" "))) { (inner) } ret.push_str(&element);
} ret.push_str(" class='");
} ret.push_str(&format!("{} {}", class, self.base_class().join(" ")));
} else if self.span { ret.push_str("'");
html! {
span class=(format!("{} {}", class, self.base_class().join(" "))) { (self.txt) } if let Some(title) = &self.title {
} ret.push_str(" title='");
} else { ret.push_str(&title);
html! { ret.push_str("'");
p class=(format!("{} {}", class, self.base_class().join(" "))) { (self.txt) }
}
} }
ret.push_str("> ");
if let Some(inner) = &self.inner {
ret.push_str(&inner.render().0);
} else {
ret.push_str(&self.txt);
}
ret.push_str("</");
ret.push_str(&element);
ret.push_str(">");
PreEscaped(ret)
} }
} }