buoyant 0.6.1

SwiftUI-like UIs in Rust for embedded devices
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
677
678
679
680
681
682
683
684
use crate::{
    environment::LayoutEnvironment,
    event::EventResult,
    layout::ResolvedLayout,
    primitives::{Point, ProposedDimension, ProposedDimensions},
    render::{OneOf2, OneOf3, OneOf4},
    transition::Opacity,
    view::{ViewLayout, ViewMarker},
};

use super::match_view::{Branch2, Branch3, Branch4};

/// The axis along which the view should be fit.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FitAxis {
    /// Use the first view with a compact height that fits vertically.
    Vertical,
    /// Use the first view with a compact width that fits horizontally.
    Horizontal,
    /// Use the first view with a compact size that fits the space offered
    Both,
}

impl FitAxis {
    #[must_use]
    const fn components(self) -> (bool, bool) {
        match self {
            Self::Vertical => (false, true),
            Self::Horizontal => (true, false),
            Self::Both => (true, true),
        }
    }
}

/// Picks the first view that fits the available space by comparing each view's
/// preferred size to the available space.
///
/// If no other view fits, the last view is used.
///
/// Unlike other conditional views, [`ViewThatFits`] does not pass through the
/// empty and priority properties of its children.
///
/// If the branch which fits changes, the state will be rebuilt.
///
/// # Examples
///
/// ```
/// # use embedded_graphics::mono_font::MonoFont;
/// # const FONT: MonoFont<'_> = embedded_graphics::mono_font::ascii::FONT_5X8;
/// use buoyant::view::{ViewThatFits, FitAxis, Text};
///
/// let charge_view = || {
///     ViewThatFits::new(FitAxis::Vertical, {
///         Text::new("12 hours, 16 minutes, and 3 seconds to full charge", &FONT)
///     })
///     .or(Text::new("12h 16m 3s remaining", &FONT))
///     .or(Text::new("12h ⚡️", &FONT))
/// };
/// ```
///
/// In units of character size, this could produce the following results
/// for the given offered size:
///
/// > **100x1**
/// >
/// > 12 hours, 16 minutes, and 3 seconds to full charge
///
/// > **16x5**
/// >
/// > 12 hours, 16
/// > minutes, and 3
/// > seconds to full
/// > charge
///
/// > **10x2**
/// >
/// > 12h 16m 3s
/// > remaining
///
/// > **5x1**
/// >
/// > 12h ⚡️
#[derive(Debug, Clone)]
pub struct ViewThatFits<T> {
    axis: FitAxis,
    choices: T,
}

impl<T> ViewThatFits<(T,)> {
    #[allow(missing_docs)]
    #[must_use]
    pub const fn new(axis: FitAxis, view: T) -> Self {
        Self {
            axis,
            choices: (view,),
        }
    }
}

impl<T> ViewThatFits<(T,)> {
    /// An alternative view to use if the first one does not fit.
    #[must_use]
    pub fn or<V>(self, alternate: V) -> ViewThatFits<(T, V)> {
        ViewThatFits {
            axis: self.axis,
            choices: (self.choices.0, alternate),
        }
    }
}

macro_rules! derive_or {
    ($(($n:tt, $type:ident)),*) => {
        impl<T0, $($type),*> ViewThatFits<(T0, $($type),*)> {
            /// An alternative view to use if the first one does not fit.
            #[must_use]
            pub fn or<V>(self, alternate: V) -> ViewThatFits<(T0, $($type),*, V)> {
                ViewThatFits {
                    axis: self.axis,
                    choices: (self.choices.0, $(self.choices.$n),*, alternate),
                }
            }
        }
    };
}

// 4 is probably enough...? Making macros for this seems tricky
derive_or!((1, T1));
derive_or!((1, T1), (2, T2)); // this derives the 4-tuple variant

impl<T: ViewMarker> ViewMarker for ViewThatFits<(T,)> {
    type Renderables = T::Renderables;
    type Transition = Opacity;
}

impl<T, Captures: ?Sized> ViewLayout<Captures> for ViewThatFits<(T,)>
where
    T: ViewLayout<Captures>,
{
    type Sublayout = T::Sublayout;
    type State = T::State;

    fn transition(&self) -> Self::Transition {
        Opacity
    }

    fn build_state(&self, captures: &mut Captures) -> Self::State {
        self.choices.0.build_state(captures)
    }

    fn layout(
        &self,
        offer: &ProposedDimensions,
        env: &impl LayoutEnvironment,
        captures: &mut Captures,
        state: &mut Self::State,
    ) -> ResolvedLayout<Self::Sublayout> {
        self.choices.0.layout(offer, env, captures, state)
    }

    fn render_tree(
        &self,
        layout: &ResolvedLayout<Self::Sublayout>,
        origin: Point,
        env: &impl LayoutEnvironment,
        captures: &mut Captures,
        state: &mut Self::State,
    ) -> Self::Renderables {
        self.choices
            .0
            .render_tree(layout, origin, env, captures, state)
    }

    fn handle_event(
        &self,
        event: &crate::view::Event,
        context: &crate::event::EventContext,
        render_tree: &mut Self::Renderables,
        captures: &mut Captures,
        state: &mut Self::State,
    ) -> EventResult {
        self.choices
            .0
            .handle_event(event, context, render_tree, captures, state)
    }
}

const fn make_compact_offer(from_offer: ProposedDimensions, axis: FitAxis) -> ProposedDimensions {
    match axis {
        FitAxis::Vertical => ProposedDimensions {
            width: from_offer.width,
            height: ProposedDimension::Compact,
        },
        FitAxis::Horizontal => ProposedDimensions {
            width: ProposedDimension::Compact,
            height: from_offer.height,
        },
        FitAxis::Both => ProposedDimensions {
            width: ProposedDimension::Compact,
            height: ProposedDimension::Compact,
        },
    }
}

impl<T0, T1> ViewMarker for ViewThatFits<(T0, T1)>
where
    T0: ViewMarker,
    T1: ViewMarker,
{
    type Renderables = OneOf2<T0::Renderables, T1::Renderables>;
    type Transition = Opacity;
}

impl<T0, T1, Captures: ?Sized> ViewLayout<Captures> for ViewThatFits<(T0, T1)>
where
    T0: ViewLayout<Captures>,
    T1: ViewLayout<Captures>,
{
    type Sublayout = Branch2<ResolvedLayout<T0::Sublayout>, ResolvedLayout<T1::Sublayout>>;
    type State = Branch2<T0::State, T1::State>;

    fn transition(&self) -> Self::Transition {
        Opacity
    }

    fn build_state(&self, captures: &mut Captures) -> Self::State {
        Branch2::Variant0(self.choices.0.build_state(captures))
    }

    fn layout(
        &self,
        offer: &ProposedDimensions,
        env: &impl LayoutEnvironment,
        captures: &mut Captures,
        state: &mut Self::State,
    ) -> ResolvedLayout<Self::Sublayout> {
        let (horizontal, vertical) = self.axis.components();
        let subview_offer = make_compact_offer(*offer, self.axis);

        // Try first choice with compact offer to see if it fits, creating new state if necessary
        if let Branch2::Variant0(state0) = state {
            let layout = self.choices.0.layout(&subview_offer, env, captures, state0);
            if offer.contains(layout.resolved_size, horizontal, vertical) {
                // We don't actually want the compact version, so layout again with the original offer
                let layout = self.choices.0.layout(offer, env, captures, state0);
                return ResolvedLayout {
                    resolved_size: layout.resolved_size,
                    sublayouts: Branch2::Variant0(layout),
                };
            }
        } else {
            let mut state0 = self.choices.0.build_state(captures);
            let layout = self
                .choices
                .0
                .layout(&subview_offer, env, captures, &mut state0);
            if offer.contains(layout.resolved_size, horizontal, vertical) {
                let layout = self.choices.0.layout(offer, env, captures, &mut state0);
                *state = Branch2::Variant0(state0);
                return ResolvedLayout {
                    resolved_size: layout.resolved_size,
                    sublayouts: Branch2::Variant0(layout),
                };
            }
        }

        // Use second choice
        if let Branch2::Variant1(state1) = state {
            let layout = self.choices.1.layout(offer, env, captures, state1);
            return ResolvedLayout {
                resolved_size: layout.resolved_size,
                sublayouts: Branch2::Variant1(layout),
            };
        }
        let mut state1 = self.choices.1.build_state(captures);
        let layout = self.choices.1.layout(offer, env, captures, &mut state1);
        *state = Branch2::Variant1(state1);
        ResolvedLayout {
            resolved_size: layout.resolved_size,
            sublayouts: Branch2::Variant1(layout),
        }
    }

    fn render_tree(
        &self,
        layout: &ResolvedLayout<Self::Sublayout>,
        origin: Point,
        env: &impl LayoutEnvironment,
        captures: &mut Captures,
        state: &mut Self::State,
    ) -> Self::Renderables {
        match (&layout.sublayouts, state) {
            (Branch2::Variant0(l0), Branch2::Variant0(s0)) => {
                OneOf2::Variant0(self.choices.0.render_tree(l0, origin, env, captures, s0))
            }
            (Branch2::Variant1(l1), Branch2::Variant1(s1)) => {
                OneOf2::Variant1(self.choices.1.render_tree(l1, origin, env, captures, s1))
            }
            _ => panic!("Layout/state branch mismatch"),
        }
    }

    fn handle_event(
        &self,
        event: &crate::view::Event,
        context: &crate::event::EventContext,
        render_tree: &mut Self::Renderables,
        captures: &mut Captures,
        state: &mut Self::State,
    ) -> EventResult {
        match (state, render_tree) {
            (Branch2::Variant0(s0), OneOf2::Variant0(t0)) => self
                .choices
                .0
                .handle_event(event, context, t0, captures, s0),
            (Branch2::Variant1(s1), OneOf2::Variant1(t1)) => self
                .choices
                .1
                .handle_event(event, context, t1, captures, s1),
            _ => {
                // FIXME: I think it's better here to build new state, leaving to see what
                // breaks...
                panic!("Layout/state branch mismatch");
            }
        }
    }
}

impl<T0, T1, T2> ViewMarker for ViewThatFits<(T0, T1, T2)>
where
    T0: ViewMarker,
    T1: ViewMarker,
    T2: ViewMarker,
{
    type Renderables = OneOf3<T0::Renderables, T1::Renderables, T2::Renderables>;
    type Transition = Opacity;
}

impl<T0, T1, T2, Captures: ?Sized> ViewLayout<Captures> for ViewThatFits<(T0, T1, T2)>
where
    T0: ViewLayout<Captures>,
    T1: ViewLayout<Captures>,
    T2: ViewLayout<Captures>,
{
    type Sublayout = Branch3<
        ResolvedLayout<T0::Sublayout>,
        ResolvedLayout<T1::Sublayout>,
        ResolvedLayout<T2::Sublayout>,
    >;
    type State = Branch3<T0::State, T1::State, T2::State>;

    fn transition(&self) -> Self::Transition {
        Opacity
    }

    fn build_state(&self, captures: &mut Captures) -> Self::State {
        Branch3::Variant0(self.choices.0.build_state(captures))
    }

    fn layout(
        &self,
        offer: &ProposedDimensions,
        env: &impl LayoutEnvironment,
        captures: &mut Captures,
        state: &mut Self::State,
    ) -> ResolvedLayout<Self::Sublayout> {
        let (horizontal, vertical) = self.axis.components();
        let subview_offer = make_compact_offer(*offer, self.axis);

        // Try first choice with compact offer to see if it fits, creating new state if necessary
        if let Branch3::Variant0(state0) = state {
            let layout = self.choices.0.layout(&subview_offer, env, captures, state0);
            if offer.contains(layout.resolved_size, horizontal, vertical) {
                let layout = self.choices.0.layout(offer, env, captures, state0);
                return ResolvedLayout {
                    resolved_size: layout.resolved_size,
                    sublayouts: Branch3::Variant0(layout),
                };
            }
        } else {
            let mut state0 = self.choices.0.build_state(captures);
            let layout = self
                .choices
                .0
                .layout(&subview_offer, env, captures, &mut state0);
            if offer.contains(layout.resolved_size, horizontal, vertical) {
                let layout = self.choices.0.layout(offer, env, captures, &mut state0);
                *state = Branch3::Variant0(state0);
                return ResolvedLayout {
                    resolved_size: layout.resolved_size,
                    sublayouts: Branch3::Variant0(layout),
                };
            }
        }

        // Try second choice
        if let Branch3::Variant1(state1) = state {
            let layout = self.choices.1.layout(&subview_offer, env, captures, state1);
            if offer.contains(layout.resolved_size, horizontal, vertical) {
                let layout = self.choices.1.layout(offer, env, captures, state1);
                return ResolvedLayout {
                    resolved_size: layout.resolved_size,
                    sublayouts: Branch3::Variant1(layout),
                };
            }
        } else {
            let mut state1 = self.choices.1.build_state(captures);
            let layout = self
                .choices
                .1
                .layout(&subview_offer, env, captures, &mut state1);
            if offer.contains(layout.resolved_size, horizontal, vertical) {
                let layout = self.choices.1.layout(offer, env, captures, &mut state1);
                *state = Branch3::Variant1(state1);
                return ResolvedLayout {
                    resolved_size: layout.resolved_size,
                    sublayouts: Branch3::Variant1(layout),
                };
            }
        }

        // Use third choice (fallback)
        if let Branch3::Variant2(state2) = state {
            let layout = self.choices.2.layout(offer, env, captures, state2);
            return ResolvedLayout {
                resolved_size: layout.resolved_size,
                sublayouts: Branch3::Variant2(layout),
            };
        }
        let mut state2 = self.choices.2.build_state(captures);
        let layout = self.choices.2.layout(offer, env, captures, &mut state2);
        *state = Branch3::Variant2(state2);
        ResolvedLayout {
            resolved_size: layout.resolved_size,
            sublayouts: Branch3::Variant2(layout),
        }
    }

    fn render_tree(
        &self,
        layout: &ResolvedLayout<Self::Sublayout>,
        origin: Point,
        env: &impl LayoutEnvironment,
        captures: &mut Captures,
        state: &mut Self::State,
    ) -> Self::Renderables {
        match (&layout.sublayouts, state) {
            (Branch3::Variant0(l0), Branch3::Variant0(s0)) => {
                OneOf3::Variant0(self.choices.0.render_tree(l0, origin, env, captures, s0))
            }
            (Branch3::Variant1(l1), Branch3::Variant1(s1)) => {
                OneOf3::Variant1(self.choices.1.render_tree(l1, origin, env, captures, s1))
            }
            (Branch3::Variant2(l2), Branch3::Variant2(s2)) => {
                OneOf3::Variant2(self.choices.2.render_tree(l2, origin, env, captures, s2))
            }
            _ => panic!("Layout/state branch mismatch"),
        }
    }

    fn handle_event(
        &self,
        event: &crate::view::Event,
        context: &crate::event::EventContext,
        render_tree: &mut Self::Renderables,
        captures: &mut Captures,
        state: &mut Self::State,
    ) -> EventResult {
        match (state, render_tree) {
            (Branch3::Variant0(s0), OneOf3::Variant0(t0)) => self
                .choices
                .0
                .handle_event(event, context, t0, captures, s0),
            (Branch3::Variant1(s1), OneOf3::Variant1(t1)) => self
                .choices
                .1
                .handle_event(event, context, t1, captures, s1),
            (Branch3::Variant2(s2), OneOf3::Variant2(t2)) => self
                .choices
                .2
                .handle_event(event, context, t2, captures, s2),
            _ => {
                // FIXME: I think it's better here to build new state
                panic!("Layout/state branch mismatch");
            }
        }
    }
}

impl<T0, T1, T2, T3> ViewMarker for ViewThatFits<(T0, T1, T2, T3)>
where
    T0: ViewMarker,
    T1: ViewMarker,
    T2: ViewMarker,
    T3: ViewMarker,
{
    type Renderables = OneOf4<T0::Renderables, T1::Renderables, T2::Renderables, T3::Renderables>;
    type Transition = Opacity;
}

impl<T0, T1, T2, T3, Captures: ?Sized> ViewLayout<Captures> for ViewThatFits<(T0, T1, T2, T3)>
where
    T0: ViewLayout<Captures>,
    T1: ViewLayout<Captures>,
    T2: ViewLayout<Captures>,
    T3: ViewLayout<Captures>,
{
    type Sublayout = Branch4<
        ResolvedLayout<T0::Sublayout>,
        ResolvedLayout<T1::Sublayout>,
        ResolvedLayout<T2::Sublayout>,
        ResolvedLayout<T3::Sublayout>,
    >;
    type State = Branch4<T0::State, T1::State, T2::State, T3::State>;

    fn transition(&self) -> Self::Transition {
        Opacity
    }

    fn build_state(&self, captures: &mut Captures) -> Self::State {
        Branch4::Variant0(self.choices.0.build_state(captures))
    }

    fn layout(
        &self,
        offer: &ProposedDimensions,
        env: &impl LayoutEnvironment,
        captures: &mut Captures,
        state: &mut Self::State,
    ) -> ResolvedLayout<Self::Sublayout> {
        let (horizontal, vertical) = self.axis.components();
        let subview_offer = make_compact_offer(*offer, self.axis);

        // Try first choice with compact offer to see if it fits, creating new state if necessary
        if let Branch4::Variant0(state0) = state {
            let layout = self.choices.0.layout(&subview_offer, env, captures, state0);
            if offer.contains(layout.resolved_size, horizontal, vertical) {
                let layout = self.choices.0.layout(offer, env, captures, state0);
                return ResolvedLayout {
                    resolved_size: layout.resolved_size,
                    sublayouts: Branch4::Variant0(layout),
                };
            }
        } else {
            let mut state0 = self.choices.0.build_state(captures);
            let layout = self
                .choices
                .0
                .layout(&subview_offer, env, captures, &mut state0);
            if offer.contains(layout.resolved_size, horizontal, vertical) {
                let layout = self.choices.0.layout(offer, env, captures, &mut state0);
                *state = Branch4::Variant0(state0);
                return ResolvedLayout {
                    resolved_size: layout.resolved_size,
                    sublayouts: Branch4::Variant0(layout),
                };
            }
        }

        // Try second choice
        if let Branch4::Variant1(state1) = state {
            let layout = self.choices.1.layout(&subview_offer, env, captures, state1);
            if offer.contains(layout.resolved_size, horizontal, vertical) {
                let layout = self.choices.1.layout(offer, env, captures, state1);
                return ResolvedLayout {
                    resolved_size: layout.resolved_size,
                    sublayouts: Branch4::Variant1(layout),
                };
            }
        } else {
            let mut state1 = self.choices.1.build_state(captures);
            let layout = self
                .choices
                .1
                .layout(&subview_offer, env, captures, &mut state1);
            if offer.contains(layout.resolved_size, horizontal, vertical) {
                let layout = self.choices.1.layout(offer, env, captures, &mut state1);
                *state = Branch4::Variant1(state1);
                return ResolvedLayout {
                    resolved_size: layout.resolved_size,
                    sublayouts: Branch4::Variant1(layout),
                };
            }
        }

        // Try third choice
        if let Branch4::Variant2(state2) = state {
            let layout = self.choices.2.layout(&subview_offer, env, captures, state2);
            if offer.contains(layout.resolved_size, horizontal, vertical) {
                let layout = self.choices.2.layout(offer, env, captures, state2);
                return ResolvedLayout {
                    resolved_size: layout.resolved_size,
                    sublayouts: Branch4::Variant2(layout),
                };
            }
        } else {
            let mut state2 = self.choices.2.build_state(captures);
            let layout = self
                .choices
                .2
                .layout(&subview_offer, env, captures, &mut state2);
            if offer.contains(layout.resolved_size, horizontal, vertical) {
                let layout = self.choices.2.layout(offer, env, captures, &mut state2);
                *state = Branch4::Variant2(state2);
                return ResolvedLayout {
                    resolved_size: layout.resolved_size,
                    sublayouts: Branch4::Variant2(layout),
                };
            }
        }

        // Use fourth choice (fallback)
        if let Branch4::Variant3(state3) = state {
            let layout = self.choices.3.layout(offer, env, captures, state3);
            return ResolvedLayout {
                resolved_size: layout.resolved_size,
                sublayouts: Branch4::Variant3(layout),
            };
        }
        let mut state3 = self.choices.3.build_state(captures);
        let layout = self.choices.3.layout(offer, env, captures, &mut state3);
        *state = Branch4::Variant3(state3);
        ResolvedLayout {
            resolved_size: layout.resolved_size,
            sublayouts: Branch4::Variant3(layout),
        }
    }

    fn render_tree(
        &self,
        layout: &ResolvedLayout<Self::Sublayout>,
        origin: Point,
        env: &impl LayoutEnvironment,
        captures: &mut Captures,
        state: &mut Self::State,
    ) -> Self::Renderables {
        match (&layout.sublayouts, state) {
            (Branch4::Variant0(l0), Branch4::Variant0(s0)) => {
                OneOf4::Variant0(self.choices.0.render_tree(l0, origin, env, captures, s0))
            }
            (Branch4::Variant1(l1), Branch4::Variant1(s1)) => {
                OneOf4::Variant1(self.choices.1.render_tree(l1, origin, env, captures, s1))
            }
            (Branch4::Variant2(l2), Branch4::Variant2(s2)) => {
                OneOf4::Variant2(self.choices.2.render_tree(l2, origin, env, captures, s2))
            }
            (Branch4::Variant3(l3), Branch4::Variant3(s3)) => {
                OneOf4::Variant3(self.choices.3.render_tree(l3, origin, env, captures, s3))
            }
            _ => panic!("Layout/state branch mismatch"),
        }
    }

    fn handle_event(
        &self,
        event: &crate::view::Event,
        context: &crate::event::EventContext,
        render_tree: &mut Self::Renderables,
        captures: &mut Captures,
        state: &mut Self::State,
    ) -> EventResult {
        match (state, render_tree) {
            (Branch4::Variant0(s0), OneOf4::Variant0(t0)) => self
                .choices
                .0
                .handle_event(event, context, t0, captures, s0),
            (Branch4::Variant1(s1), OneOf4::Variant1(t1)) => self
                .choices
                .1
                .handle_event(event, context, t1, captures, s1),
            (Branch4::Variant2(s2), OneOf4::Variant2(t2)) => self
                .choices
                .2
                .handle_event(event, context, t2, captures, s2),
            (Branch4::Variant3(s3), OneOf4::Variant3(t3)) => self
                .choices
                .3
                .handle_event(event, context, t3, captures, s3),
            _ => {
                // FIXME: I think it's better here to build new state
                panic!("Layout/state branch mismatch");
            }
        }
    }
}