use super::{Theme, TypographyKind, TypographyStyleKind};
use crate::{
ecs::{spawn, Modifier, Modify},
prelude::Compose,
use_context,
};
use actuate_macros::Data;
use bevy_text::{TextColor, TextFont};
use bevy_ui::prelude::Text as UiText;
pub fn body<'a>(content: impl Into<String>) -> Text<'a> {
text(content).typography(TypographyKind::Body)
}
pub fn headline<'a>(content: impl Into<String>) -> Text<'a> {
text(content).typography(TypographyKind::Headline)
}
pub fn label<'a>(content: impl Into<String>) -> Text<'a> {
text(content).typography(TypographyKind::Label)
}
pub fn title<'a>(content: impl Into<String>) -> Text<'a> {
text(content).typography(TypographyKind::Title)
}
pub fn text<'a>(content: impl Into<String>) -> Text<'a> {
Text {
content: content.into(),
modifier: Modifier::default(),
typography: TypographyKind::Label,
typography_style: TypographyStyleKind::Medium,
}
}
#[derive(Data)]
#[actuate(path = "crate")]
pub struct Text<'a> {
content: String,
typography: TypographyKind,
typography_style: TypographyStyleKind,
modifier: Modifier<'a>,
}
impl Text<'_> {
pub fn typography(mut self, typography: TypographyKind) -> Self {
self.typography = typography;
self
}
pub fn typography_style(mut self, typography_style: TypographyStyleKind) -> Self {
self.typography_style = typography_style;
self
}
}
impl Compose for Text<'_> {
fn compose(cx: crate::Scope<Self>) -> impl Compose {
let theme = use_context::<Theme>(&cx).cloned().unwrap_or_default();
let style = &theme.typography[cx.me().typography][cx.me().typography_style];
spawn((
UiText::new(cx.me().content.clone()),
TextColor(theme.colors.text),
TextFont {
font_size: style.font_size,
..Default::default()
},
))
}
}
impl<'a> Modify<'a> for Text<'a> {
fn modifier(&mut self) -> &mut Modifier<'a> {
&mut self.modifier
}
}