diff --git a/assets/icons/close.svg b/assets/icons/close.svg
deleted file mode 100644
index f7a6e18b..00000000
--- a/assets/icons/close.svg
+++ /dev/null
@@ -1,4 +0,0 @@
-
\ No newline at end of file
diff --git a/crates/story/src/image_story.rs b/crates/story/src/image_story.rs
index 368f7f36..6c6865c7 100644
--- a/crates/story/src/image_story.rs
+++ b/crates/story/src/image_story.rs
@@ -1,5 +1,5 @@
use gpui::{px, ParentElement as _, Render, Styled, View, VisualContext as _, WindowContext};
-use ui::{h_flex, v_flex, SvgImg};
+use ui::{h_flex, svg_img, v_flex, SvgImg};
const GOOGLE_LOGO: &str = include_str!("./fixtures/google.svg");
const PIE_JSON: &str = include_str!("./fixtures/pie.json");
@@ -7,19 +7,17 @@ const PIE_JSON: &str = include_str!("./fixtures/pie.json");
pub struct ImageStory {
google_logo: SvgImg,
pie_chart: SvgImg,
+ inbox_img: SvgImg,
}
impl ImageStory {
- pub fn new(cx: &WindowContext) -> Self {
+ pub fn new(_: &WindowContext) -> Self {
let chart = charts_rs::PieChart::from_json(PIE_JSON).unwrap();
Self {
- google_logo: SvgImg::new(800, 800)
- .svg(GOOGLE_LOGO.as_bytes(), cx)
- .unwrap(),
- pie_chart: SvgImg::new(400, 300)
- .svg(chart.svg().unwrap().as_bytes(), cx)
- .unwrap(),
+ google_logo: svg_img().source(GOOGLE_LOGO.as_bytes(), px(300.), px(300.)),
+ pie_chart: svg_img().source(chart.svg().unwrap().as_bytes(), px(400.), px(400.)),
+ inbox_img: svg_img().source("icons/inbox.svg", px(300.), px(300.)),
}
}
@@ -37,12 +35,13 @@ impl Render for ImageStory {
.child(
h_flex()
.size_full()
+ .child(self.google_logo.clone().size(px(300.)).flex_grow())
.child(self.google_logo.clone().w(px(300.)).h(px(300.)).flex_grow())
- .child(self.google_logo.clone().w(px(300.)).h(px(300.)).flex_grow())
- .child(self.google_logo.clone().w(px(300.)).h(px(300.)).flex_grow())
- .child(self.google_logo.clone().w(px(300.)).h(px(300.)).flex_grow())
- .child(self.google_logo.clone().w(px(300.)).h(px(300.)).flex_grow()),
+ .child(self.google_logo.clone().size_80().flex_grow())
+ .child(self.google_logo.clone().size_12().flex_grow())
+ .child(self.google_logo.clone().w(px(300.)).h(px(300.))),
)
+ .child(self.inbox_img.clone().w(px(80.)).h(px(80.)))
.child(self.pie_chart.clone().size_full())
}
}
diff --git a/crates/ui/src/svg_img.rs b/crates/ui/src/svg_img.rs
index 5decd31e..e3cf8cb3 100644
--- a/crates/ui/src/svg_img.rs
+++ b/crates/ui/src/svg_img.rs
@@ -1,107 +1,170 @@
-use std::sync::Arc;
+use std::{hash::Hash, ops::Deref, sync::Arc};
-use anyhow::Context;
use gpui::{
- px, Bounds, Element, Hitbox, ImageData, InteractiveElement, Interactivity, IntoElement,
- SharedString, StyleRefinement, Styled, WindowContext,
+ px, size, Asset, Bounds, Element, Hitbox, ImageCacheError, ImageData, InteractiveElement,
+ Interactivity, IntoElement, IsZero, Pixels, SharedString, Size, StyleRefinement, Styled,
+ WindowContext,
};
use image::ImageBuffer;
-#[derive(Clone, Copy, Debug)]
-struct SvgSize {
- width: u32,
- height: u32,
+#[derive(Debug, Clone, Hash)]
+pub enum SvgSource {
+ /// A svg bytes
+ Data(Arc<[u8]>),
+ /// An asset path
+ Path(SharedString),
}
-pub struct SvgImg {
- interactivity: Interactivity,
- size: SvgSize,
- data: Option>,
+impl From<&[u8]> for SvgSource {
+ fn from(data: &[u8]) -> Self {
+ Self::Data(data.into())
+ }
+}
+
+impl From> for SvgSource {
+ fn from(data: Arc<[u8]>) -> Self {
+ Self::Data(data)
+ }
+}
+
+impl From for SvgSource {
+ fn from(path: SharedString) -> Self {
+ Self::Path(path)
+ }
+}
+
+impl From<&'static str> for SvgSource {
+ fn from(path: &'static str) -> Self {
+ Self::Path(path.into())
+ }
}
impl Clone for SvgImg {
fn clone(&self) -> Self {
Self {
interactivity: Interactivity::default(),
+ source: self.source.clone(),
size: self.size,
- data: self.data.clone(),
+ }
+ }
+}
+
+enum Image {}
+
+#[derive(Debug, Clone)]
+struct ImageSource {
+ source: SvgSource,
+ size: Size,
+}
+
+impl Hash for ImageSource {
+ /// Hash to to control the Asset cache
+ fn hash(&self, state: &mut H) {
+ self.source.hash(state);
+ }
+}
+
+impl Asset for Image {
+ type Source = ImageSource;
+ type Output = Result, ImageCacheError>;
+
+ fn load(
+ source: Self::Source,
+ cx: &mut WindowContext,
+ ) -> impl std::future::Future