embedded_text/style/
builder.rs1use embedded_graphics::text::LineHeight;
3
4use crate::{
5 alignment::{HorizontalAlignment, VerticalAlignment},
6 style::{HeightMode, TabSize, TextBoxStyle, VerticalOverdraw},
7};
8
9#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
11#[must_use]
12pub struct TextBoxStyleBuilder {
13 style: TextBoxStyle,
14 leading_spaces: Option<bool>,
15 trailing_spaces: Option<bool>,
16}
17
18impl TextBoxStyleBuilder {
19 #[inline]
21 pub const fn default() -> Self {
22 Self::new()
23 }
24
25 #[inline]
27 pub const fn new() -> Self {
28 Self {
29 style: TextBoxStyle {
30 alignment: HorizontalAlignment::Left,
31 vertical_alignment: VerticalAlignment::Top,
32 height_mode: HeightMode::Exact(VerticalOverdraw::FullRowsOnly),
33 line_height: LineHeight::Percent(100),
34 paragraph_spacing: 0,
35 tab_size: TabSize::Spaces(4),
36 leading_spaces: false,
38 trailing_spaces: false,
39 },
40 leading_spaces: None,
41 trailing_spaces: None,
42 }
43 }
44
45 #[inline]
61 pub const fn line_height(mut self, line_height: LineHeight) -> Self {
62 self.style.line_height = line_height;
63
64 self
65 }
66
67 #[inline]
80 pub const fn paragraph_spacing(mut self, paragraph_spacing: u32) -> Self {
81 self.style.paragraph_spacing = paragraph_spacing;
82
83 self
84 }
85
86 #[inline]
88 pub const fn alignment(mut self, alignment: HorizontalAlignment) -> TextBoxStyleBuilder {
89 self.style.alignment = alignment;
90
91 self
92 }
93
94 #[inline]
96 pub const fn vertical_alignment(
97 mut self,
98 vertical_alignment: VerticalAlignment,
99 ) -> TextBoxStyleBuilder {
100 self.style.vertical_alignment = vertical_alignment;
101
102 self
103 }
104
105 #[inline]
107 pub const fn height_mode(mut self, height_mode: HeightMode) -> TextBoxStyleBuilder {
108 self.style.height_mode = height_mode;
109
110 self
111 }
112
113 #[inline]
115 pub const fn tab_size(mut self, tab_size: TabSize) -> Self {
116 self.style.tab_size = tab_size;
117
118 self
119 }
120
121 #[inline]
123 pub const fn leading_spaces(mut self, render: bool) -> Self {
124 self.leading_spaces = Some(render);
125
126 self
127 }
128
129 #[inline]
131 pub const fn trailing_spaces(mut self, render: bool) -> Self {
132 self.trailing_spaces = Some(render);
133
134 self
135 }
136
137 #[inline]
139 pub const fn build(mut self) -> TextBoxStyle {
140 self.style.leading_spaces = match self.leading_spaces {
141 Some(leading_spaces) => leading_spaces,
142 None => self.style.alignment.leading_spaces(),
143 };
144
145 self.style.trailing_spaces = match self.trailing_spaces {
146 Some(trailing_spaces) => trailing_spaces,
147 None => self.style.alignment.trailing_spaces(),
148 };
149
150 self.style
151 }
152}
153
154impl From<&TextBoxStyle> for TextBoxStyleBuilder {
155 #[inline]
156 fn from(style: &TextBoxStyle) -> Self {
157 Self {
158 style: *style,
159 leading_spaces: None,
160 trailing_spaces: None,
161 }
162 }
163}