max size + better scrolling size fn

This commit is contained in:
2025-11-21 02:44:59 -05:00
parent 172e7157be
commit 5785352ac0
7 changed files with 89 additions and 11 deletions

View File

@@ -0,0 +1,48 @@
use crate::prelude::*;
pub struct MaxSize {
pub inner: WidgetId,
pub x: Option<Len>,
pub y: Option<Len>,
}
impl MaxSize {
fn apply_to_outer(&self, ctx: &mut SizeCtx) {
if let Some(x) = self.x {
ctx.outer.x.select_len(x.apply_rest());
}
if let Some(y) = self.y {
ctx.outer.y.select_len(y.apply_rest());
}
}
}
impl Widget for MaxSize {
fn draw(&mut self, painter: &mut Painter) {
painter.widget(&self.inner);
}
fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
self.apply_to_outer(ctx);
let width = ctx.width(&self.inner);
if let Some(x) = self.x {
let width_px = width.apply_rest().to_abs(ctx.output_size().x);
let x_px = x.apply_rest().to_abs(ctx.output_size().x);
if width_px > x_px { x } else { width }
} else {
width
}
}
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
self.apply_to_outer(ctx);
let height = ctx.height(&self.inner);
if let Some(y) = self.y {
let height_px = height.apply_rest().to_abs(ctx.output_size().y);
let y_px = y.apply_rest().to_abs(ctx.output_size().y);
if height_px > y_px { y } else { height }
} else {
height
}
}
}

View File

@@ -1,4 +1,5 @@
mod align;
mod max_size;
mod offset;
mod pad;
mod scroll;
@@ -7,6 +8,7 @@ mod span;
mod stack;
pub use align::*;
pub use max_size::*;
pub use offset::*;
pub use pad::*;
pub use scroll::*;

View File

@@ -28,12 +28,12 @@ impl Widget for Scroll {
painter.widget_within(&self.inner, region);
}
fn desired_width(&mut self, _: &mut SizeCtx) -> Len {
Len::default()
fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
ctx.width(&self.inner)
}
fn desired_height(&mut self, _: &mut SizeCtx) -> Len {
Len::default()
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
ctx.height(&self.inner)
}
}