kael 0.2.0

GPU-accelerated native UI framework for Rust — build desktop apps with Metal, DirectX, and Vulkan rendering
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
use crate::{
    AnyElement, AnyView, Context, EventEmitter, IntoElement, ParentElement, Render, SharedString,
    StyleRefinement, Styled, Window, div, relative,
};
use std::{
    any::Any,
    rc::Rc,
    time::{Duration, Instant},
};

const SLIDE_TRANSITION_DURATION: Duration = Duration::from_millis(220);
const FADE_TRANSITION_DURATION: Duration = Duration::from_millis(180);

/// Creates a navigator initialized with a single route.
pub fn navigator(initial_route: impl Into<Route>) -> Navigator {
    Navigator::new(initial_route)
}

/// A route rendered by a [`Navigator`].
pub struct Route {
    id: SharedString,
    view: AnyView,
    memento: Option<Box<dyn Any>>,
}

impl Route {
    /// Creates a route for the given view.
    pub fn new(id: impl Into<SharedString>, view: impl Into<AnyView>) -> Self {
        Self {
            id: id.into(),
            view: view.into(),
            memento: None,
        }
    }

    /// Returns this route's identifier.
    pub fn id(&self) -> &SharedString {
        &self.id
    }

    /// Returns the view rendered for this route.
    pub fn view(&self) -> AnyView {
        self.view.clone()
    }

    /// Attaches restorable state to this route.
    pub fn with_memento<T: Any>(mut self, memento: T) -> Self {
        self.memento = Some(Box::new(memento));
        self
    }

    /// Returns a shared reference to the stored memento if it matches `T`.
    pub fn memento<T: Any>(&self) -> Option<&T> {
        self.memento
            .as_deref()
            .and_then(|memento| memento.downcast_ref())
    }

    /// Removes and returns the stored memento if it matches `T`.
    pub fn take_memento<T: Any>(&mut self) -> Option<T> {
        let memento = self.memento.take()?;
        memento.downcast::<T>().ok().map(|memento| *memento)
    }
}

/// An event emitted whenever the active route changes.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RouteChangeEvent {
    /// The route that was active before the change, if any.
    pub previous_route_id: Option<SharedString>,
    /// The route that is active after the change, if any.
    pub current_route_id: Option<SharedString>,
    /// The number of routes currently on the stack.
    pub stack_depth: usize,
}

/// A transition applied between navigation stack changes.
#[derive(Clone)]
pub enum Transition {
    /// Switch routes immediately without animation.
    None,
    /// Slide the outgoing route left while the incoming route enters from the right.
    SlideLeft,
    /// Slide the outgoing route right while the incoming route enters from the left.
    SlideRight,
    /// Slide the outgoing route up while the incoming route enters from the bottom.
    SlideUp,
    /// Slide the outgoing route down while the incoming route enters from the top.
    SlideDown,
    /// Cross-fade between routes.
    Fade,
    /// Use a custom animator to render the transition frame.
    Custom(Rc<dyn TransitionAnimator>),
}

impl Transition {
    /// Returns the total duration of the transition.
    pub fn duration(&self) -> Duration {
        match self {
            Self::None => Duration::ZERO,
            Self::SlideLeft | Self::SlideRight | Self::SlideUp | Self::SlideDown => {
                SLIDE_TRANSITION_DURATION
            }
            Self::Fade => FADE_TRANSITION_DURATION,
            Self::Custom(animator) => animator.duration(),
        }
    }
}

/// Renders a custom navigation transition.
pub trait TransitionAnimator: 'static {
    /// Returns the duration used by the custom transition.
    fn duration(&self) -> Duration;

    /// Renders a single transition frame for the given progress.
    fn render_frame(&self, progress: f32, outgoing: AnyView, incoming: AnyView) -> AnyElement;
}

struct ActiveTransition {
    transition: Transition,
    started_at: Instant,
    outgoing: AnyView,
    incoming: AnyView,
}

impl ActiveTransition {
    fn new(transition: Transition, outgoing: AnyView, incoming: AnyView) -> Self {
        Self {
            transition,
            started_at: Instant::now(),
            outgoing,
            incoming,
        }
    }

    fn progress(&self, animations_enabled: bool) -> (f32, bool) {
        if !animations_enabled {
            return (1.0, true);
        }

        let duration = self.transition.duration();
        if duration.is_zero() {
            return (1.0, true);
        }

        let elapsed = self.started_at.elapsed();
        let progress = (elapsed.as_secs_f32() / duration.as_secs_f32()).clamp(0.0, 1.0);
        (progress, progress >= 1.0)
    }
}

struct NavigationChange {
    previous_route_id: Option<SharedString>,
    current_route_id: Option<SharedString>,
}

/// A renderable navigation stack that supports animated route transitions.
pub struct Navigator {
    stack: Vec<Route>,
    transition: Option<ActiveTransition>,
}

impl Navigator {
    /// Creates an empty navigator.
    pub fn empty() -> Self {
        Self {
            stack: Vec::new(),
            transition: None,
        }
    }

    /// Creates a navigator with an initial route.
    pub fn new(initial_route: impl Into<Route>) -> Self {
        Self {
            stack: vec![initial_route.into()],
            transition: None,
        }
    }

    /// Returns the number of routes on the stack.
    pub fn len(&self) -> usize {
        self.stack.len()
    }

    /// Returns whether the navigator has no routes.
    pub fn is_empty(&self) -> bool {
        self.stack.is_empty()
    }

    /// Returns the currently visible route.
    pub fn current_route(&self) -> Option<&Route> {
        self.stack.last()
    }

    /// Returns the current route stack from root to top.
    pub fn routes(&self) -> &[Route] {
        &self.stack
    }

    /// Returns the identifier of the currently visible route.
    pub fn current_route_id(&self) -> Option<&SharedString> {
        self.current_route().map(Route::id)
    }

    /// Pushes a new route onto the stack.
    pub fn push(
        &mut self,
        route: impl Into<Route>,
        transition: Transition,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let change = self.push_route(route.into(), transition);
        self.finish_change(change, window, cx);
    }

    /// Pops the top route from the stack.
    pub fn pop(
        &mut self,
        transition: Transition,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) -> Option<Route> {
        let (route, change) = self.pop_route(transition)?;
        self.finish_change(change, window, cx);
        Some(route)
    }

    /// Replaces the current route with a new route.
    pub fn replace(
        &mut self,
        route: impl Into<Route>,
        transition: Transition,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let change = self.replace_route(route.into(), transition);
        self.finish_change(change, window, cx);
    }

    /// Replaces the full route stack atomically.
    pub fn replace_stack(
        &mut self,
        routes: impl IntoIterator<Item = Route>,
        transition: Transition,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let change = self.replace_stack_routes(routes.into_iter().collect(), transition);
        self.finish_change(change, window, cx);
    }

    /// Pops the stack back to the first route.
    pub fn pop_to_root(
        &mut self,
        transition: Transition,
        window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        let Some(change) = self.pop_to_root_routes(transition) else {
            return;
        };
        self.finish_change(change, window, cx);
    }

    fn finish_change(
        &mut self,
        change: NavigationChange,
        _window: &mut Window,
        cx: &mut Context<Self>,
    ) {
        cx.emit(RouteChangeEvent {
            previous_route_id: change.previous_route_id,
            current_route_id: change.current_route_id,
            stack_depth: self.stack.len(),
        });
        cx.notify();
    }

    fn push_route(&mut self, route: Route, transition: Transition) -> NavigationChange {
        let previous_route_id = self.current_route_id().cloned();
        let outgoing = self.current_view();
        self.stack.push(route);
        let current_route_id = self.current_route_id().cloned();
        let incoming = self.current_view();
        self.begin_transition(transition, outgoing, incoming);
        NavigationChange {
            previous_route_id,
            current_route_id,
        }
    }

    fn pop_route(&mut self, transition: Transition) -> Option<(Route, NavigationChange)> {
        if self.stack.is_empty() {
            return None;
        }

        let previous_route_id = self.current_route_id().cloned();
        let outgoing = self.current_view();
        let route = self.stack.pop().expect("checked stack is non-empty");
        let current_route_id = self.current_route_id().cloned();
        let incoming = self.current_view();
        self.begin_transition(transition, outgoing, incoming);

        Some((
            route,
            NavigationChange {
                previous_route_id,
                current_route_id,
            },
        ))
    }

    fn replace_route(&mut self, route: Route, transition: Transition) -> NavigationChange {
        let previous_route_id = self.current_route_id().cloned();
        let outgoing = self.current_view();
        if let Some(current) = self.stack.last_mut() {
            *current = route;
        } else {
            self.stack.push(route);
        }
        let current_route_id = self.current_route_id().cloned();
        let incoming = self.current_view();
        self.begin_transition(transition, outgoing, incoming);

        NavigationChange {
            previous_route_id,
            current_route_id,
        }
    }

    fn replace_stack_routes(
        &mut self,
        routes: Vec<Route>,
        transition: Transition,
    ) -> NavigationChange {
        let previous_route_id = self.current_route_id().cloned();
        let outgoing = self.current_view();
        self.stack = routes;
        let current_route_id = self.current_route_id().cloned();
        let incoming = self.current_view();
        self.begin_transition(transition, outgoing, incoming);

        NavigationChange {
            previous_route_id,
            current_route_id,
        }
    }

    fn pop_to_root_routes(&mut self, transition: Transition) -> Option<NavigationChange> {
        if self.stack.len() <= 1 {
            return None;
        }

        let previous_route_id = self.current_route_id().cloned();
        let outgoing = self.current_view();
        let root = self.stack.drain(..1).next().expect("root route exists");
        self.stack.clear();
        self.stack.push(root);
        let current_route_id = self.current_route_id().cloned();
        let incoming = self.current_view();
        self.begin_transition(transition, outgoing, incoming);

        Some(NavigationChange {
            previous_route_id,
            current_route_id,
        })
    }

    fn begin_transition(
        &mut self,
        transition: Transition,
        outgoing: Option<AnyView>,
        incoming: Option<AnyView>,
    ) {
        self.transition = None;
        if matches!(transition, Transition::None) {
            return;
        }

        let (Some(outgoing), Some(incoming)) = (outgoing, incoming) else {
            return;
        };
        if outgoing == incoming {
            return;
        }

        self.transition = Some(ActiveTransition::new(transition, outgoing, incoming));
    }

    fn current_view(&self) -> Option<AnyView> {
        self.current_route().map(Route::view)
    }
}

impl EventEmitter<RouteChangeEvent> for Navigator {}

impl Render for Navigator {
    fn render(&mut self, window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
        let mut root = div().relative().w_full().h_full().overflow_hidden();

        if let Some((transition, progress, finished, outgoing, incoming)) =
            self.transition.as_ref().map(|active_transition| {
                let (progress, finished) = active_transition.progress(window.animations_enabled());
                (
                    active_transition.transition.clone(),
                    progress,
                    finished,
                    active_transition.outgoing.clone(),
                    active_transition.incoming.clone(),
                )
            })
        {
            if finished {
                self.transition = None;
            } else {
                window.request_animation_frame();
                root = root.child(render_transition_frame(
                    transition, progress, outgoing, incoming,
                ));
                return root;
            }
        }

        if let Some(route) = self.current_route() {
            root = root.child(fill_view(route.view()));
        }

        root
    }
}

fn render_transition_frame(
    transition: Transition,
    progress: f32,
    outgoing: AnyView,
    incoming: AnyView,
) -> AnyElement {
    match transition {
        Transition::None => fill_view(incoming),
        Transition::SlideLeft => horizontal_slide(progress, outgoing, incoming, -progress),
        Transition::SlideRight => horizontal_slide(progress, incoming, outgoing, progress - 1.0),
        Transition::SlideUp => vertical_slide(progress, outgoing, incoming, -progress),
        Transition::SlideDown => vertical_slide(progress, incoming, outgoing, progress - 1.0),
        Transition::Fade => div()
            .absolute()
            .top_0()
            .left_0()
            .w_full()
            .h_full()
            .child(
                div()
                    .absolute()
                    .top_0()
                    .left_0()
                    .w_full()
                    .h_full()
                    .opacity(1.0 - progress)
                    .child(cached_view(outgoing)),
            )
            .child(
                div()
                    .absolute()
                    .top_0()
                    .left_0()
                    .w_full()
                    .h_full()
                    .opacity(progress)
                    .child(cached_view(incoming)),
            )
            .into_any_element(),
        Transition::Custom(animator) => animator.render_frame(progress, outgoing, incoming),
    }
}

fn horizontal_slide(progress: f32, leading: AnyView, trailing: AnyView, left: f32) -> AnyElement {
    let _ = progress;
    div()
        .absolute()
        .top_0()
        .left(relative(left))
        .w(relative(2.0))
        .h_full()
        .flex()
        .flex_row()
        .child(
            div()
                .w_full()
                .h_full()
                .flex_none()
                .child(cached_view(leading)),
        )
        .child(
            div()
                .w_full()
                .h_full()
                .flex_none()
                .child(cached_view(trailing)),
        )
        .into_any_element()
}

fn vertical_slide(progress: f32, leading: AnyView, trailing: AnyView, top: f32) -> AnyElement {
    let _ = progress;
    div()
        .absolute()
        .top(relative(top))
        .left_0()
        .w_full()
        .h(relative(2.0))
        .flex()
        .flex_col()
        .child(
            div()
                .w_full()
                .h_full()
                .flex_none()
                .child(cached_view(leading)),
        )
        .child(
            div()
                .w_full()
                .h_full()
                .flex_none()
                .child(cached_view(trailing)),
        )
        .into_any_element()
}

fn fill_view(view: AnyView) -> AnyElement {
    div()
        .absolute()
        .top_0()
        .left_0()
        .w_full()
        .h_full()
        .child(view)
        .into_any_element()
}

fn cached_view(view: AnyView) -> AnyElement {
    view.cached(StyleRefinement::default()).into_any_element()
}

#[cfg(test)]
mod tests {
    use super::{Navigator, Route, Transition, navigator};
    use crate::{AppContext, EmptyView, TestAppContext};

    #[kael::test]
    fn navigator_updates_stack_for_push_replace_pop_and_root(cx: &mut TestAppContext) {
        let (navigator_view, mut window) =
            cx.add_window_view(|_, cx| navigator(Route::new("home", cx.new(|_| EmptyView))));

        window.update(|window, cx| {
            navigator_view.update(cx, |navigator, cx| {
                navigator.push(
                    Route::new("settings", cx.new(|_| EmptyView)),
                    Transition::None,
                    window,
                    cx,
                );
                navigator.push(
                    Route::new("details", cx.new(|_| EmptyView)),
                    Transition::None,
                    window,
                    cx,
                );
                assert_eq!(
                    navigator
                        .stack
                        .iter()
                        .map(|route| route.id.as_ref())
                        .collect::<Vec<_>>(),
                    vec!["home", "settings", "details"]
                );

                let popped = navigator
                    .pop(Transition::None, window, cx)
                    .expect("details route should pop");
                assert_eq!(popped.id.as_ref(), "details");

                navigator.replace(
                    Route::new("profile", cx.new(|_| EmptyView)),
                    Transition::None,
                    window,
                    cx,
                );
                assert_eq!(
                    navigator.current_route_id().map(|id| id.as_ref()),
                    Some("profile")
                );

                navigator.pop_to_root(Transition::None, window, cx);
                assert_eq!(
                    navigator
                        .stack
                        .iter()
                        .map(|route| route.id.as_ref())
                        .collect::<Vec<_>>(),
                    vec!["home"]
                );

                let root = navigator
                    .pop(Transition::None, window, cx)
                    .expect("root should pop");
                assert_eq!(root.id.as_ref(), "home");
                assert!(navigator.is_empty());
            });
        });
    }

    #[kael::test]
    fn navigator_creates_transition_for_animated_changes(cx: &mut TestAppContext) {
        let (navigator_view, mut window) =
            cx.add_window_view(|_, cx| Navigator::new(Route::new("home", cx.new(|_| EmptyView))));

        window.update(|window, cx| {
            navigator_view.update(cx, |navigator, cx| {
                navigator.push(
                    Route::new("settings", cx.new(|_| EmptyView)),
                    Transition::SlideLeft,
                    window,
                    cx,
                );
                assert!(navigator.transition.is_some());
            });
        });
    }

    #[kael::test]
    fn navigator_replaces_and_exposes_route_stack(cx: &mut TestAppContext) {
        let (navigator_view, mut window) =
            cx.add_window_view(|_, cx| Navigator::new(Route::new("home", cx.new(|_| EmptyView))));

        window.update(|window, cx| {
            navigator_view.update(cx, |navigator, cx| {
                navigator.replace_stack(
                    vec![
                        Route::new("home", cx.new(|_| EmptyView)).with_memento(1usize),
                        Route::new("thread", cx.new(|_| EmptyView))
                            .with_memento(String::from("inbox/42")),
                    ],
                    Transition::None,
                    window,
                    cx,
                );

                assert_eq!(
                    navigator
                        .routes()
                        .iter()
                        .map(|route| route.id().as_ref())
                        .collect::<Vec<_>>(),
                    vec!["home", "thread"]
                );
                assert_eq!(
                    navigator.current_route_id().map(|route| route.as_ref()),
                    Some("thread")
                );
                assert_eq!(navigator.routes()[0].memento::<usize>(), Some(&1usize));
                assert_eq!(
                    navigator.routes()[1]
                        .memento::<String>()
                        .map(String::as_str),
                    Some("inbox/42")
                );

                navigator.replace_stack(Vec::new(), Transition::None, window, cx);

                assert!(navigator.routes().is_empty());
                assert!(navigator.current_route().is_none());
            });
        });
    }
}