refactor space allocation code

This commit is contained in:
Emil Ernerfeldt 2020-09-18 18:19:52 +02:00
parent 283b4ab63d
commit dd8c298cb5

View file

@ -166,12 +166,13 @@ impl Layout {
available_size: Vec2, available_size: Vec2,
mut child_size: Vec2, mut child_size: Vec2,
) -> Rect { ) -> Rect {
let available_size = available_size.max(child_size); let available_size = available_size.at_least(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();
if self.dir == Direction::Horizontal { match self.dir {
Direction::Horizontal => {
if let Some(align) = self.align { if let Some(align) = self.align {
child_move.y += match align { child_move.y += match align {
Align::Min => 0.0, Align::Min => 0.0,
@ -185,7 +186,8 @@ 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 cursor_change.x += style.spacing.item_spacing.x; // Where to put next thing, if there is a next thing
} else { }
Direction::Vertical => {
if let Some(align) = self.align { if let Some(align) = self.align {
child_move.x += match align { child_move.x += match align {
Align::Min => 0.0, Align::Min => 0.0,
@ -199,20 +201,20 @@ impl Layout {
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 cursor_change.y += style.spacing.item_spacing.y; // Where to put next thing, if there is a next thing
} }
}
if self.is_reversed() { if self.is_reversed() {
// reverse: cursor starts at bottom right corner of new widget. // reverse: cursor starts at bottom right corner of new widget.
let child_pos = if self.dir == Direction::Horizontal { let child_pos = match self.dir {
pos2( Direction::Horizontal => pos2(
cursor.x - child_size.x, cursor.x - child_size.x,
cursor.y - available_size.y + child_move.y, cursor.y - available_size.y + child_move.y,
) ),
} else { Direction::Vertical => pos2(
pos2(
cursor.x - available_size.x + child_move.x, cursor.x - available_size.x + child_move.x,
cursor.y - child_size.y, cursor.y - child_size.y,
) ),
}; };
// let child_pos = *cursor - child_move - child_size; // let child_pos = *cursor - child_move - child_size;
*cursor -= cursor_change; *cursor -= cursor_change;