use crate::prelude::*; pub struct MaxSize { pub inner: StrongWidget, pub x: Option, pub y: Option, } impl MaxSize { fn clamp(len: LayoutLen, max: Option, output: f32, density: f32) -> LayoutLen { let Some(max) = max else { return len; }; let len_px = len.apply_rest(density).to_abs(output); let max_px = max.apply_rest(density).to_abs(output); if len_px > max_px { max.fold_dp(density) } else { len } } fn clamp_region(offered_px: f32, max: Option, output: f32, density: f32) -> UiSpan { let Some(max) = max else { return UiSpan::FULL; }; let max_scalar = max.apply_rest(density); let max_px = max_scalar.to_abs(output); if offered_px > max_px { max_scalar.align(AxisAlign::Neg) } else { UiSpan::FULL } } } impl Widget for MaxSize { fn draw(&mut self, painter: &mut Painter) { let output = painter.output_size(); let density = painter.density(); let offered = painter.px_size(); let region = UiRegion { x: Self::clamp_region(offered.x, self.x, output.x, density), y: Self::clamp_region(offered.y, self.y, output.y, density), }; let used = painter.widget_within(&self.inner, region).size(); let size = Size { x: Self::clamp(used.x, self.x, output.x, density), y: Self::clamp(used.y, self.y, output.y, density), }; painter.place_used(&self.inner, size, UiRegion::FULL); painter.set_size(size); } }