egui-kit 0.6.0

A SvelteKit-inspired router for egui with typed loaders, layouts, hooks, error pages and browser history.
Documentation
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
//! Drives a `RouterApp` through real (headless) egui frames.

use core::cell::{Cell, RefCell};
use core::future::poll_fn;
use core::task::Poll;
use std::rc::Rc;

use egui::Ui;
use egui_kit::extract::{HistoryState, Nav, Path, Query, State};
use egui_kit::page::{Children, Layout, Page};
use egui_kit::{
    MemoryHistory, Rejection, Request, RouteError, Router, RouterApp, error, href, redirect,
};
use serde::Deserialize;

#[derive(Clone, Default)]
struct AppState {
    log: Rc<RefCell<Vec<String>>>,
    logged_in: Rc<Cell<bool>>,
    gate_open: Rc<Cell<bool>>,
    /// Run by the root layout, to call `Nav` methods from a test.
    commands: Rc<RefCell<Vec<Command>>>,
    photo_loads: Rc<Cell<u32>>,
}

type Command = Box<dyn FnOnce(Nav<'_>)>;

impl AppState {
    fn push(&self, entry: impl Into<String>) {
        self.log.borrow_mut().push(entry.into());
    }
}

#[derive(Deserialize)]
struct Search {
    q: Option<String>,
}

async fn search(Query(search): Query<Search>) -> Result<String, Rejection> {
    Ok(search.q.unwrap_or_default())
}

async fn user(Path(id): Path<u32>) -> Result<u32, Rejection> {
    if id == 0 {
        return Err(error(410, "gone"));
    }
    Ok(id)
}

async fn require_login(State(state): State<AppState>) -> Result<(), Rejection> {
    if state.logged_in.get() {
        Ok(())
    } else {
        Err(redirect("/login"))
    }
}

async fn photos(State(state): State<AppState>) -> Result<(), Rejection> {
    state.photo_loads.set(state.photo_loads.get() + 1);
    Ok(())
}

fn history_state(HistoryState(state): HistoryState<&u32>) -> String {
    state.map_or_else(|| "-".into(), |state| state.to_string())
}

async fn slow(State(state): State<AppState>) -> Result<&'static str, Rejection> {
    poll_fn(|_| {
        if state.gate_open.get() {
            Poll::Ready(())
        } else {
            Poll::Pending
        }
    })
    .await;
    Ok("slow")
}

fn app(state: AppState, url: &str) -> RouterApp<AppState> {
    let users = Router::new()
        .layout(Layout::with_load(
            |Path(id): Path<u32>| async move { Ok::<_, Rejection>(format!("user {id}")) },
            |ui: &mut Ui,
             mut children: Children<'_>,
             State(state): State<&AppState>,
             title: &String| {
                state.push(format!("users-layout:{title}"));
                children.show(ui);
            },
        ))
        .error(
            |_: &mut Ui, State(state): State<&AppState>, err: &RouteError| {
                state.push(format!("users-error:{}", err.status));
            },
        )
        .page(
            "/",
            Page::with_load(
                user,
                |_: &mut Ui, State(state): State<&AppState>, id: &mut u32| {
                    state.push(format!("user:{id}"));
                },
            ),
        );

    Router::new()
        .layout(Layout::new(
            |ui: &mut Ui,
             mut children: Children<'_>,
             State(state): State<&AppState>,
             nav: Nav<'_>| {
                state.push("root-layout");
                let commands = core::mem::take(&mut *state.commands.borrow_mut());
                for command in commands {
                    command(nav);
                }
                children.show(ui);
            },
        ))
        .page(
            "/",
            Page::new(|_: &mut Ui, State(state): State<&AppState>| state.push("home")),
        )
        .page(
            "/login",
            Page::new(|_: &mut Ui, State(state): State<&AppState>| state.push("login")),
        )
        .page(
            "/search",
            Page::with_load(
                search,
                |_: &mut Ui, State(state): State<&AppState>, q: &String| {
                    state.push(format!("search:{q}"));
                },
            ),
        )
        .page(
            "/slow",
            Page::with_load(
                slow,
                |_: &mut Ui, State(state): State<&AppState>, s: &&str| state.push(*s),
            ),
        )
        .page(
            "/photos",
            Page::with_load(
                photos,
                |_: &mut Ui,
                 State(state): State<&AppState>,
                 nav: Nav<'_>,
                 ps: HistoryState<&u32>| {
                    state.push(format!("photos:{}:{}", nav.url(), history_state(ps)));
                },
            ),
        )
        .page(
            "/photos/{id}",
            Page::new(
                |_: &mut Ui,
                 State(state): State<&AppState>,
                 request: Request,
                 ps: HistoryState<&u32>| {
                    state.push(format!(
                        "photo:{}:{}",
                        request.param("id").expect("route has an id"),
                        history_state(ps)
                    ));
                },
            ),
        )
        .nest("/users/{id}", users)
        .nest(
            "/admin",
            Router::new().hook(require_login).page(
                "/",
                Page::new(|_: &mut Ui, State(state): State<&AppState>| state.push("admin")),
            ),
        )
        .with_state_and_history(state, MemoryHistory::new(url))
}

/// Runs one frame and returns what was rendered.
fn frame(ctx: &egui::Context, app: &mut RouterApp<AppState>) -> Vec<String> {
    let mut output = ctx.run_ui(Default::default(), |ui| app.ui(ui));
    output.textures_delta.clear();
    core::mem::take(&mut *app.state().log.borrow_mut())
}

#[test]
fn renders_initial_page_with_query() {
    let ctx = egui::Context::default();
    let mut app = app(AppState::default(), "/search?q=egui");
    assert_eq!(frame(&ctx, &mut app), ["root-layout", "search:egui"]);
}

#[test]
fn nested_params_layout_load_and_error_boundary() {
    let ctx = egui::Context::default();
    let mut app = app(AppState::default(), "/users/7");
    assert_eq!(
        frame(&ctx, &mut app),
        ["root-layout", "users-layout:user 7", "user:7"]
    );

    // The page load fails: the nested error page renders inside the nested layout.
    app.goto("/users/0");
    assert_eq!(
        frame(&ctx, &mut app),
        ["root-layout", "users-layout:user 0", "users-error:410"]
    );

    // Unknown path below the nested router: its 404, inside its layout.
    app.goto("/users/7/nope");
    assert_eq!(
        frame(&ctx, &mut app),
        ["root-layout", "users-layout:user 7", "users-error:404"]
    );

    // Typed param fails in the nested layout's load: falls back to the root's (default) error page.
    app.goto("/users/abc");
    assert_eq!(frame(&ctx, &mut app), ["root-layout"]);
    assert_eq!(app.request().unwrap().path(), "/users/abc");
}

#[test]
fn hook_redirects_and_history() {
    let ctx = egui::Context::default();
    let state = AppState::default();
    let mut app = app(state.clone(), "/");
    assert_eq!(frame(&ctx, &mut app), ["root-layout", "home"]);

    app.goto("/admin");
    assert_eq!(frame(&ctx, &mut app), ["root-layout", "login"]);
    assert_eq!(app.request().unwrap().path(), "/login");

    state.logged_in.set(true);
    app.goto("/admin");
    assert_eq!(frame(&ctx, &mut app), ["root-layout", "admin"]);

    app.back();
    assert_eq!(frame(&ctx, &mut app), ["root-layout", "login"]);
    app.back();
    assert_eq!(frame(&ctx, &mut app), ["root-layout", "home"]);
    app.forward();
    assert_eq!(frame(&ctx, &mut app), ["root-layout", "login"]);
}

#[test]
fn keeps_previous_page_while_loading() {
    let ctx = egui::Context::default();
    let state = AppState::default();
    let mut app = app(state.clone(), "/");
    frame(&ctx, &mut app);

    app.goto("/slow");
    assert!(app.is_loading());
    assert_eq!(frame(&ctx, &mut app), ["root-layout", "home"]);

    state.gate_open.set(true);
    assert_eq!(frame(&ctx, &mut app), ["root-layout", "slow"]);
    assert!(!app.is_loading());
}

/// Calls `command` with a `Nav` during a frame; its effects show from the next frame on.
fn act(
    ctx: &egui::Context,
    app: &mut RouterApp<AppState>,
    command: impl FnOnce(Nav<'_>) + 'static,
) {
    app.state().commands.borrow_mut().push(Box::new(command));
    frame(ctx, app);
}

#[test]
fn shallow_routing() {
    let ctx = egui::Context::default();
    let state = AppState::default();
    let mut app = app(state.clone(), "/photos");
    let loads = || state.photo_loads.get();
    assert_eq!(frame(&ctx, &mut app), ["root-layout", "photos:/photos:-"]);
    assert_eq!(loads(), 1);

    // New entry with state, same page, no load.
    act(&ctx, &mut app, |nav| nav.push_state("/photos/3", 3u32));
    assert_eq!(frame(&ctx, &mut app), ["root-layout", "photos:/photos/3:3"]);
    act(&ctx, &mut app, |nav| {
        assert!(nav.is_active("/photos/3"), "the shallow url is active");
    });
    act(&ctx, &mut app, |nav| nav.replace_state("", 4u32));
    assert_eq!(frame(&ctx, &mut app), ["root-layout", "photos:/photos/3:4"]);
    assert_eq!(loads(), 1);

    // Back and forward between entries of the same page only swap the state.
    app.back();
    assert_eq!(frame(&ctx, &mut app), ["root-layout", "photos:/photos:-"]);
    app.forward();
    assert_eq!(frame(&ctx, &mut app), ["root-layout", "photos:/photos/3:4"]);
    assert_eq!(loads(), 1);

    // Invalidating reloads the data but keeps the url, the state and the page instance.
    act(&ctx, &mut app, |nav| nav.invalidate_all());
    assert_eq!(frame(&ctx, &mut app), ["root-layout", "photos:/photos/3:4"]);
    assert_eq!(loads(), 2);
    app.back();
    assert_eq!(frame(&ctx, &mut app), ["root-layout", "photos:/photos:-"]);
    assert_eq!(loads(), 2);
    app.forward();
    frame(&ctx, &mut app);

    // A full navigation starts without state...
    app.goto("/");
    assert_eq!(frame(&ctx, &mut app), ["root-layout", "home"]);

    // ...and coming back to a shallow entry of an earlier page loads its url for real,
    // restoring the entry's state.
    app.back();
    assert_eq!(frame(&ctx, &mut app), ["root-layout", "photo:3:4"]);
    app.back();
    assert_eq!(frame(&ctx, &mut app), ["root-layout", "photos:/photos:-"]);
    assert_eq!(loads(), 3);

    act(&ctx, &mut app, |nav| nav.push_state("", 7u32));
    assert_eq!(frame(&ctx, &mut app), ["root-layout", "photos:/photos:7"]);
    act(&ctx, &mut app, |nav| nav.clear_history_state());
    assert_eq!(frame(&ctx, &mut app), ["root-layout", "photos:/photos:-"]);
    assert_eq!(app.url(), Some("/photos"));

    // Only the url.
    act(&ctx, &mut app, |nav| nav.replace_state("", 8u32));
    act(&ctx, &mut app, |nav| nav.replace_url("/photos?sort=new"));
    assert_eq!(
        frame(&ctx, &mut app),
        ["root-layout", "photos:/photos?sort=new:8"]
    );
    assert_eq!(loads(), 3);
}

#[test]
fn sixteen_extractors() {
    // Loads own the state, views borrow it.
    type S = State<AppState>;
    type V<'a> = State<&'a AppState>;

    #[expect(clippy::too_many_arguments)]
    async fn load(
        Path(id): Path<u32>,
        _: Query<Search>,
        _: Request,
        _: S,
        _: S,
        _: S,
        _: S,
        _: S,
        _: S,
        _: S,
        _: S,
        _: S,
        _: S,
        _: S,
        _: S,
        State(state): S,
    ) -> Result<u32, Rejection> {
        state.push(format!("load:{id}"));
        Ok(id)
    }

    #[expect(clippy::too_many_arguments)]
    fn view(
        _: &mut Ui,
        id: &u32,
        _: Nav<'_>,
        _: Request,
        _: HistoryState<&u32>,
        _: egui_kit::Navigation,
        _: egui_kit::extract::Scroll<'_>,
        _: V<'_>,
        _: V<'_>,
        _: V<'_>,
        _: V<'_>,
        _: V<'_>,
        _: V<'_>,
        _: V<'_>,
        _: V<'_>,
        _: V<'_>,
        State(state): V<'_>,
    ) {
        state.push(format!("view:{id}"));
    }

    #[expect(clippy::too_many_arguments)]
    fn layout(
        ui: &mut Ui,
        mut children: Children<'_>,
        _: &(),
        _: V<'_>,
        _: V<'_>,
        _: V<'_>,
        _: V<'_>,
        _: V<'_>,
        _: V<'_>,
        _: V<'_>,
        _: V<'_>,
        _: V<'_>,
        _: V<'_>,
        _: V<'_>,
        _: V<'_>,
        _: V<'_>,
        _: V<'_>,
        State(state): V<'_>,
    ) {
        state.push("layout");
        children.show(ui);
    }

    let state = AppState::default();
    let mut app = Router::new()
        .layout(Layout::new(layout))
        .page("/{id}", Page::with_load(load, view))
        .with_state_and_history(state, MemoryHistory::new("/7"));
    let ctx = egui::Context::default();
    assert_eq!(frame(&ctx, &mut app), ["load:7", "layout", "view:7"]);
}

#[test]
fn fragments_are_ignored_by_matching() {
    let ctx = egui::Context::default();
    let mut app = app(AppState::default(), "/search?q=egui#results");
    assert_eq!(frame(&ctx, &mut app), ["root-layout", "search:egui"]);
    assert_eq!(app.request().unwrap().query(), Some("q=egui"));
    assert_eq!(app.matches("/photos/3#info"), Some("/photos/{id}"));
    act(&ctx, &mut app, |nav| {
        assert_eq!(nav.fragment(), Some("results"));
        assert!(nav.is_active("/search#x"), "the fragment is ignored");
    });
}

#[test]
fn href_round_trips_through_the_router() {
    async fn load(Path(value): Path<String>) -> Result<String, Rejection> {
        Ok(value)
    }
    let value = "a b/c?d#e%20f";
    let url = href("/blog/{slug}").param("slug", value).to_string();
    let state = AppState::default();
    let mut app = Router::new()
        .page(
            "/blog/{slug}",
            Page::with_load(
                load,
                |_: &mut Ui, State(state): State<&AppState>, value: &String| state.push(value),
            ),
        )
        .with_state_and_history(state, MemoryHistory::new(url.clone()));
    assert_eq!(app.matches(&url), Some("/blog/{slug}"));
    let ctx = egui::Context::default();
    assert_eq!(frame(&ctx, &mut app), [value]);
}