diff --git a/src/core/text.rs b/src/core/text.rs index ba1bb69..093875b 100644 --- a/src/core/text.rs +++ b/src/core/text.rs @@ -81,6 +81,63 @@ impl Widget for Text { } } -pub fn text(content: impl Into) -> Text { - Text::new(content) +pub fn text(content: impl Into) -> TextBuilder { + 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 FnOnce<(&mut Ui,)> for TextBuilder { + type Output = Text; + + extern "rust-call" fn call_once(self, args: (&mut Ui,)) -> 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 + } }