mistakes were fixed and sins were committed

This commit is contained in:
2025-11-20 00:18:30 -05:00
parent db248de8f4
commit a952b34a72
18 changed files with 418 additions and 142 deletions

View File

@@ -10,8 +10,12 @@ impl Widget for Image {
painter.texture(&self.handle);
}
fn desired_size(&mut self, _: &mut SizeCtx) -> Size {
Size::abs(self.handle.size())
fn desired_width(&mut self, _: &mut SizeCtx) -> Len {
Len::abs(self.handle.size().x)
}
fn desired_height(&mut self, _: &mut SizeCtx) -> Len {
Len::abs(self.handle.size().y)
}
}

View File

@@ -9,4 +9,12 @@ impl Widget for Masked {
painter.set_mask(painter.region());
painter.widget(&self.inner);
}
fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
ctx.width(&self.inner)
}
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
ctx.height(&self.inner)
}
}

View File

@@ -7,11 +7,16 @@ pub struct Aligned {
impl Widget for Aligned {
fn draw(&mut self, painter: &mut Painter) {
let region = UiRegion::from_ui_size_align(painter.region_size(&self.inner), self.align);
let region =
UiRegion::from_ui_size_align(painter.size(&self.inner).to_uivec2(), self.align);
painter.widget_within(&self.inner, region);
}
fn desired_size(&mut self, ctx: &mut SizeCtx) -> Size {
ctx.size(&self.inner)
fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
ctx.width(&self.inner)
}
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
ctx.height(&self.inner)
}
}

View File

@@ -11,7 +11,11 @@ impl Widget for Offset {
painter.widget_within(&self.inner, region);
}
fn desired_size(&mut self, ctx: &mut SizeCtx) -> Size {
ctx.size(&self.inner)
fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
ctx.width(&self.inner)
}
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
ctx.height(&self.inner)
}
}

View File

@@ -10,14 +10,23 @@ impl Widget for Pad {
painter.widget_within(&self.inner, self.padding.region());
}
fn desired_size(&mut self, ctx: &mut SizeCtx) -> Size {
let mut size = ctx.size(&self.inner);
if size.x.rest == 0.0 {
size.x.abs += self.padding.left + self.padding.right;
}
if size.y.rest == 0.0 {
size.y.abs += self.padding.top + self.padding.bottom;
}
fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
let width = self.padding.left + self.padding.right;
let height = self.padding.top + self.padding.bottom;
ctx.outer.abs.x -= width;
ctx.outer.abs.y -= height;
let mut size = ctx.width(&self.inner);
size.abs += width;
size
}
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
let width = self.padding.left + self.padding.right;
let height = self.padding.top + self.padding.bottom;
ctx.outer.abs.x -= width;
ctx.outer.abs.y -= height;
let mut size = ctx.height(&self.inner);
size.abs += height;
size
}
}

View File

@@ -6,22 +6,35 @@ pub struct Sized {
pub y: Option<Len>,
}
impl Sized {
fn apply_to_outer(&self, ctx: &mut SizeCtx) {
if let Some(x) = self.x {
let outer = ctx.outer.axis(Axis::X);
ctx.outer
.axis_mut(Axis::X)
.set(x.apply_rest().within_len(outer));
}
if let Some(y) = self.y {
let outer = ctx.outer.axis(Axis::Y);
ctx.outer
.axis_mut(Axis::Y)
.set(y.apply_rest().within_len(outer));
}
}
}
impl Widget for Sized {
fn draw(&mut self, painter: &mut Painter) {
painter.widget(&self.inner);
}
fn desired_size(&mut self, ctx: &mut SizeCtx) -> Size {
let rel = ctx.size.rel;
if let Some(x) = self.x {
ctx.size.axis_mut(Axis::X).set(x.apply_rest(rel.x));
}
if let Some(y) = self.y {
ctx.size.axis_mut(Axis::Y).set(y.apply_rest(rel.y));
}
Size {
x: self.x.unwrap_or_else(|| ctx.size(&self.inner).x),
y: self.y.unwrap_or_else(|| ctx.size(&self.inner).y),
}
fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
self.apply_to_outer(ctx);
self.x.unwrap_or_else(|| ctx.width(&self.inner))
}
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
self.apply_to_outer(ctx);
self.y.unwrap_or_else(|| ctx.height(&self.inner))
}
}

View File

@@ -16,7 +16,7 @@ impl Widget for Span {
let mut child_region = UiRegion::full();
let mut axis = child_region.axis_mut(self.dir.axis);
axis.top_left.set(start);
let len = painter.size(child).axis(self.dir.axis);
let len = painter.len_axis(child, self.dir.axis);
if len.rest > 0.0 {
let offset = UiScalar::new(total.rel, total.abs);
let rel_end = UiScalar::from_anchor(len.rest / total.rest);
@@ -33,25 +33,18 @@ impl Widget for Span {
}
}
fn desired_size(&mut self, ctx: &mut SizeCtx) -> Size {
let sums = self.len_sum(ctx);
let dir_len = if sums.rest == 0.0 && sums.rel == 0.0 {
sums
} else {
Len::default()
};
let mut max_ortho = Len::ZERO;
for child in &self.children {
let len = ctx.size(child).axis(!self.dir.axis);
// TODO: rel shouldn't do this, but no easy way before actually calculating pixels
if len.rel > 0.0 || len.rest > 0.0 {
max_ortho.rest = 1.0;
max_ortho.abs = 0.0;
break;
}
max_ortho.abs = max_ortho.abs.max(len.abs);
fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
match self.dir.axis {
Axis::X => self.desired_len(ctx),
Axis::Y => self.desired_ortho(ctx),
}
}
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
match self.dir.axis {
Axis::X => self.desired_ortho(ctx),
Axis::Y => self.desired_len(ctx),
}
Size::from_axis(self.dir.axis, dir_len, max_ortho)
}
}
@@ -71,11 +64,85 @@ impl Span {
fn len_sum(&mut self, ctx: &mut SizeCtx) -> Len {
let gap = self.gap * self.children.len().saturating_sub(1) as f32;
self.children.iter_mut().fold(Len::abs(gap), |mut s, id| {
s += ctx.size(id).axis(self.dir.axis);
self.children.iter().fold(Len::abs(gap), |mut s, id| {
// it's tempting to subtract the abs & rel from the ctx outer,
// but that would create inconsistent sizing if you put
// a rest first vs last & only speed up in one direction.
// I think this is only solvable by restricting how you can
// compute size, bc currently you need child to define parent's
// sectioning and you need parent's sectioning to define child.
// Fortunately, that doesn't matter in most cases
let len = ctx.len_axis(id, self.dir.axis);
s += len;
s
})
}
fn desired_len(&mut self, ctx: &mut SizeCtx) -> Len {
let len = self.len_sum(ctx);
if len.rest == 0.0 && len.rel == 0.0 {
len
} else {
Len::default()
}
}
fn desired_ortho(&mut self, ctx: &mut SizeCtx) -> Len {
// this is an awful hack to get text wrapping to work properly when in a downward span
if self.dir.axis == Axis::X {
// so....... this literally copies draw so that the lengths are correctly set in the
// context, which makes this slow and not cool
let total = self.len_sum(ctx);
let mut start = UiScalar::rel_min();
let outer = ctx.outer.axis(self.dir.axis);
let mut ortho_len = Len::ZERO;
for child in &self.children {
let mut child_region = UiRegion::full();
let mut axis = child_region.axis_mut(self.dir.axis);
axis.top_left.set(start);
let len = ctx.len_axis(child, self.dir.axis);
if len.rest > 0.0 {
let offset = UiScalar::new(total.rel, total.abs);
let rel_end = UiScalar::from_anchor(len.rest / total.rest);
start = rel_end.within(start, (UiScalar::rel_max() + start) - offset);
}
start.abs += len.abs;
start.rel += len.rel;
axis.bot_right.set(start);
// if self.dir.sign == Sign::Neg {
// child_region.flip(self.dir.axis);
// }
let scalar = child_region.size().axis(self.dir.axis);
ctx.outer
.axis_mut(self.dir.axis)
.set(scalar.within_len(outer));
let ortho = ctx.len_axis(child, !self.dir.axis);
// TODO: rel shouldn't do this, but no easy way before actually calculating pixels
if ortho.rel > 0.0 || ortho.rest > 0.0 {
ortho_len.rest = 1.0;
ortho_len.abs = 0.0;
break;
}
ortho_len.abs = ortho_len.abs.max(ortho.abs);
start.abs += self.gap;
}
ortho_len
} else {
let mut ortho_len = Len::ZERO;
let ortho = !self.dir.axis;
for child in &self.children {
let len = ctx.len_axis(child, ortho);
// TODO: rel shouldn't do this, but no easy way before actually calculating pixels
if len.rel > 0.0 || len.rest > 0.0 {
ortho_len.rest = 1.0;
ortho_len.abs = 0.0;
break;
}
ortho_len.abs = ortho_len.abs.max(len.abs);
}
ortho_len
}
}
}
pub struct SpanBuilder<const LEN: usize, Wa: WidgetArrLike<LEN, Tag>, Tag> {

View File

@@ -24,10 +24,17 @@ impl Widget for Stack {
}
}
fn desired_size(&mut self, ctx: &mut SizeCtx) -> Size {
fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
match self.size {
StackSize::Default => Size::default(),
StackSize::Child(i) => ctx.size(&self.children[i]),
StackSize::Default => Len::default(),
StackSize::Child(i) => ctx.width(&self.children[i]),
}
}
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
match self.size {
StackSize::Default => Len::default(),
StackSize::Child(i) => ctx.height(&self.children[i]),
}
}
}

View File

@@ -12,11 +12,19 @@ impl Widget for WidgetPtr {
}
}
fn desired_size(&mut self, ctx: &mut SizeCtx) -> Size {
fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
if let Some(id) = &self.inner {
ctx.size(id)
ctx.width(id)
} else {
Size::ZERO
Len::ZERO
}
}
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
if let Some(id) = &self.inner {
ctx.height(id)
} else {
Len::ZERO
}
}
}

View File

@@ -36,6 +36,14 @@ impl Widget for Rect {
inner_radius: self.inner_radius,
});
}
fn desired_width(&mut self, _: &mut SizeCtx) -> Len {
Len::rest(1)
}
fn desired_height(&mut self, _: &mut SizeCtx) -> Len {
Len::rest(1)
}
}
pub fn rect(color: UiColor) -> Rect {

View File

@@ -51,8 +51,17 @@ impl Widget for TextEdit {
}
}
fn desired_size(&mut self, ctx: &mut SizeCtx) -> Size {
Size::abs(self.view.draw(ctx).size())
fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
Len::abs(self.view.draw(ctx).size().x)
}
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
let res = Len::abs(self.view.draw(ctx).size().y);
if (res.abs - 639.0).abs() < 0.5 && ctx.label(ctx.id()) == "WHAT" {
let source = ctx.label(ctx.source());
println!("{source} => {:?}: [{}] -> {}", ctx.id(), ctx.outer, res.abs);
}
res
}
}

View File

@@ -91,8 +91,12 @@ impl Widget for Text {
painter.texture_within(&tex.handle, region);
}
fn desired_size(&mut self, ctx: &mut SizeCtx) -> Size {
Size::abs(self.update_buf(ctx).size())
fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
Len::abs(self.update_buf(ctx).size().x)
}
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
Len::abs(self.update_buf(ctx).size().y)
}
}