Skip to main content

velox_engine/element/
text.rs

1//! 文本原语节点 `Text<'a>` 与排版修饰符(字号、颜色、对齐、溢出省略、单行不折行)。
2
3use 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/// 文本原语节点。
18#[derive(Copy, Clone, Debug)]
19pub struct Text<'a> {
20    pub content: StrRef<'a>,
21    pub font_size: u8,
22    /// 位域:`align(2b: 0=Left, 1=Center, 2=Right) | ellipsis(1b: 0x04) | nowrap(1b: 0x08) | explicit_size(1b: 0x10) | explicit_color(1b: 0x20)`。
23    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/// 构造一个文本原子节点(同时支持静态字符串 `&'static str` 与 `fmt!(cx, ...)` 生成的 `StrRef<'a>`)。
31///
32/// 未显式调用 `.size(...)` / `.color(...)` 时,自动继承父容器 `Div` 上设置的 `.text_xs()` / `.text_color(...)`。
33#[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    /// 设置字号大小(像素)。
45    #[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    /// 设置字号大小(与 `Div::text_size` 对齐的别名)。
53    #[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    /// 设置文本颜色。
67    #[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    /// 设置文本颜色(与 GPUI `.text_color(...)` 对齐的别名)。
75    #[inline(always)]
76    pub const fn text_color(self, color: Color) -> Self {
77        self.color(color)
78    }
79
80    /// 是否显式覆盖了字号(否则继承父 `Div` 的 `text_size`)。
81    #[inline(always)]
82    pub const fn has_explicit_size(&self) -> bool {
83        (self.flags & TEXT_FLAG_EXPLICIT_SIZE) != 0
84    }
85
86    /// 是否显式覆盖了颜色(否则继承父 `Div` 的 `text_color`)。
87    #[inline(always)]
88    pub const fn has_explicit_color(&self) -> bool {
89        (self.flags & TEXT_FLAG_EXPLICIT_COLOR) != 0
90    }
91
92    /// 设置文本水平对齐方式。
93    #[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    /// 设置文本左对齐。
100    #[inline(always)]
101    pub const fn text_left(self) -> Self {
102        self.align(TextAlign::Left)
103    }
104
105    /// 设置文本水平居中对齐。
106    #[inline(always)]
107    pub const fn text_center(self) -> Self {
108        self.align(TextAlign::Center)
109    }
110
111    /// 设置文本右对齐。
112    #[inline(always)]
113    pub const fn text_right(self) -> Self {
114        self.align(TextAlign::Right)
115    }
116
117    /// 按当前语言的书写方向自动设置“逻辑起始端对齐”(LTR 为左对齐,RTL 如阿拉伯语/希伯来语自动镜像为右对齐)。
118    #[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    /// 按当前语言的书写方向自动设置“逻辑末端对齐”(LTR 为右对齐,RTL 自动镜像为左对齐)。
128    #[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    /// 开启单行省略号截断(超出容器宽度时自动截断并追加 `...`)。
138    #[inline(always)]
139    pub const fn ellipsis(mut self) -> Self {
140        self.flags |= 0x04 | 0x08;
141        self
142    }
143
144    /// 禁用自动多行折行(强制单行排版)。
145    #[inline(always)]
146    pub const fn nowrap(mut self) -> Self {
147        self.flags |= 0x08;
148        self
149    }
150
151    /// 读取水平对齐方式。
152    #[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    /// 是否启用省略号截断。
162    #[inline(always)]
163    pub const fn is_ellipsis(&self) -> bool {
164        (self.flags & 0x04) != 0
165    }
166
167    /// 是否禁用自动多行换行。
168    #[inline(always)]
169    pub const fn is_nowrap(&self) -> bool {
170        (self.flags & 0x08) != 0
171    }
172}
173