Browse Source

Fix wrong cursor advancement for `allocate_ui`

pull/60/head
Emil Ernerfeldt 4 years ago
parent
commit
a6ffe83349
  1. 4
      egui/src/demos/demo_window.rs
  2. 18
      egui/src/layout.rs
  3. 47
      egui/src/ui.rs

4
egui/src/demos/demo_window.rs

@ -342,12 +342,12 @@ impl LayoutDemo {
.show(ui, |ui| {
if self.main_wrap {
if self.main_dir.is_horizontal() {
ui.allocate_ui_min(
ui.allocate_ui(
vec2(ui.available_finite().width(), self.wrap_row_height),
|ui| ui.with_layout(self.layout(), |ui| self.demo_ui(ui)),
);
} else {
ui.allocate_ui_min(
ui.allocate_ui(
vec2(self.wrap_column_width, ui.available_finite().height()),
|ui| ui.with_layout(self.layout(), |ui| self.demo_ui(ui)),
);

18
egui/src/layout.rs

@ -462,12 +462,20 @@ impl Layout {
}
/// Advance cursor after a widget was added to a specific rectangle.
pub fn advance_after_outer_rect(self, region: &mut Region, rect: Rect, item_spacing: Vec2) {
/// `outer_rect` is a hack needed because the Vec2 cursor is not quite sufficient to keep track
/// of what is happening when we are doing wrapping layouts.
pub fn advance_after_outer_rect(
self,
region: &mut Region,
outer_rect: Rect,
inner_rect: Rect,
item_spacing: Vec2,
) {
region.cursor = match self.main_dir {
Direction::LeftToRight => pos2(rect.right() + item_spacing.x, rect.top()),
Direction::RightToLeft => pos2(rect.left() - item_spacing.x, rect.top()),
Direction::TopDown => pos2(rect.left(), rect.bottom() + item_spacing.y),
Direction::BottomUp => pos2(rect.left(), rect.top() - item_spacing.y),
Direction::LeftToRight => pos2(inner_rect.right() + item_spacing.x, outer_rect.top()),
Direction::RightToLeft => pos2(inner_rect.left() - item_spacing.x, outer_rect.top()),
Direction::TopDown => pos2(outer_rect.left(), inner_rect.bottom() + item_spacing.y),
Direction::BottomUp => pos2(outer_rect.left(), inner_rect.top() - item_spacing.y),
};
}
}

47
egui/src/ui.rs

@ -452,49 +452,23 @@ impl Ui {
.next_space(&self.region, desired_size, item_spacing);
let inner_child_rect = self.layout.justify_or_align(outer_child_rect, desired_size);
self.layout
.advance_after_outer_rect(&mut self.region, outer_child_rect, item_spacing);
self.region.expand_to_include_rect(inner_child_rect);
self.next_auto_id = self.next_auto_id.wrapping_add(1);
inner_child_rect
}
/// Allocated the given space and then adds content to that space.
/// If the contents overflow, more space will be allocated.
/// At least the amount of space requested will always be allocated.
/// Thus you can ask for a little and use more, but you cannot ask for a lot and use less.
pub fn allocate_ui_max<R>(
&mut self,
desired_size: Vec2,
add_contents: impl FnOnce(&mut Ui) -> R,
) -> (R, Response) {
let item_spacing = self.style().spacing.item_spacing;
let outer_child_rect = self
.layout
.next_space(&self.region, desired_size, item_spacing);
let inner_child_rect = self.layout.justify_or_align(outer_child_rect, desired_size);
let mut child_ui = self.child_ui(inner_child_rect, self.layout);
let ret = add_contents(&mut child_ui);
let final_child_rect = child_ui.region.max_rect;
self.layout.advance_after_outer_rect(
&mut self.region,
outer_child_rect.union(final_child_rect),
outer_child_rect,
inner_child_rect,
item_spacing,
);
self.region.expand_to_include_rect(final_child_rect);
self.region.expand_to_include_rect(inner_child_rect);
let response = self.interact_hover(final_child_rect);
(ret, response)
self.next_auto_id = self.next_auto_id.wrapping_add(1);
inner_child_rect
}
/// Allocated the given space and then adds content to that space.
/// If the contents overflow, more space will be allocated.
/// When finished, the amount of space actually used (`min_rect`) will be allocated.
/// So you can request a lot of space and then use less.
pub fn allocate_ui_min<R>(
pub fn allocate_ui<R>(
&mut self,
desired_size: Vec2,
add_contents: impl FnOnce(&mut Self) -> R,
@ -512,6 +486,7 @@ impl Ui {
self.layout.advance_after_outer_rect(
&mut self.region,
outer_child_rect.union(final_child_rect),
final_child_rect,
item_spacing,
);
self.region.expand_to_include_rect(final_child_rect);
@ -731,13 +706,13 @@ impl Ui {
})
}
#[deprecated = "Use `ui.allocate_ui_max` or `ui.allocate_ui_min` instead"]
#[deprecated = "Use `ui.allocate_ui` instead"]
pub fn add_custom_contents(
&mut self,
desired_size: Vec2,
add_contents: impl FnOnce(&mut Ui),
) -> Rect {
self.allocate_ui_max(desired_size, add_contents).1.rect
self.allocate_ui(desired_size, add_contents).1.rect
}
/// A `CollapsingHeader` that starts out collapsed.
@ -888,7 +863,7 @@ impl Ui {
}
.with_main_wrap(main_wrap);
self.allocate_ui_min(initial_size, |ui| ui.with_layout(layout, add_contents).0)
self.allocate_ui(initial_size, |ui| ui.with_layout(layout, add_contents).0)
}
/// Start a ui with vertical layout.
@ -907,7 +882,7 @@ impl Ui {
let rect = child_ui.min_rect();
let item_spacing = self.style().spacing.item_spacing;
self.layout
.advance_after_outer_rect(&mut self.region, rect, item_spacing);
.advance_after_outer_rect(&mut self.region, rect, rect, item_spacing);
self.region.expand_to_include_rect(rect);
(ret, self.interact_hover(rect))
}

Loading…
Cancel
Save