diff --git a/docs/example_wasm_bg.wasm b/docs/example_wasm_bg.wasm index dee00d47..5c15348f 100644 Binary files a/docs/example_wasm_bg.wasm and b/docs/example_wasm_bg.wasm differ diff --git a/emigui/src/collapsing_header.rs b/emigui/src/collapsing_header.rs index a65f8d6a..3fe901fd 100644 --- a/emigui/src/collapsing_header.rs +++ b/emigui/src/collapsing_header.rs @@ -118,7 +118,11 @@ impl CollapsingHeader { add_contents(region); - region.bounding_size.y = region.bounding_size.y.min(max_height); + region.child_bounds.max.y = region + .child_bounds + .max + .y + .min(region.child_bounds.min.y + max_height); }); } else if state.open { region.indent(id, add_contents); diff --git a/emigui/src/floating.rs b/emigui/src/floating.rs index 08a374c2..2d3fc889 100644 --- a/emigui/src/floating.rs +++ b/emigui/src/floating.rs @@ -66,7 +66,7 @@ impl Floating { Rect::from_min_size(state.outer_pos, Vec2::infinity()), ); add_contents(&mut region); - let size = region.bounding_size.ceil(); + let size = region.bounding_size().ceil(); state.outer_rect = Rect::from_min_size(state.outer_pos, size); let move_interact = ctx.interact(layer, &state.outer_rect, Some(id.with("move"))); diff --git a/emigui/src/frame.rs b/emigui/src/frame.rs index 9ce4068c..a1fa4f83 100644 --- a/emigui/src/frame.rs +++ b/emigui/src/frame.rs @@ -19,7 +19,7 @@ impl Frame { add_contents(&mut child_region); // TODO: handle the last item_spacing in a nicer way - let inner_size = child_region.bounding_size; + let inner_size = child_region.bounding_size(); let inner_size = inner_size.ceil(); // TODO: round to pixel let outer_rect = Rect::from_min_size(outer_pos, margin + inner_size + margin); @@ -36,6 +36,9 @@ impl Frame { }, ); - region.bounding_size = margin + child_region.bounding_size + margin; // TODO: this is not really right + // TODO: move up corsor? + region + .child_bounds + .extend_with(child_region.child_bounds.max + margin); } } diff --git a/emigui/src/layout.rs b/emigui/src/layout.rs index 1d7e5237..fe59bd65 100644 --- a/emigui/src/layout.rs +++ b/emigui/src/layout.rs @@ -110,7 +110,7 @@ pub fn show_popup(ctx: &Arc, window_pos: Pos2, add_contents: impl FnOnc // Now insert popup background: // TODO: handle the last item_spacing in a nicer way - let inner_size = contents_region.bounding_size - style.item_spacing; + let inner_size = contents_region.bounding_size() - style.item_spacing; let outer_size = inner_size + 2.0 * window_padding; let rect = Rect::from_min_size(window_pos, outer_size); diff --git a/emigui/src/math.rs b/emigui/src/math.rs index 46f21e26..9e83ebb0 100644 --- a/emigui/src/math.rs +++ b/emigui/src/math.rs @@ -392,6 +392,7 @@ impl Rect { *self = self.translate(center - self.center()); } + #[must_use] pub fn contains(&self, p: Pos2) -> bool { self.min.x <= p.x && p.x <= self.min.x + self.size().x @@ -399,6 +400,11 @@ impl Rect { && p.y <= self.min.y + self.size().y } + pub fn extend_with(&mut self, p: Pos2) { + self.min = self.min.min(p); + self.max = self.max.max(p); + } + pub fn center(&self) -> Pos2 { Pos2 { x: self.min.x + self.size().x / 2.0, diff --git a/emigui/src/region.rs b/emigui/src/region.rs index 38a9a870..48897f23 100644 --- a/emigui/src/region.rs +++ b/emigui/src/region.rs @@ -30,13 +30,11 @@ pub struct Region { /// Note that the size may be infinite in one or both dimensions. /// The widgets will TRY to fit within the rect, /// but may overflow (which you will see in bounding_size). - pub(crate) desired_rect: Rect, // TODO: rename desired_rect + pub(crate) desired_rect: Rect, // TODO: rename /// Bounding box of children. - /// We keep track of our max-size along the orthogonal to self.dir - /// Initially set to zero. - /// TODO: make into `child_bounds: Rect` - pub(crate) bounding_size: Vec2, + /// Initially set to Rect::nothing(). + pub(crate) child_bounds: Rect, /// Overide default style in this region pub(crate) style: Style, @@ -65,7 +63,7 @@ impl Region { layer, clip_rect: rect.expand(CLIP_RECT_MARGIN), desired_rect: rect, - bounding_size: Vec2::default(), + child_bounds: Rect::from_min_size(rect.min, Vec2::zero()), // TODO: Rect::nothing() ? style, cursor: rect.min, dir: Direction::Vertical, @@ -85,7 +83,7 @@ impl Region { clip_rect, desired_rect: child_rect, cursor: child_rect.min, - bounding_size: vec2(0.0, 0.0), + child_bounds: Rect::from_min_size(child_rect.min, Vec2::zero()), // TODO: Rect::nothing() ? dir: self.dir, align: self.align, } @@ -170,6 +168,11 @@ impl Region { self.desired_rect.max - self.cursor } + /// Size of content + pub fn bounding_size(&self) -> Vec2 { + self.child_bounds.max - self.desired_rect.min + } + pub fn direction(&self) -> Direction { self.dir } @@ -199,7 +202,7 @@ impl Region { ..self.child_region(child_rect) }; add_contents(&mut child_region); - self.reserve_space_without_padding(child_region.bounding_size); + self.reserve_space_without_padding(child_region.bounding_size()); } /// Create a child region which is indented to the right @@ -216,7 +219,7 @@ impl Region { ..self.child_region(child_rect) }; add_contents(&mut child_region); - let size = child_region.bounding_size; + let size = child_region.bounding_size(); // draw a grey line on the left to mark the region let line_start = child_rect.min - indent * 0.5; @@ -269,7 +272,7 @@ impl Region { ..self.child_region(child_rect) }; add_contents(&mut child_region); - let size = child_region.bounding_size; + let size = child_region.bounding_size(); self.reserve_space_without_padding(size); } @@ -316,7 +319,7 @@ impl Region { let mut max_height = 0.0; for region in columns { - let size = region.bounding_size; + let size = region.bounding_size(); max_height = size.y.max(max_height); } @@ -382,18 +385,16 @@ impl Region { Align::Center => 0.5 * (self.available_height() - size.y), Align::Max => self.available_height() - size.y, }; + self.child_bounds.extend_with(self.cursor + size); self.cursor.x += size.x; - self.bounding_size.x += size.x; - self.bounding_size.y = self.bounding_size.y.max(size.y); } else { pos.x += match self.align { Align::Min => 0.0, Align::Center => 0.5 * (self.available_width() - size.x), Align::Max => self.available_width() - size.x, }; + self.child_bounds.extend_with(self.cursor + size); self.cursor.y += size.y; - self.bounding_size.y += size.y; - self.bounding_size.x = self.bounding_size.x.max(size.x); } pos } diff --git a/emigui/src/resize.rs b/emigui/src/resize.rs index b521d494..00148557 100644 --- a/emigui/src/resize.rs +++ b/emigui/src/resize.rs @@ -103,8 +103,7 @@ impl Resize { let desired_size = { let mut contents_region = region.child_region(inner_rect); add_contents(&mut contents_region); - let desired_size = contents_region.bounding_size; - desired_size + contents_region.bounding_size() }; let desired_size = desired_size.ceil(); // Avoid rounding errors in math diff --git a/emigui/src/scroll_area.rs b/emigui/src/scroll_area.rs index 77730905..f9add704 100644 --- a/emigui/src/scroll_area.rs +++ b/emigui/src/scroll_area.rs @@ -53,7 +53,7 @@ impl ScrollArea { content_region.cursor -= state.offset; content_region.desired_rect = content_region.desired_rect.translate(-state.offset); add_contents(&mut content_region); - let content_size = content_region.bounding_size; + let content_size = content_region.bounding_size(); let content_interact = ctx.interact( outer_region.layer, diff --git a/emigui/src/window.rs b/emigui/src/window.rs index f04b0a0a..c62f2474 100644 --- a/emigui/src/window.rs +++ b/emigui/src/window.rs @@ -157,7 +157,7 @@ impl Window { add_contents(&mut contents_region); // TODO: handle the last item_spacing in a nicer way - let desired_inner_size = contents_region.bounding_size - style.item_spacing; + let desired_inner_size = contents_region.bounding_size() - style.item_spacing; let desired_inner_size = desired_inner_size.ceil(); // Avoid rounding errors in math let mut new_inner_size = state.inner_size;