use core::any::Any;
use std::rc::Rc;
use egui::{Response, Ui};
use super::FromView;
use crate::load::Invalidation;
use crate::navigation::{GotoOptions, Navigation};
use crate::page::{Frame, ViewContext};
pub(crate) enum NavAction {
Goto(String, GotoOptions),
Back,
Forward,
Invalidate(Invalidation),
Preload(String),
PushState(String, Option<Rc<dyn Any>>),
ReplaceState(String, Option<Rc<dyn Any>>),
ConfirmNavigation,
CancelNavigation,
}
#[derive(Default)]
pub(crate) struct NavState {
pub(crate) actions: Vec<NavAction>,
pub(crate) loading: Option<String>,
pub(crate) can_go_back: bool,
pub(crate) can_go_forward: bool,
pub(crate) preload_on_hover: bool,
pub(crate) blocking: bool,
pub(crate) blocked: Option<Navigation>,
}
#[derive(Clone, Copy)]
pub struct Nav<'f> {
pub(crate) frame: &'f Frame<'f>,
}
impl<S: 'static, D> FromView<S, D> for Nav<'_> {
type Item<'f> = Nav<'f>;
fn from_view<'f>(cx: &'f ViewContext<'_, S>) -> Nav<'f> {
cx.nav()
}
}
impl Nav<'_> {
fn push(&self, action: NavAction) {
self.frame.nav.borrow_mut().actions.push(action);
}
}
impl<'f> Nav<'f> {
pub fn url(&self) -> &'f str {
self.frame.url
}
pub fn is_active(&self, href: &str) -> bool {
let href = crate::router::normalize(href.split('?').next().unwrap_or_default());
let path = self.frame.request.untracked_path();
path == href
|| (href != "/" && path.starts_with(&href) && path[href.len()..].starts_with('/'))
}
pub fn is_outgoing(&self) -> bool {
self.frame.outgoing
}
}
impl Nav<'_> {
pub fn goto(&self, url: impl Into<String>) {
self.goto_with(url, GotoOptions::default());
}
pub fn goto_with(&self, url: impl Into<String>, options: GotoOptions) {
self.push(NavAction::Goto(url.into(), options));
}
pub fn replace(&self, url: impl Into<String>) {
self.goto_with(url, GotoOptions::default().replace_state());
}
pub fn back(&self) {
self.push(NavAction::Back);
}
pub fn forward(&self) {
self.push(NavAction::Forward);
}
pub fn can_go_back(&self) -> bool {
self.frame.nav.borrow().can_go_back
}
pub fn can_go_forward(&self) -> bool {
self.frame.nav.borrow().can_go_forward
}
pub fn link(&self, ui: &mut Ui, text: impl Into<egui::WidgetText>, href: &str) -> Response {
let response = ui.link(text).on_hover_text(href);
self.follow(&response, href);
response
}
pub fn nav_link(&self, ui: &mut Ui, text: impl Into<egui::WidgetText>, href: &str) -> Response {
let response = ui.selectable_label(self.is_active(href), text);
self.follow(&response, href);
response
}
fn follow(&self, response: &Response, href: &str) {
if response.clicked() {
self.goto(href);
} else if response.hovered() && self.frame.nav.borrow().preload_on_hover {
self.preload(href);
}
}
}
impl Nav<'_> {
pub fn is_loading(&self) -> bool {
self.frame.nav.borrow().loading.is_some()
}
pub fn loading_url(&self) -> Option<String> {
self.frame.nav.borrow().loading.clone()
}
pub fn preload(&self, url: impl Into<String>) {
self.push(NavAction::Preload(url.into()));
}
pub fn invalidate(&self, key: impl Into<String>) {
self.push(NavAction::Invalidate(Invalidation::Keys(vec![key.into()])));
}
pub fn invalidate_all(&self) {
self.push(NavAction::Invalidate(Invalidation::All));
}
}
impl Nav<'_> {
pub fn push_state<T: Any>(&self, url: impl Into<String>, state: T) {
self.push(NavAction::PushState(url.into(), Some(Rc::new(state))));
}
pub fn replace_state<T: Any>(&self, url: impl Into<String>, state: T) {
self.push(NavAction::ReplaceState(url.into(), Some(Rc::new(state))));
}
pub fn clear_history_state(&self) {
self.push(NavAction::ReplaceState(String::new(), None));
}
}
impl Nav<'_> {
pub fn block_navigation(&self) {
self.frame.nav.borrow_mut().blocking = true;
}
pub fn blocked_navigation(&self) -> Option<Navigation> {
self.frame.nav.borrow().blocked.clone()
}
pub fn confirm_navigation(&self) {
self.push(NavAction::ConfirmNavigation);
}
pub fn cancel_navigation(&self) {
self.push(NavAction::CancelNavigation);
}
}