preload text by default

This commit is contained in:
2025-09-24 12:33:02 -04:00
parent 70d3027bfb
commit 2adf7a43a1

View File

@@ -81,6 +81,63 @@ impl Widget for Text {
} }
} }
pub fn text(content: impl Into<String>) -> Text { pub fn text(content: impl Into<String>) -> TextBuilder {
Text::new(content) TextBuilder {
content: content.into(),
align: Align::Center,
attrs: TextAttrs::default(),
}
}
pub struct TextBuilder {
pub content: String,
pub attrs: TextAttrs,
/// inner alignment of text region (within where its drawn)
pub align: Align,
}
impl TextBuilder {
pub fn font_size(mut self, size: impl UiNum) -> Self {
self.attrs.font_size = size.to_f32();
self.attrs.line_height = self.attrs.font_size * 1.1;
self
}
pub fn color(mut self, color: UiColor) -> Self {
self.attrs.color = color;
self
}
pub fn family(mut self, family: Family<'static>) -> Self {
self.attrs.family = family;
self
}
pub fn line_height(mut self, height: f32) -> Self {
self.attrs.line_height = height;
self
}
}
impl<Ctx> FnOnce<(&mut Ui<Ctx>,)> for TextBuilder {
type Output = Text;
extern "rust-call" fn call_once(self, args: (&mut Ui<Ctx>,)) -> Self::Output {
let mut buf =
TextBuffer::new_empty(Metrics::new(self.attrs.font_size, self.attrs.line_height));
buf.set_text(
&mut args.0.text.font_system,
&self.content,
&Attrs::new(),
Shaping::Advanced,
);
let mut text = Text {
content: self.content.into(),
buf,
attrs: self.attrs,
align: self.align,
size: Vec2::ZERO,
};
text.content.changed = false;
self.attrs
.apply(&mut args.0.text.font_system, &mut text.buf);
text
}
} }