smudge-core 0.2.0

Widget tree, reactive bindings, layout, paint and damage tracking for the Smudge GUI library
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
//! Behavior tests for style variants.
//!
//! Every test drives the same frame pipeline a window does, through the
//! harness, and checks what the surface shows against a harness that mounts
//! the expected scene plainly.

use smudge_core::element::IntoElement;
use smudge_core::geometry::{Color, Point};
use smudge_core::input::{InputEvent, PointerButton};
use smudge_core::reactive::{self, prelude::*};
use smudge_core::style::{Edges, Length, px};
use smudge_core::widget::panel;
use smudge_test::Harness;

/// The background a button rests in.
const IDLE: Color = Color::rgb(50, 50, 50);

/// The background the hovered patch asks for.
const HOVER: Color = Color::rgb(90, 90, 90);

/// The background the pressed patch asks for.
const PRESSED: Color = Color::rgb(30, 30, 30);

/// The background the disabled patch asks for.
const SWITCHED_OFF: Color = Color::rgb(35, 35, 35);

/// Wraps a node under test in a plain parent.
///
/// The layout pass sizes the root to the whole viewport, whatever its style
/// says, so a node with a size of its own has to sit under the root.
fn scene(child: impl IntoElement) -> impl IntoElement {
    panel().child(child)
}

/// A harness that shows `background` on a plainly mounted button of the size
/// the tests use, for pixel comparisons.
fn button_in_color(background: Color) -> Harness {
    let mut harness = Harness::new(100.0, 60.0, 1.0, move || {
        scene(
            panel()
                .width(px(40.0))
                .height(px(20.0))
                .background(background),
        )
    });
    harness.frame();
    harness
}

/// Moves the pointer off every control and lets the change settle.
///
/// The pointer starts at the origin, and the first layout pass hovers a
/// control that sits there, so a test that asks for a specific hover change
/// parks the pointer on neutral ground first.
fn park_pointer(harness: &mut Harness) {
    harness.move_to(Point { x: 80.0, y: 50.0 });
    harness.frame();
}

#[test]
fn hovering_a_button_repaints_its_background_without_layout() {
    let mut harness = Harness::new(100.0, 60.0, 1.0, || {
        scene(
            panel()
                .width(px(40.0))
                .height(px(20.0))
                .background(IDLE)
                .hover(|p| p.background(HOVER)),
        )
    });
    assert!(harness.frame().full, "the first frame repaints everything");
    park_pointer(&mut harness);

    harness.move_to(Point { x: 10.0, y: 10.0 });
    let frame = harness.frame();
    assert_eq!(frame.nodes_painted, 1, "the hovered button repaints");
    assert!(
        !frame.layout_ran,
        "a patch that touches only paint skips layout"
    );
    assert!(!frame.full);

    harness.assert_matches(&button_in_color(HOVER));
}

#[test]
fn a_hover_patch_that_changes_the_border_width_triggers_layout() {
    let mut harness = Harness::new(100.0, 60.0, 1.0, || {
        scene(
            panel()
                .width(px(40.0))
                .height(px(20.0))
                .border_color(Color::BLACK)
                .hover(|p| p.border(2.0)),
        )
    });
    harness.frame();
    park_pointer(&mut harness);

    harness.move_to(Point { x: 10.0, y: 10.0 });
    let frame = harness.frame();
    assert!(
        frame.layout_ran,
        "a border width takes space, so the node is laid out again"
    );
    assert_eq!(frame.nodes_painted, 1);

    let mut expected = Harness::new(100.0, 60.0, 1.0, || {
        scene(
            panel()
                .width(px(40.0))
                .height(px(20.0))
                .border(2.0)
                .border_color(Color::BLACK),
        )
    });
    expected.frame();
    harness.assert_matches(&expected);
}

#[test]
fn a_background_binding_change_while_hovered_keeps_the_hover_background() {
    let (color, set_color) = reactive::signal_local(IDLE);
    let mut harness = Harness::new(100.0, 60.0, 1.0, move || {
        scene(
            panel()
                .width(px(40.0))
                .height(px(20.0))
                .background(move || color.get().into())
                .hover(|p| p.background(HOVER)),
        )
    });
    harness.frame();

    harness.move_to(Point { x: 10.0, y: 10.0 });
    harness.frame();
    harness.assert_matches(&button_in_color(HOVER));

    set_color.set(Color::rgb(140, 60, 60));
    let frame = harness.frame();
    assert_eq!(
        frame.nodes_painted, 0,
        "the patch wins over the base style, so nothing repaints"
    );
    harness.assert_matches(&button_in_color(HOVER));

    harness.move_to(Point { x: 80.0, y: 50.0 });
    let frame = harness.frame();
    assert_eq!(
        frame.nodes_painted, 1,
        "when the hover ends the binding's color shows"
    );
    harness.assert_matches(&button_in_color(Color::rgb(140, 60, 60)));
}

#[test]
fn a_hover_style_reaches_a_node_without_handlers_while_an_ancestor_has_one() {
    let mut harness = Harness::new(100.0, 60.0, 1.0, || {
        scene(
            panel()
                .width(px(60.0))
                .height(px(40.0))
                .background(IDLE)
                .hover(|p| p.background(Color::rgb(70, 70, 70)))
                .child(
                    panel()
                        .width(px(20.0))
                        .height(px(20.0))
                        .background(IDLE)
                        .hover(|p| p.background(HOVER)),
                ),
        )
    });
    harness.frame();

    harness.move_to(Point { x: 10.0, y: 10.0 });
    harness.frame();

    let mut expected = Harness::new(100.0, 60.0, 1.0, || {
        scene(
            panel()
                .width(px(60.0))
                .height(px(40.0))
                .background(Color::rgb(70, 70, 70))
                .child(panel().width(px(20.0)).height(px(20.0)).background(HOVER)),
        )
    });
    expected.frame();
    harness.assert_matches(&expected);
}

/// A harness that shows a plainly mounted knob at `left`, for pixel
/// comparisons with the moving-knob scene. `None` leaves the knob
/// unpainted, as the scene's knob is when it is not hovered.
fn knob_at(left: f32, background: Option<Color>) -> Harness {
    let mut harness = Harness::new(100.0, 60.0, 1.0, move || {
        let mut knob = panel()
            .absolute()
            .width(px(20.0))
            .height(px(20.0))
            .inset(Edges {
                left: Length::px(left),
                right: Length::auto(),
                top: Length::auto(),
                bottom: Length::auto(),
            });
        if let Some(color) = background {
            knob = knob.background(color);
        }
        scene(panel().width(px(60.0)).height(px(40.0)).child(knob))
    });
    harness.frame();
    harness
}

#[test]
fn a_node_moved_under_a_still_pointer_becomes_hovered() {
    let (left, set_left) = reactive::signal_local(30.0f32);
    let mut harness = Harness::new(100.0, 60.0, 1.0, move || {
        scene(
            panel().width(px(60.0)).height(px(40.0)).child(
                panel()
                    .absolute()
                    .width(px(20.0))
                    .height(px(20.0))
                    .inset(move || Edges {
                        left: Length::px(left.get()),
                        right: Length::auto(),
                        top: Length::auto(),
                        bottom: Length::auto(),
                    })
                    .hover(|p| p.background(HOVER)),
            ),
        )
    });
    harness.frame();
    harness.move_to(Point { x: 55.0, y: 10.0 });
    harness.frame();
    harness.assert_matches(&knob_at(30.0, None));

    set_left.set(45.0);
    let frame = harness.frame();
    assert_eq!(
        frame.nodes_painted, 1,
        "the knob moved and no pointer event announced it"
    );
    harness.assert_matches(&knob_at(45.0, Some(HOVER)));
}

#[test]
fn a_hovered_node_that_becomes_disabled_paints_its_base_style_again() {
    let (disabled, set_disabled) = reactive::signal_local(false);
    let mut harness = Harness::new(100.0, 60.0, 1.0, move || {
        scene(
            panel()
                .width(px(40.0))
                .height(px(20.0))
                .background(IDLE)
                .hover(|p| p.background(HOVER))
                .disabled(move || disabled.get()),
        )
    });
    harness.frame();

    harness.move_to(Point { x: 10.0, y: 10.0 });
    harness.frame();
    harness.assert_matches(&button_in_color(HOVER));

    set_disabled.set(true);
    let frame = harness.frame();
    assert!(
        frame.nodes_painted >= 1,
        "the hovered node is switched off, so its hover style goes"
    );
    harness.assert_matches(&button_in_color(IDLE));

    harness.move_to(Point { x: 10.0, y: 10.0 });
    harness.frame();
    harness.assert_matches(&button_in_color(IDLE));
}

#[test]
fn a_pressed_patch_applies_while_the_press_is_held() {
    let mut harness = Harness::new(100.0, 60.0, 1.0, || {
        scene(
            panel()
                .width(px(40.0))
                .height(px(20.0))
                .background(IDLE)
                .pressed(|p| p.background(PRESSED)),
        )
    });
    harness.frame();

    harness.event(InputEvent::PointerDown {
        position: Point { x: 10.0, y: 10.0 },
        button: PointerButton::Primary,
    });
    harness.frame();
    harness.assert_matches(&button_in_color(PRESSED));

    harness.event(InputEvent::PointerUp {
        position: Point { x: 10.0, y: 10.0 },
        button: PointerButton::Primary,
    });
    harness.frame();
    harness.assert_matches(&button_in_color(IDLE));
}

#[test]
fn a_disabled_patch_applies_while_the_node_is_disabled() {
    let mut harness = Harness::new(100.0, 60.0, 1.0, || {
        scene(
            panel()
                .width(px(40.0))
                .height(px(20.0))
                .background(IDLE)
                .disabled(true)
                .disabled_style(|p| p.background(SWITCHED_OFF)),
        )
    });
    harness.frame();
    harness.assert_matches(&button_in_color(SWITCHED_OFF));

    harness.move_to(Point { x: 10.0, y: 10.0 });
    harness.frame();
    // A disabled node is never hovered, so its patch stands alone.
    harness.assert_matches(&button_in_color(SWITCHED_OFF));
}

#[test]
fn a_focus_style_draws_for_keyboard_focus_alone_and_drops_with_a_disabled_node() {
    let (disabled, set_disabled) = reactive::signal_local(false);
    let mut harness = Harness::new(100.0, 60.0, 1.0, move || {
        scene(
            panel()
                .width(px(40.0))
                .height(px(20.0))
                .background(IDLE)
                .focusable()
                .focused(|p| p.border(1.0).border_color(Color::rgb(0, 120, 215)))
                .disabled(move || disabled.get()),
        )
    });
    harness.frame();

    // A focus that a press moved draws no focus style.
    harness.click(Point { x: 10.0, y: 10.0 });
    harness.frame();
    harness.assert_matches(&button_in_color(IDLE));

    // Tab moves the focus by keyboard, and the ring appears.
    harness.press_key(smudge_core::input::Key::Tab);
    let frame = harness.frame();
    assert!(
        frame.layout_ran,
        "the ring's border takes space, so the node is laid out again"
    );
    let mut focused = Harness::new(100.0, 60.0, 1.0, || {
        scene(
            panel()
                .width(px(40.0))
                .height(px(20.0))
                .background(IDLE)
                .border(1.0)
                .border_color(Color::rgb(0, 120, 215)),
        )
    });
    focused.frame();
    harness.assert_matches(&focused);

    // Switching the focused node off drops the focus, and the ring with it.
    set_disabled.set(true);
    harness.frame();
    assert!(harness.app().focused().is_none());
    harness.assert_matches(&button_in_color(IDLE));
}

#[test]
fn a_hover_style_that_resizes_a_node_moved_under_a_still_pointer_applies_in_one_frame() {
    let (grow, set_grow) = reactive::signal_local(false);
    let mut harness = Harness::new(200.0, 60.0, 1.0, move || {
        panel().child(
            panel()
                .row()
                .child(
                    panel()
                        .width(move || if grow.get() { px(50.0) } else { px(0.0) })
                        .height(px(20.0)),
                )
                .child(
                    panel()
                        .width(px(40.0))
                        .height(px(20.0))
                        .background(Color::BLACK)
                        .hover(|p| p.width(px(70.0))),
                ),
        )
    });
    harness.frame();
    harness.move_to(Point { x: 60.0, y: 10.0 });
    harness.frame();

    set_grow.set(true);
    harness.frame();

    let mut widths = Vec::new();
    harness.app().visit_nodes(|depth, _name, rect| {
        if depth == 2 {
            widths.push(rect.width());
        }
    });
    assert_eq!(
        widths,
        [50.0, 70.0],
        "the node moved under the pointer has its hovered width after one frame"
    );
}

#[test]
fn an_event_that_changes_a_variant_asks_for_a_frame_and_a_plain_move_does_not() {
    let mut harness = Harness::new(100.0, 60.0, 1.0, || {
        panel().child(
            panel()
                .width(px(40.0))
                .height(px(20.0))
                .background(Color::BLACK)
                .hover(|p| p.background(Color::WHITE)),
        )
    });
    harness.frame();

    harness.move_to(Point { x: 80.0, y: 50.0 });
    assert!(
        !harness.app().needs_frame(),
        "a move over nothing changes nothing"
    );

    harness.move_to(Point { x: 10.0, y: 10.0 });
    assert!(
        harness.app().needs_frame(),
        "hovering a styled node needs a frame"
    );

    harness.frame();
    assert!(!harness.app().needs_frame(), "the frame finished the work");
}

#[test]
fn a_layout_change_after_the_pointer_left_hovers_nothing() {
    let (grow, set_grow) = reactive::signal_local(false);
    let (log, on_hover) = {
        let log = std::rc::Rc::new(std::cell::RefCell::new(Vec::new()));
        let handler = {
            let log = std::rc::Rc::clone(&log);
            move |hovered: bool| log.borrow_mut().push(hovered)
        };
        (log, handler)
    };
    let mut harness = Harness::new(200.0, 60.0, 1.0, move || {
        panel().child(
            panel()
                .row()
                .child(
                    panel()
                        .width(move || if grow.get() { px(50.0) } else { px(0.0) })
                        .height(px(20.0)),
                )
                .child(panel().width(px(40.0)).height(px(20.0)).on_hover(on_hover)),
        )
    });
    harness.frame();
    harness.move_to(Point { x: 60.0, y: 10.0 });
    harness.event(InputEvent::PointerLeft);

    set_grow.set(true);
    harness.frame();

    assert!(
        log.borrow().is_empty(),
        "no pointer is in the window to hover with"
    );
}

/// Returns the width of the node at depth 2 that comes first in paint order.
fn first_width(harness: &mut Harness) -> f32 {
    let mut width = None;
    harness.app().visit_nodes(|depth, _name, rect| {
        if depth == 2 && width.is_none() {
            width = Some(rect.width());
        }
    });
    width.expect("the scene shows a node at depth 2")
}

#[test]
fn a_hover_style_that_moves_its_node_off_a_resting_pointer_does_not_flicker() {
    let (tick, set_tick) = reactive::signal_local(0.0f32);
    let mut harness = Harness::new(200.0, 60.0, 1.0, move || {
        panel().child(
            panel()
                .row()
                .child(
                    panel()
                        .width(px(100.0))
                        .height(px(20.0))
                        .background(Color::BLACK)
                        .hover(|p| p.width(px(20.0))),
                )
                .child(
                    panel()
                        .width(move || px(10.0 + tick.get()))
                        .height(px(20.0)),
                ),
        )
    });
    harness.frame();
    park_pointer(&mut harness);
    harness.move_to(Point { x: 60.0, y: 10.0 });
    harness.frame();
    assert_eq!(
        first_width(&mut harness),
        20.0,
        "the pointer hovers the node, whose patch narrows it off the pointer"
    );

    // Later frames lay out for reasons of their own while the pointer rests.
    for step in 1..=4 {
        set_tick.set(step as f32);
        harness.frame();
        assert_eq!(
            first_width(&mut harness),
            20.0,
            "frame {step}: the hover holds until the pointer moves"
        );
    }

    harness.move_to(Point { x: 150.0, y: 10.0 });
    harness.frame();
    assert_eq!(
        first_width(&mut harness),
        100.0,
        "a pointer move away from the node drops the hover"
    );
}