1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//! Extractors: the arguments of hooks, `load`s and views.
//!
//! Hooks and `load`s are async functions whose arguments implement [`FromRequest`]; views are
//! functions of a `&mut egui::Ui` whose other arguments implement [`FromView`]. The extractors
//! can be in any order.
//!
//! # In hooks and `load`s
//!
//! | Extractor | Gives you |
//! |---|---|
//! | [`Path<T>`] | the route's path parameters, deserialized with serde |
//! | [`Query<T>`] | the query string, deserialized with serde |
//! | [`State<T>`] | a clone of the app state, or of any part of it that implements [`FromRef`] |
//! | [`Parent`] | the data loaded by the layouts above |
//! | [`Request`] | the url and the route's parameters, untyped |
//!
//! What a `load` extracts decides when it reruns: [`Path`] and [`Query`] make it depend on the
//! parameters and the query, [`Parent`] on the layouts above.
//!
//! # In views
//!
//! | Extractor | Gives you |
//! |---|---|
//! | `&mut D` / `&D` | the data the `load` returned (`()` without one), checked at compile time |
//! | `&RouteError` | the error, in error pages |
//! | [`LayoutData<T>`] | the data of the nearest layout around the view that loaded a `T` |
//! | [`State<&T>`](State) | the app state, or any part of it that implements [`FromRef`], borrowed |
//! | [`Nav`] | the router: links, `goto`, back/forward, loading, invalidation, shallow routing, blocking |
//! | [`Scroll`] | the page's scroll area, whose position the router restores |
//! | [`PageState<T>`] | state the view owns while its page is shown |
//! | [`PageStateFrom<T>`] | like [`PageState`], created from the `load`'s data |
//! | [`Snapshot<T>`] | like [`PageState`], and restored when coming back with back/forward |
//! | [`PersistentState<T>`] | app-wide state that survives app restarts, like `localStorage` |
//! | [`HistoryState<&T>`](HistoryState) | the shallow-routing state of the current history entry |
//! | [`Navigation`] | how the page was reached: from where, how, in which history direction |
//! | [`Request`] | the url the page was loaded with, and its route's parameters |
//!
//! Layout views also get the [`Outlet`](crate::page::Outlet) as their second argument; it is not an
//! extractor, every layout has exactly one.
//!
//! # Borrowing and mutating
//!
//! Views run synchronously within the frame, so what they extract is borrowed from the router:
//! nothing big is cloned every frame. Hooks and `load`s are futures that can outlive the frame,
//! so they get owned values, cloned once per navigation.
//!
//! # Which state to use
//!
//! | | Lives | Mutable by views |
//! |---|---|---|
//! | [`State<&T>`](State) | as long as the app, shared by everything | through `Cell` / `RefCell` |
//! | `&mut D` (the `load`'s data) | until the `load` reruns | yes |
//! | [`PageState<T>`] | while the page (or layout) is shown, across invalidations | yes |
//! | [`Snapshot<T>`] | like [`PageState`], and restored when coming back with back/forward | yes |
//! | [`HistoryState<&T>`](HistoryState) | with its history entry, set by [`Nav::push_state`] | through [`Nav`] |
//! | [`PersistentState<T>`] | across app restarts, shared by the whole app | yes |
//!
//! A rule of thumb: what you loaded goes in the `load`'s data, what the user did goes in a
//! [`PageState`] or a [`Snapshot`], and preferences go in a [`PersistentState`]. The app state
//! is shared by every view at once, so no view can get `&mut` access to it.
//!
//! [`RouteError`]: crate::RouteError
pub
use crateRejection;
use crateNavigation;
use crateViewContext;
use crateRequest;
pub use HistoryState;
pub use LayoutData;
pub use Nav;
pub use ;
pub use Parent;
pub use Path;
pub use PersistentState;
pub use Query;
pub use PageScroll;
pub use Scroll;
pub use ;
pub use ViewStore;
pub use ;
/// Types that can be extracted from a navigation, used as arguments of hooks and `load`s.
///
/// Extraction is synchronous and runs before the handler's future is created. What an extractor
/// reads from the [`Request`] (path, query, params) decides when the `load` reruns.
/// Types that can be extracted for a view, used as arguments of page, layout and error views.
///
/// `S` is the app state and `D` the data of the view: what its `load` returned
/// (`()` without one), or the [`RouteError`](crate::RouteError) for error pages. `Item` is the
/// extractor with the lifetime of the frame, e.g. `Nav<'f>` for `Nav<'_>`.
///
/// A custom extractor, e.g. for the signed-in user:
///
/// ```
/// use egui_kit::page::ViewContext;
/// use egui_kit::extract::FromView;
///
/// #[derive(Clone)]
/// struct AppState {
/// user: Option<String>,
/// }
///
/// struct User(Option<String>);
///
/// impl<D> FromView<AppState, D> for User {
/// type Item<'f> = User;
///
/// fn from_view(cx: &ViewContext<'_, AppState>) -> User {
/// User(cx.state().user.clone())
/// }
/// }
/// ```
/// The url the page was loaded with and the parameters of its route. Unaffected by shallow
/// routing, see [`Nav::url`].
/// How the page was reached.
/// The view's data: what its `load` returned, or the error of an error page.
/// Read-only access to the view's data.
const MISMATCH: &str = "egui-kit: the data does not match the view (this is a bug)";