From 12341e518e6fde8760f7e0b6aa1fd7b63da8e85e Mon Sep 17 00:00:00 2001 From: Marshall Bowers Date: Tue, 3 Sep 2024 13:15:37 -0400 Subject: [PATCH] gpui: Add `svg` example (#17315) This PR adds an example for working with SVGs. Release Notes: - N/A --- crates/gpui/Cargo.toml | 4 + crates/gpui/examples/svg/dragon.svg | 240 ++++++++++++++++++++++++++++ crates/gpui/examples/svg/svg.rs | 82 ++++++++++ 3 files changed, 326 insertions(+) create mode 100644 crates/gpui/examples/svg/dragon.svg create mode 100644 crates/gpui/examples/svg/svg.rs diff --git a/crates/gpui/Cargo.toml b/crates/gpui/Cargo.toml index d66f10fa17..ab61cf04c5 100644 --- a/crates/gpui/Cargo.toml +++ b/crates/gpui/Cargo.toml @@ -181,6 +181,10 @@ path = "examples/input.rs" name = "shadow" path = "examples/shadow.rs" +[[example]] +name = "svg" +path = "examples/svg/svg.rs" + [[example]] name = "text_wrapper" path = "examples/text_wrapper.rs" diff --git a/crates/gpui/examples/svg/dragon.svg b/crates/gpui/examples/svg/dragon.svg new file mode 100644 index 0000000000..e2de1a0ece --- /dev/null +++ b/crates/gpui/examples/svg/dragon.svg @@ -0,0 +1,240 @@ + + + + + + + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/crates/gpui/examples/svg/svg.rs b/crates/gpui/examples/svg/svg.rs new file mode 100644 index 0000000000..79b83b31e1 --- /dev/null +++ b/crates/gpui/examples/svg/svg.rs @@ -0,0 +1,82 @@ +use std::path::PathBuf; + +use gpui::*; +use std::fs; + +struct Assets { + base: PathBuf, +} + +impl AssetSource for Assets { + fn load(&self, path: &str) -> Result>> { + fs::read(self.base.join(path)) + .map(|data| Some(std::borrow::Cow::Owned(data))) + .map_err(|err| err.into()) + } + + fn list(&self, path: &str) -> Result> { + fs::read_dir(self.base.join(path)) + .map(|entries| { + entries + .filter_map(|entry| { + entry + .ok() + .and_then(|entry| entry.file_name().into_string().ok()) + .map(SharedString::from) + }) + .collect() + }) + .map_err(|err| err.into()) + } +} + +struct SvgExample; + +impl Render for SvgExample { + fn render(&mut self, _cx: &mut ViewContext) -> impl IntoElement { + div() + .flex() + .flex_row() + .size_full() + .justify_center() + .items_center() + .gap_8() + .bg(rgb(0xffffff)) + .child( + svg() + .path("svg/dragon.svg") + .size_8() + .text_color(rgb(0xff0000)), + ) + .child( + svg() + .path("svg/dragon.svg") + .size_8() + .text_color(rgb(0x00ff00)), + ) + .child( + svg() + .path("svg/dragon.svg") + .size_8() + .text_color(rgb(0x0000ff)), + ) + } +} + +fn main() { + App::new() + .with_assets(Assets { + base: PathBuf::from("crates/gpui/examples"), + }) + .run(|cx: &mut AppContext| { + let bounds = Bounds::centered(None, size(px(300.0), px(300.0)), cx); + cx.open_window( + WindowOptions { + window_bounds: Some(WindowBounds::Windowed(bounds)), + ..Default::default() + }, + |cx| cx.new_view(|_cx| SvgExample), + ) + .unwrap(); + }); +}