based/src/ui/primitives/text.rs
2025-01-15 18:28:59 +01:00

163 lines
3.8 KiB
Rust

use crate::ui::{UIWidget, color::UIColor};
use maud::{Markup, Render, html};
#[allow(non_snake_case)]
/// Text UI Widget
pub fn Text(txt: &str) -> TextWidget {
TextWidget {
inner: None,
txt: txt.to_string(),
font: String::new(),
color: String::new(),
size: String::new(),
span: false,
}
}
#[allow(non_snake_case)]
/// HTML `<p>` Paragraph
pub fn Paragraph<T: UIWidget + 'static>(inner: T) -> TextWidget {
TextWidget {
inner: Some(Box::new(inner)),
font: String::new(),
color: String::new(),
txt: String::new(),
size: String::new(),
span: false,
}
}
#[allow(non_snake_case)]
/// `<span>` element
pub fn Span(txt: &str) -> TextWidget {
TextWidget {
inner: None,
txt: txt.to_string(),
font: String::new(),
color: String::new(),
size: String::new(),
span: true,
}
}
pub struct TextWidget {
inner: Option<Box<dyn UIWidget>>,
txt: String,
font: String,
color: String,
size: String,
span: bool,
}
impl TextWidget {
/// Turn `Text` semibold.
///
/// Adds the class `font-semibold`
pub fn semibold(mut self) -> Self {
self.font = "font-semibold".to_owned();
self
}
/// Turn `Text` bold.
///
/// Adds the class `font-bold`
pub fn bold(mut self) -> Self {
self.font = "font-bold".to_owned();
self
}
/// Turn `Text` medium.
///
/// Adds the class `font-medium`
pub fn medium(mut self) -> Self {
self.font = "font-medium".to_owned();
self
}
/// Turn `Text` size to 2XL.
///
/// Adds the class `text-2xl`
pub fn _2xl(mut self) -> Self {
self.size = "text-2xl".to_owned();
self
}
/// Turn `Text` size to xl.
///
/// Adds the class `text-xl`
pub fn xl(mut self) -> Self {
self.size = "text-xl".to_owned();
self
}
/// Turn `Text` size to small.
///
/// Adds the class `text-sm`
pub fn sm(mut self) -> Self {
self.size = "text-sm".to_owned();
self
}
pub fn color<T: UIColor>(mut self, color: T) -> Self {
self.color = format!("text-{}", color.color_class());
self
}
pub fn black(mut self) -> Self {
self.color = "text-black".to_owned();
self
}
pub fn white(mut self) -> Self {
self.color = "text-white".to_owned();
self
}
}
impl Render for TextWidget {
fn render(&self) -> Markup {
self.render_with_class("")
}
}
impl UIWidget for TextWidget {
fn can_inherit(&self) -> bool {
true
}
fn base_class(&self) -> Vec<String> {
vec![self.color.clone(), self.font.clone(), self.size.clone()]
}
fn extended_class(&self) -> Vec<String> {
let mut c = self.base_class();
if let Some(inner) = &self.inner {
c.extend_from_slice(&inner.extended_class());
}
c
}
fn render_with_class(&self, class: &str) -> Markup {
if let Some(inner) = &self.inner {
if self.span {
html! {
span class=(format!("{} {}", class, self.base_class().join(" "))) { (inner) }
}
} else {
html! {
p class=(format!("{} {}", class, self.base_class().join(" "))) { (inner) }
}
}
} else {
if self.span {
html! {
span class=(format!("{} {}", class, self.base_class().join(" "))) { (self.txt) }
}
} else {
html! {
p class=(format!("{} {}", class, self.base_class().join(" "))) { (self.txt) }
}
}
}
}
}