use embedded_graphics::text::LineHeight;
use crate::{
alignment::{HorizontalAlignment, VerticalAlignment},
style::{HeightMode, TabSize, TextBoxStyle, VerticalOverdraw},
};
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
#[must_use]
pub struct TextBoxStyleBuilder {
style: TextBoxStyle,
leading_spaces: Option<bool>,
trailing_spaces: Option<bool>,
}
impl TextBoxStyleBuilder {
#[inline]
pub const fn default() -> Self {
Self::new()
}
#[inline]
pub const fn new() -> Self {
Self {
style: TextBoxStyle {
alignment: HorizontalAlignment::Left,
vertical_alignment: VerticalAlignment::Top,
height_mode: HeightMode::Exact(VerticalOverdraw::FullRowsOnly),
line_height: LineHeight::Percent(100),
paragraph_spacing: 0,
tab_size: TabSize::Spaces(4),
leading_spaces: false,
trailing_spaces: false,
},
leading_spaces: None,
trailing_spaces: None,
}
}
#[inline]
pub const fn line_height(mut self, line_height: LineHeight) -> Self {
self.style.line_height = line_height;
self
}
#[inline]
pub const fn paragraph_spacing(mut self, paragraph_spacing: u32) -> Self {
self.style.paragraph_spacing = paragraph_spacing;
self
}
#[inline]
pub const fn alignment(mut self, alignment: HorizontalAlignment) -> TextBoxStyleBuilder {
self.style.alignment = alignment;
self
}
#[inline]
pub const fn vertical_alignment(
mut self,
vertical_alignment: VerticalAlignment,
) -> TextBoxStyleBuilder {
self.style.vertical_alignment = vertical_alignment;
self
}
#[inline]
pub const fn height_mode(mut self, height_mode: HeightMode) -> TextBoxStyleBuilder {
self.style.height_mode = height_mode;
self
}
#[inline]
pub const fn tab_size(mut self, tab_size: TabSize) -> Self {
self.style.tab_size = tab_size;
self
}
#[inline]
pub const fn leading_spaces(mut self, render: bool) -> Self {
self.leading_spaces = Some(render);
self
}
#[inline]
pub const fn trailing_spaces(mut self, render: bool) -> Self {
self.trailing_spaces = Some(render);
self
}
#[inline]
pub const fn build(mut self) -> TextBoxStyle {
self.style.leading_spaces = match self.leading_spaces {
Some(leading_spaces) => leading_spaces,
None => self.style.alignment.leading_spaces(),
};
self.style.trailing_spaces = match self.trailing_spaces {
Some(trailing_spaces) => trailing_spaces,
None => self.style.alignment.trailing_spaces(),
};
self.style
}
}
impl From<&TextBoxStyle> for TextBoxStyleBuilder {
#[inline]
fn from(style: &TextBoxStyle) -> Self {
Self {
style: *style,
leading_spaces: None,
trailing_spaces: None,
}
}
}