Skip to content

Commit 9603bb4

Browse files
authored
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
1 parent 89cea7a commit 9603bb4

File tree

2 files changed

+70
-20
lines changed

2 files changed

+70
-20
lines changed

egui/src/grid.rs

+26-20
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ impl GridLayout {
148148
self.align_size_within_rect(size, frame)
149149
}
150150

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

181+
cursor.min.x += self.prev_col_width(self.col) + self.spacing.x;
181182
self.col += 1;
182-
cursor.min.x += frame_rect.width() + self.spacing.x;
183183
}
184184

185185
pub(crate) fn end_row(&mut self, cursor: &mut Rect, painter: &Painter) {
186-
let row_height = self.prev_row_height(self.row);
187-
188186
cursor.min.x = self.initial_x;
189-
cursor.min.y += row_height + self.spacing.y;
187+
cursor.min.y += self.spacing.y;
188+
cursor.min.y += self
189+
.curr_state
190+
.row_height(self.row)
191+
.unwrap_or(self.min_cell_size.y);
192+
190193
self.col = 0;
191194
self.row += 1;
192195

@@ -329,21 +332,24 @@ impl Grid {
329332
// If somebody wants to wrap more things inside a cell,
330333
// then we should pick a default layout that matches that alignment,
331334
// which we do here:
332-
ui.horizontal(|ui| {
333-
let id = ui.make_persistent_id(id_source);
334-
let grid = GridLayout {
335-
striped,
336-
spacing,
337-
min_cell_size: vec2(min_col_width, min_row_height),
338-
max_cell_size,
339-
row: start_row,
340-
..GridLayout::new(ui, id)
341-
};
342-
343-
ui.set_grid(grid);
344-
let r = add_contents(ui);
345-
ui.save_grid();
346-
r
335+
ui.allocate_ui_at_rect(ui.cursor(), |ui| {
336+
ui.horizontal(|ui| {
337+
let id = ui.make_persistent_id(id_source);
338+
let grid = GridLayout {
339+
striped,
340+
spacing,
341+
min_cell_size: vec2(min_col_width, min_row_height),
342+
max_cell_size,
343+
row: start_row,
344+
..GridLayout::new(ui, id)
345+
};
346+
347+
ui.set_grid(grid);
348+
let r = add_contents(ui);
349+
ui.save_grid();
350+
r
351+
})
352+
.inner
347353
})
348354
}
349355
}

egui_demo_lib/src/apps/demo/tests.rs

+44
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ pub struct TableTest {
179179
num_rows: usize,
180180
min_col_width: f32,
181181
max_col_width: f32,
182+
text_length: usize,
182183
}
183184

184185
impl Default for TableTest {
@@ -188,6 +189,7 @@ impl Default for TableTest {
188189
num_rows: 4,
189190
min_col_width: 10.0,
190191
max_col_width: 200.0,
192+
text_length: 10,
191193
}
192194
}
193195
}
@@ -247,6 +249,48 @@ impl super::View for TableTest {
247249
}
248250
});
249251

252+
ui.separator();
253+
ui.add(egui::Slider::new(&mut self.text_length, 1..=40).text("Text length"));
254+
egui::Grid::new("parent grid").striped(true).show(ui, |ui| {
255+
ui.vertical(|ui| {
256+
ui.label("Vertical nest1");
257+
ui.label("Vertical nest2");
258+
});
259+
ui.label("First row, second column");
260+
ui.end_row();
261+
262+
ui.horizontal(|ui| {
263+
ui.label("Horizontal nest1");
264+
ui.label("Horizontal nest2");
265+
});
266+
ui.label("Second row, second column");
267+
ui.end_row();
268+
269+
ui.scope(|ui| {
270+
ui.label("Scope nest 1");
271+
ui.label("Scope nest 2");
272+
});
273+
ui.label("Third row, second column");
274+
ui.end_row();
275+
276+
egui::Grid::new("nested grid").show(ui, |ui| {
277+
ui.label("Grid nest11");
278+
ui.label("Grid nest12");
279+
ui.end_row();
280+
ui.label("Grid nest21");
281+
ui.label("Grid nest22");
282+
ui.end_row();
283+
});
284+
ui.label("Fourth row, second column");
285+
ui.end_row();
286+
287+
let mut dyn_text = String::from("O");
288+
dyn_text.extend(std::iter::repeat('h').take(self.text_length));
289+
ui.label(dyn_text);
290+
ui.label("Fifth row, second column");
291+
ui.end_row();
292+
});
293+
250294
ui.vertical_centered(|ui| {
251295
egui::reset_button(ui, self);
252296
ui.add(crate::__egui_github_link_file!());

0 commit comments

Comments
 (0)