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
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
//! Window commands, window state and cursors, through the public API.

use std::cell::{Cell, RefCell};
use std::rc::Rc;

use smudge_core::app::{App, AppConfig, RepaintMode};
use smudge_core::geometry::{Color, Point, Size};
use smudge_core::input::{InputEvent, PointerButton};
use smudge_core::reactive::prelude::*;
use smudge_core::reactive::{Memo, Owner, RwSignal};
use smudge_core::style::{Edges, Length, px};
use smudge_core::widget::panel;
use smudge_core::window::{self, Cursor, ResizeEdge, WindowState};
use smudge_test::Harness;

/// The config of a headless app that changes nothing on its own.
fn config() -> AppConfig {
    AppConfig {
        viewport: Size {
            width: 200.0,
            height: 200.0,
        },
        scale: 1.0,
        clear_color: Color::WHITE,
        repaint: RepaintMode::Damage,
    }
}

#[test]
fn a_press_handler_that_moves_the_window_queues_one_command() {
    let mut app = App::new(config(), || {
        panel().child(
            panel()
                .width(px(40.0))
                .height(px(20.0))
                .on_press(window::start_move),
        )
    });
    app.frame();

    let press = Point { x: 10.0, y: 5.0 };
    app.handle_event(InputEvent::PointerMoved { position: press });
    app.handle_event(InputEvent::PointerDown {
        position: press,
        button: PointerButton::Primary,
    });
    app.handle_event(InputEvent::PointerUp {
        position: press,
        button: PointerButton::Primary,
    });

    assert_eq!(
        app.take_window_commands(),
        vec![window::WindowCommand::Move],
        "the press that released over the panel queued exactly one command"
    );
}

#[test]
fn taking_the_commands_empties_the_queue() {
    let mut app = App::new(config(), || {
        panel()
            .child(
                panel()
                    .width(px(40.0))
                    .height(px(20.0))
                    .on_pointer_down(|_| window::start_resize(ResizeEdge::NorthEast)),
            )
            .child(panel().width(px(40.0)).height(px(20.0)).on_press(|| {
                window::toggle_maximize();
                window::close();
            }))
    });
    app.frame();

    let first = Point { x: 10.0, y: 5.0 };
    app.handle_event(InputEvent::PointerDown {
        position: first,
        button: PointerButton::Primary,
    });
    let second = Point { x: 50.0, y: 10.0 };
    app.handle_event(InputEvent::PointerMoved { position: second });
    app.handle_event(InputEvent::PointerDown {
        position: second,
        button: PointerButton::Primary,
    });
    app.handle_event(InputEvent::PointerUp {
        position: second,
        button: PointerButton::Primary,
    });

    assert_eq!(
        app.take_window_commands(),
        vec![
            window::WindowCommand::Resize(ResizeEdge::NorthEast),
            window::WindowCommand::ToggleMaximize,
            window::WindowCommand::Close,
        ],
        "the commands queue in the order their handlers ran"
    );
    assert!(
        app.take_window_commands().is_empty(),
        "taking the commands empties the queue, so a headless app keeps it \
         only until a test inspects it"
    );
}

#[test]
fn maximized_reruns_a_binding_when_the_state_changes() {
    let mut app = App::new(config(), || {
        panel().child(
            panel()
                .width(px(40.0))
                .height(px(20.0))
                .hidden(window::maximized),
        )
    });
    app.frame();

    let mut visible = 0;
    app.visit_nodes(|_, _, _| visible += 1);
    assert_eq!(
        visible, 2,
        "the window starts unmaximized, so the node shows"
    );

    app.set_window_state(WindowState::default().with_maximized(true));
    app.frame();

    visible = 0;
    app.visit_nodes(|_, _, _| visible += 1);
    assert_eq!(
        visible, 1,
        "the binding hid the node when the state changed"
    );
}

#[test]
fn an_equal_window_state_report_reruns_no_binding() {
    let runs = Rc::new(Cell::new(0_u32));
    let counted = Rc::clone(&runs);
    let mut app = App::new(config(), move || {
        let counted = Rc::clone(&counted);
        panel().child(panel().width(px(40.0)).height(px(20.0)).hidden(move || {
            counted.set(counted.get() + 1);
            window::maximized()
        }))
    });
    app.frame();

    app.set_window_state(WindowState::default().with_maximized(true));
    app.frame();
    let after_change = runs.get();

    app.set_window_state(WindowState::default().with_maximized(true));
    app.frame();
    assert_eq!(
        runs.get(),
        after_change,
        "an equal report wakes no binding that reads the state"
    );
}

#[test]
fn a_memo_that_reads_maximized_follows_every_change() {
    let mut app = App::new(config(), || {
        let maximized = Memo::new(window::maximized);
        panel().child(
            panel()
                .width(px(40.0))
                .height(px(20.0))
                .hidden(move || maximized.get()),
        )
    });
    app.frame();

    let mut visible = 0;
    for maximized in [true, false, true] {
        app.set_window_state(WindowState::default().with_maximized(maximized));
        app.frame();
        visible = 0;
        app.visit_nodes(|_, _, _| visible += 1);
        assert_eq!(
            visible,
            if maximized { 1 } else { 2 },
            "the memo recomputed while no owner was current and kept \
             reading the state"
        );
    }
    assert_eq!(visible, 1);
}

#[test]
fn a_window_command_with_no_live_application_does_nothing() {
    window::close();
    window::start_move();

    let kept: Rc<RefCell<Option<Owner>>> = Rc::new(RefCell::new(None));
    let slot = Rc::clone(&kept);
    let app = App::new(config(), move || {
        slot.replace(Some(Owner::new()));
        panel()
    });
    drop(app);
    let owner = kept.take().expect("the build closure stored its owner");
    owner.with(|| {
        window::toggle_maximize();
        window::start_resize(ResizeEdge::South);
    });
    assert!(
        !owner.with(window::maximized),
        "an owner whose application is gone reads an unmaximized window"
    );
}

/// Returns the height of the first node under the root.
fn header_height(harness: &mut Harness) -> f32 {
    let mut height = None;
    harness.app().visit_nodes(|depth, _, rect| {
        if depth == 1 && height.is_none() {
            height = Some(rect.height());
        }
    });
    height.expect("the header is visible")
}

#[test]
fn releasing_a_press_restores_hover_without_running_on_press() {
    let presses = Rc::new(Cell::new(0_u32));
    let pressed = Rc::clone(&presses);
    let hovered = Rc::new(Cell::new(false));
    let hover = Rc::clone(&hovered);
    let ups = Rc::new(Cell::new(0_u32));
    let up = Rc::clone(&ups);
    let mut harness = Harness::new(200.0, 200.0, 1.0, move || {
        let pressed = Rc::clone(&pressed);
        let hover = Rc::clone(&hover);
        let up = Rc::clone(&up);
        panel()
            .child(
                panel()
                    .width(px(100.0))
                    .height(px(40.0))
                    .pressed(|patch| patch.height(px(60.0)))
                    .on_pointer_down(|_| window::start_move())
                    .on_pointer_up(move |_| up.set(up.get() + 1))
                    .on_press(move || pressed.set(pressed.get() + 1)),
            )
            .child(
                panel()
                    .width(px(40.0))
                    .height(px(20.0))
                    .cursor(Cursor::Pointer)
                    .on_hover(move |on| hover.set(on)),
            )
    });
    harness.frame();

    let header = Point { x: 20.0, y: 20.0 };
    harness.move_to(header);
    harness.button_down(header);
    assert_eq!(
        harness.app().take_window_commands(),
        vec![window::WindowCommand::Move]
    );
    harness.frame();
    assert_eq!(header_height(&mut harness), 60.0, "the press style shows");
    // The window system took the press for the move, so its release never
    // reaches the app.
    harness.app().release_press();
    assert_eq!(ups.get(), 1, "the header hears the press end");
    harness.frame();
    assert_eq!(
        header_height(&mut harness),
        40.0,
        "the pressed state, and the style it drew, ended with the press"
    );
    harness.event(InputEvent::PointerLeft);

    harness.move_to(Point { x: 120.0, y: 10.0 });
    assert!(
        hovered.get(),
        "hover follows the pointer again once the press is released"
    );
    assert_eq!(harness.app().cursor(), Cursor::Pointer);

    harness.button_up(Point { x: 20.0, y: 20.0 });
    assert_eq!(
        presses.get(),
        0,
        "a press that the window system took does not count as a press"
    );
}

#[test]
fn the_release_of_a_press_the_window_system_refused_reaches_no_handler() {
    let ups = Rc::new(Cell::new(0_u32));
    let up = Rc::clone(&ups);
    let presses = Rc::new(Cell::new(0_u32));
    let pressed = Rc::clone(&presses);
    let mut harness = Harness::new(200.0, 200.0, 1.0, move || {
        let up = Rc::clone(&up);
        let pressed = Rc::clone(&pressed);
        panel().child(
            panel()
                .width(px(100.0))
                .height(px(40.0))
                .on_pointer_down(|_| window::start_move())
                .on_pointer_up(move |_| up.set(up.get() + 1))
                .on_press(move || pressed.set(pressed.get() + 1)),
        )
    });
    harness.frame();

    let header = Point { x: 20.0, y: 20.0 };
    harness.move_to(header);
    harness.button_down(header);
    harness.app().release_press();
    assert_eq!(ups.get(), 1, "releasing the press ends it once");

    // The window system refused the move, so the release arrives after all.
    harness.button_up(header);
    assert_eq!(
        ups.get(),
        1,
        "the late release does not end the press again"
    );

    harness.button_down(header);
    harness.button_up(header);
    assert_eq!(
        (ups.get(), presses.get()),
        (2, 1),
        "the next press and release count as usual"
    );
}

#[test]
fn a_new_press_after_a_released_press_keeps_its_own_release() {
    let ups = Rc::new(Cell::new(0_u32));
    let up = Rc::clone(&ups);
    let mut harness = Harness::new(200.0, 200.0, 1.0, move || {
        let up = Rc::clone(&up);
        panel().child(
            panel()
                .width(px(100.0))
                .height(px(40.0))
                .on_pointer_down(|_| window::start_move())
                .on_pointer_up(move |_| up.set(up.get() + 1)),
        )
    });
    harness.frame();

    let header = Point { x: 20.0, y: 20.0 };
    harness.move_to(header);
    harness.button_down(header);
    harness.app().release_press();
    assert_eq!(ups.get(), 1);

    // The window system took the press and its release, so the next thing
    // the app hears is a new press, here on empty space, whose release over
    // the header must reach the header.
    let empty = Point { x: 150.0, y: 150.0 };
    harness.move_to(empty);
    harness.button_down(empty);
    harness.button_up(header);
    assert_eq!(ups.get(), 2, "the new press's release is heard");
}

#[test]
fn cursor_follows_the_hovered_chain_and_a_nested_setter_wins() {
    let mut harness = Harness::new(200.0, 200.0, 1.0, || {
        panel().child(
            panel()
                .width(px(200.0))
                .height(px(40.0))
                .cursor(Cursor::Pointer)
                .child(
                    panel()
                        .absolute()
                        .inset(Edges {
                            top: px(0.0),
                            right: px(50.0),
                            bottom: px(0.0),
                            left: px(100.0),
                        })
                        .cursor(Cursor::Text),
                ),
        )
    });
    harness.frame();

    harness.move_to(Point { x: 125.0, y: 20.0 });
    assert_eq!(
        harness.app().cursor(),
        Cursor::Text,
        "the deepest setter under the pointer wins"
    );

    harness.move_to(Point { x: 50.0, y: 20.0 });
    assert_eq!(
        harness.app().cursor(),
        Cursor::Pointer,
        "over the parent alone, the parent's cursor shows"
    );

    harness.move_to(Point { x: 150.0, y: 150.0 });
    assert_eq!(
        harness.app().cursor(),
        Cursor::Default,
        "over no node that sets one, the default shows"
    );
}

#[test]
fn a_bound_cursor_follows_its_signal() {
    let wide = RwSignal::new(true);
    let wide_for_width = wide.clone();
    let wide_for_cursor = wide.clone();
    let mut harness = Harness::new(200.0, 200.0, 1.0, move || {
        panel().child(
            panel()
                .width(move || {
                    if wide_for_width.get() {
                        Length::px(200.0)
                    } else {
                        Length::px(40.0)
                    }
                })
                .height(px(40.0))
                .cursor(move || {
                    Some(if wide_for_cursor.get() {
                        Cursor::Resize(ResizeEdge::East)
                    } else {
                        Cursor::Pointer
                    })
                }),
        )
    });
    harness.frame();

    harness.move_to(Point { x: 150.0, y: 20.0 });
    assert_eq!(harness.app().cursor(), Cursor::Resize(ResizeEdge::East));

    wide.set(false);
    harness.frame();
    assert!(
        harness.app().cursor() == Cursor::Default,
        "the node shrank away from the pointer, so nothing under it sets a \
         cursor"
    );

    harness.move_to(Point { x: 20.0, y: 20.0 });
    assert_eq!(harness.app().cursor(), Cursor::Pointer);
}

#[test]
fn a_cursor_only_child_leaves_the_press_to_its_parent() {
    let presses = Rc::new(Cell::new(0_u32));
    let pressed = Rc::clone(&presses);
    let mut harness = Harness::new(200.0, 200.0, 1.0, move || {
        let pressed = Rc::clone(&pressed);
        panel().child(
            panel()
                .width(px(120.0))
                .height(px(40.0))
                .on_press(move || pressed.set(pressed.get() + 1))
                .child(
                    panel()
                        .width(px(40.0))
                        .height(px(20.0))
                        .cursor(Cursor::Text),
                ),
        )
    });
    harness.frame();

    let over_child = Point { x: 20.0, y: 10.0 };
    harness.move_to(over_child);
    assert_eq!(
        harness.app().cursor(),
        Cursor::Text,
        "the child that sets no handler still shows its cursor"
    );
    harness.click(over_child);
    assert_eq!(
        presses.get(),
        1,
        "a press on a child that only sets a cursor reaches the parent's \
         handler"
    );
}

#[test]
fn a_cursor_only_child_keeps_the_focus_on_its_focusable_parent() {
    let focused = Rc::new(Cell::new(false));
    let focus = Rc::clone(&focused);
    let mut harness = Harness::new(200.0, 200.0, 1.0, move || {
        panel().child(
            panel()
                .width(px(120.0))
                .height(px(40.0))
                .focusable()
                .on_focus(move |on| focus.set(on))
                .child(
                    panel()
                        .width(px(40.0))
                        .height(px(20.0))
                        .cursor(Cursor::Text),
                ),
        )
    });
    harness.frame();

    harness.click(Point { x: 20.0, y: 10.0 });
    assert!(
        focused.get(),
        "a press on a child that only sets a cursor focuses the parent"
    );
}

#[test]
fn a_disabled_node_shows_no_cursor_of_its_own() {
    let mut harness = Harness::new(200.0, 200.0, 1.0, || {
        panel().child(
            panel()
                .width(px(120.0))
                .height(px(40.0))
                .cursor(Cursor::Pointer)
                .child(
                    panel()
                        .width(px(40.0))
                        .height(px(20.0))
                        .disabled(true)
                        .cursor(Cursor::Text),
                ),
        )
    });
    harness.frame();

    harness.move_to(Point { x: 20.0, y: 10.0 });
    assert_eq!(
        harness.app().cursor(),
        Cursor::Pointer,
        "a disabled node passes the cursor choice to its ancestors"
    );
}

#[test]
fn the_cursor_resets_when_the_pointer_leaves() {
    let mut harness = Harness::new(200.0, 200.0, 1.0, || {
        panel().child(
            panel()
                .width(px(40.0))
                .height(px(20.0))
                .cursor(Cursor::Pointer),
        )
    });
    harness.frame();

    harness.move_to(Point { x: 20.0, y: 10.0 });
    assert_eq!(harness.app().cursor(), Cursor::Pointer);
    harness.event(InputEvent::PointerLeft);
    assert_eq!(harness.app().cursor(), Cursor::Default);
}

#[test]
fn a_bound_cursor_of_none_falls_back_to_the_ancestor() {
    let own = RwSignal::new(true);
    let own_cursor = own.clone();
    let mut harness = Harness::new(200.0, 200.0, 1.0, move || {
        panel().child(
            panel()
                .width(px(120.0))
                .height(px(40.0))
                .cursor(Cursor::Text)
                .child(
                    panel()
                        .width(px(40.0))
                        .height(px(20.0))
                        .cursor(move || own_cursor.get().then_some(Cursor::Pointer)),
                ),
        )
    });
    harness.frame();

    harness.move_to(Point { x: 20.0, y: 10.0 });
    assert_eq!(harness.app().cursor(), Cursor::Pointer);

    own.set(false);
    harness.frame();
    assert_eq!(
        harness.app().cursor(),
        Cursor::Text,
        "a binding that sets no cursor leaves the choice to the ancestor"
    );
}

#[test]
fn a_press_keeps_the_cursor_of_a_cursor_only_child_under_the_pointer() {
    let mut harness = Harness::new(200.0, 200.0, 1.0, || {
        panel().child(
            panel()
                .width(px(120.0))
                .height(px(40.0))
                .cursor(Cursor::Text)
                .on_press(|| {})
                .child(
                    panel()
                        .width(px(40.0))
                        .height(px(20.0))
                        .cursor(Cursor::Pointer),
                ),
        )
    });
    harness.frame();

    let over_child = Point { x: 20.0, y: 10.0 };
    harness.move_to(over_child);
    harness.button_down(over_child);
    assert_eq!(
        harness.app().cursor(),
        Cursor::Pointer,
        "the press lands on the parent, but the child under the pointer \
         keeps its cursor"
    );

    harness.move_to(Point { x: 180.0, y: 180.0 });
    assert_eq!(
        harness.app().cursor(),
        Cursor::Text,
        "dragged away from the pressed node, its own cursor shows"
    );
    harness.button_up(Point { x: 180.0, y: 180.0 });
}