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) shows_links: bool,
37 pub(crate) hovered_link: Option<String>,
39 pub(crate) blocking: bool,
41 pub(crate) blocked: Option<Navigation>,
42}
43
44#[derive(Clone, Copy)]
49pub struct Nav<'f> {
50 pub(crate) frame: &'f Frame<'f>,
51}
52
53impl<S: 'static, D> FromView<S, D> for Nav<'_> {
54 type Item<'f> = Nav<'f>;
55
56 fn from_view<'f>(cx: &'f ViewContext<'_, S>) -> Nav<'f> {
57 cx.nav()
58 }
59}
60
61impl Nav<'_> {
62 fn push(&self, action: NavAction) {
63 self.frame.nav.borrow_mut().actions.push(action);
64 }
65}
66
67impl<'f> Nav<'f> {
69 pub fn url(&self) -> &'f str {
73 self.frame.url
74 }
75
76 pub fn is_active(&self, href: &str) -> bool {
78 let href = crate::router::normalize(href.split('?').next().unwrap_or_default());
79 let path = self.frame.request.untracked_path();
80 path == href
81 || (href != "/" && path.starts_with(&href) && path[href.len()..].starts_with('/'))
82 }
83
84 pub fn is_outgoing(&self) -> bool {
88 self.frame.outgoing
89 }
90}
91
92impl Nav<'_> {
94 pub fn goto(&self, url: impl Into<String>) {
100 self.goto_with(url, GotoOptions::default());
101 }
102
103 pub fn goto_with(&self, url: impl Into<String>, options: GotoOptions) {
105 self.push(NavAction::Goto(url.into(), options));
106 }
107
108 pub fn replace(&self, url: impl Into<String>) {
110 self.goto_with(url, GotoOptions::default().replace_state());
111 }
112
113 pub fn back(&self) {
114 self.push(NavAction::Back);
115 }
116
117 pub fn forward(&self) {
118 self.push(NavAction::Forward);
119 }
120
121 pub fn can_go_back(&self) -> bool {
122 self.frame.nav.borrow().can_go_back
123 }
124
125 pub fn can_go_forward(&self) -> bool {
126 self.frame.nav.borrow().can_go_forward
127 }
128
129 pub fn link(&self, ui: &mut Ui, text: impl Into<egui::WidgetText>, href: &str) -> Response {
132 let mut response = ui.link(text);
133 if !self.frame.nav.borrow().shows_links {
135 response = response.on_hover_text(href);
136 }
137 self.follow(&response, href);
138 response
139 }
140
141 pub fn nav_link(&self, ui: &mut Ui, text: impl Into<egui::WidgetText>, href: &str) -> Response {
143 let response = ui.selectable_label(self.is_active(href), text);
144 self.follow(&response, href);
145 response
146 }
147
148 fn follow(&self, response: &Response, href: &str) {
149 if response.hovered() && !self.frame.outgoing {
150 self.frame.nav.borrow_mut().hovered_link = Some(href.to_owned());
151 }
152 if response.clicked() {
153 let modified = response
155 .ctx
156 .input(|i| i.modifiers.ctrl || i.modifiers.command || i.modifiers.shift);
157 if !(modified && self.frame.nav.borrow().shows_links) {
158 self.goto(href);
159 }
160 } else if response.hovered() && self.frame.nav.borrow().preload_on_hover {
161 self.preload(href);
162 }
163 }
164}
165
166impl Nav<'_> {
168 pub fn is_loading(&self) -> bool {
170 self.frame.nav.borrow().loading.is_some()
171 }
172
173 pub fn loading_url(&self) -> Option<String> {
175 self.frame.nav.borrow().loading.clone()
176 }
177
178 pub fn preload(&self, url: impl Into<String>) {
181 self.push(NavAction::Preload(url.into()));
182 }
183
184 pub fn invalidate(&self, key: impl Into<String>) {
187 self.push(NavAction::Invalidate(Invalidation::Keys(vec![key.into()])));
188 }
189
190 pub fn invalidate_all(&self) {
192 self.push(NavAction::Invalidate(Invalidation::All));
193 }
194}
195
196impl Nav<'_> {
198 pub fn push_state<T: Any>(&self, url: impl Into<String>, state: T) {
207 self.push(NavAction::PushState(url.into(), Some(Rc::new(state))));
208 }
209
210 pub fn replace_state<T: Any>(&self, url: impl Into<String>, state: T) {
212 self.push(NavAction::ReplaceState(url.into(), Some(Rc::new(state))));
213 }
214
215 pub fn clear_history_state(&self) {
217 self.push(NavAction::ReplaceState(String::new(), None));
218 }
219}
220
221impl Nav<'_> {
223 pub fn block_navigation(&self) {
230 self.frame.nav.borrow_mut().blocking = true;
231 }
232
233 pub fn blocked_navigation(&self) -> Option<Navigation> {
235 self.frame.nav.borrow().blocked.clone()
236 }
237
238 pub fn confirm_navigation(&self) {
240 self.push(NavAction::ConfirmNavigation);
241 }
242
243 pub fn cancel_navigation(&self) {
245 self.push(NavAction::CancelNavigation);
246 }
247}