animation.rs

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