Browse Source

Add `Margin::expand_rect` and `shrink_rect` (#3214)

* Add `Margin::expand_rect` and `shrink_rect`

* fix typo

* Use the new helpers
pull/3105/merge
Emil Ernerfeldt 1 year ago
committed by GitHub
parent
commit
0e5f93b65d
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 16
      crates/egui/src/containers/frame.rs
  2. 2
      crates/egui/src/containers/scroll_area.rs
  3. 17
      crates/egui/src/style.rs

16
crates/egui/src/containers/frame.rs

@ -193,9 +193,7 @@ impl Frame {
let where_to_put_background = ui.painter().add(Shape::Noop);
let outer_rect_bounds = ui.available_rect_before_wrap();
let mut inner_rect = outer_rect_bounds;
inner_rect.min += self.outer_margin.left_top() + self.inner_margin.left_top();
inner_rect.max -= self.outer_margin.right_bottom() + self.inner_margin.right_bottom();
let mut inner_rect = (self.inner_margin + self.outer_margin).shrink_rect(outer_rect_bounds);
// Make sure we don't shrink to the negative:
inner_rect.max.x = inner_rect.max.x.max(inner_rect.min.x);
@ -256,17 +254,13 @@ impl Frame {
impl Prepared {
fn paint_rect(&self) -> Rect {
let mut rect = self.content_ui.min_rect();
rect.min -= self.frame.inner_margin.left_top();
rect.max += self.frame.inner_margin.right_bottom();
rect
self.frame
.inner_margin
.expand_rect(self.content_ui.min_rect())
}
fn content_with_margin(&self) -> Rect {
let mut rect = self.content_ui.min_rect();
rect.min -= self.frame.inner_margin.left_top() + self.frame.outer_margin.left_top();
rect.max += self.frame.inner_margin.right_bottom() + self.frame.outer_margin.right_bottom();
rect
(self.frame.inner_margin + self.frame.outer_margin).expand_rect(self.content_ui.min_rect())
}
pub fn end(self, ui: &mut Ui) -> Response {

2
crates/egui/src/containers/scroll_area.rs

@ -459,7 +459,7 @@ impl ScrollArea {
content_clip_rect.max[d] = ui.clip_rect().max[d] - current_bar_use[d];
}
}
// Make sure we din't accidentally expand the clip rect
// Make sure we didn't accidentally expand the clip rect
content_clip_rect = content_clip_rect.intersect(ui.clip_rect());
content_ui.set_clip_rect(content_clip_rect);
}

17
crates/egui/src/style.rs

@ -360,30 +360,46 @@ impl Margin {
}
/// Total margins on both sides
#[inline]
pub fn sum(&self) -> Vec2 {
vec2(self.left + self.right, self.top + self.bottom)
}
#[inline]
pub fn left_top(&self) -> Vec2 {
vec2(self.left, self.top)
}
#[inline]
pub fn right_bottom(&self) -> Vec2 {
vec2(self.right, self.bottom)
}
#[inline]
pub fn is_same(&self) -> bool {
self.left == self.right && self.left == self.top && self.left == self.bottom
}
#[inline]
pub fn expand_rect(&self, rect: Rect) -> Rect {
Rect::from_min_max(rect.min - self.left_top(), rect.max + self.right_bottom())
}
#[inline]
pub fn shrink_rect(&self, rect: Rect) -> Rect {
Rect::from_min_max(rect.min + self.left_top(), rect.max - self.right_bottom())
}
}
impl From<f32> for Margin {
#[inline]
fn from(v: f32) -> Self {
Self::same(v)
}
}
impl From<Vec2> for Margin {
#[inline]
fn from(v: Vec2) -> Self {
Self::symmetric(v.x, v.y)
}
@ -392,6 +408,7 @@ impl From<Vec2> for Margin {
impl std::ops::Add for Margin {
type Output = Self;
#[inline]
fn add(self, other: Self) -> Self {
Self {
left: self.left + other.left,

Loading…
Cancel
Save