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,53 +166,55 @@ 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 {
if let Some(align) = self.align { Direction::Horizontal => {
child_move.y += match align { if let Some(align) = self.align {
Align::Min => 0.0, child_move.y += match align {
Align::Center => 0.5 * (available_size.y - child_size.y), Align::Min => 0.0,
Align::Max => available_size.y - child_size.y, Align::Center => 0.5 * (available_size.y - child_size.y),
}; Align::Max => available_size.y - child_size.y,
} else { };
// justified: fill full height } else {
child_size.y = child_size.y.max(available_size.y); // justified: fill full height
} child_size.y = child_size.y.max(available_size.y);
}
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 { }
if let Some(align) = self.align { Direction::Vertical => {
child_move.x += match align { if let Some(align) = self.align {
Align::Min => 0.0, child_move.x += match align {
Align::Center => 0.5 * (available_size.x - child_size.x), Align::Min => 0.0,
Align::Max => available_size.x - child_size.x, Align::Center => 0.5 * (available_size.x - child_size.x),
Align::Max => available_size.x - child_size.x,
};
} else {
// justified: fill full width
child_size.x = child_size.x.max(available_size.x);
}; };
} else { cursor_change.y += child_size.y;
// justified: fill full width cursor_change.y += style.spacing.item_spacing.y; // Where to put next thing, if there is a next thing
child_size.x = child_size.x.max(available_size.x); }
};
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
} }
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;