Add `TableBuilder::id_source` (#5022)

* Closes https://github.com/emilk/egui/issues/4982
This commit is contained in:
Emil Ernerfeldt 2024-08-28 10:27:21 +02:00 committed by GitHub
parent 8e5492b6e8
commit 82036cf59a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 18 additions and 5 deletions

View File

@ -5,7 +5,7 @@
use egui::{
scroll_area::{ScrollAreaOutput, ScrollBarVisibility},
Align, NumExt as _, Rangef, Rect, Response, ScrollArea, Ui, Vec2, Vec2b,
Align, Id, NumExt as _, Rangef, Rect, Response, ScrollArea, Ui, Vec2, Vec2b,
};
use crate::{
@ -199,7 +199,7 @@ impl Default for TableScrollOptions {
/// You must pre-allocate all columns with [`Self::column`]/[`Self::columns`].
///
/// If you have multiple [`Table`]:s in the same [`Ui`]
/// you will need to give them unique id:s by surrounding them with [`Ui::push_id`].
/// you will need to give them unique id:s by with [`Self::id_source`].
///
/// ### Example
/// ```
@ -230,6 +230,7 @@ impl Default for TableScrollOptions {
/// ```
pub struct TableBuilder<'a> {
ui: &'a mut Ui,
id_source: Id,
columns: Vec<Column>,
striped: Option<bool>,
resizable: bool,
@ -243,6 +244,7 @@ impl<'a> TableBuilder<'a> {
let cell_layout = *ui.layout();
Self {
ui,
id_source: Id::new("__table_state"),
columns: Default::default(),
striped: None,
resizable: false,
@ -252,6 +254,15 @@ impl<'a> TableBuilder<'a> {
}
}
/// Give this table a unique id within the parent [`Ui`].
///
/// This is required if you have multiple tables in the same [`Ui`].
#[inline]
pub fn id_source(mut self, id_source: impl std::hash::Hash) -> Self {
self.id_source = Id::new(id_source);
self
}
/// Enable striped row background for improved readability.
///
/// Default is whatever is in [`egui::Visuals::striped`].
@ -406,7 +417,7 @@ impl<'a> TableBuilder<'a> {
/// Reset all column widths.
pub fn reset(&mut self) {
let state_id = self.ui.id().with("__table_state");
let state_id = self.ui.id().with(self.id_source);
TableState::reset(self.ui, state_id);
}
@ -416,6 +427,7 @@ impl<'a> TableBuilder<'a> {
let Self {
ui,
id_source,
columns,
striped,
resizable,
@ -426,7 +438,7 @@ impl<'a> TableBuilder<'a> {
let striped = striped.unwrap_or(ui.visuals().striped);
let state_id = ui.id().with("__table_state");
let state_id = ui.id().with(id_source);
let (is_sizing_pass, state) =
TableState::load(ui, state_id, resizable, &columns, available_width);
@ -482,6 +494,7 @@ impl<'a> TableBuilder<'a> {
let Self {
ui,
id_source,
columns,
striped,
resizable,
@ -492,7 +505,7 @@ impl<'a> TableBuilder<'a> {
let striped = striped.unwrap_or(ui.visuals().striped);
let state_id = ui.id().with("__table_state");
let state_id = ui.id().with(id_source);
let (is_sizing_pass, state) =
TableState::load(ui, state_id, resizable, &columns, available_width);