From 9603bb4f85fbad328bce527ff0850fbc925393d6 Mon Sep 17 00:00:00 2001 From: Simon Persson Date: Fri, 2 Jul 2021 15:25:53 +0800 Subject: [PATCH] Grid fixes (#473) * 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 --- egui/src/grid.rs | 46 ++++++++++++++++------------ egui_demo_lib/src/apps/demo/tests.rs | 44 ++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 20 deletions(-) diff --git a/egui/src/grid.rs b/egui/src/grid.rs index e219e2aa600..210d05490ef 100644 --- a/egui/src/grid.rs +++ b/egui/src/grid.rs @@ -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 { @@ -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; @@ -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 }) } } diff --git a/egui_demo_lib/src/apps/demo/tests.rs b/egui_demo_lib/src/apps/demo/tests.rs index 6c0656fa6f9..0261c18789d 100644 --- a/egui_demo_lib/src/apps/demo/tests.rs +++ b/egui_demo_lib/src/apps/demo/tests.rs @@ -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 { @@ -188,6 +189,7 @@ impl Default for TableTest { num_rows: 4, min_col_width: 10.0, max_col_width: 200.0, + text_length: 10, } } } @@ -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!());