1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-31 22:00:03 -04:00

egui_extras: Add Table::vertical_scroll_offset (#1946)

* egui_extras: Add Table::vertical_scroll_offset

* Added example for TableBuilder::vertical_scroll_offset

* Format code

* Add link to PR in the changelog

Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
This commit is contained in:
njust
2022-08-28 09:37:23 +02:00
committed by GitHub
parent af63101fdc
commit dbfaa4527b
3 changed files with 51 additions and 4 deletions

View File

@@ -12,6 +12,8 @@ pub struct TableDemo {
demo: DemoType,
resizable: bool,
num_rows: usize,
row_to_scroll_to: i32,
vertical_scroll_offset: Option<f32>,
}
impl Default for TableDemo {
@@ -20,6 +22,8 @@ impl Default for TableDemo {
demo: DemoType::Manual,
resizable: true,
num_rows: 10_000,
row_to_scroll_to: 0,
vertical_scroll_offset: None,
}
}
}
@@ -41,6 +45,12 @@ impl super::Demo for TableDemo {
}
}
fn scroll_offset_for_row(ui: &egui::Ui, row: i32) -> f32 {
let text_height = egui::TextStyle::Body.resolve(ui.style()).size;
let row_item_spacing = ui.spacing().item_spacing.y;
row as f32 * (text_height + row_item_spacing)
}
impl super::View for TableDemo {
fn ui(&mut self, ui: &mut egui::Ui) {
ui.vertical(|ui| {
@@ -66,6 +76,19 @@ impl super::View for TableDemo {
.text("Num rows"),
);
}
if self.demo == DemoType::ManyHomogenous {
ui.add(
egui::Slider::new(&mut self.row_to_scroll_to, 0..=self.num_rows as i32)
.logarithmic(true)
.text("Row to scroll to"),
);
if ui.button("Scroll to row").clicked() {
self.vertical_scroll_offset
.replace(scroll_offset_for_row(ui, self.row_to_scroll_to));
}
}
});
ui.separator();
@@ -94,13 +117,19 @@ impl TableDemo {
let text_height = egui::TextStyle::Body.resolve(ui.style()).size;
TableBuilder::new(ui)
let mut table = TableBuilder::new(ui)
.striped(true)
.cell_layout(egui::Layout::left_to_right(egui::Align::Center))
.column(Size::initial(60.0).at_least(40.0))
.column(Size::initial(60.0).at_least(40.0))
.column(Size::remainder().at_least(60.0))
.resizable(self.resizable)
.resizable(self.resizable);
if let Some(y_scroll) = self.vertical_scroll_offset.take() {
table = table.vertical_scroll_offset(y_scroll);
}
table
.header(20.0, |mut header| {
header.col(|ui| {
ui.heading("Row");