Skip to main content

egui_kit/extract/
history_state.rs

1//! [`HistoryState`]: the shallow-routing state of the current history entry.
2
3use core::any::Any;
4
5use super::FromView;
6use crate::page::ViewContext;
7
8/// The state attached to the current history entry, if it is a `T`.
9///
10/// It is set with [`Nav::push_state`](super::Nav::push_state) or
11/// [`Nav::replace_state`](super::Nav::replace_state). Borrowed: extract it as `HistoryState<&T>`.
12///
13/// It belongs to the history entry, not to the page: back and forward between the entries of a
14/// page swap it, and coming back to an entry of a page that was left restores it. A new
15/// navigation starts without it, and a browser reload loses it.
16///
17/// For state the view owns and mutates, see [`PageState`](super::PageState) and
18/// [`Snapshot`](super::Snapshot).
19#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20pub struct HistoryState<T>(pub Option<T>);
21
22impl<S: 'static, D, T: Any> FromView<S, D> for HistoryState<&'_ T> {
23    type Item<'f> = HistoryState<&'f T>;
24
25    fn from_view<'f>(cx: &'f ViewContext<'_, S>) -> HistoryState<&'f T> {
26        HistoryState(
27            cx.frame()
28                .history_state
29                .and_then(|state| state.downcast_ref()),
30        )
31    }
32}