1use core::any::Any;
4use std::rc::Rc;
5
6use egui::{Response, Ui};
7
8use super::FromView;
9use crate::load::Invalidation;
10use crate::navigation::{GotoOptions, Navigation};
11use crate::page::{Frame, ViewContext};
12
13pub(crate) enum NavAction {
15 Goto(String, GotoOptions),
16 Back,
17 Forward,
18 Invalidate(Invalidation),
19 Preload(String),
20 PushState(String, Option<Rc<dyn Any>>),
22 ReplaceState(String, Option<Rc<dyn Any>>),
23 ConfirmNavigation,
24 CancelNavigation,
25}
26
27#[derive(Default)]
29pub(crate) struct NavState {
30 pub(crate) actions: Vec<NavAction>,
31 pub(crate) loading: Option<String>,
32 pub(crate) can_go_back: bool,
33 pub(crate) can_go_forward: bool,
34 pub(crate) preload_on_hover: bool,
35 pub(crate) blocking: bool,
37 pub(crate) blocked: Option<Navigation>,
38}
39
40#[derive(Clone, Copy)]
45pub struct Nav<'f> {
46 pub(crate) frame: &'f Frame<'f>,
47}
48
49impl<S: 'static, D> FromView<S, D> for Nav<'_> {
50 type Item<'f> = Nav<'f>;
51
52 fn from_view<'f>(cx: &'f ViewContext<'_, S>) -> Nav<'f> {
53 cx.nav()
54 }
55}
56
57impl Nav<'_> {
58 fn push(&self, action: NavAction) {
59 self.frame.nav.borrow_mut().actions.push(action);
60 }
61}
62
63impl<'f> Nav<'f> {
65 pub fn url(&self) -> &'f str {
69 self.frame.url
70 }
71
72 pub fn is_active(&self, href: &str) -> bool {
74 let href = crate::router::normalize(href.split('?').next().unwrap_or_default());
75 let path = self.frame.request.untracked_path();
76 path == href
77 || (href != "/" && path.starts_with(&href) && path[href.len()..].starts_with('/'))
78 }
79
80 pub fn is_outgoing(&self) -> bool {
84 self.frame.outgoing
85 }
86}
87
88impl Nav<'_> {
90 pub fn goto(&self, url: impl Into<String>) {
94 self.goto_with(url, GotoOptions::default());
95 }
96
97 pub fn goto_with(&self, url: impl Into<String>, options: GotoOptions) {
99 self.push(NavAction::Goto(url.into(), options));
100 }
101
102 pub fn replace(&self, url: impl Into<String>) {
104 self.goto_with(url, GotoOptions::default().replace_state());
105 }
106
107 pub fn back(&self) {
108 self.push(NavAction::Back);
109 }
110
111 pub fn forward(&self) {
112 self.push(NavAction::Forward);
113 }
114
115 pub fn can_go_back(&self) -> bool {
116 self.frame.nav.borrow().can_go_back
117 }
118
119 pub fn can_go_forward(&self) -> bool {
120 self.frame.nav.borrow().can_go_forward
121 }
122
123 pub fn link(&self, ui: &mut Ui, text: impl Into<egui::WidgetText>, href: &str) -> Response {
126 let response = ui.link(text).on_hover_text(href);
127 self.follow(&response, href);
128 response
129 }
130
131 pub fn nav_link(&self, ui: &mut Ui, text: impl Into<egui::WidgetText>, href: &str) -> Response {
133 let response = ui.selectable_label(self.is_active(href), text);
134 self.follow(&response, href);
135 response
136 }
137
138 fn follow(&self, response: &Response, href: &str) {
139 if response.clicked() {
140 self.goto(href);
141 } else if response.hovered() && self.frame.nav.borrow().preload_on_hover {
142 self.preload(href);
143 }
144 }
145}
146
147impl Nav<'_> {
149 pub fn is_loading(&self) -> bool {
151 self.frame.nav.borrow().loading.is_some()
152 }
153
154 pub fn loading_url(&self) -> Option<String> {
156 self.frame.nav.borrow().loading.clone()
157 }
158
159 pub fn preload(&self, url: impl Into<String>) {
162 self.push(NavAction::Preload(url.into()));
163 }
164
165 pub fn invalidate(&self, key: impl Into<String>) {
168 self.push(NavAction::Invalidate(Invalidation::Keys(vec![key.into()])));
169 }
170
171 pub fn invalidate_all(&self) {
173 self.push(NavAction::Invalidate(Invalidation::All));
174 }
175}
176
177impl Nav<'_> {
179 pub fn push_state<T: Any>(&self, url: impl Into<String>, state: T) {
188 self.push(NavAction::PushState(url.into(), Some(Rc::new(state))));
189 }
190
191 pub fn replace_state<T: Any>(&self, url: impl Into<String>, state: T) {
193 self.push(NavAction::ReplaceState(url.into(), Some(Rc::new(state))));
194 }
195
196 pub fn clear_history_state(&self) {
198 self.push(NavAction::ReplaceState(String::new(), None));
199 }
200}
201
202impl Nav<'_> {
204 pub fn block_navigation(&self) {
211 self.frame.nav.borrow_mut().blocking = true;
212 }
213
214 pub fn blocked_navigation(&self) -> Option<Navigation> {
216 self.frame.nav.borrow().blocked.clone()
217 }
218
219 pub fn confirm_navigation(&self) {
221 self.push(NavAction::ConfirmNavigation);
222 }
223
224 pub fn cancel_navigation(&self) {
226 self.push(NavAction::CancelNavigation);
227 }
228}