Skip to content

Commit

Permalink
Grid fixes (#473)
Browse files Browse the repository at this point in the history
* Fix margin for grid layout nested inside grid

* Minor fix for grid layout

At time of end_row, current state is finished updating row_height.
Might as well use that instead of previous state.

* Fix horizontal advancing for nested layouts in grid

* Add back horizontal layout

* Add test for nested layouts in grids

* make test table striped

* Improve table test case with slider for dynamic text
  • Loading branch information
spersson authored Jul 2, 2021
1 parent 89cea7a commit 9603bb4
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 20 deletions.
46 changes: 26 additions & 20 deletions egui/src/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ impl GridLayout {
self.align_size_within_rect(size, frame)
}

pub(crate) fn advance(&mut self, cursor: &mut Rect, frame_rect: Rect, widget_rect: Rect) {
pub(crate) fn advance(&mut self, cursor: &mut Rect, _frame_rect: Rect, widget_rect: Rect) {
let debug_expand_width = self.style.debug.show_expand_width;
let debug_expand_height = self.style.debug.show_expand_height;
if debug_expand_width || debug_expand_height {
Expand Down Expand Up @@ -178,15 +178,18 @@ impl GridLayout {
widget_rect.height().at_least(self.min_cell_size.y),
);

cursor.min.x += self.prev_col_width(self.col) + self.spacing.x;
self.col += 1;
cursor.min.x += frame_rect.width() + self.spacing.x;
}

pub(crate) fn end_row(&mut self, cursor: &mut Rect, painter: &Painter) {
let row_height = self.prev_row_height(self.row);

cursor.min.x = self.initial_x;
cursor.min.y += row_height + self.spacing.y;
cursor.min.y += self.spacing.y;
cursor.min.y += self
.curr_state
.row_height(self.row)
.unwrap_or(self.min_cell_size.y);

self.col = 0;
self.row += 1;

Expand Down Expand Up @@ -329,21 +332,24 @@ impl Grid {
// If somebody wants to wrap more things inside a cell,
// then we should pick a default layout that matches that alignment,
// which we do here:
ui.horizontal(|ui| {
let id = ui.make_persistent_id(id_source);
let grid = GridLayout {
striped,
spacing,
min_cell_size: vec2(min_col_width, min_row_height),
max_cell_size,
row: start_row,
..GridLayout::new(ui, id)
};

ui.set_grid(grid);
let r = add_contents(ui);
ui.save_grid();
r
ui.allocate_ui_at_rect(ui.cursor(), |ui| {
ui.horizontal(|ui| {
let id = ui.make_persistent_id(id_source);
let grid = GridLayout {
striped,
spacing,
min_cell_size: vec2(min_col_width, min_row_height),
max_cell_size,
row: start_row,
..GridLayout::new(ui, id)
};

ui.set_grid(grid);
let r = add_contents(ui);
ui.save_grid();
r
})
.inner
})
}
}
44 changes: 44 additions & 0 deletions egui_demo_lib/src/apps/demo/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ pub struct TableTest {
num_rows: usize,
min_col_width: f32,
max_col_width: f32,
text_length: usize,
}

impl Default for TableTest {
Expand All @@ -188,6 +189,7 @@ impl Default for TableTest {
num_rows: 4,
min_col_width: 10.0,
max_col_width: 200.0,
text_length: 10,
}
}
}
Expand Down Expand Up @@ -247,6 +249,48 @@ impl super::View for TableTest {
}
});

ui.separator();
ui.add(egui::Slider::new(&mut self.text_length, 1..=40).text("Text length"));
egui::Grid::new("parent grid").striped(true).show(ui, |ui| {
ui.vertical(|ui| {
ui.label("Vertical nest1");
ui.label("Vertical nest2");
});
ui.label("First row, second column");
ui.end_row();

ui.horizontal(|ui| {
ui.label("Horizontal nest1");
ui.label("Horizontal nest2");
});
ui.label("Second row, second column");
ui.end_row();

ui.scope(|ui| {
ui.label("Scope nest 1");
ui.label("Scope nest 2");
});
ui.label("Third row, second column");
ui.end_row();

egui::Grid::new("nested grid").show(ui, |ui| {
ui.label("Grid nest11");
ui.label("Grid nest12");
ui.end_row();
ui.label("Grid nest21");
ui.label("Grid nest22");
ui.end_row();
});
ui.label("Fourth row, second column");
ui.end_row();

let mut dyn_text = String::from("O");
dyn_text.extend(std::iter::repeat('h').take(self.text_length));
ui.label(dyn_text);
ui.label("Fifth row, second column");
ui.end_row();
});

ui.vertical_centered(|ui| {
egui::reset_button(ui, self);
ui.add(crate::__egui_github_link_file!());
Expand Down

0 comments on commit 9603bb4

Please sign in to comment.