From db248de8f487007afa07f856ef1a5c6fa532c41a Mon Sep 17 00:00:00 2001 From: shadow cat Date: Tue, 18 Nov 2025 17:52:45 -0500 Subject: [PATCH] fix span sizing (still some layout tho) --- src/core/position/span.rs | 6 +++--- src/core/sense.rs | 2 +- src/layout/painter.rs | 10 +++++++--- src/layout/pos.rs | 9 ++++++++- src/layout/ui.rs | 6 +++++- 5 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/core/position/span.rs b/src/core/position/span.rs index d963948..d876467 100644 --- a/src/core/position/span.rs +++ b/src/core/position/span.rs @@ -34,9 +34,8 @@ impl Widget for Span { } fn desired_size(&mut self, ctx: &mut SizeCtx) -> Size { - let mut sums = self.len_sum(ctx); + let sums = self.len_sum(ctx); let dir_len = if sums.rest == 0.0 && sums.rel == 0.0 { - sums.abs += self.gap * self.children.len().saturating_sub(1) as f32; sums } else { Len::default() @@ -71,7 +70,8 @@ impl Span { } fn len_sum(&mut self, ctx: &mut SizeCtx) -> Len { - self.children.iter_mut().fold(Len::ZERO, |mut s, id| { + 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); s }) diff --git a/src/core/sense.rs b/src/core/sense.rs index 32285b0..9e7bbc4 100644 --- a/src/core/sense.rs +++ b/src/core/sense.rs @@ -179,7 +179,7 @@ impl CursorModule { let mut sensed = false; for (id, shape) in list.iter() { let group = module.map.get_mut(id).unwrap(); - let region = shape.to_screen(window_size); + let region = shape.to_px(window_size); let in_shape = cursor.exists && region.contains(cursor.pos); group.hover.update(in_shape); if group.hover == ActivationState::Off { diff --git a/src/layout/painter.rs b/src/layout/painter.rs index 5d9a9d3..dbd9dd2 100644 --- a/src/layout/painter.rs +++ b/src/layout/painter.rs @@ -387,6 +387,10 @@ impl<'a, 'c> Painter<'a, 'c> { pub fn next_layer(&mut self) { self.layer = self.ctx.layers.next(self.layer); } + + pub fn label(&self) -> &str { + &self.ctx.widgets.data(&self.id).unwrap().label + } } pub struct SizeCtx<'a> { @@ -411,9 +415,9 @@ impl SizeCtx<'_> { size } pub fn size(&mut self, id: &WidgetId) -> Size { - if let Some(&size) = self.checked.get(&id.id) { - return size; - } + // if let Some(&size) = self.checked.get(&id.id) { + // return size; + // } self.size_inner(id.id, self.size) } fn size_raw(&mut self, id: Id) -> Size { diff --git a/src/layout/pos.rs b/src/layout/pos.rs index 30287bd..6ad4aaf 100644 --- a/src/layout/pos.rs +++ b/src/layout/pos.rs @@ -168,6 +168,7 @@ impl_op!(UiScalar Sub sub; rel abs); impl UiScalar { pub const ZERO: Self = Self { rel: 0.0, abs: 0.0 }; + pub const FULL: Self = Self { rel: 1.0, abs: 0.0 }; pub fn new(rel: f32, abs: f32) -> Self { Self { rel, abs } @@ -272,7 +273,7 @@ impl UiRegion { self } - pub fn to_screen(&self, size: Vec2) -> PixelRegion { + pub fn to_px(&self, size: Vec2) -> PixelRegion { PixelRegion { top_left: self.top_left.rel * size + self.top_left.abs, bot_right: self.bot_right.rel * size + self.bot_right.abs, @@ -347,6 +348,12 @@ impl PixelRegion { } } +impl Display for PixelRegion { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write!(f, "{} -> {}", self.top_left, self.bot_right) + } +} + pub struct UIRegionAxisView<'a> { pub top_left: UiScalarView<'a>, pub bot_right: UiScalarView<'a>, diff --git a/src/layout/ui.rs b/src/layout/ui.rs index dc9cdbe..86d4acb 100644 --- a/src/layout/ui.rs +++ b/src/layout/ui.rs @@ -181,7 +181,7 @@ impl Ui { pub fn window_region(&self, id: &impl IdLike) -> Option { let region = self.data.active.get(&id.id())?.region; - Some(region.to_screen(self.data.output_size)) + Some(region.to_px(self.data.output_size)) } pub fn debug(&self, label: &str) { @@ -192,6 +192,10 @@ impl Ui { } println!("\"{label}\" {{"); println!(" region: {}", inst.region); + println!( + " pixel region: {}", + inst.region.to_px(self.data.output_size) + ); println!(" desired_size: {}", inst.desired_size); println!("}}"); }