use crate::arena::StrRef;
use crate::pod::FrameScope;
use crate::style::{Color, TextAlign};
use super::{Element, IntoElement};
impl<'a> IntoElement<'a> for Text<'a> {
#[inline(always)]
fn into_element(self, _cx: &'a FrameScope) -> Element<'a> {
Element::Text(self)
}
}
#[derive(Copy, Clone, Debug)]
pub struct Text<'a> {
pub content: StrRef<'a>,
pub font_size: u8,
pub flags: u8,
pub color: Color,
}
const TEXT_FLAG_EXPLICIT_SIZE: u8 = 0x10;
const TEXT_FLAG_EXPLICIT_COLOR: u8 = 0x20;
#[inline(always)]
pub fn text<'a>(content: impl Into<StrRef<'a>>) -> Text<'a> {
Text {
content: content.into(),
font_size: 14,
flags: 0,
color: Color::WHITE,
}
}
impl<'a> Text<'a> {
#[inline(always)]
pub const fn size(mut self, px: u8) -> Self {
self.font_size = px;
self.flags |= TEXT_FLAG_EXPLICIT_SIZE;
self
}
#[inline(always)]
pub const fn text_size(self, px: u8) -> Self {
self.size(px)
}
#[inline(always)] pub const fn text_xs(self) -> Self { self.size(11) }
#[inline(always)] pub const fn text_sm(self) -> Self { self.size(12) }
#[inline(always)] pub const fn text_base(self) -> Self { self.size(14) }
#[inline(always)] pub const fn text_lg(self) -> Self { self.size(16) }
#[inline(always)] pub const fn text_xl(self) -> Self { self.size(18) }
#[inline(always)] pub const fn text_2xl(self) -> Self { self.size(22) }
#[inline(always)] pub const fn text_3xl(self) -> Self { self.size(28) }
#[inline(always)]
pub const fn color(mut self, color: Color) -> Self {
self.color = color;
self.flags |= TEXT_FLAG_EXPLICIT_COLOR;
self
}
#[inline(always)]
pub const fn text_color(self, color: Color) -> Self {
self.color(color)
}
#[inline(always)]
pub const fn has_explicit_size(&self) -> bool {
(self.flags & TEXT_FLAG_EXPLICIT_SIZE) != 0
}
#[inline(always)]
pub const fn has_explicit_color(&self) -> bool {
(self.flags & TEXT_FLAG_EXPLICIT_COLOR) != 0
}
#[inline(always)]
pub const fn align(mut self, align: TextAlign) -> Self {
self.flags = (self.flags & !0x03) | (align as u8 & 0x03);
self
}
#[inline(always)]
pub const fn text_left(self) -> Self {
self.align(TextAlign::Left)
}
#[inline(always)]
pub const fn text_center(self) -> Self {
self.align(TextAlign::Center)
}
#[inline(always)]
pub const fn text_right(self) -> Self {
self.align(TextAlign::Right)
}
#[inline(always)]
pub fn text_start(self, cx: &FrameScope) -> Self {
if cx.is_rtl() {
self.align(TextAlign::Right)
} else {
self.align(TextAlign::Left)
}
}
#[inline(always)]
pub fn text_end(self, cx: &FrameScope) -> Self {
if cx.is_rtl() {
self.align(TextAlign::Left)
} else {
self.align(TextAlign::Right)
}
}
#[inline(always)]
pub const fn ellipsis(mut self) -> Self {
self.flags |= 0x04 | 0x08;
self
}
#[inline(always)]
pub const fn nowrap(mut self) -> Self {
self.flags |= 0x08;
self
}
#[inline(always)]
pub const fn text_align(&self) -> TextAlign {
match self.flags & 0x03 {
1 => TextAlign::Center,
2 => TextAlign::Right,
_ => TextAlign::Left,
}
}
#[inline(always)]
pub const fn is_ellipsis(&self) -> bool {
(self.flags & 0x04) != 0
}
#[inline(always)]
pub const fn is_nowrap(&self) -> bool {
(self.flags & 0x08) != 0
}
}