sizing actually working correctly now

This commit is contained in:
2025-09-16 17:31:54 -04:00
parent b48acccb8d
commit f9097807a2
17 changed files with 333 additions and 194 deletions

View File

@@ -24,16 +24,17 @@ impl TextEdit {
impl Widget for TextEdit {
fn draw(&mut self, painter: &mut Painter) {
let font_system = &mut painter.text_data().font_system;
self.buf.shape_until_scroll(font_system, false);
self.attrs.apply(font_system, &mut self.buf);
self.buf.shape_until_scroll(font_system, false);
let (handle, tex_offset) = painter.render_text(&mut self.buf, &self.attrs);
let dims = handle.size();
self.size = tex_offset.size(&handle);
let region = self.region();
let mut tex_region = region;
tex_region.top_left.offset += tex_offset.top_left;
tex_region.bot_right.offset = tex_region.top_left.offset + dims;
tex_region.top_left.abs += tex_offset.top_left;
tex_region.bot_right.abs = tex_region.top_left.abs + dims;
painter.texture_within(&handle, tex_region);
if let Some(cursor) = &self.cursor
&& let Some(pos) = cursor_pos(cursor, &self.buf)
{
@@ -45,12 +46,15 @@ impl Widget for TextEdit {
.shifted(offset)
.within(&region),
);
} else {
// keep number of primitives constant so shifting isn't needed
painter.primitive(RectPrimitive::color(Color::NONE));
}
}
fn get_size(&mut self, ctx: &mut SizeCtx) -> Vec2 {
fn desired_size(&mut self, ctx: &mut SizeCtx) -> UiVec2 {
let (handle, offset) = ctx.draw_text(&mut self.buf, &self.attrs);
offset.size(&handle)
UiVec2::abs(offset.size(&handle))
}
}
@@ -105,6 +109,10 @@ impl TextEditBuilder {
self.attrs.line_height = height;
self
}
pub fn text_align(mut self, align: Align) -> Self {
self.align = align;
self
}
}
pub struct TextEditCtx<'a> {
@@ -113,6 +121,21 @@ pub struct TextEditCtx<'a> {
}
impl<'a> TextEditCtx<'a> {
pub fn take(&mut self) -> String {
let text = self
.text
.buf
.lines
.drain(..)
.map(|l| l.into_text())
.collect::<Vec<_>>()
.join("\n");
self.text
.buf
.set_text(self.font_system, "", &Attrs::new(), Shaping::Advanced);
text
}
pub fn motion(&mut self, motion: Motion) {
if let Some(cursor) = self.text.cursor
&& let Some((cursor, _)) =