egui_kit/history.rs
1//! Where the current url lives: in memory (native) or in the browser's address bar (wasm).
2
3#[cfg(target_arch = "wasm32")]
4mod browser;
5mod memory;
6
7#[cfg(target_arch = "wasm32")]
8pub use browser::{BrowserHistory, HistoryMode};
9pub use memory::MemoryHistory;
10
11/// Identifies one history entry, so the router can recognise entries it created
12/// (e.g. by [shallow routing](crate::extract::Nav::push_state)) when the user goes back or forward.
13///
14/// Keys must be unique among the entries of a history, including entries left over from an
15/// earlier session (a browser reload).
16#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
17pub struct EntryKey(pub u64);
18
19#[derive(Debug, Clone, PartialEq, Eq)]
20pub struct HistoryEntry {
21 /// An app-relative url: a path plus optional query and fragment, e.g. `/blog?page=2#top`.
22 pub url: String,
23 pub key: EntryKey,
24}
25
26/// A session history the router keeps in sync with the current page.
27pub trait History {
28 /// The current entry. Used to pick the initial route.
29 fn current(&self) -> HistoryEntry;
30
31 /// Adds a new entry and returns its key.
32 fn push(&mut self, url: &str) -> EntryKey;
33
34 /// Changes the url of the current entry, keeping its key.
35 fn replace(&mut self, url: &str);
36
37 /// Moves one entry back. The resulting entry is reported through [`History::poll`].
38 fn back(&mut self);
39
40 /// Moves one entry forward. The resulting entry is reported through [`History::poll`].
41 fn forward(&mut self);
42
43 fn can_go_back(&self) -> bool;
44
45 fn can_go_forward(&self) -> bool;
46
47 /// Returns the entry the history moved to by itself since the last call (back/forward,
48 /// or the browser's buttons). Entries the history doesn't know (e.g. the user edited the
49 /// url's `#hash`) must be given a fresh key.
50 ///
51 /// `ctx` can be stored to request a repaint when such a change happens outside a frame.
52 fn poll(&mut self, ctx: &egui::Context) -> Option<HistoryEntry>;
53
54 /// Whether [`History::hover_link`] shows hovered links to the platform, which then also
55 /// opens them in a new tab on Ctrl/Cmd/Shift-click (so the router doesn't follow those).
56 fn shows_links(&self) -> bool {
57 false
58 }
59
60 /// Called after every frame with the url of the [link](crate::extract::Nav::link) under the
61 /// pointer, if any.
62 fn hover_link(&mut self, _url: Option<&str>) {}
63
64 /// Called whenever the page starts or stops
65 /// [blocking navigation](crate::extract::Nav::block_navigation), e.g. to have the browser
66 /// confirm closing or reloading the tab.
67 fn set_blocking(&mut self, _blocking: bool) {}
68}
69
70/// A history chosen at runtime, e.g. a [`MemoryHistory`] in tests and the default one otherwise.
71///
72/// ```
73/// use egui_kit::{History, MemoryHistory, Router, default_history};
74///
75/// let history: Box<dyn History> = if cfg!(test) {
76/// Box::new(MemoryHistory::new("/settings"))
77/// } else {
78/// Box::new(default_history())
79/// };
80/// let app = Router::<()>::new().with_state_and_history((), history);
81/// ```
82impl<H: History + ?Sized> History for Box<H> {
83 fn current(&self) -> HistoryEntry {
84 (**self).current()
85 }
86
87 fn push(&mut self, url: &str) -> EntryKey {
88 (**self).push(url)
89 }
90
91 fn replace(&mut self, url: &str) {
92 (**self).replace(url);
93 }
94
95 fn back(&mut self) {
96 (**self).back();
97 }
98
99 fn forward(&mut self) {
100 (**self).forward();
101 }
102
103 fn can_go_back(&self) -> bool {
104 (**self).can_go_back()
105 }
106
107 fn can_go_forward(&self) -> bool {
108 (**self).can_go_forward()
109 }
110
111 fn poll(&mut self, ctx: &egui::Context) -> Option<HistoryEntry> {
112 (**self).poll(ctx)
113 }
114
115 fn shows_links(&self) -> bool {
116 (**self).shows_links()
117 }
118
119 fn hover_link(&mut self, url: Option<&str>) {
120 (**self).hover_link(url);
121 }
122
123 fn set_blocking(&mut self, blocking: bool) {
124 (**self).set_blocking(blocking);
125 }
126}
127
128/// `BrowserHistory` on wasm, [`MemoryHistory`] starting at `/` elsewhere.
129pub fn default_history() -> impl History {
130 #[cfg(target_arch = "wasm32")]
131 {
132 BrowserHistory::new(HistoryMode::Path)
133 }
134 #[cfg(not(target_arch = "wasm32"))]
135 {
136 MemoryHistory::new("/")
137 }
138}