Simplify table demo

This commit is contained in:
Emil Ernerfeldt 2022-04-11 17:25:44 +02:00
parent de038b9546
commit 56b127f209

View file

@ -1,11 +1,15 @@
use egui::TextStyle; #[derive(PartialEq)]
use egui_extras::{Size, StripBuilder, TableBuilder, TableRow}; #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
enum DemoType {
Manual,
ManyHomogenous,
ManyHeterogenous,
}
/// Shows off a table with dynamic layout /// Shows off a table with dynamic layout
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct TableDemo { pub struct TableDemo {
heterogeneous_rows: bool, demo: DemoType,
virtual_scroll: bool,
resizable: bool, resizable: bool,
num_rows: usize, num_rows: usize,
} }
@ -13,10 +17,9 @@ pub struct TableDemo {
impl Default for TableDemo { impl Default for TableDemo {
fn default() -> Self { fn default() -> Self {
Self { Self {
heterogeneous_rows: true, demo: DemoType::Manual,
virtual_scroll: false,
resizable: true, resizable: true,
num_rows: 100, num_rows: 10_000,
} }
} }
} }
@ -40,16 +43,23 @@ impl super::Demo for TableDemo {
impl super::View for TableDemo { impl super::View for TableDemo {
fn ui(&mut self, ui: &mut egui::Ui) { fn ui(&mut self, ui: &mut egui::Ui) {
ui.horizontal(|ui| { ui.vertical(|ui| {
ui.vertical(|ui| { ui.checkbox(&mut self.resizable, "Resizable columns");
ui.checkbox(&mut self.resizable, "Resizable columns");
ui.checkbox(&mut self.virtual_scroll, "Virtual Scroll");
if self.virtual_scroll {
ui.checkbox(&mut self.heterogeneous_rows, "Heterogeneous row heights");
}
});
if self.virtual_scroll { ui.label("Table type:");
ui.radio_value(&mut self.demo, DemoType::Manual, "Few, manual rows");
ui.radio_value(
&mut self.demo,
DemoType::ManyHomogenous,
"Thousands of rows of same height",
);
ui.radio_value(
&mut self.demo,
DemoType::ManyHeterogenous,
"Thousands of rows of differing heights",
);
if self.demo != DemoType::Manual {
ui.add( ui.add(
egui::Slider::new(&mut self.num_rows, 0..=100_000) egui::Slider::new(&mut self.num_rows, 0..=100_000)
.logarithmic(true) .logarithmic(true)
@ -58,7 +68,10 @@ impl super::View for TableDemo {
} }
}); });
ui.separator();
// Leave room for the source code link after the table demo: // Leave room for the source code link after the table demo:
use egui_extras::{Size, StripBuilder};
StripBuilder::new(ui) StripBuilder::new(ui)
.size(Size::remainder()) // for the table .size(Size::remainder()) // for the table
.size(Size::exact(10.0)) // for the source code link .size(Size::exact(10.0)) // for the source code link
@ -77,7 +90,9 @@ impl super::View for TableDemo {
impl TableDemo { impl TableDemo {
fn table_ui(&mut self, ui: &mut egui::Ui) { fn table_ui(&mut self, ui: &mut egui::Ui) {
let text_height = TextStyle::Body.resolve(ui.style()).size; use egui_extras::{Size, TableBuilder};
let text_height = egui::TextStyle::Body.resolve(ui.style()).size;
TableBuilder::new(ui) TableBuilder::new(ui)
.striped(true) .striped(true)
@ -97,31 +112,11 @@ impl TableDemo {
ui.heading("Content"); ui.heading("Content");
}); });
}) })
.body(|mut body| { .body(|mut body| match self.demo {
if self.virtual_scroll { DemoType::Manual => {
if !self.heterogeneous_rows {
body.rows(text_height, self.num_rows, |row_index, mut row| {
row.col(|ui| {
ui.label(row_index.to_string());
});
row.col(|ui| {
ui.label(clock_emoji(row_index));
});
row.col(|ui| {
ui.add(
egui::Label::new("Thousands of rows of even height")
.wrap(false),
);
});
});
} else {
let rows = DemoRows::new(self.num_rows);
body.heterogeneous_rows(rows, DemoRows::populate_row);
}
} else {
for row_index in 0..20 { for row_index in 0..20 {
let thick = row_index % 6 == 0; let is_thick = thick_row(row_index);
let row_height = if thick { 30.0 } else { 18.0 }; let row_height = if is_thick { 30.0 } else { 18.0 };
body.row(row_height, |mut row| { body.row(row_height, |mut row| {
row.col(|ui| { row.col(|ui| {
ui.label(row_index.to_string()); ui.label(row_index.to_string());
@ -131,7 +126,7 @@ impl TableDemo {
}); });
row.col(|ui| { row.col(|ui| {
ui.style_mut().wrap = Some(false); ui.style_mut().wrap = Some(false);
if thick { if is_thick {
ui.heading("Extra thick row"); ui.heading("Extra thick row");
} else { } else {
ui.label("Normal row"); ui.label("Normal row");
@ -140,59 +135,56 @@ impl TableDemo {
}); });
} }
} }
}); DemoType::ManyHomogenous => {
} body.rows(text_height, self.num_rows, |row_index, mut row| {
} row.col(|ui| {
ui.label(row_index.to_string());
struct DemoRows { });
row_count: usize, row.col(|ui| {
current_row: usize, ui.label(clock_emoji(row_index));
} });
row.col(|ui| {
impl DemoRows { ui.add(
fn new(row_count: usize) -> Self { egui::Label::new("Thousands of rows of even height").wrap(false),
Self { );
row_count, });
current_row: 0, });
} }
} DemoType::ManyHeterogenous => {
fn row_thickness(row_index: usize) -> f32 {
fn populate_row(index: usize, mut row: TableRow<'_, '_>) { if thick_row(row_index) {
let thick = index % 6 == 0; 30.0
row.col(|ui| { } else {
ui.centered_and_justified(|ui| { 18.0
ui.label(index.to_string()); }
}); }
}); body.heterogeneous_rows(
row.col(|ui| { (0..self.num_rows).into_iter().map(row_thickness),
ui.centered_and_justified(|ui| { |row_index, mut row| {
ui.label(clock_emoji(index)); row.col(|ui| {
}); ui.centered_and_justified(|ui| {
}); ui.label(row_index.to_string());
row.col(|ui| { });
ui.centered_and_justified(|ui| { });
ui.style_mut().wrap = Some(false); row.col(|ui| {
if thick { ui.centered_and_justified(|ui| {
ui.heading("Extra thick row"); ui.label(clock_emoji(row_index));
} else { });
ui.label("Normal row"); });
row.col(|ui| {
ui.centered_and_justified(|ui| {
ui.style_mut().wrap = Some(false);
if thick_row(row_index) {
ui.heading("Extra thick row");
} else {
ui.label("Normal row");
}
});
});
},
);
} }
}); });
});
}
}
impl Iterator for DemoRows {
type Item = f32;
fn next(&mut self) -> Option<Self::Item> {
if self.current_row < self.row_count {
let thick = self.current_row % 6 == 0;
self.current_row += 1;
Some(if thick { 30.0 } else { 18.0 })
} else {
None
}
} }
} }
@ -201,3 +193,7 @@ fn clock_emoji(row_index: usize) -> String {
.unwrap() .unwrap()
.to_string() .to_string()
} }
fn thick_row(row_index: usize) -> bool {
row_index % 6 == 0
}