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

implement stick-to-end scroll (#765)

* implement stick-to-end scroll

* improve comment grammar

* accept emilk suggestion for demo text tweak

Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>

* request repaint on each frame to show incoming scroll demo rows

* simplify pub api + doc strings

* disable scroll_stuck_to_end when wheel-scrolling or dragging

Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
This commit is contained in:
Ben Postlethwaite
2021-10-09 03:59:42 -07:00
committed by GitHub
parent 1dfc399d98
commit 5799758c2b
2 changed files with 104 additions and 1 deletions

View File

@@ -6,6 +6,7 @@ enum ScrollDemo {
ScrollTo,
ManyLines,
LargeCanvas,
StickToEnd,
}
impl Default for ScrollDemo {
@@ -20,6 +21,7 @@ impl Default for ScrollDemo {
pub struct Scrolling {
demo: ScrollDemo,
scroll_to: ScrollTo,
scroll_stick_to: ScrollStickTo,
}
impl super::Demo for Scrolling {
@@ -52,6 +54,7 @@ impl super::View for Scrolling {
ScrollDemo::LargeCanvas,
"Scroll a large canvas",
);
ui.selectable_value(&mut self.demo, ScrollDemo::StickToEnd, "Stick to end");
});
ui.separator();
match self.demo {
@@ -64,6 +67,9 @@ impl super::View for Scrolling {
ScrollDemo::LargeCanvas => {
huge_content_painter(ui);
}
ScrollDemo::StickToEnd => {
self.scroll_stick_to.ui(ui);
}
}
}
}
@@ -244,3 +250,41 @@ impl super::View for ScrollTo {
});
}
}
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "serde", serde(default))]
#[derive(PartialEq)]
struct ScrollStickTo {
n_items: usize,
}
impl Default for ScrollStickTo {
fn default() -> Self {
Self { n_items: 0 }
}
}
impl super::View for ScrollStickTo {
fn ui(&mut self, ui: &mut Ui) {
ui.label("Rows enter from the bottom, we want the scroll handle to start and stay at bottom unless moved");
ui.add_space(4.0);
let text_style = TextStyle::Body;
let row_height = ui.fonts()[text_style].row_height();
ScrollArea::vertical().stick_to_bottom().show_rows(
ui,
row_height,
self.n_items,
|ui, row_range| {
for row in row_range {
let text = format!("This is row {}", row + 1);
ui.label(text);
}
},
);
self.n_items += 1;
ui.ctx().request_repaint();
}
}