freya_router/contexts/
navigator.rs

1use crate::prelude::{
2    ExternalNavigationFailure,
3    NavigationTarget,
4    RouterContext,
5};
6
7/// A view into the navigation state of a router.
8#[derive(Clone, Copy)]
9pub struct Navigator(pub(crate) RouterContext);
10
11impl Navigator {
12    pub fn try_get() -> Option<Self> {
13        RouterContext::try_get().map(Navigator)
14    }
15
16    pub fn get() -> Self {
17        Navigator(RouterContext::get())
18    }
19
20    /// Check whether there is a previous page to navigate back to.
21    #[must_use]
22    pub fn can_go_back(&self) -> bool {
23        self.0.can_go_back()
24    }
25
26    /// Check whether there is a future page to navigate forward to.
27    #[must_use]
28    pub fn can_go_forward(&self) -> bool {
29        self.0.can_go_forward()
30    }
31
32    /// Go back to the previous location.
33    ///
34    /// Will fail silently if there is no previous location to go to.
35    pub fn go_back(&self) {
36        self.0.go_back();
37    }
38
39    /// Go back to the next location.
40    ///
41    /// Will fail silently if there is no next location to go to.
42    pub fn go_forward(&self) {
43        self.0.go_forward();
44    }
45
46    /// Push a new location.
47    ///
48    /// The previous location will be available to go back to.
49    pub fn push(&self, target: impl Into<NavigationTarget>) -> Option<ExternalNavigationFailure> {
50        self.0.push(target)
51    }
52
53    /// Replace the current location.
54    ///
55    /// The previous location will **not** be available to go back to.
56    pub fn replace(
57        &self,
58        target: impl Into<NavigationTarget>,
59    ) -> Option<ExternalNavigationFailure> {
60        self.0.replace(target)
61    }
62}