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)]
15pub enum NavigationKind {
16    /// The first page of the app.
17    Enter,
18    /// [`Nav::goto`](crate::extract::Nav::goto) or a link (also when it was redirected).
19    Goto,
20    /// [`Nav::replace`](crate::extract::Nav::replace), or a redirect after back/forward.
21    Replace,
22    /// Back/forward.
23    Popstate,
24    /// [`Nav::invalidate`](crate::extract::Nav::invalidate): the same page was reloaded.
25    Invalidate,
26}
27
28/// Where in the history the new page is, relative to the previous one.
29#[derive(Debug, Clone, Copy, PartialEq, Eq)]
30pub enum Direction {
31    Forward,
32    Back,
33    /// Same entry (replace, invalidate) or unknown (entries from before a browser reload).
34    None,
35}
36
37impl Direction {
38    /// `1.0` forward, `-1.0` back, `0.0` otherwise. Handy to mirror slide animations.
39    pub fn sign(self) -> f32 {
40        match self {
41            Self::Forward => 1.0,
42            Self::Back => -1.0,
43            Self::None => 0.0,
44        }
45    }
46}
47
48/// Options for [`Nav::goto_with`](crate::extract::Nav::goto_with).
49#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
50pub struct GotoOptions {
51    pub(crate) replace_state: bool,
52    pub(crate) no_scroll: bool,
53    pub(crate) invalidate_all: bool,
54}
55
56impl GotoOptions {
57    /// Replace the current history entry instead of adding one.
58    pub fn replace_state(mut self) -> Self {
59        self.replace_state = true;
60        self
61    }
62
63    /// Keep the scroll position of the current page (see [`Scroll`](crate::extract::Scroll)).
64    pub fn no_scroll(mut self) -> Self {
65        self.no_scroll = true;
66        self
67    }
68
69    /// Rerun every `load`, even those whose dependencies didn't change.
70    pub fn invalidate_all(mut self) -> Self {
71        self.invalidate_all = true;
72        self
73    }
74}