2022-04-01 13:27:42 +00:00
|
|
|
use egui::TextStyle;
|
2022-04-09 11:18:33 +00:00
|
|
|
use egui_extras::{Size, StripBuilder, TableBuilder, TableRow};
|
2022-03-31 19:13:25 +00:00
|
|
|
|
|
|
|
/// Shows off a table with dynamic layout
|
|
|
|
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
|
|
|
|
pub struct TableDemo {
|
2022-04-09 11:18:33 +00:00
|
|
|
heterogeneous_rows: bool,
|
2022-03-31 19:13:25 +00:00
|
|
|
virtual_scroll: bool,
|
2022-04-01 10:01:00 +00:00
|
|
|
resizable: bool,
|
2022-04-09 11:18:33 +00:00
|
|
|
num_rows: usize,
|
2022-03-31 19:13:25 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 06:27:26 +00:00
|
|
|
impl Default for TableDemo {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
heterogeneous_rows: true,
|
|
|
|
virtual_scroll: false,
|
|
|
|
resizable: true,
|
|
|
|
num_rows: 100,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-31 19:13:25 +00:00
|
|
|
impl super::Demo for TableDemo {
|
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
"☰ Table Demo"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn show(&mut self, ctx: &egui::Context, open: &mut bool) {
|
|
|
|
egui::Window::new(self.name())
|
|
|
|
.open(open)
|
|
|
|
.resizable(true)
|
|
|
|
.default_width(400.0)
|
|
|
|
.show(ctx, |ui| {
|
|
|
|
use super::View as _;
|
|
|
|
self.ui(ui);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl super::View for TableDemo {
|
|
|
|
fn ui(&mut self, ui: &mut egui::Ui) {
|
2022-04-05 06:27:26 +00:00
|
|
|
ui.horizontal(|ui| {
|
|
|
|
ui.vertical(|ui| {
|
|
|
|
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.add(
|
|
|
|
egui::Slider::new(&mut self.num_rows, 0..=100_000)
|
|
|
|
.logarithmic(true)
|
|
|
|
.text("Num rows"),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-03-31 19:13:25 +00:00
|
|
|
// Leave room for the source code link after the table demo:
|
|
|
|
StripBuilder::new(ui)
|
2022-04-01 10:01:00 +00:00
|
|
|
.size(Size::remainder()) // for the table
|
|
|
|
.size(Size::exact(10.0)) // for the source code link
|
2022-03-31 19:13:25 +00:00
|
|
|
.vertical(|mut strip| {
|
2022-04-01 13:27:42 +00:00
|
|
|
strip.cell(|ui| {
|
2022-03-31 19:13:25 +00:00
|
|
|
self.table_ui(ui);
|
|
|
|
});
|
|
|
|
strip.cell(|ui| {
|
|
|
|
ui.vertical_centered(|ui| {
|
|
|
|
ui.add(crate::__egui_github_link_file!());
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TableDemo {
|
|
|
|
fn table_ui(&mut self, ui: &mut egui::Ui) {
|
2022-04-01 13:27:42 +00:00
|
|
|
let text_height = TextStyle::Body.resolve(ui.style()).size;
|
|
|
|
|
2022-03-31 19:13:25 +00:00
|
|
|
TableBuilder::new(ui)
|
|
|
|
.striped(true)
|
2022-04-11 08:29:34 +00:00
|
|
|
.cell_layout(egui::Layout::left_to_right().with_cross_align(egui::Align::Center))
|
2022-04-01 10:01:00 +00:00
|
|
|
.column(Size::initial(60.0).at_least(40.0))
|
|
|
|
.column(Size::initial(60.0).at_least(40.0))
|
2022-04-01 13:27:42 +00:00
|
|
|
.column(Size::remainder().at_least(60.0))
|
2022-04-01 10:01:00 +00:00
|
|
|
.resizable(self.resizable)
|
2022-03-31 19:13:25 +00:00
|
|
|
.header(20.0, |mut header| {
|
2022-04-01 13:27:42 +00:00
|
|
|
header.col(|ui| {
|
2022-04-11 08:29:34 +00:00
|
|
|
ui.heading("Row");
|
2022-03-31 19:13:25 +00:00
|
|
|
});
|
2022-04-01 13:27:42 +00:00
|
|
|
header.col(|ui| {
|
2022-04-11 08:29:34 +00:00
|
|
|
ui.heading("Clock");
|
2022-03-31 19:13:25 +00:00
|
|
|
});
|
2022-04-01 13:27:42 +00:00
|
|
|
header.col(|ui| {
|
2022-04-11 08:29:34 +00:00
|
|
|
ui.heading("Content");
|
2022-03-31 19:13:25 +00:00
|
|
|
});
|
|
|
|
})
|
|
|
|
.body(|mut body| {
|
|
|
|
if self.virtual_scroll {
|
2022-04-09 11:18:33 +00:00
|
|
|
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),
|
|
|
|
);
|
|
|
|
});
|
2022-03-31 19:13:25 +00:00
|
|
|
});
|
2022-04-09 11:18:33 +00:00
|
|
|
} else {
|
|
|
|
let rows = DemoRows::new(self.num_rows);
|
|
|
|
body.heterogeneous_rows(rows, DemoRows::populate_row);
|
|
|
|
}
|
2022-03-31 19:13:25 +00:00
|
|
|
} else {
|
2022-04-01 13:27:42 +00:00
|
|
|
for row_index in 0..20 {
|
|
|
|
let thick = row_index % 6 == 0;
|
|
|
|
let row_height = if thick { 30.0 } else { 18.0 };
|
|
|
|
body.row(row_height, |mut row| {
|
|
|
|
row.col(|ui| {
|
2022-04-11 08:29:34 +00:00
|
|
|
ui.label(row_index.to_string());
|
2022-03-31 19:13:25 +00:00
|
|
|
});
|
2022-04-01 13:27:42 +00:00
|
|
|
row.col(|ui| {
|
2022-04-11 08:29:34 +00:00
|
|
|
ui.label(clock_emoji(row_index));
|
2022-03-31 19:13:25 +00:00
|
|
|
});
|
2022-04-01 13:27:42 +00:00
|
|
|
row.col(|ui| {
|
2022-04-11 08:29:34 +00:00
|
|
|
ui.style_mut().wrap = Some(false);
|
|
|
|
if thick {
|
|
|
|
ui.heading("Extra thick row");
|
|
|
|
} else {
|
|
|
|
ui.label("Normal row");
|
|
|
|
}
|
2022-03-31 19:13:25 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2022-04-01 13:27:42 +00:00
|
|
|
|
2022-04-09 11:18:33 +00:00
|
|
|
struct DemoRows {
|
|
|
|
row_count: usize,
|
|
|
|
current_row: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DemoRows {
|
|
|
|
fn new(row_count: usize) -> Self {
|
|
|
|
Self {
|
|
|
|
row_count,
|
|
|
|
current_row: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn populate_row(index: usize, mut row: TableRow<'_, '_>) {
|
|
|
|
let thick = index % 6 == 0;
|
|
|
|
row.col(|ui| {
|
|
|
|
ui.centered_and_justified(|ui| {
|
|
|
|
ui.label(index.to_string());
|
|
|
|
});
|
|
|
|
});
|
|
|
|
row.col(|ui| {
|
|
|
|
ui.centered_and_justified(|ui| {
|
|
|
|
ui.label(clock_emoji(index));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
row.col(|ui| {
|
|
|
|
ui.centered_and_justified(|ui| {
|
|
|
|
ui.style_mut().wrap = Some(false);
|
|
|
|
if thick {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-01 13:27:42 +00:00
|
|
|
fn clock_emoji(row_index: usize) -> String {
|
|
|
|
char::from_u32(0x1f550 + row_index as u32 % 24)
|
|
|
|
.unwrap()
|
|
|
|
.to_string()
|
|
|
|
}
|