Skip to content

Commit

Permalink
egui_extras: Add Table::stick_to_bottom (#1849)
Browse files Browse the repository at this point in the history
  • Loading branch information
waynr authored Jul 25, 2022
1 parent 0bf9fc9 commit 74ccde5
Showing 1 changed file with 33 additions and 13 deletions.
46 changes: 33 additions & 13 deletions egui_extras/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pub struct TableBuilder<'a> {
striped: bool,
resizable: bool,
clip: bool,
stick_to_bottom: bool,
cell_layout: egui::Layout,
}

Expand All @@ -69,6 +70,7 @@ impl<'a> TableBuilder<'a> {
striped: false,
resizable: false,
clip: true,
stick_to_bottom: false,
cell_layout,
}
}
Expand Down Expand Up @@ -105,6 +107,14 @@ impl<'a> TableBuilder<'a> {
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?
pub fn cell_layout(mut self, cell_layout: egui::Layout) -> Self {
self.cell_layout = cell_layout;
Expand Down Expand Up @@ -145,6 +155,7 @@ impl<'a> TableBuilder<'a> {
striped,
resizable,
clip,
stick_to_bottom,
cell_layout,
} = self;

Expand Down Expand Up @@ -177,6 +188,7 @@ impl<'a> TableBuilder<'a> {
scroll,
striped,
clip,
stick_to_bottom,
cell_layout,
}
}
Expand All @@ -195,6 +207,7 @@ impl<'a> TableBuilder<'a> {
striped,
resizable,
clip,
stick_to_bottom,
cell_layout,
} = self;

Expand All @@ -215,6 +228,7 @@ impl<'a> TableBuilder<'a> {
scroll,
striped,
clip,
stick_to_bottom,
cell_layout,
}
.body(body);
Expand Down Expand Up @@ -253,6 +267,7 @@ pub struct Table<'a> {
scroll: bool,
striped: bool,
clip: bool,
stick_to_bottom: bool,
cell_layout: egui::Layout,
}

Expand All @@ -272,27 +287,32 @@ impl<'a> Table<'a> {
scroll,
striped,
clip,
stick_to_bottom,
cell_layout,
} = self;

let avail_rect = ui.available_rect_before_wrap();

let mut new_widths = widths.clone();

egui::ScrollArea::new([false, scroll])
.auto_shrink([true; 2])
.show(ui, move |ui| {
let layout = StripLayout::new(ui, CellDirection::Horizontal, clip, cell_layout);

body(TableBody {
layout,
widths,
striped,
row_nr: 0,
start_y: avail_rect.top(),
end_y: avail_rect.bottom(),
});
let mut scroll_area = egui::ScrollArea::new([false, scroll]).auto_shrink([true; 2]);

if stick_to_bottom {
scroll_area = scroll_area.stick_to_bottom();
}

scroll_area.show(ui, move |ui| {
let layout = StripLayout::new(ui, CellDirection::Horizontal, clip, cell_layout);

body(TableBody {
layout,
widths,
striped,
row_nr: 0,
start_y: avail_rect.top(),
end_y: avail_rect.bottom(),
});
});

let bottom = ui.min_rect().bottom();

Expand Down

0 comments on commit 74ccde5

Please sign in to comment.