freya-components 0.5.1

Components for Freya apps
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
use std::time::Duration;

use freya::prelude::*;
use freya_testing::prelude::*;

#[test]
fn scrolling_does_not_press_button_on_release() {
    for virtualized in [false, true] {
        let (mut test, (presses, controller)) = TestingRunner::new(
            move || {
                let mut presses = use_consume::<State<usize>>();
                let controller = use_consume::<ScrollController>();
                let button = move |index| {
                    Button::new()
                        .key(index)
                        .height(Size::px(150.))
                        .on_press(move |_| *presses.write() += 1)
                        .child(format!("Button {index}"))
                        .into_element()
                };

                if virtualized {
                    VirtualScrollView::new_controlled(move |item, _| button(item.index), controller)
                        .length(20usize)
                        .item_size(150.)
                        .into_element()
                } else {
                    ScrollView::new_controlled(controller)
                        .children((0..20).map(button))
                        .into_element()
                }
            },
            (500., 500.).into(),
            |runner| {
                let presses = runner.provide_root_context(|| State::create(0usize));
                let controller = runner.provide_root_context(|| ScrollController::new(0, 0));
                (presses, controller)
            },
            1.,
        );
        test.sync_and_update();

        test.press_touch((40., 80.));
        test.move_touch((40., 50.));
        assert!(test.run_in(|| controller.position().y) < 0.);
        test.release_touch((40., 50.));
        assert_eq!(*presses.peek(), 0);
    }
}

#[test]
fn scroll_view_auto_hide_scrollbars() {
    for auto_hide_scrollbars in [true, false] {
        let mut test = launch_test(move || {
            ScrollView::new()
                .auto_hide_scrollbars(auto_hide_scrollbars)
                .scrollbar(move |context: ScrollBarContext| {
                    assert_eq!(context.auto_hide_scrollbars, auto_hide_scrollbars);
                    ScrollBar::new(context).into()
                })
                .child(rect().width(Size::px(1000.)).height(Size::px(1000.)))
        });
        test.sync_and_update();
        let scrollbars = test.find_many(|node, element| {
            Rect::try_downcast(element)
                .filter(|rect| rect.relative_layer == 999.into())
                .map(|_| node)
        });
        assert_eq!(scrollbars.len(), 2);
        for scrollbar in &scrollbars {
            assert!(scrollbar.layout().area.width() > 0.);
            assert!(scrollbar.layout().area.height() > 0.);
        }

        test.poll(Duration::from_millis(20), Duration::from_millis(1000));

        for scrollbar in &scrollbars {
            assert_eq!(scrollbar.layout().area.width() > 0., !auto_hide_scrollbars);
            assert_eq!(scrollbar.layout().area.height() > 0., !auto_hide_scrollbars);
            assert_eq!(scrollbar.children().is_empty(), auto_hide_scrollbars);
        }
    }
}

#[test]
fn scroll_view_mixed_auto_hide_scrollbars() {
    fn app() -> impl IntoElement {
        rect()
            .child(
                ScrollView::new().height(Size::percent(50.)).child(
                    rect().spacing(6.).children(
                        (0..30).map(|_| rect().width(Size::fill()).height(Size::px(80.))),
                    ),
                ),
            )
            .child(
                ScrollView::new()
                    .auto_hide_scrollbars(false)
                    .height(Size::percent(50.))
                    .child(rect().horizontal().spacing(6.).children(
                        (0..30).map(|_| rect().width(Size::px(80.)).height(Size::fill())),
                    )),
            )
    }

    let mut test = launch_test(app);
    test.sync_and_update();
    let scrollbars = test.find_many(|node, element| {
        Rect::try_downcast(element)
            .filter(|rect| rect.relative_layer == 999.into())
            .map(|_| node)
    });
    assert_eq!(scrollbars.len(), 2);
    test.move_cursor((495., 20.));
    test.sync_and_update();
    test.poll(Duration::from_millis(20), Duration::from_secs(2));
    for scrollbar in &scrollbars {
        assert!(scrollbar.layout().area.width() > 0.);
        assert!(!scrollbar.children().is_empty());
    }

    test.move_cursor((100., 100.));
    test.sync_and_update();
    test.poll(Duration::from_millis(20), Duration::from_secs(2));

    let visible_scrollbars = scrollbars
        .iter()
        .filter(|scrollbar| scrollbar.layout().area.width() > 0.)
        .collect::<Vec<_>>();
    for scrollbar in &scrollbars {
        if scrollbar.layout().area.width() == 0. {
            assert!(scrollbar.children().is_empty());
        }
    }
    assert_eq!(visible_scrollbars.len(), 1);
    assert!(visible_scrollbars[0].layout().area.width() > 20.);
    assert_eq!(visible_scrollbars[0].layout().area.height(), 20.);
}

#[test]
pub fn scroll_view_wheel() {
    fn scroll_view_wheel_app() -> impl IntoElement {
        ScrollView::new()
            .child(rect().height(Size::px(200.)).width(Size::px(200.)))
            .child(rect().height(Size::px(200.)).width(Size::px(200.)))
            .child(rect().height(Size::px(200.)).width(Size::px(200.)))
            .child(rect().height(Size::px(200.)).width(Size::px(200.)))
    }

    let mut test = launch_test(scroll_view_wheel_app);
    let scrollview = test
        .find(|node, element| {
            Rect::try_downcast(element)
                .filter(|rect| rect.accessibility.builder.role() == AccessibilityRole::ScrollView)
                .map(move |_| node)
        })
        .unwrap();
    let content = scrollview.children()[0].children()[0].children();

    // Only the first three items are visible
    // Scrollview height is 500 and the user hasn't scrolled yet
    assert!(content[0].is_visible()); // 1. 0   -> 200, 200 < 500
    assert!(content[1].is_visible()); // 2. 200 -> 400, 200 < 500
    assert!(content[2].is_visible()); // 3. 400 -> 600, 400 < 500
    assert!(!content[3].is_visible()); // 4. 600 -> 800, 600 is NOT < 500, which means it is not visible.

    test.scroll((5., 5.), (0., -300.));

    // Only the last three items are visible
    // Scrollview height is 500 but the user has scrolled 300 pixels
    assert!(!content[0].is_visible()); // 1. 0   -> 200, 200 is NOT > 300, which means it is not visible.
    assert!(content[1].is_visible()); // 2. 200 -> 400, 400 > 300
    assert!(content[2].is_visible()); // 3. 400 -> 600, 600 > 300
    assert!(content[3].is_visible()); // 4. 600 -> 800, 800 > 300
}

#[test]
pub fn scroll_view_smooth_scrolling() {
    fn scroll_view_smooth_scrolling_app() -> impl IntoElement {
        ScrollView::new()
            .child(rect().height(Size::px(200.)).width(Size::px(200.)))
            .child(rect().height(Size::px(200.)).width(Size::px(200.)))
            .child(rect().height(Size::px(200.)).width(Size::px(200.)))
            .child(rect().height(Size::px(200.)).width(Size::px(200.)))
    }

    let mut test = launch_test(scroll_view_smooth_scrolling_app);
    let scrollview = test
        .find(|node, element| {
            Rect::try_downcast(element)
                .filter(|rect| rect.accessibility.builder.role() == AccessibilityRole::ScrollView)
                .map(move |_| node)
        })
        .unwrap();
    let content = scrollview.children()[0].children()[0].children();

    test.send_event(PlatformEvent::Wheel {
        name: WheelEventName::Wheel,
        scroll: (0., -300.).into(),
        cursor: (5., 5.).into(),
        source: WheelSource::Line,
        phase: TouchPhase::Moved,
    });
    test.sync_and_update();

    // A line-based wheel scroll animates, so nothing has moved yet
    assert!(content[0].is_visible());
    assert!(!content[3].is_visible());

    test.poll(Duration::from_millis(16), Duration::from_secs(1));

    // The animation has settled on the destination
    assert!(!content[0].is_visible());
    assert!(content[3].is_visible());
}

#[test]
pub fn scroll_view_hover_updates_on_scroll() {
    fn scroll_view_hover_app() -> impl IntoElement {
        let mut hovered = use_state(|| None::<usize>);

        rect()
            .child(
                label()
                    .height(Size::px(100.))
                    .text(format!("hovered {:?}", hovered())),
            )
            .child(
                ScrollView::new()
                    .height(Size::px(400.))
                    .children((0..4).map(|i| {
                        rect()
                            .key(i)
                            .height(Size::px(200.))
                            .width(Size::px(200.))
                            .on_pointer_enter(move |_| hovered.set(Some(i)))
                    })),
            )
    }

    let mut test = launch_test(scroll_view_hover_app);

    let hovered_label = |test: &TestingRunner| {
        test.find(|_, element| Label::try_downcast(element).map(|label| label.text.to_string()))
            .unwrap()
    };

    assert_eq!(hovered_label(&test), "hovered None");

    test.move_cursor((100., 350.));
    test.sync_and_update();
    assert_eq!(hovered_label(&test), "hovered Some(1)");

    // Scrolling moves the third item under the cursor
    test.scroll((100., 350.), (0., -300.));
    assert_eq!(hovered_label(&test), "hovered Some(2)");
}

#[test]
pub fn scroll_view_scrollbar() {
    fn scroll_view_scrollbar_app() -> impl IntoElement {
        ScrollView::new()
            .child(rect().height(Size::px(200.)).width(Size::px(200.)))
            .child(rect().height(Size::px(200.)).width(Size::px(200.)))
            .child(rect().height(Size::px(200.)).width(Size::px(200.)))
            .child(rect().height(Size::px(200.)).width(Size::px(200.)))
    }

    let mut test = launch_test(scroll_view_scrollbar_app);
    test.animation_clock().disable();
    let scrollview = test
        .find(|node, element| {
            Rect::try_downcast(element)
                .filter(|rect| rect.accessibility.builder.role() == AccessibilityRole::ScrollView)
                .map(move |_| node)
        })
        .unwrap();
    let content = scrollview.children()[0].children()[0].children();

    // Only the first three items are visible
    // Scrollview height is 500 and the user hasn't scrolled yet
    assert!(content[0].is_visible()); // 1. 0   -> 200, 200 < 500
    assert!(content[1].is_visible()); // 2. 200 -> 400, 200 < 500
    assert!(content[2].is_visible()); // 3. 400 -> 600, 400 < 500
    assert!(!content[3].is_visible()); // 4. 600 -> 800, 600 is NOT < 500, which means it is not visible.

    // Simulate the user dragging the scrollbar
    test.move_cursor((495., 20.));
    test.sync_and_update();
    test.press_cursor((495., 20.));
    test.sync_and_update();
    test.move_cursor((495., 320.));
    test.sync_and_update();
    test.release_cursor((495., 320.));
    test.sync_and_update();

    // Only the last three items are visible
    // Scrollview height is 500 but the user has dragged the scrollbar 300 pixels
    assert!(!content[0].is_visible()); // 1. 0   -> 200, 200 is NOT > 300, which means it is not visible.
    assert!(content[1].is_visible()); // 2. 200 -> 400, 400 > 300
    assert!(content[2].is_visible()); // 3. 400 -> 600, 600 > 300
    assert!(content[3].is_visible()); // 4. 600 -> 800, 800 > 300

    // Scroll up with arrows
    for _ in 0..5 {
        test.press_key(Key::Named(NamedKey::ArrowUp));
        test.poll_n(Duration::from_millis(1), 5);
    }

    assert!(content[0].is_visible());
    assert!(content[1].is_visible());
    assert!(content[2].is_visible());
    assert!(!content[3].is_visible());

    // Scroll to the bottom with arrows
    test.press_key(Key::Named(NamedKey::End));
    test.poll_n(Duration::from_millis(1), 5);

    assert!(!content[0].is_visible());
    assert!(content[1].is_visible());
    assert!(content[2].is_visible());
    assert!(content[3].is_visible());
}

#[test]
pub fn scroll_view_drag_scrolling() {
    fn scroll_view_drag_scrolling_app() -> impl IntoElement {
        ScrollView::new()
            .drag_scrolling(true)
            .child(rect().height(Size::px(200.)).width(Size::px(200.)))
            .child(rect().height(Size::px(200.)).width(Size::px(200.)))
            .child(rect().height(Size::px(200.)).width(Size::px(200.)))
            .child(rect().height(Size::px(200.)).width(Size::px(200.)))
    }

    let mut test = launch_test(scroll_view_drag_scrolling_app);
    let scrollview = test
        .find(|node, element| {
            Rect::try_downcast(element)
                .filter(|rect| rect.accessibility.builder.role() == AccessibilityRole::ScrollView)
                .map(move |_| node)
        })
        .unwrap();
    let content = scrollview.children()[0].children()[0].children();

    // Initial state: first three items visible
    assert!(content[0].is_visible());
    assert!(content[1].is_visible());
    assert!(content[2].is_visible());
    assert!(!content[3].is_visible());

    // Simulate a touch drag: press down on content, drag upward (scroll down)
    test.press_touch((100., 400.));
    test.move_touch((100., 100.));
    test.release_touch((100., 100.));

    // After dragging 300px upward, first item should be hidden and last visible
    assert!(!content[0].is_visible());
    assert!(content[1].is_visible());
    assert!(content[2].is_visible());
    assert!(content[3].is_visible());
}

#[test]
pub fn scroll_view_drag_scrolling_release_stops() {
    fn scroll_view_drag_release_app() -> impl IntoElement {
        ScrollView::new()
            .drag_scrolling(true)
            .child(rect().height(Size::px(200.)).width(Size::px(200.)))
            .child(rect().height(Size::px(200.)).width(Size::px(200.)))
            .child(rect().height(Size::px(200.)).width(Size::px(200.)))
            .child(rect().height(Size::px(200.)).width(Size::px(200.)))
    }

    let mut test = launch_test(scroll_view_drag_release_app);
    let scrollview = test
        .find(|node, element| {
            Rect::try_downcast(element)
                .filter(|rect| rect.accessibility.builder.role() == AccessibilityRole::ScrollView)
                .map(move |_| node)
        })
        .unwrap();
    let content = scrollview.children()[0].children()[0].children();

    // Drag down a small amount, then release
    test.press_touch((100., 300.));
    test.move_touch((100., 200.));
    test.release_touch((100., 200.));

    // Scrolled 100px: all four items partially in view since content is 800px, viewport 500px
    assert!(content[0].is_visible());
    assert!(content[1].is_visible());
    assert!(content[2].is_visible());
    assert!(!content[3].is_visible());

    // Move touch further after releasing, should NOT scroll further
    test.move_touch((100., 50.));

    // Visibility should remain unchanged since drag ended on release
    assert!(content[0].is_visible());
    assert!(content[1].is_visible());
    assert!(content[2].is_visible());
    assert!(!content[3].is_visible());
}

#[test]
pub fn scroll_view_drag_scrolling_horizontal() {
    fn scroll_view_drag_horizontal_app() -> impl IntoElement {
        ScrollView::new()
            .drag_scrolling(true)
            .direction(Direction::Horizontal)
            .child(rect().height(Size::px(200.)).width(Size::px(200.)))
            .child(rect().height(Size::px(200.)).width(Size::px(200.)))
            .child(rect().height(Size::px(200.)).width(Size::px(200.)))
            .child(rect().height(Size::px(200.)).width(Size::px(200.)))
    }

    let mut test = launch_test(scroll_view_drag_horizontal_app);
    let scrollview = test
        .find(|node, element| {
            Rect::try_downcast(element)
                .filter(|rect| rect.accessibility.builder.role() == AccessibilityRole::ScrollView)
                .map(move |_| node)
        })
        .unwrap();
    let content = scrollview.children()[0].children()[0].children();

    // Initial state: first three items visible (viewport 500px wide, items 200px each)
    assert!(content[0].is_visible());
    assert!(content[1].is_visible());
    assert!(content[2].is_visible());
    assert!(!content[3].is_visible());

    // Drag left (scroll right) by 300px
    test.press_touch((400., 100.));
    test.move_touch((100., 100.));
    test.release_touch((100., 100.));

    // After dragging 300px, first item hidden, last item visible
    assert!(!content[0].is_visible());
    assert!(content[1].is_visible());
    assert!(content[2].is_visible());
    assert!(content[3].is_visible());
}

#[test]
pub fn scroll_view_scrollbar_smaller_than_thumb() {
    fn small_scroll_view_app() -> impl IntoElement {
        rect()
            .height(Size::px(48.))
            .width(Size::px(200.))
            .child(ScrollView::new().children((0..30).map(|i| {
                label()
                    .key(i)
                    .height(Size::px(20.))
                    .text(format!("Row {i}"))
            })))
    }

    let mut test = launch_test(small_scroll_view_app);
    test.sync_and_update();

    test.move_cursor((100., 24.));
    test.sync_and_update();
    test.scroll((100., 24.), (0., -20.));
    test.sync_and_update();

    let scrollview = test
        .find(|node, element| {
            Rect::try_downcast(element)
                .filter(|rect| rect.accessibility.builder.role() == AccessibilityRole::ScrollView)
                .map(move |_| node)
        })
        .unwrap();
    let scrolled_before = scrollview.children()[0].children()[0].children()[0]
        .layout()
        .area
        .min_y();

    test.move_cursor((192., 5.));
    test.sync_and_update();
    test.press_cursor((192., 5.));
    test.sync_and_update();
    test.move_cursor((192., 30.));
    test.sync_and_update();
    test.release_cursor((192., 30.));
    test.sync_and_update();

    let scrolled_after = scrollview.children()[0].children()[0].children()[0]
        .layout()
        .area
        .min_y();
    assert_eq!(scrolled_before, scrolled_after);
}