animation.rs

 1use std::time::Duration;
 2
 3use gpui::*;
 4
 5struct Assets {}
 6
 7impl AssetSource for Assets {
 8    fn load(&self, path: &str) -> Result<std::borrow::Cow<'static, [u8]>> {
 9        std::fs::read(path).map(Into::into).map_err(Into::into)
10    }
11
12    fn list(&self, path: &str) -> Result<Vec<SharedString>> {
13        Ok(std::fs::read_dir(path)?
14            .filter_map(|entry| {
15                Some(SharedString::from(
16                    entry.ok()?.path().to_string_lossy().to_string(),
17                ))
18            })
19            .collect::<Vec<_>>())
20    }
21}
22
23struct AnimationExample {}
24
25impl Render for AnimationExample {
26    fn render(&mut self, _cx: &mut ViewContext<Self>) -> impl IntoElement {
27        div().flex().flex_col().size_full().justify_around().child(
28            div().flex().flex_row().w_full().justify_around().child(
29                div()
30                    .flex()
31                    .bg(rgb(0x2e7d32))
32                    .size(Length::Definite(Pixels(300.0).into()))
33                    .justify_center()
34                    .items_center()
35                    .shadow_lg()
36                    .text_xl()
37                    .text_color(black())
38                    .child("hello")
39                    .child(
40                        svg()
41                            .size_8()
42                            .path("examples/image/arrow_circle.svg")
43                            .text_color(black())
44                            .with_animation(
45                                "image_circle",
46                                Animation::new(Duration::from_secs(2))
47                                    .repeat()
48                                    .with_easing(bounce(ease_in_out)),
49                                |svg, delta| {
50                                    svg.with_transformation(Transformation::rotate(percentage(
51                                        delta,
52                                    )))
53                                },
54                            ),
55                    ),
56            ),
57        )
58    }
59}
60
61fn main() {
62    App::new()
63        .with_assets(Assets {})
64        .run(|cx: &mut AppContext| {
65            let options = WindowOptions {
66                bounds: Some(Bounds::centered(None, size(px(300.), px(300.)), cx)),
67                ..Default::default()
68            };
69            cx.open_window(options, |cx| {
70                cx.activate(false);
71                cx.new_view(|_cx| AnimationExample {})
72            });
73        });
74}