egui_extras: Add Table::stick_to_bottom (#1849)

This commit is contained in:
wayne 2022-07-25 13:42:24 +00:00 committed by GitHub
parent 0bf9fc9428
commit 74ccde566d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -56,6 +56,7 @@ pub struct TableBuilder<'a> {
striped: bool, striped: bool,
resizable: bool, resizable: bool,
clip: bool, clip: bool,
stick_to_bottom: bool,
cell_layout: egui::Layout, cell_layout: egui::Layout,
} }
@ -69,6 +70,7 @@ impl<'a> TableBuilder<'a> {
striped: false, striped: false,
resizable: false, resizable: false,
clip: true, clip: true,
stick_to_bottom: false,
cell_layout, cell_layout,
} }
} }
@ -105,6 +107,14 @@ impl<'a> TableBuilder<'a> {
self self
} }
/// Should the scroll handle stick to the bottom position even as the content size changes
/// dynamically? The scroll handle remains stuck until manually changed, and will become stuck
/// once again when repositioned to the bottom. Default: `false`.
pub fn stick_to_bottom(mut self) -> Self {
self.stick_to_bottom = true;
self
}
/// What layout should we use for the individual cells? /// What layout should we use for the individual cells?
pub fn cell_layout(mut self, cell_layout: egui::Layout) -> Self { pub fn cell_layout(mut self, cell_layout: egui::Layout) -> Self {
self.cell_layout = cell_layout; self.cell_layout = cell_layout;
@ -145,6 +155,7 @@ impl<'a> TableBuilder<'a> {
striped, striped,
resizable, resizable,
clip, clip,
stick_to_bottom,
cell_layout, cell_layout,
} = self; } = self;
@ -177,6 +188,7 @@ impl<'a> TableBuilder<'a> {
scroll, scroll,
striped, striped,
clip, clip,
stick_to_bottom,
cell_layout, cell_layout,
} }
} }
@ -195,6 +207,7 @@ impl<'a> TableBuilder<'a> {
striped, striped,
resizable, resizable,
clip, clip,
stick_to_bottom,
cell_layout, cell_layout,
} = self; } = self;
@ -215,6 +228,7 @@ impl<'a> TableBuilder<'a> {
scroll, scroll,
striped, striped,
clip, clip,
stick_to_bottom,
cell_layout, cell_layout,
} }
.body(body); .body(body);
@ -253,6 +267,7 @@ pub struct Table<'a> {
scroll: bool, scroll: bool,
striped: bool, striped: bool,
clip: bool, clip: bool,
stick_to_bottom: bool,
cell_layout: egui::Layout, cell_layout: egui::Layout,
} }
@ -272,6 +287,7 @@ impl<'a> Table<'a> {
scroll, scroll,
striped, striped,
clip, clip,
stick_to_bottom,
cell_layout, cell_layout,
} = self; } = self;
@ -279,20 +295,24 @@ impl<'a> Table<'a> {
let mut new_widths = widths.clone(); let mut new_widths = widths.clone();
egui::ScrollArea::new([false, scroll]) let mut scroll_area = egui::ScrollArea::new([false, scroll]).auto_shrink([true; 2]);
.auto_shrink([true; 2])
.show(ui, move |ui| {
let layout = StripLayout::new(ui, CellDirection::Horizontal, clip, cell_layout);
body(TableBody { if stick_to_bottom {
layout, scroll_area = scroll_area.stick_to_bottom();
widths, }
striped,
row_nr: 0, scroll_area.show(ui, move |ui| {
start_y: avail_rect.top(), let layout = StripLayout::new(ui, CellDirection::Horizontal, clip, cell_layout);
end_y: avail_rect.bottom(),
}); body(TableBody {
layout,
widths,
striped,
row_nr: 0,
start_y: avail_rect.top(),
end_y: avail_rect.bottom(),
}); });
});
let bottom = ui.min_rect().bottom(); let bottom = ui.min_rect().bottom();