sized widgets!

This commit is contained in:
2025-08-28 01:35:43 -04:00
parent d4d0b3b580
commit 834182ffe8
14 changed files with 313 additions and 106 deletions

View File

@@ -1,13 +1,17 @@
use cosmic_text::Metrics;
use crate::prelude::*;
pub struct Text {
pub content: String,
pub attrs: TextAttrs,
buf: TextBuffer,
}
impl Text {
pub fn size(mut self, size: impl UiNum) -> Self {
self.attrs.size = size.to_f32();
self.attrs.line_height = self.attrs.size * 1.1;
self
}
pub fn color(mut self, color: UiColor) -> Self {
@@ -21,16 +25,31 @@ impl Text {
}
impl Widget for Text {
fn draw(&self, painter: &mut Painter) {
fn draw(&mut self, painter: &mut Painter) {
let (handle, offset) = painter.render_text(&mut self.buf, &self.content, &self.attrs);
let dims = handle.size();
let size = offset.size(&handle);
let mut region = painter.region().center().expand(size);
region.top_left.offset += offset.top_left;
region.bot_right.offset = region.top_left.offset + dims;
painter.draw_texture_at(&handle, region);
// TODO: when on_update is added to painter,
// return & store TextureHandle to reuse
painter.draw_text(&self.content, &self.attrs);
// reuse TextureHandle
}
fn size(&mut self, ctx: SizeCtx) -> Vec2 {
let (handle, offset) =
ctx.text
.draw(&mut self.buf, &self.content, &self.attrs, ctx.textures);
offset.size(&handle)
}
}
pub fn text(text: impl Into<String>) -> Text {
let attrs = TextAttrs::default();
Text {
content: text.into(),
attrs: TextAttrs::default(),
buf: TextBuffer::new_empty(Metrics::new(attrs.size, attrs.line_height)),
attrs,
}
}