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
use std::time::Duration;

use buoyant::{
    animation::Animation,
    environment::DefaultEnvironment,
    font::CharacterBufferFont,
    if_view,
    primitives::Point,
    render::{AnimatedJoin, AnimationDomain, Render},
    render_target::FixedTextBuffer,
    view::prelude::*,
};
mod common;
use common::make_render_tree;

const FONT: CharacterBufferFont = CharacterBufferFont;

fn x_bar(alignment: HorizontalAlignment) -> impl View<char, ()> {
    Text::new("X", &FONT)
        .flex_infinite_width(alignment)
        .animated(Animation::linear(Duration::from_secs(1)), alignment)
}

/// Repeatedly render animation of X from left to right without clearing buffer
/// Check the buffer is filled with X.
#[test]
fn sanity_animation_wipe() {
    let mut buffer = FixedTextBuffer::<10, 1>::default();

    let mut view = x_bar(HorizontalAlignment::Leading);

    let source_tree = make_render_tree(&view, buffer.size(), &mut ());

    view = x_bar(HorizontalAlignment::Trailing);

    let target_tree = make_render_tree(&view, buffer.size(), &mut ());

    // render 101 steps, 10 ms increments
    for i in 0..101u64 {
        Render::<char>::render_animated(
            &mut buffer,
            &source_tree,
            &target_tree,
            &' ',
            &AnimationDomain::new(255, Duration::from_millis(i * 10)),
        );
    }
    assert_eq!(buffer.text[0].iter().collect::<String>(), "XXXXXXXXXX");
}

#[test]
fn sanity_animation_wipe_leading_half() {
    let mut buffer = FixedTextBuffer::<10, 1>::default();

    let mut view = x_bar(HorizontalAlignment::Leading);

    let source_tree = make_render_tree(&view, buffer.size(), &mut ());

    view = x_bar(HorizontalAlignment::Trailing);

    let target_tree = make_render_tree(&view, buffer.size(), &mut ());

    for i in 0..50u64 {
        Render::render_animated(
            &mut buffer,
            &source_tree,
            &target_tree,
            &' ',
            &AnimationDomain::new(255, Duration::from_millis(i * 10)),
        );
    }
    assert_eq!(buffer.text[0].iter().collect::<String>(), "XXXXX     ");
}

#[test]
fn sanity_animation_wipe_trailing_half() {
    let mut buffer = FixedTextBuffer::<10, 1>::default();

    let mut view = x_bar(HorizontalAlignment::Leading);

    let source_tree = make_render_tree(&view, buffer.size(), &mut ());

    view = x_bar(HorizontalAlignment::Trailing);

    let target_tree = make_render_tree(&view, buffer.size(), &mut ());

    // The first frame detects the changed value and sets the animation end time in
    // the target tree.
    Render::render_animated(
        &mut buffer,
        &source_tree,
        &target_tree,
        &' ',
        &AnimationDomain::new(255, Duration::from_millis(0)),
    );
    assert_eq!(buffer.text[0].iter().collect::<String>(), "X         ");
    buffer.clear();

    for i in 60..101u64 {
        Render::render_animated(
            &mut buffer,
            &source_tree,
            &target_tree,
            &' ',
            &AnimationDomain::new(255, Duration::from_millis(i * 10)),
        );
    }
    assert_eq!(buffer.text[0].iter().collect::<String>(), "     XXXXX");
}

fn stacked_bars(alignment: HorizontalAlignment) -> impl View<char, ()> {
    VStack::new((
        Text::new("X", &FONT).animated(Animation::linear(Duration::from_secs(1)), alignment),
        Text::new("Y", &FONT),
        Divider::new(1), // Ensure the stack spans the offered width
    ))
    .with_alignment(alignment)
}

#[test]
fn animation_only_occurs_on_animated_subtrees() {
    let mut buffer = FixedTextBuffer::<10, 3>::default();

    let mut view = stacked_bars(HorizontalAlignment::Leading);

    let source_tree = make_render_tree(&view, buffer.size(), &mut ());

    view = stacked_bars(HorizontalAlignment::Trailing);

    let target_tree = make_render_tree(&view, buffer.size(), &mut ());

    Render::render_animated(
        &mut buffer,
        &source_tree,
        &target_tree,
        &' ',
        &AnimationDomain::new(255, Duration::from_millis(0)),
    );
    assert_eq!(buffer.text[0].iter().collect::<String>(), "X         ");
    assert_eq!(
        buffer.text[1].iter().collect::<String>(),
        "         Y",
        "Y text should immediately render at the target location"
    );
    buffer.clear();

    for i in 1..101u64 {
        Render::render_animated(
            &mut buffer,
            &source_tree,
            &target_tree,
            &' ',
            &AnimationDomain::new(255, Duration::from_millis(i * 10)),
        );
    }
    assert_eq!(buffer.text[0].iter().collect::<String>(), "XXXXXXXXXX");
    assert_eq!(buffer.text[1].iter().collect::<String>(), "         Y");
}

/// Value is used to trigger the top text to animate.
/// Changing alignment should not trigger animation.
fn stacked_bars_value(
    x_value: u8,
    y_value: u8,
    alignment: HorizontalAlignment,
) -> impl View<char, ()> {
    VStack::new((
        Text::new("X", &FONT).animated(Animation::linear(Duration::from_secs(1)), x_value),
        Text::new("Y", &FONT).animated(Animation::linear(Duration::from_secs(2)), y_value),
        Divider::new(1), // Ensure the stack spans the offered width
    ))
    .with_alignment(alignment)
}

/// Even though the Y text is animated, it will never animate because the value is constant
#[test]
fn no_animation_when_value_doesnt_change() {
    let mut buffer = FixedTextBuffer::<10, 3>::default();

    let mut view = stacked_bars_value(0, 0, HorizontalAlignment::Leading);

    let source_tree = make_render_tree(&view, buffer.size(), &mut ());

    view = stacked_bars_value(1, 0, HorizontalAlignment::Trailing);

    let target_tree = make_render_tree(&view, buffer.size(), &mut ());

    Render::render_animated(
        &mut buffer,
        &source_tree,
        &target_tree,
        &' ',
        &AnimationDomain::new(255, Duration::from_millis(0)),
    );
    assert_eq!(buffer.text[0].iter().collect::<String>(), "X         ");
    assert_eq!(buffer.text[1].iter().collect::<String>(), "         Y");
    buffer.clear();

    for i in 1..101u64 {
        Render::render_animated(
            &mut buffer,
            &source_tree,
            &target_tree,
            &' ',
            &AnimationDomain::new(255, Duration::from_millis(i * 10)),
        );
    }
    assert_eq!(buffer.text[0].iter().collect::<String>(), "XXXXXXXXXX");
    assert_eq!(buffer.text[1].iter().collect::<String>(), "         Y");
}

/// Value is used to trigger the top text to animate.
/// Changing alignment should not trigger animation.
fn stacked_bars_3_value(
    x_value: u8,
    y_value: u8,
    z_value: u8,
    alignment: HorizontalAlignment,
) -> impl View<char, ()> {
    VStack::new((
        Text::new("X", &FONT).animated(Animation::linear(Duration::from_secs(1)), x_value),
        Text::new("Y", &FONT).animated(Animation::linear(Duration::from_secs(2)), y_value),
        Text::new("Z", &FONT).animated(Animation::linear(Duration::from_secs(2)), z_value),
        Divider::new(1), // Ensure the stack spans the offered width
    ))
    .with_alignment(alignment)
}

#[test]
fn partial_animation_join() {
    let mut buffer = FixedTextBuffer::<11, 4>::default();

    let mut view = stacked_bars_3_value(0, 0, 0, HorizontalAlignment::Leading);

    let mut env = DefaultEnvironment::new(Duration::from_millis(0));
    let mut state = view.build_state(&mut ());
    let layout = view.layout(&buffer.size().into(), &env, &mut (), &mut state);
    let mut source_tree = view.render_tree(&layout, Point::default(), &env, &mut (), &mut state);

    // change both x and y
    // don't update the env app time, so both frames are generated at the same time
    view = stacked_bars_3_value(1, 1, 1, HorizontalAlignment::Trailing);
    let layout = view.layout(&buffer.size().into(), &env, &mut (), &mut state);
    let mut target_tree = view.render_tree(&layout, Point::default(), &env, &mut (), &mut state);

    // initial render sets target animation times
    Render::render_animated(
        &mut buffer,
        &source_tree,
        &target_tree,
        &' ',
        &AnimationDomain::new(255, Duration::from_millis(0)),
    );
    assert_eq!(buffer.text[0].iter().collect::<String>(), "X          ");
    assert_eq!(buffer.text[1].iter().collect::<String>(), "Y          ");
    assert_eq!(buffer.text[2].iter().collect::<String>(), "Z          ");
    buffer.clear();

    // first real interpolated frame, at .5s
    Render::render_animated(
        &mut buffer,
        &source_tree,
        &target_tree,
        &' ',
        &AnimationDomain::new(255, Duration::from_millis(550)),
    );
    assert_eq!(buffer.text[0].iter().collect::<String>(), "     X     ");
    assert_eq!(buffer.text[1].iter().collect::<String>(), "  Y        ");
    assert_eq!(buffer.text[2].iter().collect::<String>(), "  Z        ");
    buffer.clear();

    // Join the views at 1s of animation
    target_tree.join_from(
        &source_tree,
        &AnimationDomain::new(255, Duration::from_millis(1050)),
    );
    source_tree = target_tree;

    // The joined view should render to the correct partial animation state
    source_tree.render(&mut buffer, &' ');
    assert_eq!(buffer.text[0].iter().collect::<String>(), "          X");
    assert_eq!(buffer.text[1].iter().collect::<String>(), "     Y     ");
    assert_eq!(buffer.text[2].iter().collect::<String>(), "     Z     ");
    buffer.clear();

    // Create a new view, only changing the y value
    // However, in the new target align leading
    env.app_time = Duration::from_millis(1050);
    view = stacked_bars_3_value(1, 2, 1, HorizontalAlignment::Leading);
    let layout = view.layout(&buffer.size().into(), &env, &mut (), &mut state);
    target_tree = view.render_tree(&layout, Point::default(), &env, &mut (), &mut state);

    // The previous y animation should continue, but x should jump because the state changed
    // without a change in value

    // No time elapsed since the join, so Y shouldn't have moved, but X jumps
    // Z value didn't change, so it should continue the old animation
    Render::render_animated(
        &mut buffer,
        &source_tree,
        &target_tree,
        &' ',
        &AnimationDomain::new(255, Duration::from_millis(1050)),
    );

    assert_eq!(buffer.text[0].iter().collect::<String>(), "X          ");
    assert_eq!(buffer.text[1].iter().collect::<String>(), "     Y     ");
    assert_eq!(buffer.text[2].iter().collect::<String>(), "     Z     ");
    buffer.clear();

    // Y changed, so the animation duration is reset and it takes 2s to move from the middle
    // to the left
    Render::render_animated(
        &mut buffer,
        &source_tree,
        &target_tree,
        &' ',
        &AnimationDomain::new(255, Duration::from_millis(2000)),
    );

    assert_eq!(buffer.text[0].iter().collect::<String>(), "X          ");
    assert_eq!(buffer.text[1].iter().collect::<String>(), "  Y        ");
    assert_eq!(buffer.text[2].iter().collect::<String>(), "Z          ");
    buffer.clear();

    Render::render_animated(
        &mut buffer,
        &source_tree,
        &target_tree,
        &' ',
        &AnimationDomain::new(255, Duration::from_millis(3000)),
    );

    assert_eq!(buffer.text[0].iter().collect::<String>(), "X          ");
    assert_eq!(buffer.text[1].iter().collect::<String>(), "Y          ");
    assert_eq!(buffer.text[2].iter().collect::<String>(), "Z          ");
}

/// Renders a toggle switch that animates between on and off. The subtext is only displayed if the
/// switch is on
///
///   ____#
/// subtext
///
/// and
///
/// #____
///
/// Only the toggle is animated, not the text
fn toggle_switch(is_on: bool, subtext: &str) -> impl View<char, ()> + use<'_> {
    let alignment = if is_on {
        HorizontalAlignment::Trailing
    } else {
        HorizontalAlignment::Leading
    };

    VStack::new((
        ZStack::new((
            Rectangle.foreground_color('_').frame_sized(5, 1),
            Rectangle.foreground_color('#').frame_sized(1, 1),
        ))
        .with_horizontal_alignment(alignment)
        .animated(Animation::linear(Duration::from_secs(1)), is_on),
        if_view!((is_on) {
            Text::new(subtext, &FONT).multiline_text_alignment(HorizontalTextAlignment::Trailing)
        } else {
            EmptyView
        }),
    ))
    .with_alignment(HorizontalAlignment::Trailing)
}

fn toggle_move(is_on: bool, alignment: HorizontalAlignment) -> impl View<char, ()> {
    toggle_switch(is_on, "xxx")
        .flex_frame()
        .with_infinite_max_width()
        .with_infinite_max_height()
        .with_vertical_alignment(VerticalAlignment::Top)
        .with_horizontal_alignment(alignment)
}

#[test]
fn jump_toggle_animation() {
    let mut buffer = FixedTextBuffer::<11, 4>::default();

    let mut view = toggle_move(false, HorizontalAlignment::Trailing);
    let mut state = view.build_state(&mut ());
    let mut env = DefaultEnvironment::new(Duration::from_millis(0));
    let layout = view.layout(&buffer.size().into(), &env, &mut (), &mut state);
    let mut source_tree = view.render_tree(&layout, Point::default(), &env, &mut (), &mut state);

    view = toggle_move(true, HorizontalAlignment::Trailing);
    let layout = view.layout(&buffer.size().into(), &env, &mut (), &mut state);
    let mut target_tree = view.render_tree(&layout, Point::default(), &env, &mut (), &mut state);

    // initial render sets target animation times
    Render::render_animated(
        &mut buffer,
        &source_tree,
        &target_tree,
        &' ',
        &AnimationDomain::top_level(Duration::from_millis(0)),
    );
    assert_eq!(buffer.text[0].iter().collect::<String>(), "      #____");
    assert_eq!(buffer.text[1].iter().collect::<String>(), "        xxx");
    assert_eq!(buffer.text[2].iter().collect::<String>(), "           ");
    buffer.clear();

    // first real interpolated frame, at .5s
    Render::render_animated(
        &mut buffer,
        &source_tree,
        &target_tree,
        &' ',
        &AnimationDomain::top_level(Duration::from_millis(550)),
    );
    assert_eq!(buffer.text[0].iter().collect::<String>(), "      __#__");
    assert_eq!(buffer.text[1].iter().collect::<String>(), "        xxx");
    assert_eq!(buffer.text[2].iter().collect::<String>(), "           ");
    buffer.clear();

    target_tree.join_from(
        &source_tree,
        &AnimationDomain::top_level(Duration::from_millis(550)),
    );
    source_tree = target_tree;

    source_tree.render(&mut buffer, &' ');
    assert_eq!(buffer.text[0].iter().collect::<String>(), "      __#__");
    assert_eq!(buffer.text[1].iter().collect::<String>(), "        xxx");
    assert_eq!(buffer.text[2].iter().collect::<String>(), "           ");
    buffer.clear();

    env.app_time = Duration::from_millis(550);
    view = toggle_move(true, HorizontalAlignment::Leading);
    let layout = view.layout(&buffer.size().into(), &env, &mut (), &mut state);
    target_tree = view.render_tree(&layout, Point::default(), &env, &mut (), &mut state);

    Render::render_animated(
        &mut buffer,
        &source_tree,
        &target_tree,
        &' ',
        &AnimationDomain::top_level(Duration::from_millis(550)),
    );

    // Toggle should continue to animate, but the subtext jumps
    assert_eq!(buffer.text[0].iter().collect::<String>(), "      __#__");
    assert_eq!(buffer.text[1].iter().collect::<String>(), "  xxx      ");
    assert_eq!(buffer.text[2].iter().collect::<String>(), "           ");
    buffer.clear();

    Render::render_animated(
        &mut buffer,
        &source_tree,
        &target_tree,
        &' ',
        &AnimationDomain::top_level(Duration::from_millis(1000)),
    );

    // The toggle completes its animation, catching up to the subtext
    assert_eq!(buffer.text[0].iter().collect::<String>(), "____#      ");
    assert_eq!(buffer.text[1].iter().collect::<String>(), "  xxx      ");
    assert_eq!(buffer.text[2].iter().collect::<String>(), "           ");
}

fn toggle_stack(is_on: bool) -> impl View<char, ()> {
    VStack::new((
        toggle_switch(is_on, "123456\n123"),
        toggle_switch(is_on, "xxx"),
    ))
    .with_alignment(HorizontalAlignment::Trailing)
    // this animation should do nothing, because the text is transitioned, not moved
    .animated(Animation::linear(Duration::from_millis(2000)), is_on)
    .flex_frame()
    .with_infinite_max_width()
    .with_infinite_max_height()
    .with_vertical_alignment(VerticalAlignment::Top)
    .with_horizontal_alignment(HorizontalAlignment::Trailing)
}

#[test]
fn nested_toggle_animation() {
    let mut buffer = FixedTextBuffer::<11, 5>::default();

    let mut view = toggle_stack(false);

    let mut state = view.build_state(&mut ());
    let mut env = DefaultEnvironment::new(Duration::from_millis(0));
    let layout = view.layout(&buffer.size().into(), &env, &mut (), &mut state);
    let mut source_tree = view.render_tree(&layout, Point::default(), &env, &mut (), &mut state);

    // don't update the env app time, so both frames are generated at the same time
    view = toggle_stack(true);
    let layout = view.layout(&buffer.size().into(), &env, &mut (), &mut state);
    let mut target_tree = view.render_tree(&layout, Point::default(), &env, &mut (), &mut state);

    // initial render sets target animation times
    Render::render_animated(
        &mut buffer,
        &source_tree,
        &target_tree,
        &' ',
        &AnimationDomain::top_level(Duration::from_millis(0)),
    );
    // subtext should jump
    assert_eq!(buffer.text[0].iter().collect::<String>(), "      #____");
    assert_eq!(buffer.text[1].iter().collect::<String>(), "     1#____"); // toggle 1 subtext is
    // overwritten by toggle 2
    assert_eq!(buffer.text[2].iter().collect::<String>(), "        123");
    assert_eq!(buffer.text[3].iter().collect::<String>(), "           ");
    assert_eq!(buffer.text[4].iter().collect::<String>(), "        xxx");
    buffer.clear();

    // first real interpolated frame, at .5s
    Render::render_animated(
        &mut buffer,
        &source_tree,
        &target_tree,
        &' ',
        &AnimationDomain::top_level(Duration::from_millis(550)),
    );
    assert_eq!(buffer.text[0].iter().collect::<String>(), "      __#__");
    assert_eq!(buffer.text[1].iter().collect::<String>(), "     123456");
    assert_eq!(buffer.text[2].iter().collect::<String>(), "      __#__"); // partially animated
    assert_eq!(buffer.text[3].iter().collect::<String>(), "           ");
    assert_eq!(buffer.text[4].iter().collect::<String>(), "        xxx");
    buffer.clear();

    // Join the views at 1s of animation
    target_tree.join_from(
        &source_tree,
        &AnimationDomain::top_level(Duration::from_millis(550)),
    );
    source_tree = target_tree;

    // The joined view should render to the partial animation state
    source_tree.render(&mut buffer, &' ');
    assert_eq!(buffer.text[0].iter().collect::<String>(), "      __#__");
    assert_eq!(buffer.text[1].iter().collect::<String>(), "     123456");
    assert_eq!(buffer.text[2].iter().collect::<String>(), "      __#__");
    assert_eq!(buffer.text[3].iter().collect::<String>(), "           ");
    assert_eq!(buffer.text[4].iter().collect::<String>(), "        xxx");
    buffer.clear();

    env.app_time = Duration::from_millis(550);
    view = toggle_stack(true);
    let layout = view.layout(&buffer.size().into(), &env, &mut (), &mut state);
    target_tree = view.render_tree(&layout, Point::default(), &env, &mut (), &mut state);

    Render::render_animated(
        &mut buffer,
        &source_tree,
        &target_tree,
        &' ',
        &AnimationDomain::top_level(Duration::from_millis(550)),
    );
    // again, should be the same view
    assert_eq!(buffer.text[0].iter().collect::<String>(), "      __#__");
    assert_eq!(buffer.text[1].iter().collect::<String>(), "     123456");
    assert_eq!(buffer.text[2].iter().collect::<String>(), "      __#__");
    assert_eq!(buffer.text[3].iter().collect::<String>(), "           ");
    assert_eq!(buffer.text[4].iter().collect::<String>(), "        xxx");

    buffer.clear();

    Render::render_animated(
        &mut buffer,
        &source_tree,
        &target_tree,
        &' ',
        &AnimationDomain::top_level(Duration::from_millis(1000)),
    );

    assert_eq!(buffer.text[0].iter().collect::<String>(), "      ____#");
    assert_eq!(buffer.text[1].iter().collect::<String>(), "     123456");
    assert_eq!(buffer.text[2].iter().collect::<String>(), "        123");
    assert_eq!(buffer.text[3].iter().collect::<String>(), "      ____#");
    assert_eq!(buffer.text[4].iter().collect::<String>(), "        xxx");
}