use crate::prelude::*; pub struct Aligned { pub inner: StrongWidget, pub align: Align, } impl Widget for Aligned { fn draw(&mut self, painter: &mut Painter) -> Size { let known = match self.align.tuple() { (Some(_), Some(_)) => painter .known_len(&self.inner, Axis::X, UiRegion::FULL) .zip(painter.known_len(&self.inner, Axis::Y, UiRegion::FULL)) .map(|(x, y)| Size { x, y }), (Some(_), None) => painter .known_len(&self.inner, Axis::X, UiRegion::FULL) .map(|x| Size { x, y: Len::LEFTOVER, }), (None, Some(_)) => painter .known_len(&self.inner, Axis::Y, UiRegion::FULL) .map(|y| Size { x: Len::LEFTOVER, y, }), (None, None) => Some(Size::LEFTOVER), }; // Drawn where it may be too big only when the aligned axes are not // already known, then given its aligned box once its size is known. let had_size = known.is_some(); let size = known.unwrap_or_else(|| painter.place(&self.inner, UiRegion::FULL).size()); let region = match self.align.tuple() { (Some(x), Some(y)) => size.to_uivec2().align(RegionAlign { x, y }), (Some(x), None) => UiRegion::new(size.x.apply_leftover().align(x), UiSpan::FULL), (None, Some(y)) => UiRegion::new(UiSpan::FULL, size.y.apply_leftover().align(y)), (None, None) => UiRegion::FULL, }; let placed = painter.place(&self.inner, region).size(); if had_size { placed } else { size } } /// The aligned box is a fraction of its own, so the child keeps its /// length and stays against the edge it was aligned to. fn on_resize(&self, _: Axis) -> OnResize { OnResize::Scale } }