egui_kit/page.rs
1//! Pages and layouts: what a route loads and shows.
2//!
3//! A [`Page`] is made of two functions:
4//!
5//! - an optional **load**: an `async fn` whose arguments are [extractors](crate::extract) and
6//! which returns `Result<D, E>` with `E: Into<Rejection>`. Any such function implements
7//! [`Handler`]. Hooks are handlers too, returning `Result<(), E>`.
8//! - a **view**: a function of `&mut egui::Ui` and extractors, called every frame. Any such
9//! function implements [`View`]. Among the extractors, `&mut D` (or `&D`) is what the load
10//! returned; that the types match is checked at compile time.
11//!
12//! ```
13//! use egui::Ui;
14//! use egui_kit::Rejection;
15//! use egui_kit::extract::{Nav, Path};
16//! use egui_kit::page::Page;
17//!
18//! struct Post {
19//! title: String,
20//! }
21//!
22//! async fn load(Path(id): Path<u32>) -> Result<Post, Rejection> {
23//! Ok(Post { title: format!("Post #{id}") })
24//! }
25//!
26//! fn view(ui: &mut Ui, post: &mut Post, nav: Nav) {
27//! ui.heading(&post.title);
28//! nav.link(ui, "Home", "/");
29//! }
30//!
31//! let page = Page::<()>::with_load(load, view);
32//! ```
33//!
34//! A [`Layout`] wraps all pages of a router. Its view
35//! ([`LayoutView`]) gets the [`Outlet`] to draw the page in as its second argument.
36//!
37//! Views can be closures too, with annotated arguments: `|ui: &mut Ui, nav: Nav| …`.
38
39mod handler;
40mod layout;
41mod loader;
42mod view;
43
44use std::rc::Rc;
45
46use crate::error::Rejection;
47pub use handler::Handler;
48pub(crate) use handler::{ErasedHandler, LocalBoxFuture, erase, unit};
49pub use layout::Layout;
50pub(crate) use loader::{ErasedView, LayoutReset, Loader, NodeId};
51pub(crate) use view::Frame;
52pub use view::{LayoutView, Outlet, View, ViewContext};
53
54/// A page: an optional `load` handler and a [view](View).
55///
56/// The view extracts the data returned by `load` as `&mut D`, so it can also use it to keep
57/// page-local UI state (text inputs, toggles, …) for as long as the page is shown.
58pub struct Page<S> {
59 pub(crate) loader: Loader<S>,
60 pub(crate) view: ErasedView<S>,
61 pub(crate) reset: Option<LayoutReset>,
62}
63
64impl<S: 'static> Page<S> {
65 /// A page without a `load` handler.
66 pub fn new<V, U>(view: V) -> Self
67 where
68 V: View<U, S, ()>,
69 {
70 Self {
71 loader: Loader::new(unit()),
72 view: Rc::new(move |ui, cx| view.call(ui, cx)),
73 reset: None,
74 }
75 }
76
77 /// A page whose view can extract the data produced by `load` (as `&mut D` or `&D`).
78 pub fn with_load<H, T, D, E, V, U>(load: H, view: V) -> Self
79 where
80 H: Handler<T, S, Output = Result<D, E>>,
81 D: 'static,
82 E: Into<Rejection>,
83 V: View<U, S, D>,
84 {
85 Self {
86 loader: Loader::new(erase(load)),
87 view: Rc::new(move |ui, cx| view.call(ui, cx)),
88 reset: None,
89 }
90 }
91
92 /// Reruns `load` when `key` is [invalidated](crate::extract::Nav::invalidate).
93 pub fn depends(mut self, key: impl Into<String>) -> Self {
94 self.loader.keys.push(key.into());
95 self
96 }
97
98 /// Renders the page in the root layout only, skipping the layouts in between.
99 /// Hooks and error pages still apply.
100 pub fn reset_layouts(mut self) -> Self {
101 self.reset = Some(LayoutReset::Root);
102 self
103 }
104
105 /// Renders the page in the layouts up to the router [named](crate::Router::named) `name`.
106 pub fn reset_layouts_to(mut self, name: impl Into<String>) -> Self {
107 self.reset = Some(LayoutReset::To(name.into()));
108 self
109 }
110}