Fix: assign a different id to each table cell, avoiding id clashes (#4076)

Each cell in a table now has a `Ui` with a unique `Id` based on the row
and column.

This avoids Id-clashes, e.g. when putting a `CollapsingHeader` in a
table cell.
This commit is contained in:
Emil Ernerfeldt 2024-02-20 13:35:19 +01:00 committed by GitHub
parent 68b3ef7f6b
commit a33ae64785
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 21 additions and 5 deletions

View File

@ -1,4 +1,4 @@
use egui::{Pos2, Rect, Response, Sense, Ui};
use egui::{Id, Pos2, Rect, Response, Sense, Ui};
#[derive(Clone, Copy)]
pub(crate) enum CellSize {
@ -113,6 +113,7 @@ impl<'l> StripLayout<'l> {
flags: StripLayoutFlags,
width: CellSize,
height: CellSize,
child_ui_id_source: Id,
add_cell_contents: impl FnOnce(&mut Ui),
) -> (Rect, Response) {
let max_rect = self.cell_rect(&width, &height);
@ -145,7 +146,7 @@ impl<'l> StripLayout<'l> {
);
}
let used_rect = self.cell(flags, max_rect, add_cell_contents);
let used_rect = self.cell(flags, max_rect, child_ui_id_source, add_cell_contents);
self.set_pos(max_rect);
@ -186,9 +187,12 @@ impl<'l> StripLayout<'l> {
&mut self,
flags: StripLayoutFlags,
rect: Rect,
child_ui_id_source: egui::Id,
add_cell_contents: impl FnOnce(&mut Ui),
) -> Rect {
let mut child_ui = self.ui.child_ui(rect, self.cell_layout);
let mut child_ui =
self.ui
.child_ui_with_id_source(rect, self.cell_layout, child_ui_id_source);
if flags.clip {
let margin = egui::Vec2::splat(self.ui.visuals().clip_rect_margin);

View File

@ -194,7 +194,13 @@ impl<'a, 'b> Strip<'a, 'b> {
clip: self.clip,
..Default::default()
};
self.layout.add(flags, width, height, add_contents);
self.layout.add(
flags,
width,
height,
egui::Id::new(self.size_index),
add_contents,
);
}
/// Add an empty cell.

View File

@ -1166,7 +1166,13 @@ impl<'a, 'b> TableRow<'a, 'b> {
selected: self.selected,
};
let (used_rect, response) = self.layout.add(flags, width, height, add_cell_contents);
let (used_rect, response) = self.layout.add(
flags,
width,
height,
egui::Id::new((self.row_index, col_index)),
add_cell_contents,
);
if let Some(max_w) = self.max_used_widths.get_mut(col_index) {
*max_w = max_w.max(used_rect.width());