Skip to main content

egui_kit/
navigation.rs

1//! Describing navigations: where from, where to, how.
2
3/// How the page on screen was reached. An extractor for views,
4/// and passed to [`after_navigate`](crate::RouterApp::after_navigate) callbacks.
5#[derive(Debug, Clone, PartialEq, Eq)]
6pub struct Navigation {
7    /// The url of the previous page, `None` for the first page.
8    pub from: Option<String>,
9    pub to: String,
10    pub kind: NavigationKind,
11    pub direction: Direction,
12}
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq)]
15#[non_exhaustive]
16pub enum NavigationKind {
17    /// The first page of the app.
18    Enter,
19    /// [`Nav::goto`](crate::extract::Nav::goto) or a link (also when it was redirected).
20    Goto,
21    /// [`Nav::replace`](crate::extract::Nav::replace), or a redirect after back/forward.
22    Replace,
23    /// Back/forward.
24    Popstate,
25    /// [`Nav::invalidate`](crate::extract::Nav::invalidate): the same page was reloaded.
26    Invalidate,
27    /// The window is being closed; `to` is empty. Only seen as a
28    /// [blocked navigation](crate::extract::Nav::blocked_navigation), natively.
29    Leave,
30}
31
32/// Where in the history the new page is, relative to the previous one.
33#[derive(Debug, Clone, Copy, PartialEq, Eq)]
34pub enum Direction {
35    Forward,
36    Back,
37    /// Same entry (replace, invalidate) or unknown (entries from before a browser reload).
38    None,
39}
40
41impl Direction {
42    /// `1.0` forward, `-1.0` back, `0.0` otherwise. Handy to mirror slide animations.
43    pub fn sign(self) -> f32 {
44        match self {
45            Self::Forward => 1.0,
46            Self::Back => -1.0,
47            Self::None => 0.0,
48        }
49    }
50}
51
52/// Options for [`Nav::goto_with`](crate::extract::Nav::goto_with).
53#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
54pub struct GotoOptions {
55    pub(crate) replace_state: bool,
56    pub(crate) no_scroll: bool,
57    pub(crate) invalidate_all: bool,
58}
59
60impl GotoOptions {
61    /// Replace the current history entry instead of adding one.
62    pub fn replace_state(mut self) -> Self {
63        self.replace_state = true;
64        self
65    }
66
67    /// Keep the scroll position of the current page (see [`Scroll`](crate::extract::Scroll)).
68    pub fn no_scroll(mut self) -> Self {
69        self.no_scroll = true;
70        self
71    }
72
73    /// Rerun every `load`, even those whose dependencies didn't change.
74    pub fn invalidate_all(mut self) -> Self {
75        self.invalidate_all = true;
76        self
77    }
78}