proper auto sizing

This commit is contained in:
2026-02-01 17:25:49 +01:00
parent c4addd7f01
commit e9541c0d21
+50 -2
View File
@@ -20,8 +20,14 @@ pub fn size_auto(ctx: &mut DrawContext, _view: &View) {
for i in &ctx.drawing {
match i {
DrawInstruction::Text(_, text) => {
ctx.allocated.width = text.len() as u16;
ctx.allocated.height = 1;
let mut height = 0;
for line in text.lines() {
ctx.allocated.width = ctx.allocated.width.max(line.len() as u16);
height += 1;
}
ctx.allocated.height = height;
}
DrawInstruction::View(area, view) => {
let mut c = DrawContext::new(area.clone());
@@ -35,6 +41,48 @@ pub fn size_auto(ctx: &mut DrawContext, _view: &View) {
}
}
pub fn width_auto(ctx: &mut DrawContext, _view: &View) {
for i in &ctx.drawing {
match i {
DrawInstruction::Text(_, text) => {
for line in text.lines() {
ctx.allocated.width = ctx.allocated.width.max(line.len() as u16);
}
}
DrawInstruction::View(area, view) => {
let mut c = DrawContext::new(area.clone());
view(&mut c);
size_auto(&mut c, view);
ctx.allocated.width = c.allocated.width;
}
DrawInstruction::Child(_, _) => {}
}
}
}
pub fn height_auto(ctx: &mut DrawContext, _view: &View) {
for i in &ctx.drawing {
match i {
DrawInstruction::Text(_, text) => {
let mut height = 0;
for _ in text.lines() {
height += 1;
}
ctx.allocated.height = height;
}
DrawInstruction::View(area, view) => {
let mut c = DrawContext::new(area.clone());
view(&mut c);
size_auto(&mut c, view);
ctx.allocated.height = c.allocated.height;
}
DrawInstruction::Child(_, _) => {}
}
}
}
pub fn redraw(ctx: &mut DrawContext, view: &View) {
ctx.clear();
ctx.draw_view(ctx.allocated.clone(), view.clone());