[refactor] Separate space allocation from cursor advancement

This commit is contained in:
Emil Ernerfeldt 2020-10-12 08:50:06 +02:00
parent f0a45f5055
commit 73dc3484ae
2 changed files with 18 additions and 11 deletions

View file

@ -1,4 +1,4 @@
use crate::{math::*, style::Style, Align}; use crate::{math::*, Align};
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -176,6 +176,14 @@ impl Layout {
} }
} }
/// Advance the cursor by this spacing
pub fn advance_cursor2(self, cursor: &mut Pos2, amount: Vec2) {
match self.dir() {
Direction::Horizontal => self.advance_cursor(cursor, amount.x),
Direction::Vertical => self.advance_cursor(cursor, amount.y),
}
}
pub fn rect_from_cursor_size(self, cursor: Pos2, size: Vec2) -> Rect { pub fn rect_from_cursor_size(self, cursor: Pos2, size: Vec2) -> Rect {
let mut rect = Rect::from_min_size(cursor, size); let mut rect = Rect::from_min_size(cursor, size);
@ -212,12 +220,12 @@ impl Layout {
pub fn allocate_space( pub fn allocate_space(
self, self,
cursor: &mut Pos2, cursor: &mut Pos2,
style: &Style,
available_size: Vec2, available_size: Vec2,
mut child_size: Vec2, minimum_child_size: Vec2,
) -> Rect { ) -> Rect {
let available_size = available_size.at_least(child_size); let available_size = available_size.at_least(minimum_child_size);
let mut child_size = minimum_child_size;
let mut child_move = Vec2::default(); let mut child_move = Vec2::default();
let mut cursor_change = Vec2::default(); let mut cursor_change = Vec2::default();
@ -235,7 +243,6 @@ impl Layout {
} }
cursor_change.x += child_size.x; cursor_change.x += child_size.x;
cursor_change.x += style.spacing.item_spacing.x; // Where to put next thing, if there is a next thing
} }
Direction::Vertical => { Direction::Vertical => {
if let Some(align) = self.align { if let Some(align) = self.align {
@ -249,7 +256,6 @@ impl Layout {
child_size.x = child_size.x.max(available_size.x); child_size.x = child_size.x.max(available_size.x);
}; };
cursor_change.y += child_size.y; cursor_change.y += child_size.y;
cursor_change.y += style.spacing.item_spacing.y; // Where to put next thing, if there is a next thing
} }
} }

View file

@ -475,11 +475,12 @@ impl Ui {
/// Returns where to put the widget. /// Returns where to put the widget.
fn reserve_space_impl(&mut self, child_size: Vec2) -> Rect { fn reserve_space_impl(&mut self, child_size: Vec2) -> Rect {
let available_size = self.available_finite().size(); let available_size = self.available_finite().size();
let child_rect = let child_rect = self
self.layout .layout
.allocate_space(&mut self.cursor, &self.style, available_size, child_size); .allocate_space(&mut self.cursor, available_size, child_size);
self.min_rect = self.min_rect.union(child_rect); let item_spacing = self.style().spacing.item_spacing;
self.max_rect = self.max_rect.union(child_rect); self.layout.advance_cursor2(&mut self.cursor, item_spacing);
self.expand_to_include_rect(child_rect);
self.child_count += 1; self.child_count += 1;
child_rect child_rect
} }