[refactor] Simplify how ui calls placer after adding a widget

This commit is contained in:
Emil Ernerfeldt 2021-01-14 23:57:46 +01:00
parent 641a302e0a
commit 8e1c7625f1
3 changed files with 17 additions and 15 deletions

View file

@ -446,12 +446,12 @@ impl Layout {
} }
/// Advance the cursor by this many points. /// Advance the cursor by this many points.
pub(crate) fn advance_cursor(&self, region: &mut Region, amount: f32) { pub(crate) fn advance_cursor(&self, cursor: &mut Pos2, amount: f32) {
match self.main_dir { match self.main_dir {
Direction::LeftToRight => region.cursor.x += amount, Direction::LeftToRight => cursor.x += amount,
Direction::RightToLeft => region.cursor.x -= amount, Direction::RightToLeft => cursor.x -= amount,
Direction::TopDown => region.cursor.y += amount, Direction::TopDown => cursor.y += amount,
Direction::BottomUp => region.cursor.y -= amount, Direction::BottomUp => cursor.y -= amount,
} }
} }
@ -461,12 +461,12 @@ impl Layout {
/// * `widget_rect`: the actual rect used by the widget /// * `widget_rect`: the actual rect used by the widget
pub(crate) fn advance_after_rects( pub(crate) fn advance_after_rects(
&self, &self,
region: &mut Region, cursor: &mut Pos2,
frame_rect: Rect, frame_rect: Rect,
widget_rect: Rect, widget_rect: Rect,
item_spacing: Vec2, item_spacing: Vec2,
) { ) {
region.cursor = match self.main_dir { *cursor = match self.main_dir {
Direction::LeftToRight => pos2(widget_rect.right() + item_spacing.x, frame_rect.top()), Direction::LeftToRight => pos2(widget_rect.right() + item_spacing.x, frame_rect.top()),
Direction::RightToLeft => pos2(widget_rect.left() - item_spacing.x, frame_rect.top()), Direction::RightToLeft => pos2(widget_rect.left() - item_spacing.x, frame_rect.top()),
Direction::TopDown => pos2(frame_rect.left(), widget_rect.bottom() + item_spacing.y), Direction::TopDown => pos2(frame_rect.left(), widget_rect.bottom() + item_spacing.y),

View file

@ -111,10 +111,11 @@ impl Placer {
self.grid.is_none(), self.grid.is_none(),
"You cannot advance the cursor when in a grid layout" "You cannot advance the cursor when in a grid layout"
); );
self.layout.advance_cursor(&mut self.region, amount) self.layout.advance_cursor(&mut self.region.cursor, amount)
} }
/// Advance cursor after a widget was added to a specific rectangle. /// Advance cursor after a widget was added to a specific rectangle
/// and expand the region min_rect.
/// ///
/// * `frame_rect`: the frame inside which a widget was e.g. centered /// * `frame_rect`: the frame inside which a widget was e.g. centered
/// * `widget_rect`: the actual rect used by the widget /// * `widget_rect`: the actual rect used by the widget
@ -127,9 +128,14 @@ impl Placer {
if let Some(grid) = &mut self.grid { if let Some(grid) = &mut self.grid {
grid.advance(&mut self.region.cursor, frame_rect, widget_rect) grid.advance(&mut self.region.cursor, frame_rect, widget_rect)
} else { } else {
self.layout self.layout.advance_after_rects(
.advance_after_rects(&mut self.region, frame_rect, widget_rect, item_spacing) &mut self.region.cursor,
frame_rect,
widget_rect,
item_spacing,
)
} }
self.region.expand_to_include_rect(widget_rect);
} }
/// Move to the next row in a grid layout or wrapping layout. /// Move to the next row in a grid layout or wrapping layout.

View file

@ -512,7 +512,6 @@ impl Ui {
self.placer self.placer
.advance_after_rects(frame_rect, widget_rect, item_spacing); .advance_after_rects(frame_rect, widget_rect, item_spacing);
self.expand_to_include_rect(widget_rect);
widget_rect widget_rect
} }
@ -520,7 +519,6 @@ impl Ui {
pub(crate) fn advance_cursor_after_rect(&mut self, rect: Rect) -> Id { pub(crate) fn advance_cursor_after_rect(&mut self, rect: Rect) -> Id {
let item_spacing = self.style().spacing.item_spacing; let item_spacing = self.style().spacing.item_spacing;
self.placer.advance_after_rects(rect, rect, item_spacing); self.placer.advance_after_rects(rect, rect, item_spacing);
self.expand_to_include_rect(rect);
self.next_auto_id = self.next_auto_id.wrapping_add(1); self.next_auto_id = self.next_auto_id.wrapping_add(1);
Id::new(self.next_auto_id) Id::new(self.next_auto_id)
@ -552,7 +550,6 @@ impl Ui {
final_child_rect, final_child_rect,
item_spacing, item_spacing,
); );
self.expand_to_include_rect(final_child_rect);
let response = self.interact(final_child_rect, child_ui.id, Sense::hover()); let response = self.interact(final_child_rect, child_ui.id, Sense::hover());
(ret, response) (ret, response)
@ -1104,7 +1101,6 @@ impl Ui {
let rect = child_ui.min_rect(); let rect = child_ui.min_rect();
let item_spacing = self.style().spacing.item_spacing; let item_spacing = self.style().spacing.item_spacing;
self.placer.advance_after_rects(rect, rect, item_spacing); self.placer.advance_after_rects(rect, rect, item_spacing);
self.expand_to_include_rect(rect);
(ret, self.interact(rect, child_ui.id, Sense::hover())) (ret, self.interact(rect, child_ui.id, Sense::hover()))
} }