Skip to content

Commit

Permalink
Add TableBuilder::id_source (#5022)
Browse files Browse the repository at this point in the history
* Closes #4982
  • Loading branch information
emilk authored Aug 28, 2024
1 parent 8e5492b commit 82036cf
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions crates/egui_extras/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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
/// ```
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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`].
Expand Down Expand Up @@ -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);
}

Expand All @@ -416,6 +427,7 @@ impl<'a> TableBuilder<'a> {

let Self {
ui,
id_source,
columns,
striped,
resizable,
Expand All @@ -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);
Expand Down Expand Up @@ -482,6 +494,7 @@ impl<'a> TableBuilder<'a> {

let Self {
ui,
id_source,
columns,
striped,
resizable,
Expand All @@ -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);
Expand Down

0 comments on commit 82036cf

Please sign in to comment.