use core::cell::Cell;
use egui::Ui;
use super::FromView;
use crate::page::{Frame, ViewContext};
#[derive(Debug, Default)]
pub(crate) struct PageScroll {
pub(crate) restore: Cell<Option<f32>>,
pub(crate) offset: Cell<f32>,
}
#[derive(Clone, Copy)]
pub struct Scroll<'f> {
pub(crate) frame: &'f Frame<'f>,
}
impl<S: 'static, D> FromView<S, D> for Scroll<'_> {
type Item<'f> = Scroll<'f>;
fn from_view<'f>(cx: &'f ViewContext<'_, S>) -> Scroll<'f> {
Scroll { frame: cx.frame() }
}
}
impl Scroll<'_> {
pub fn show<R>(&self, ui: &mut Ui, add_contents: impl FnOnce(&mut Ui) -> R) -> R {
let frame = self.frame;
let mut area = egui::ScrollArea::vertical()
.id_salt(("egui_kit_page_scroll", frame.page_id))
.auto_shrink(false);
let offset = if frame.outgoing {
Some(frame.scroll.offset.get())
} else {
frame.scroll.restore.take()
};
if let Some(offset) = offset {
area = area.vertical_scroll_offset(offset);
}
let output = area.show(ui, add_contents);
if !frame.outgoing {
frame.scroll.offset.set(output.state.clamped_offset().y);
}
output.inner
}
}