1#![cfg_attr(target_family = "wasm", no_main)]
2
3use std::fs;
4use std::path::PathBuf;
5
6use anyhow::Result;
7use gpui::{
8 App, AssetSource, Bounds, Context, SharedString, Window, WindowBounds, WindowOptions, div,
9 prelude::*, px, rgb, size, svg,
10};
11use gpui_platform::application;
12
13struct Assets {
14 base: PathBuf,
15}
16
17impl AssetSource for Assets {
18 fn load(&self, path: &str) -> Result<Option<std::borrow::Cow<'static, [u8]>>> {
19 fs::read(self.base.join(path))
20 .map(|data| Some(std::borrow::Cow::Owned(data)))
21 .map_err(|err| err.into())
22 }
23
24 fn list(&self, path: &str) -> Result<Vec<SharedString>> {
25 fs::read_dir(self.base.join(path))
26 .map(|entries| {
27 entries
28 .filter_map(|entry| {
29 entry
30 .ok()
31 .and_then(|entry| entry.file_name().into_string().ok())
32 .map(SharedString::from)
33 })
34 .collect()
35 })
36 .map_err(|err| err.into())
37 }
38}
39
40struct SvgExample;
41
42impl Render for SvgExample {
43 fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
44 div()
45 .flex()
46 .flex_row()
47 .size_full()
48 .justify_center()
49 .items_center()
50 .gap_8()
51 .bg(rgb(0xffffff))
52 .child(
53 svg()
54 .path("svg/dragon.svg")
55 .size_8()
56 .text_color(rgb(0xff0000)),
57 )
58 .child(
59 svg()
60 .path("svg/dragon.svg")
61 .size_8()
62 .text_color(rgb(0x00ff00)),
63 )
64 .child(
65 svg()
66 .path("svg/dragon.svg")
67 .size_8()
68 .text_color(rgb(0x0000ff)),
69 )
70 }
71}
72
73fn run_example() {
74 application()
75 .with_assets(Assets {
76 base: PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("examples"),
77 })
78 .run(|cx: &mut App| {
79 let bounds = Bounds::centered(None, size(px(300.0), px(300.0)), cx);
80 cx.open_window(
81 WindowOptions {
82 window_bounds: Some(WindowBounds::Windowed(bounds)),
83 ..Default::default()
84 },
85 |_, cx| cx.new(|_| SvgExample),
86 )
87 .unwrap();
88 cx.activate(true);
89 });
90}
91
92#[cfg(not(target_family = "wasm"))]
93fn main() {
94 run_example();
95}
96
97#[cfg(target_family = "wasm")]
98#[wasm_bindgen::prelude::wasm_bindgen(start)]
99pub fn start() {
100 gpui_platform::web_init();
101 run_example();
102}