From 50d759d1b444691d0f95a1bd3f2820262c3c9aca Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Tue, 5 May 2020 02:55:31 +0200 Subject: [PATCH] Make Region::desired_rect private --- emigui/src/example_app.rs | 2 +- emigui/src/region.rs | 52 ++++++++++++++++++++++++++++++------ emigui/src/widgets/slider.rs | 4 +-- 3 files changed, 46 insertions(+), 12 deletions(-) diff --git a/emigui/src/example_app.rs b/emigui/src/example_app.rs index 15d1e15c..97ff4d99 100644 --- a/emigui/src/example_app.rs +++ b/emigui/src/example_app.rs @@ -247,7 +247,7 @@ impl Painting { // Frame it: region.add_paint_cmd(PaintCmd::Rect { - rect: region.desired_rect, + rect: region.rect(), corner_radius: 0.0, fill_color: None, outline: Some(Outline::new(1.0, WHITE)), diff --git a/emigui/src/region.rs b/emigui/src/region.rs index 9b8f952b..d4df06fc 100644 --- a/emigui/src/region.rs +++ b/emigui/src/region.rs @@ -6,7 +6,6 @@ use crate::{color::*, containers::*, font::TextFragment, layout::*, widgets::*, /// with a type of layout (horizontal or vertical). /// TODO: make Region a trait so we can have type-safe HorizontalRegion etc? pub struct Region { - // TODO: remove pub(crate) from all members. /// How we access input, output and memory ctx: Arc, @@ -29,10 +28,10 @@ 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 child_bounds). - pub(crate) desired_rect: Rect, // TODO: rename? + desired_rect: Rect, // TODO: rename? /// Bounding box of children. - /// Initially set to Rect::nothing(). + // TODO: remove pub(crate) pub(crate) child_bounds: Rect, /// Overide default style in this region @@ -145,11 +144,53 @@ impl Region { self.clip_rect = clip_rect; } + // ------------------------------------------------------------------------ + + /// Screen-space position of this Region. + /// This may have moved from its original if a child overflowed to the left or up (rare). + pub fn top_left(&self) -> Pos2 { + // If a child doesn't fit in desired_rect, we have effectively expanded: + self.desired_rect.min.min(self.child_bounds.min) + } + + /// Screen-space position of the current bottom right corner of this Region. + /// This may move when we add children that overflow our desired rectangle bounds. pub fn bottom_right(&self) -> Pos2 { // If a child doesn't fit in desired_rect, we have effectively expanded: self.desired_rect.max.max(self.child_bounds.max) } + /// Position and current size of the region. + /// The size is the maximum of the origional (minimum/desired) size and + /// the size of the containted children. + pub fn rect(&self) -> Rect { + Rect::from_min_max(self.top_left(), self.bottom_right()) + } + + /// Set the width of the region. + /// You won't be able to shrink it beyond its current child bounds. + pub fn set_width(&mut self, width: f32) { + let min_width = self.child_bounds.max.x - self.top_left().x; + let width = width.max(min_width); + self.desired_rect.max.x = self.top_left().x + width; + } + + /// Set the height of the region. + /// You won't be able to shrink it beyond its current child bounds. + pub fn set_height(&mut self, height: f32) { + let min_height = self.child_bounds.max.y - self.top_left().y; + let height = height.max(min_height); + self.desired_rect.max.y = self.top_left().y + height; + } + + /// Size of content + pub fn bounding_size(&self) -> Vec2 { + self.child_bounds.max - self.desired_rect.min + } + + // ------------------------------------------------------------------------ + // Layout related measures: + pub fn available_width(&self) -> f32 { self.available_space().x } @@ -167,11 +208,6 @@ impl Region { self.bottom_right() - 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 } diff --git a/emigui/src/widgets/slider.rs b/emigui/src/widgets/slider.rs index 0e804b43..ebf2f8d2 100644 --- a/emigui/src/widgets/slider.rs +++ b/emigui/src/widgets/slider.rs @@ -127,9 +127,7 @@ impl<'a> Widget for Slider<'a> { let slider_response = columns[0].add(slider_sans_text); // Place the text in line with the slider on the left: - columns[1] - .desired_rect - .set_height(slider_response.rect.height()); + columns[1].set_height(slider_response.rect.height()); columns[1].horizontal(|region| { region.set_align(Align::Center); region.add(Label::new(full_text).multiline(false));