Skip to main content

desktop_example/app/routes/
scroll.rs

1use freya::{
2    animation::*,
3    prelude::*,
4};
5
6#[derive(PartialEq)]
7pub struct ScrollViewDemo;
8
9impl Component for ScrollViewDemo {
10    fn render(&self) -> impl IntoElement {
11        VirtualScrollView::new(|i, _| {
12            AnimatedContainer {
13                height: 70.,
14                i,
15                children: rect()
16                    .width(Size::fill())
17                    .height(Size::fill())
18                    .padding(4.)
19                    .corner_radius(8.)
20                    .color((255, 255, 255))
21                    .background((0, 119, 182))
22                    .child(format!("Item {i}"))
23                    .into(),
24            }
25            .into()
26        })
27        .length(300usize)
28        .item_size(70.)
29        .height(Size::percent(100.))
30    }
31}
32
33#[derive(PartialEq)]
34struct AnimatedContainer {
35    height: f32,
36    i: usize,
37    children: Element,
38}
39
40impl Component for AnimatedContainer {
41    fn render(&self) -> impl IntoElement {
42        let animation = use_animation(|conf| {
43            conf.on_creation(OnCreation::Run);
44            AnimNum::new(350., 0.)
45                .time(500)
46                .ease(Ease::InOut)
47                .function(Function::Expo)
48        });
49
50        rect()
51            .offset_x(animation.get().value())
52            .width(Size::fill())
53            .height(Size::px(self.height))
54            .padding(4.)
55            .child(self.children.clone())
56    }
57
58    fn render_key(&self) -> DiffKey {
59        DiffKey::from(&self.i)
60    }
61}