use crate::style::color::Color;
use crate::text::TextWrap;
use std::fmt::Display;
pub struct RichText {
pub(crate) text: String,
pub(crate) size: Option<f32>,
pub(crate) color: Color,
pub(crate) height: f32,
pub(crate) width: f32,
pub(crate) wrap: TextWrap,
pub(crate) family: Option<String>,
}
impl RichText {
pub fn new(text: impl ToString) -> RichText {
RichText {
text: text.to_string(),
size: None,
color: Color::BLACK,
height: 0.0,
width: 0.0,
wrap: TextWrap::NoWrap,
family: None,
}
}
pub fn wrap(mut self, wrap: TextWrap) -> RichText {
self.wrap = wrap;
self
}
pub fn size(mut self, size: f32) -> RichText {
self.size = Some(size);
self
}
pub fn family(mut self, family: impl ToString) -> RichText {
self.family = Some(family.to_string());
self
}
pub fn color(mut self, color: Color) -> RichText {
self.color = color;
self
}
pub(crate) fn font_size(&self) -> f32 {
self.size.unwrap()
}
pub(crate) fn font_family(&self) -> glyphon::Attrs<'_> {
let family = self.family.as_ref().unwrap();
let glyphon_family = glyphon::Family::Name(&family);
glyphon::Attrs::new().family(glyphon_family)
}
}
impl<T: Display> From<T> for RichText {
fn from(value: T) -> Self {
RichText::new(value)
}
}
pub trait RichTextExt {
fn color(self, color: Color) -> RichText;
fn size(self, size: f32) -> RichText;
fn wrap(self, wrap: TextWrap) -> RichText;
fn family(self, family: impl ToString) -> RichText;
}
impl<T: Display> RichTextExt for T {
fn color(self, color: Color) -> RichText {
RichText::new(self.to_string()).color(color)
}
fn size(self, size: f32) -> RichText {
RichText::new(self.to_string()).size(size)
}
fn wrap(self, wrap: TextWrap) -> RichText {
RichText::new(self.to_string()).wrap(wrap)
}
fn family(self, family: impl ToString) -> RichText {
RichText::new(self.to_string()).family(family.to_string())
}
}