Skip to content

Commit

Permalink
Add active_route fn
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmerlin committed May 15, 2024
1 parent 1d0ec09 commit 6d926a8
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions crates/egui_router/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub trait Route<State> {
static ID: AtomicUsize = AtomicUsize::new(0);

struct RouteState<State> {
path: String,
route: Box<dyn Route<State>>,
id: usize,
}
Expand Down Expand Up @@ -148,6 +149,10 @@ impl<State> EguiRouter<State> {
self
}

pub fn active_route(&self) -> Option<&str> {
self.history.last().map(|r| r.path.as_str())
}

pub fn route(
mut self,
route: impl Into<String>,
Expand All @@ -161,18 +166,19 @@ impl<State> EguiRouter<State> {

pub fn navigate_transition(
&mut self,
route: impl Into<String>,
path: impl Into<String>,
transition_config: TransitionConfig,
) {
let route = route.into();
let mut handler = self.router.at_mut(&route);
let path = path.into();
let mut handler = self.router.at_mut(&path);

if let Ok(handler) = handler {
let route = handler.value.handle(Request {
state: &mut self.state,
params: handler.params,
});
self.history.push(RouteState {
path,
route,
id: ID.fetch_add(1, Ordering::SeqCst),
});
Expand All @@ -183,7 +189,7 @@ impl<State> EguiRouter<State> {
leaving_route: None,
});
} else {
eprintln!("Failed to navigate to route: {}", route);
eprintln!("Failed to navigate to route");
}
}

Expand All @@ -208,11 +214,11 @@ impl<State> EguiRouter<State> {

pub fn replace_transition(
&mut self,
route: impl Into<String>,
path: impl Into<String>,
transition_config: TransitionConfig,
) {
let route = route.into();
let handler = self.router.at_mut(&route);
let path = path.into();
let handler = self.router.at_mut(&path);

if let Ok(handler) = handler {
let leaving_route = self.history.pop();
Expand All @@ -221,6 +227,7 @@ impl<State> EguiRouter<State> {
params: handler.params,
});
self.history.push(RouteState {
path,
route,
id: ID.fetch_add(1, Ordering::SeqCst),
});
Expand All @@ -231,7 +238,7 @@ impl<State> EguiRouter<State> {
leaving_route,
});
} else {
eprintln!("Failed to navigate to route: {}", route);
eprintln!("Failed to navigate to route");
}
}

Expand Down

0 comments on commit 6d926a8

Please sign in to comment.