sequoia-view 0.1.1

Sequoia GUI framework — view tree and runtime
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
#![allow(clippy::unwrap_used, clippy::expect_used)]

use sequoia_core::prelude::*;
use sequoia_reactive::Signal;
use sequoia_text::TextSystem;
use sequoia_view::prelude::*;

#[test]
fn panel_paints_inset_quad() {
    let rt = Runtime::new();
    rt.mount(
        Panel::new()
            .background(Color::RED)
            .width(200.0)
            .height(100.0),
    );

    let mut text = TextSystem::new();
    rt.layout(Rect::from_xywh(0.0, 0.0, 400.0, 400.0), &mut text);
    let mut list = RenderList::new();
    rt.paint(&mut list, &mut text);

    assert_eq!(list.quads().count(), 1);
    let q = list.quads().next().unwrap();
    assert_eq!(q.color, Color::RED);
    assert_eq!(q.rect.size, Size::new(200.0, 100.0));
}

#[test]
fn empty_runtime_paints_nothing() {
    let rt = Runtime::new();
    let mut text = TextSystem::new();
    let mut list = RenderList::new();
    rt.paint(&mut list, &mut text);
    assert!(list.is_empty());
}

struct DynPanel {
    clicks: Signal<u32>,
}

impl View for DynPanel {
    fn body(&self) -> impl View + 'static {
        let n = self.clicks.get();
        let color = if n.is_multiple_of(2) {
            Color::RED
        } else {
            Color::BLUE
        };

        Panel::new().background(color).width(50.0).height(50.0)
    }
}

#[test]
fn color_follows_signal_after_tick() {
    let clicks = Signal::new(0u32);
    let rt = Runtime::new();
    rt.mount(DynPanel {
        clicks: clicks.clone(),
    });

    let mut text = TextSystem::new();
    rt.layout(Rect::from_xywh(0.0, 0.0, 50.0, 50.0), &mut text);
    let mut list = RenderList::new();
    rt.paint(&mut list, &mut text);
    assert_eq!(list.quads().next().unwrap().color, Color::RED);

    clicks.set(1);
    rt.tick();
    rt.layout(Rect::from_xywh(0.0, 0.0, 50.0, 50.0), &mut text);
    let mut list = RenderList::new();
    rt.paint(&mut list, &mut text);
    assert_eq!(list.quads().next().unwrap().color, Color::BLUE);
}

#[test]
fn opacity_wrapper_modulates_child_alpha() {
    let rt = Runtime::new();
    rt.mount(Opacity::new(
        Panel::new()
            .background(Color::RED)
            .width(200.0)
            .height(100.0),
        0.5,
    ));

    let mut text = TextSystem::new();
    rt.layout(Rect::from_xywh(0.0, 0.0, 400.0, 400.0), &mut text);
    let mut list = RenderList::new();
    rt.paint(&mut list, &mut text);

    let q = list.quads().next().expect("dziecko maluje quad");
    assert!(
        (q.color.a - 0.5).abs() < 1e-6,
        "alfa po Opacity(0.5): {}",
        q.color.a
    );
    assert_eq!(q.color.r, 1.0);
}

#[test]
fn opacity_wrappers_nest_multiplicatively() {
    let rt = Runtime::new();
    rt.mount(Opacity::new(
        Opacity::new(
            Panel::new().background(Color::RED).width(50.0).height(50.0),
            0.5,
        ),
        0.5,
    ));

    let mut text = TextSystem::new();
    rt.layout(Rect::from_xywh(0.0, 0.0, 400.0, 400.0), &mut text);
    let mut list = RenderList::new();
    rt.paint(&mut list, &mut text);

    let q = list.quads().next().expect("quad");
    assert!((q.color.a - 0.25).abs() < 1e-6, "0.5 * 0.5: {}", q.color.a);
}

#[test]
fn translate_wrapper_shifts_child_paint() {
    let rt = Runtime::new();
    rt.mount(Translate::new(
        Panel::new()
            .background(Color::RED)
            .width(200.0)
            .height(100.0),
        10.0,
        20.0,
    ));

    let mut text = TextSystem::new();
    rt.layout(Rect::from_xywh(0.0, 0.0, 400.0, 400.0), &mut text);
    let mut list = RenderList::new();
    rt.paint(&mut list, &mut text);

    let q = list.quads().next().expect("quad");
    assert_eq!(q.rect.left(), 10.0, "malarskie dx");
    assert_eq!(q.rect.top(), 20.0, "malarskie dy");
    assert_eq!(q.rect.size, Size::new(200.0, 100.0), "rozmiar bez zmian");
}

#[test]
fn nested_translate_wrappers_accumulate() {
    let rt = Runtime::new();
    rt.mount(Translate::new(
        Translate::new(
            Panel::new().background(Color::RED).width(50.0).height(50.0),
            5.0,
            0.0,
        ),
        10.0,
        20.0,
    ));

    let mut text = TextSystem::new();
    rt.layout(Rect::from_xywh(0.0, 0.0, 400.0, 400.0), &mut text);
    let mut list = RenderList::new();
    rt.paint(&mut list, &mut text);

    let q = list.quads().next().expect("quad");
    assert_eq!(q.rect.left(), 15.0, "dx adds up down");
    assert_eq!(q.rect.top(), 20.0);
}

#[test]
fn text_view_produces_text_item() {
    let rt = Runtime::new();
    rt.mount(Text::new("Sequoia").size(24.0));

    let mut text = TextSystem::new();
    if text.shape("Sequoia", 24.0).glyph_count() == 0 {
        eprintln!("skip: no fonts in environment");
        return;
    }

    rt.layout(Rect::from_xywh(0.0, 0.0, 400.0, 200.0), &mut text);
    let mut list = RenderList::new();
    rt.paint(&mut list, &mut text);

    let Some(RenderItem::Text(draw)) = list
        .items()
        .iter()
        .find(|i| matches!(i, RenderItem::Text(_)))
    else {
        panic!("expected RenderItem::Text");
    };
    assert!(!draw.glyphs.is_empty());
}

#[test]
fn text_decoration_paints_a_line_under_the_text() {
    let rt = Runtime::new();
    rt.mount(Text::new("Sequoia").size(20.0).underline(true));

    let mut text = TextSystem::new();
    if text.shape("Sequoia", 20.0).glyph_count() == 0 {
        eprintln!("skip: no fonts in environment");
        return;
    }

    rt.layout(Rect::from_xywh(0.0, 0.0, 400.0, 100.0), &mut text);
    let mut list = RenderList::new();
    rt.paint(&mut list, &mut text);

    let quads: Vec<_> = list.quads().collect();
    assert_eq!(quads.len(), 1, "underline is one quad");
    let d = quads[0];
    assert!(d.rect.size.width > 0.0, "line spans the text width");
    assert!(
        (1.0..3.0).contains(&d.rect.size.height),
        "thickness ~7% em: {}",
        d.rect.size.height
    );
    assert!(
        list.items()
            .iter()
            .any(|i| matches!(i, RenderItem::Text(_)))
    );
}

#[test]
fn text_without_decoration_paints_no_extra_quads() {
    let rt = Runtime::new();
    rt.mount(Text::new("Sequoia").size(20.0));
    let mut text = TextSystem::new();
    if text.shape("Sequoia", 20.0).glyph_count() == 0 {
        eprintln!("skip: no fonts in environment");
        return;
    }
    rt.layout(Rect::from_xywh(0.0, 0.0, 400.0, 100.0), &mut text);
    let mut list = RenderList::new();
    rt.paint(&mut list, &mut text);
    assert_eq!(list.quads().count(), 0, "`Text` alone adds no quads");
}

struct TextOverPanel;

impl View for TextOverPanel {
    fn body(&self) -> impl View + 'static {
        Column::new()
            .child(Text::new("underneath"))
            .child(
                Panel::new()
                    .background(Color::RED)
                    .width(200.0)
                    .height(100.0),
            )
            .child(Text::new("on top"))
    }
}

#[test]
fn render_list_keeps_paint_order_across_types() {
    let rt = Runtime::new();
    rt.mount(TextOverPanel);

    let mut text = TextSystem::new();
    if text.shape("underneath", 14.0).glyph_count() == 0 {
        eprintln!("skip: no fonts in environment");
        return;
    }

    rt.layout(Rect::from_xywh(0.0, 0.0, 400.0, 400.0), &mut text);
    let mut list = RenderList::new();
    rt.paint(&mut list, &mut text);

    let kinds: Vec<char> = list
        .items()
        .iter()
        .map(|i| match i {
            RenderItem::Text(_) => 'T',
            RenderItem::Quad(_) => 'Q',
            RenderItem::Image(_) => 'I',
            RenderItem::Shadow(_) => 'S',
            RenderItem::Waveform(_) => 'W',

            _ => '?',
        })
        .collect();
    assert_eq!(kinds, vec!['T', 'Q', 'T'], "paint order: {kinds:?}");
}

struct BackdropApp {
    on: Signal<bool>,
}

impl View for BackdropApp {
    fn body(&self) -> impl View + 'static {
        let on = self.on.get();
        Stack::new()
            .width(400.0)
            .height(400.0)
            .fill(
                Panel::new()
                    .background(Color::RED)
                    .width(400.0)
                    .height(400.0),
            )
            .child(
                Panel::new()
                    .background(Color::BLUE)
                    .width(200.0)
                    .height(100.0)
                    .blur_behind_with(move |_| {
                        if on {
                            Backdrop::blur(16.0).radius(12.0)
                        } else {
                            Backdrop::NONE
                        }
                    }),
            )
    }
}

#[test]
fn backdrop_toggle_emits_marker_between_layers() {
    let on = Signal::new(false);
    let rt = Runtime::new();
    rt.mount(BackdropApp { on: on.clone() });

    let mut text = TextSystem::new();
    rt.layout(Rect::from_xywh(0.0, 0.0, 400.0, 400.0), &mut text);
    let mut list = RenderList::new();
    rt.paint(&mut list, &mut text);
    assert!(
        !list.has_backdrops(),
        "disabled effect does not emit marker"
    );

    on.set(true);
    rt.tick();
    rt.layout(Rect::from_xywh(0.0, 0.0, 400.0, 400.0), &mut text);
    let mut list = RenderList::new();
    rt.paint(&mut list, &mut text);
    assert_eq!(list.backdrops().len(), 1);
    let span = list.backdrops()[0];
    assert!(span.at > 0, "background below: at={}", span.at);
    assert!(
        span.at < list.len(),
        "content of the panel above the marker: at={} len={}",
        span.at,
        list.len()
    );
    assert_eq!(span.draw.blur, 16.0);
    assert_eq!(span.draw.radius, 12.0);
    assert_eq!(span.draw.rect.size, Size::new(200.0, 100.0));

    on.set(false);
    rt.tick();
    rt.layout(Rect::from_xywh(0.0, 0.0, 400.0, 400.0), &mut text);
    let mut list = RenderList::new();
    rt.paint(&mut list, &mut text);
    assert!(!list.has_backdrops(), "disabling clears the marker");
}

#[test]
fn filter_wrapper_opens_span_over_child_items() {
    let rt = Runtime::new();
    rt.mount(
        Panel::new()
            .background(Color::RED)
            .width(200.0)
            .height(100.0)
            .blur_own(16.0),
    );

    let mut text = TextSystem::new();
    rt.layout(Rect::from_xywh(0.0, 0.0, 400.0, 400.0), &mut text);
    let mut list = RenderList::new();
    rt.paint(&mut list, &mut text);

    assert!(list.has_filters(), "niepusty filtr otwiera zakres");
    assert_eq!(list.filter_spans().len(), 1);
    let span = list.filter_spans()[0];
    assert_eq!((span.start, span.end), (0, 1), "quad panelu w zakresie");
    assert_eq!(span.draw.blur, 16.0);
    assert_eq!(span.draw.rect.size, Size::new(200.0, 100.0));

    assert_eq!(list.quads().count(), 1);
}

#[test]
fn filter_mask_rides_span_to_renderer() {
    use sequoia_style::gradient;
    let rt = Runtime::new();
    rt.mount(
        Panel::new()
            .background(Color::RED)
            .width(200.0)
            .height(100.0)
            .blur_own_with(|b| b.blur_radius(16.0).mask(gradient(1.0, 0.0))),
    );

    let mut text = TextSystem::new();
    rt.layout(Rect::from_xywh(0.0, 0.0, 400.0, 400.0), &mut text);
    let mut list = RenderList::new();
    rt.paint(&mut list, &mut text);

    assert!(list.has_filters());
    let span = list.filter_spans()[0];
    assert_eq!(
        span.draw.mask.mask_vec(),
        [1.0, 1.0, 0.0, std::f32::consts::FRAC_PI_2],
        "rampa pionowa do shadera"
    );
}

#[test]
fn backdrop_filter_mask_rides_marker() {
    use sequoia_style::gradient;
    let rt = Runtime::new();
    rt.mount(
        Panel::new()
            .background(Color::RED)
            .width(200.0)
            .height(100.0)
            .blur_behind_with(|b| b.blur_radius(8.0).mask(gradient(1.0, 0.0))),
    );

    let mut text = TextSystem::new();
    rt.layout(Rect::from_xywh(0.0, 0.0, 400.0, 400.0), &mut text);
    let mut list = RenderList::new();
    rt.paint(&mut list, &mut text);

    assert!(list.has_backdrops());
    assert_eq!(
        list.backdrops()[0].draw.mask.mask_vec()[0],
        1.0,
        "marker carries ramp mask"
    );
}

#[test]
fn filter_none_costs_nothing() {
    let rt = Runtime::new();
    rt.mount(
        Panel::new()
            .background(Color::RED)
            .width(200.0)
            .height(100.0)
            .blur_own(0.0),
    );

    let mut text = TextSystem::new();
    rt.layout(Rect::from_xywh(0.0, 0.0, 400.0, 400.0), &mut text);
    let mut list = RenderList::new();
    rt.paint(&mut list, &mut text);

    assert!(!list.has_filters(), "pusty filtr nie emituje nic");
    assert_eq!(list.quads().count(), 1);
}

#[test]
fn typing_keeps_frames_deterministic_and_blur_marker_stable() {
    let text = Signal::new("ala".to_string());
    let probe = text.clone();
    let rt = Runtime::new();
    rt.mount(
        Column::new()
            .child(Text::with(move || probe.get()))
            .child(Text::new("pill").blur_behind(4.0)),
    );

    let mut textsys = TextSystem::new();
    let mut frame = || {
        rt.layout(Rect::from_xywh(0.0, 0.0, 400.0, 400.0), &mut textsys);
        let mut list = RenderList::new();
        rt.paint(&mut list, &mut textsys);
        list
    };

    let dump = |list: &RenderList| format!("{:?}", list);
    assert_eq!(
        dump(&frame()),
        dump(&frame()),
        "klatki bez zmian identyczne"
    );

    let mark = |list: &RenderList| {
        assert_eq!(list.backdrops().len(), 1, "jeden marker blur");
        list.backdrops()[0].draw.rect
    };
    let r0 = mark(&frame());

    for word in ["ala ", "ala m", "ala ma", "ala ma ", "ala ma k"] {
        text.set(word.to_string());
        rt.tick();
        let list = frame();
        assert_eq!(mark(&list), r0, "marker stoi w miejscu podczas pisania");
        assert_eq!(dump(&list), dump(&frame()), "repeated paint stable");
    }
}