egui_kit/extract/
scroll.rs1use core::cell::Cell;
4
5use egui::Ui;
6
7use super::FromView;
8use crate::page::{Frame, ViewContext};
9
10#[derive(Debug, Default)]
12pub(crate) struct PageScroll {
13 pub(crate) restore: Cell<Option<f32>>,
15 pub(crate) offset: Cell<f32>,
16}
17
18#[derive(Clone, Copy)]
33pub struct Scroll<'f> {
34 pub(crate) frame: &'f Frame<'f>,
35}
36
37impl<S: 'static, D> FromView<S, D> for Scroll<'_> {
38 type Item<'f> = Scroll<'f>;
39
40 fn from_view<'f>(cx: &'f ViewContext<'_, S>) -> Scroll<'f> {
41 Scroll { frame: cx.frame() }
42 }
43}
44
45impl Scroll<'_> {
46 pub fn show<R>(&self, ui: &mut Ui, add_contents: impl FnOnce(&mut Ui) -> R) -> R {
48 let frame = self.frame;
49 let mut area = egui::ScrollArea::vertical()
50 .id_salt(("egui_kit_page_scroll", frame.page_id))
51 .auto_shrink(false);
52 let offset = if frame.outgoing {
54 Some(frame.scroll.offset.get())
55 } else {
56 frame.scroll.restore.take()
57 };
58 if let Some(offset) = offset {
59 area = area.vertical_scroll_offset(offset);
60 }
61 let output = area.show(ui, add_contents);
62 if !frame.outgoing {
63 frame.scroll.offset.set(output.state.clamped_offset().y);
64 }
65 output.inner
66 }
67}