sized widgets!

This commit is contained in:
iris committed 2025-08-28 01:35:43 -04:00
1 parent d4d0b3b580
commit 834182ffe8
14 files changed
+313 -106

No files matched your search

+35 -14
View File
@@ -6,8 +6,8 @@ pub struct Span {
}
impl Widget for Span {
fn draw(&self, painter: &mut Painter) {
let total = self.sums();
fn draw(&mut self, painter: &mut Painter) {
let total = self.setup(painter);
let mut start = UIScalar::min();
for (child, length) in &self.children {
let mut child_region = UiRegion::full();
@@ -29,6 +29,14 @@ impl Widget for Span {
start.anchor += rel;
axis.bot_right.set(start);
}
SpanLen::Sized(size) => {
let offset = size.axis(self.dir.axis);
start.offset += offset;
*axis.bot_right.offset = start.offset;
child_region.bot_right.anchor = child_region.top_left.anchor;
let opposite = !self.dir.axis;
*child_region.bot_right.offset.axis_mut(opposite) += size.axis(opposite);
}
}
if self.dir.sign == Sign::Neg {
child_region.flip();
@@ -53,18 +61,23 @@ impl Span {
}
}
pub fn sums(&self) -> SpanLenSums {
self.lengths().fold(SpanLenSums::default(), |mut s, l| {
match l {
SpanLen::Fixed(v) => s.fixed += v,
SpanLen::Ratio(v) => s.ratio += v,
SpanLen::Relative(v) => s.relative += v,
}
s
})
}
pub fn lengths(&self) -> impl ExactSizeIterator<Item = &SpanLen> {
self.children.iter().map(|(_, s)| s)
fn setup(&mut self, painter: &mut Painter) -> SpanLenSums {
self.children
.iter_mut()
.fold(SpanLenSums::default(), |mut s, (id, l)| {
match l {
SpanLen::Fixed(v) => s.fixed += *v,
SpanLen::Ratio(v) => s.ratio += *v,
SpanLen::Relative(v) => s.relative += *v,
SpanLen::Sized(v) => {
let size = painter.size(id);
let len = size.axis(self.dir.axis);
*v = size;
s.fixed += len;
}
}
s
})
}
}
@@ -78,6 +91,10 @@ pub enum SpanLen {
/// relative to the total space (of the entire span)
/// eg. 0.5 means 1/2 of the total space
Relative(f32),
/// 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),
}
pub fn fixed<N: UiNum>(x: N) -> SpanLen {
@@ -92,6 +109,10 @@ pub fn relative<N: UiNum>(x: N) -> SpanLen {
SpanLen::Relative(x.to_f32())
}
pub fn sized() -> SpanLen {
SpanLen::Sized(Vec2::ZERO)
}
impl<N: UiNum> From<N> for SpanLen {
fn from(value: N) -> Self {
Self::Ratio(value.to_f32())