navigator.rs 1.5 KB

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