sizing actually working correctly now

This commit is contained in:
iris committed 2025-09-16 17:31:54 -04:00
1 parent b48acccb8d
commit f9097807a2
17 files changed
+333 -194

No files matched your search

+27 -27
View File
@@ -8,34 +8,30 @@ pub struct Span {
impl Widget for Span {
fn draw(&mut self, painter: &mut Painter) {
let total = self.setup(&mut painter.size_ctx());
let mut start = UIScalar::min();
let mut start = UiScalar::rel_min();
for (child, length) in &self.children {
let mut child_region = UiRegion::full();
let mut axis = child_region.axis_mut(self.dir.axis);
axis.top_left.set(start);
match *length {
SpanLen::Fixed(offset) => {
start.offset += offset;
*axis.bot_right.offset = start.offset;
*axis.bot_right.anchor = *axis.top_left.anchor;
start.abs += offset;
}
SpanLen::Ratio(ratio) => {
let offset = UIScalar::new(total.relative, total.fixed);
let rel_end = UIScalar::from_anchor(ratio / total.ratio);
start = rel_end.within(start, (UIScalar::max() + start) - offset);
axis.bot_right.set(start);
let offset = UiScalar::new(total.rel, total.abs);
let rel_end = UiScalar::from_anchor(ratio / total.ratio);
start = rel_end.within(start, (UiScalar::rel_max() + start) - offset);
}
SpanLen::Relative(rel) => {
start.anchor += rel;
axis.bot_right.set(start);
start.rel += rel;
}
SpanLen::Sized(size) => {
let offset = size.axis(self.dir.axis);
start.offset += offset;
*axis.bot_right.offset = start.offset;
*axis.bot_right.anchor = *axis.top_left.anchor;
let size_axis = size.axis(self.dir.axis);
start.abs += size_axis.abs;
start.rel += size_axis.rel;
}
}
axis.bot_right.set(start);
if self.dir.sign == Sign::Neg {
child_region.flip();
}
@@ -43,24 +39,28 @@ impl Widget for Span {
}
}
fn get_size(&mut self, ctx: &mut SizeCtx) -> Vec2 {
fn desired_size(&mut self, ctx: &mut SizeCtx) -> UiVec2 {
let total = self.setup(ctx);
let axis = self.dir.axis;
let dir_len = if total.ratio != 0.0 {
ctx.size.axis(axis)
UiScalar::rel_max()
} else {
total.fixed + total.relative * ctx.size.axis(axis)
UiScalar::new(total.rel, total.abs)
};
Vec2::from_axis(axis, dir_len, total.max_sized)
let mut max_ortho = UiScalar::default();
for (child, _) in &self.children {
let size = ctx.size(child);
max_ortho = max_ortho.max(size.axis(!self.dir.axis));
}
UiVec2::from_axis(axis, dir_len, max_ortho)
}
}
#[derive(Default)]
pub struct SpanLenSums {
pub fixed: f32,
pub abs: f32,
pub ratio: f32,
pub relative: f32,
pub max_sized: f32,
pub rel: f32,
}
impl Span {
@@ -76,15 +76,15 @@ impl Span {
.iter_mut()
.fold(SpanLenSums::default(), |mut s, (id, l)| {
match l {
SpanLen::Fixed(v) => s.fixed += *v,
SpanLen::Fixed(v) => s.abs += *v,
SpanLen::Ratio(v) => s.ratio += *v,
SpanLen::Relative(v) => s.relative += *v,
SpanLen::Relative(v) => s.rel += *v,
SpanLen::Sized(v) => {
let size = ctx.size(id);
let len = size.axis(self.dir.axis);
s.max_sized = s.max_sized.max(size.axis(!self.dir.axis));
*v = size;
s.fixed += len;
s.abs += len.abs;
s.rel += len.rel;
}
}
s
@@ -105,7 +105,7 @@ pub enum SpanLen {
/// size determined by the child widget itself
/// the value is not used externally, I just don't wanna make a duplicate enum
/// there are util functions instead so
Sized(Vec2),
Sized(UiVec2),
}
pub fn fixed<N: UiNum>(x: N) -> SpanLen {
@@ -121,7 +121,7 @@ pub fn relative<N: UiNum>(x: N) -> SpanLen {
}
pub fn sized() -> SpanLen {
SpanLen::Sized(Vec2::ZERO)
SpanLen::Sized(UiVec2::default())
}
impl<N: UiNum> From<N> for SpanLen {