diff --git a/emigui/src/containers/collapsing_header.rs b/emigui/src/containers/collapsing_header.rs index 5c7c49a8..91eb8914 100644 --- a/emigui/src/containers/collapsing_header.rs +++ b/emigui/src/containers/collapsing_header.rs @@ -120,9 +120,11 @@ impl CollapsingHeader { let top_left = child_region.top_left(); add_contents(child_region); - // Pretend children took up less space than they did: - child_region.child_bounds.max.y = - child_region.child_bounds.max.y.min(top_left.y + max_height); + + // Pretend children took up less space: + let mut child_bounds = child_region.child_bounds(); + child_bounds.max.y = child_bounds.max.y.min(top_left.y + max_height); + child_region.force_set_child_bounds(child_bounds); }); } else if state.open { region.indent(id, add_contents); diff --git a/emigui/src/containers/frame.rs b/emigui/src/containers/frame.rs index 19ed27b3..e4353f7b 100644 --- a/emigui/src/containers/frame.rs +++ b/emigui/src/containers/frame.rs @@ -35,9 +35,7 @@ impl Frame { }, ); - // TODO: move up corsor? - region - .child_bounds - .extend_with(child_region.child_bounds.max + margin); + region.expand_to_include_child(child_region.rect().expand2(margin)); + // TODO: move up cursor? } } diff --git a/emigui/src/math.rs b/emigui/src/math.rs index 468c8784..5ea03ce7 100644 --- a/emigui/src/math.rs +++ b/emigui/src/math.rs @@ -358,12 +358,18 @@ impl Rect { } } - /// Expand by this much in each direction + /// Expand by this much in each direction, keeping the center #[must_use] pub fn expand(self, amnt: f32) -> Self { Rect::from_center_size(self.center(), self.size() + 2.0 * vec2(amnt, amnt)) } + /// Expand by this much in each direction, keeping the center + #[must_use] + pub fn expand2(self, amnt: Vec2) -> Self { + Rect::from_center_size(self.center(), self.size() + 2.0 * amnt) + } + #[must_use] pub fn translate(self, amnt: Vec2) -> Self { Rect::from_min_size(self.min + amnt, self.size()) diff --git a/emigui/src/region.rs b/emigui/src/region.rs index d4df06fc..45c541b2 100644 --- a/emigui/src/region.rs +++ b/emigui/src/region.rs @@ -30,9 +30,10 @@ pub struct Region { /// but may overflow (which you will see in child_bounds). desired_rect: Rect, // TODO: rename? - /// Bounding box of children. - // TODO: remove pub(crate) - pub(crate) child_bounds: Rect, + /// Bounding box of all children. + /// This is used to see how large a region actually + /// needs to be after all children has been added. + child_bounds: Rect, /// Overide default style in this region style: Style, @@ -188,6 +189,21 @@ impl Region { self.child_bounds.max - self.desired_rect.min } + /// Expand the bounding rect of this region to include a child at the given rect. + pub fn expand_to_include_child(&mut self, rect: Rect) { + self.child_bounds.extend_with(rect.min); + self.child_bounds.extend_with(rect.max); + } + + /// Bounding box of all contained children + pub fn child_bounds(&self) -> Rect { + self.child_bounds + } + + pub fn force_set_child_bounds(&mut self, child_bounds: Rect) { + self.child_bounds = child_bounds; + } + // ------------------------------------------------------------------------ // Layout related measures: