Refactor: replace bounding_size: Vec2 with child_bounds: Rect

This commit is contained in:
Emil Ernerfeldt 2020-04-25 15:46:50 +02:00
parent cce048509f
commit 649dcec09c
10 changed files with 37 additions and 24 deletions

Binary file not shown.

View file

@ -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);

View file

@ -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")));

View file

@ -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);
}
}

View file

@ -110,7 +110,7 @@ pub fn show_popup(ctx: &Arc<Context>, 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);

View file

@ -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,

View file

@ -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
}

View file

@ -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

View file

@ -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,

View file

@ -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;