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(Some)
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
26const ARROW_CIRCLE_SVG: &str = concat!(
27    env!("CARGO_MANIFEST_DIR"),
28    "/examples/image/arrow_circle.svg"
29);
30
31struct AnimationExample {}
32
33impl Render for AnimationExample {
34    fn render(&mut self, _cx: &mut ViewContext<Self>) -> impl IntoElement {
35        div().flex().flex_col().size_full().justify_around().child(
36            div().flex().flex_row().w_full().justify_around().child(
37                div()
38                    .flex()
39                    .bg(rgb(0x2e7d32))
40                    .size(Length::Definite(Pixels(300.0).into()))
41                    .justify_center()
42                    .items_center()
43                    .shadow_lg()
44                    .text_xl()
45                    .text_color(black())
46                    .child("hello")
47                    .child(
48                        svg()
49                            .size_8()
50                            .path(ARROW_CIRCLE_SVG)
51                            .text_color(black())
52                            .with_animation(
53                                "image_circle",
54                                Animation::new(Duration::from_secs(2))
55                                    .repeat()
56                                    .with_easing(bounce(ease_in_out)),
57                                |svg, delta| {
58                                    svg.with_transformation(Transformation::rotate(percentage(
59                                        delta,
60                                    )))
61                                },
62                            ),
63                    ),
64            ),
65        )
66    }
67}
68
69fn main() {
70    App::new()
71        .with_assets(Assets {})
72        .run(|cx: &mut AppContext| {
73            let options = WindowOptions {
74                window_bounds: Some(WindowBounds::Windowed(Bounds::centered(
75                    None,
76                    size(px(300.), px(300.)),
77                    cx,
78                ))),
79                ..Default::default()
80            };
81            cx.open_window(options, |cx| {
82                cx.activate(false);
83                cx.new_view(|_cx| AnimationExample {})
84            })
85            .unwrap();
86        });
87}