use crate::{context::Paint, Color};
use super::{font_size, Response, Ui, Widget};
pub struct Text {
text: String,
size: Option<f32>,
weight: Option<crate::FontWeight>,
color: Option<Color>,
wrap: bool,
family: Option<crate::TextFamily>,
tabular: Option<bool>,
tab: Option<u16>,
muted: bool,
style: crate::TextStyle,
role: crate::TypographyRole,
}
impl Text {
pub fn new(text: impl Into<String>) -> Self {
Self {
text: text.into(),
size: None,
weight: None,
color: None,
wrap: true,
family: None,
tabular: None,
tab: None,
muted: false,
style: Default::default(),
role: Default::default(),
}
}
pub fn style(mut self, style: crate::TextStyle) -> Self {
self.style = style;
self
}
pub fn typography(mut self, role: crate::TypographyRole) -> Self {
self.role = role;
self
}
#[track_caller]
pub fn size(mut self, size: f32) -> Self {
self.size = super::sanitize::positive("Text::size", size).or(self.size);
self
}
pub fn weight(mut self, weight: crate::FontWeight) -> Self {
self.weight = Some(weight);
self
}
pub fn monospace(self) -> Self {
self.family(crate::TextFamily::Monospace)
}
pub fn family(mut self, family: crate::TextFamily) -> Self {
self.family = Some(family);
self
}
pub fn tabular_numbers(mut self, tabular: bool) -> Self {
self.tabular = Some(tabular);
self
}
pub fn tab_size(mut self, cells: u16) -> Self {
self.tab = Some(cells.max(1));
self
}
pub fn muted(mut self) -> Self {
self.muted = true;
self
}
pub fn color(mut self, color: Color) -> Self {
self.color = Some(color);
self
}
pub fn wrap(mut self, wrap: bool) -> Self {
self.wrap = wrap;
self
}
}
impl Widget for Text {
fn ui(self, ui: &mut Ui<'_>) -> Response {
let id = ui.next_id("text");
let mut style = ui.style().text;
style.merge(self.style);
let inherited = match self.role {
crate::TypographyRole::Body => ui.style().font_size,
role => ui.style().typography.size(role),
};
let size = font_size(self.size.or(style.size).unwrap_or(inherited));
let weight = self
.weight
.or(style.weight)
.unwrap_or_else(|| ui.style().typography.weights.role(self.role));
let family = self.family.or(style.family).unwrap_or(match self.role {
crate::TypographyRole::Code => crate::TextFamily::Monospace,
_ => crate::TextFamily::Proportional,
});
let font = crate::text::TextFont::new(
weight,
family,
self.tabular.or(style.tabular_numbers).unwrap_or(false),
);
let tab = self.tab.unwrap_or(crate::text::DEFAULT_TAB);
let color = self.color.or(style.color).unwrap_or(if !ui.is_enabled() {
ui.style().disabled_text
} else if self.muted {
style.muted.unwrap_or(ui.style().muted_text)
} else {
ui.style().text_color
});
let wrap_width = if self.wrap {
ui.available_width()
} else {
f32::INFINITY
};
let (text_size, wrap_width) = ui
.context
.measure_text_layout_tab(&self.text, size, font, wrap_width, tab);
let rect = ui.allocate_space(text_size);
if ui.context.a11y_on() {
let layout = ui
.context
.paragraph_layout(&self.text, size, font, wrap_width, tab);
let lines = ui.context.a11y_text(&self.text, &layout);
let (role, level) = match self.role {
crate::TypographyRole::Title => (crate::AccessRole::Heading, Some(1)),
crate::TypographyRole::Heading => (crate::AccessRole::Heading, Some(2)),
_ => (crate::AccessRole::Label, None),
};
ui.a11y(id, rect, role, |node| {
node.value(self.text.as_str()).text(lines, None);
if let Some(level) = level {
node.level(level);
}
});
}
ui.context.paint(
id,
ui.window,
ui.clip,
vec![Paint::text(
self.text,
rect.min,
size,
font,
wrap_width,
tab,
super::appearance::alpha(color, ui.style().opacity),
)],
);
ui.response(id, rect, false)
}
}
impl Ui<'_> {
pub fn typography(&mut self, role: crate::TypographyRole, text: impl Into<String>) -> Response {
self.add(Text::new(text).typography(role))
}
pub fn code(&mut self, text: impl Into<String>) -> Response {
self.add(Text::new(text).typography(crate::TypographyRole::Code))
}
pub fn monospace_metrics(&mut self) -> crate::MonospaceMetrics {
let typography = self.style().typography;
self.context
.monospace_metrics(typography.code, typography.weights.code)
}
pub fn heading(&mut self, text: impl Into<String>) -> Response {
self.typography(crate::TypographyRole::Heading, text)
}
pub fn title(&mut self, text: impl Into<String>) -> Response {
self.typography(crate::TypographyRole::Title, text)
}
pub fn small(&mut self, text: impl Into<String>) -> Response {
self.typography(crate::TypographyRole::Small, text)
}
pub fn muted(&mut self, text: impl Into<String>) -> Response {
self.add(Text::new(text).muted())
}
pub fn label(&mut self, text: impl Into<String>) -> Response {
self.add(Text::new(text))
}
}