velox_engine/element/
text.rs1use crate::arena::StrRef;
4use crate::pod::FrameScope;
5use crate::style::{Color, TextAlign};
6
7use super::{Element, IntoElement};
8
9impl<'a> IntoElement<'a> for Text<'a> {
10 #[inline(always)]
11 fn into_element(self, _cx: &'a FrameScope) -> Element<'a> {
12 Element::Text(self)
13 }
14}
15
16
17#[derive(Copy, Clone, Debug)]
19pub struct Text<'a> {
20 pub content: StrRef<'a>,
21 pub font_size: u8,
22 pub flags: u8,
24 pub color: Color,
25}
26
27const TEXT_FLAG_EXPLICIT_SIZE: u8 = 0x10;
28const TEXT_FLAG_EXPLICIT_COLOR: u8 = 0x20;
29
30#[inline(always)]
34pub fn text<'a>(content: impl Into<StrRef<'a>>) -> Text<'a> {
35 Text {
36 content: content.into(),
37 font_size: 14,
38 flags: 0,
39 color: Color::WHITE,
40 }
41}
42
43impl<'a> Text<'a> {
44 #[inline(always)]
46 pub const fn size(mut self, px: u8) -> Self {
47 self.font_size = px;
48 self.flags |= TEXT_FLAG_EXPLICIT_SIZE;
49 self
50 }
51
52 #[inline(always)]
54 pub const fn text_size(self, px: u8) -> Self {
55 self.size(px)
56 }
57
58 #[inline(always)] pub const fn text_xs(self) -> Self { self.size(11) }
59 #[inline(always)] pub const fn text_sm(self) -> Self { self.size(12) }
60 #[inline(always)] pub const fn text_base(self) -> Self { self.size(14) }
61 #[inline(always)] pub const fn text_lg(self) -> Self { self.size(16) }
62 #[inline(always)] pub const fn text_xl(self) -> Self { self.size(18) }
63 #[inline(always)] pub const fn text_2xl(self) -> Self { self.size(22) }
64 #[inline(always)] pub const fn text_3xl(self) -> Self { self.size(28) }
65
66 #[inline(always)]
68 pub const fn color(mut self, color: Color) -> Self {
69 self.color = color;
70 self.flags |= TEXT_FLAG_EXPLICIT_COLOR;
71 self
72 }
73
74 #[inline(always)]
76 pub const fn text_color(self, color: Color) -> Self {
77 self.color(color)
78 }
79
80 #[inline(always)]
82 pub const fn has_explicit_size(&self) -> bool {
83 (self.flags & TEXT_FLAG_EXPLICIT_SIZE) != 0
84 }
85
86 #[inline(always)]
88 pub const fn has_explicit_color(&self) -> bool {
89 (self.flags & TEXT_FLAG_EXPLICIT_COLOR) != 0
90 }
91
92 #[inline(always)]
94 pub const fn align(mut self, align: TextAlign) -> Self {
95 self.flags = (self.flags & !0x03) | (align as u8 & 0x03);
96 self
97 }
98
99 #[inline(always)]
101 pub const fn text_left(self) -> Self {
102 self.align(TextAlign::Left)
103 }
104
105 #[inline(always)]
107 pub const fn text_center(self) -> Self {
108 self.align(TextAlign::Center)
109 }
110
111 #[inline(always)]
113 pub const fn text_right(self) -> Self {
114 self.align(TextAlign::Right)
115 }
116
117 #[inline(always)]
119 pub fn text_start(self, cx: &FrameScope) -> Self {
120 if cx.is_rtl() {
121 self.align(TextAlign::Right)
122 } else {
123 self.align(TextAlign::Left)
124 }
125 }
126
127 #[inline(always)]
129 pub fn text_end(self, cx: &FrameScope) -> Self {
130 if cx.is_rtl() {
131 self.align(TextAlign::Left)
132 } else {
133 self.align(TextAlign::Right)
134 }
135 }
136
137 #[inline(always)]
139 pub const fn ellipsis(mut self) -> Self {
140 self.flags |= 0x04 | 0x08;
141 self
142 }
143
144 #[inline(always)]
146 pub const fn nowrap(mut self) -> Self {
147 self.flags |= 0x08;
148 self
149 }
150
151 #[inline(always)]
153 pub const fn text_align(&self) -> TextAlign {
154 match self.flags & 0x03 {
155 1 => TextAlign::Center,
156 2 => TextAlign::Right,
157 _ => TextAlign::Left,
158 }
159 }
160
161 #[inline(always)]
163 pub const fn is_ellipsis(&self) -> bool {
164 (self.flags & 0x04) != 0
165 }
166
167 #[inline(always)]
169 pub const fn is_nowrap(&self) -> bool {
170 (self.flags & 0x08) != 0
171 }
172}
173