Add `TableBuilder::id_source` (#5022)
* Closes https://github.com/emilk/egui/issues/4982
This commit is contained in:
parent
8e5492b6e8
commit
82036cf59a
|
|
@ -5,7 +5,7 @@
|
||||||
|
|
||||||
use egui::{
|
use egui::{
|
||||||
scroll_area::{ScrollAreaOutput, ScrollBarVisibility},
|
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::{
|
use crate::{
|
||||||
|
|
@ -199,7 +199,7 @@ impl Default for TableScrollOptions {
|
||||||
/// You must pre-allocate all columns with [`Self::column`]/[`Self::columns`].
|
/// You must pre-allocate all columns with [`Self::column`]/[`Self::columns`].
|
||||||
///
|
///
|
||||||
/// If you have multiple [`Table`]:s in the same [`Ui`]
|
/// 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
|
/// ### Example
|
||||||
/// ```
|
/// ```
|
||||||
|
|
@ -230,6 +230,7 @@ impl Default for TableScrollOptions {
|
||||||
/// ```
|
/// ```
|
||||||
pub struct TableBuilder<'a> {
|
pub struct TableBuilder<'a> {
|
||||||
ui: &'a mut Ui,
|
ui: &'a mut Ui,
|
||||||
|
id_source: Id,
|
||||||
columns: Vec<Column>,
|
columns: Vec<Column>,
|
||||||
striped: Option<bool>,
|
striped: Option<bool>,
|
||||||
resizable: bool,
|
resizable: bool,
|
||||||
|
|
@ -243,6 +244,7 @@ impl<'a> TableBuilder<'a> {
|
||||||
let cell_layout = *ui.layout();
|
let cell_layout = *ui.layout();
|
||||||
Self {
|
Self {
|
||||||
ui,
|
ui,
|
||||||
|
id_source: Id::new("__table_state"),
|
||||||
columns: Default::default(),
|
columns: Default::default(),
|
||||||
striped: None,
|
striped: None,
|
||||||
resizable: false,
|
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.
|
/// Enable striped row background for improved readability.
|
||||||
///
|
///
|
||||||
/// Default is whatever is in [`egui::Visuals::striped`].
|
/// Default is whatever is in [`egui::Visuals::striped`].
|
||||||
|
|
@ -406,7 +417,7 @@ impl<'a> TableBuilder<'a> {
|
||||||
|
|
||||||
/// Reset all column widths.
|
/// Reset all column widths.
|
||||||
pub fn reset(&mut self) {
|
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);
|
TableState::reset(self.ui, state_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -416,6 +427,7 @@ impl<'a> TableBuilder<'a> {
|
||||||
|
|
||||||
let Self {
|
let Self {
|
||||||
ui,
|
ui,
|
||||||
|
id_source,
|
||||||
columns,
|
columns,
|
||||||
striped,
|
striped,
|
||||||
resizable,
|
resizable,
|
||||||
|
|
@ -426,7 +438,7 @@ impl<'a> TableBuilder<'a> {
|
||||||
|
|
||||||
let striped = striped.unwrap_or(ui.visuals().striped);
|
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) =
|
let (is_sizing_pass, state) =
|
||||||
TableState::load(ui, state_id, resizable, &columns, available_width);
|
TableState::load(ui, state_id, resizable, &columns, available_width);
|
||||||
|
|
@ -482,6 +494,7 @@ impl<'a> TableBuilder<'a> {
|
||||||
|
|
||||||
let Self {
|
let Self {
|
||||||
ui,
|
ui,
|
||||||
|
id_source,
|
||||||
columns,
|
columns,
|
||||||
striped,
|
striped,
|
||||||
resizable,
|
resizable,
|
||||||
|
|
@ -492,7 +505,7 @@ impl<'a> TableBuilder<'a> {
|
||||||
|
|
||||||
let striped = striped.unwrap_or(ui.visuals().striped);
|
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) =
|
let (is_sizing_pass, state) =
|
||||||
TableState::load(ui, state_id, resizable, &columns, available_width);
|
TableState::load(ui, state_id, resizable, &columns, available_width);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue