add virtual scroll demo

This commit is contained in:
René Rössler 2021-12-17 18:18:25 +01:00
parent e00c726bff
commit 31d8cbb2c0

View file

@ -1,10 +1,12 @@
use egui::RichText;
use egui::{Label, RichText};
use egui_dynamic_grid::{Padding, Size, TableBuilder};
/// Shows off a table with dynamic layout
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[derive(Default)]
pub struct TableDemo {}
pub struct TableDemo {
virtual_scrool: bool,
}
impl super::Demo for TableDemo {
fn name(&self) -> &'static str {
@ -15,7 +17,7 @@ impl super::Demo for TableDemo {
egui::Window::new(self.name())
.open(open)
.resizable(true)
.default_width(300.0)
.default_width(400.0)
.show(ctx, |ui| {
use super::View as _;
self.ui(ui);
@ -25,12 +27,14 @@ impl super::Demo for TableDemo {
impl super::View for TableDemo {
fn ui(&mut self, ui: &mut egui::Ui) {
ui.checkbox(&mut self.virtual_scrool, "Virtual scroll demo");
// TODO: Fix table as a padding smaller than 16 grows the window
TableBuilder::new(ui, Padding::new(3.0, 16.0))
.striped(true)
.column(Size::Absolute(120.0))
.column(Size::RemainderMinimum(180.0))
.column(Size::Absolute(100.0))
.column(Size::RemainderMinimum(150.0))
.column(Size::Absolute(50.0))
.header(20.0, |mut header| {
header.col(|ui| {
ui.label(RichText::new("Left").heading());
@ -43,18 +47,45 @@ impl super::View for TableDemo {
});
})
.body(|mut body| {
for i in 0..100 {
body.row(20.0, |mut row| {
if self.virtual_scrool {
body.rows(20.0, 100_000, |index, mut row| {
row.col(|ui| {
ui.label(format!("{}", i));
ui.label(format!("{}", index));
});
row.col(|ui| {
ui.label(format!("{}", i));
ui.add(
Label::new("virtual scroll, easily with thousands of rows!")
.wrap(false),
);
});
row.col(|ui| {
ui.label(format!("{}", i));
ui.label(format!("{}", index));
});
});
} else {
for i in 0..100 {
let height = match i % 8 {
0 => 25.0,
4 => 30.0,
_ => 20.0,
};
body.row(height, |mut row| {
row.col(|ui| {
ui.label(format!("{}", i));
});
row.col(|ui| {
ui.add(
Label::new(
format!("Normal scroll, each row can have a different height. Height: {}", height),
)
.wrap(false),
);
});
row.col(|ui| {
ui.label(format!("{}", i));
});
});
}
}
});
}