svg.rs

 1use std::path::PathBuf;
 2
 3use gpui::*;
 4use std::fs;
 5
 6struct Assets {
 7    base: PathBuf,
 8}
 9
10impl AssetSource for Assets {
11    fn load(&self, path: &str) -> Result<Option<std::borrow::Cow<'static, [u8]>>> {
12        fs::read(self.base.join(path))
13            .map(|data| Some(std::borrow::Cow::Owned(data)))
14            .map_err(|err| err.into())
15    }
16
17    fn list(&self, path: &str) -> Result<Vec<SharedString>> {
18        fs::read_dir(self.base.join(path))
19            .map(|entries| {
20                entries
21                    .filter_map(|entry| {
22                        entry
23                            .ok()
24                            .and_then(|entry| entry.file_name().into_string().ok())
25                            .map(SharedString::from)
26                    })
27                    .collect()
28            })
29            .map_err(|err| err.into())
30    }
31}
32
33struct SvgExample;
34
35impl Render for SvgExample {
36    fn render(&mut self, _cx: &mut ViewContext<Self>) -> impl IntoElement {
37        div()
38            .flex()
39            .flex_row()
40            .size_full()
41            .justify_center()
42            .items_center()
43            .gap_8()
44            .bg(rgb(0xffffff))
45            .child(
46                svg()
47                    .path("svg/dragon.svg")
48                    .size_8()
49                    .text_color(rgb(0xff0000)),
50            )
51            .child(
52                svg()
53                    .path("svg/dragon.svg")
54                    .size_8()
55                    .text_color(rgb(0x00ff00)),
56            )
57            .child(
58                svg()
59                    .path("svg/dragon.svg")
60                    .size_8()
61                    .text_color(rgb(0x0000ff)),
62            )
63    }
64}
65
66fn main() {
67    App::new()
68        .with_assets(Assets {
69            base: PathBuf::from("crates/gpui/examples"),
70        })
71        .run(|cx: &mut AppContext| {
72            let bounds = Bounds::centered(None, size(px(300.0), px(300.0)), cx);
73            cx.open_window(
74                WindowOptions {
75                    window_bounds: Some(WindowBounds::Windowed(bounds)),
76                    ..Default::default()
77                },
78                |cx| cx.new_view(|_cx| SvgExample),
79            )
80            .unwrap();
81        });
82}