From ab772c04f13cd49c6603ef656c644b1a141abffe Mon Sep 17 00:00:00 2001 From: Floyd Wang Date: Fri, 8 Nov 2024 16:42:33 +0800 Subject: [PATCH] svg_img: Remove the `svg_img` method (#404) Use `SvgImg::new()`. --- crates/story/src/image_story.rs | 10 +++++----- crates/ui/src/svg_img.rs | 13 ++++--------- 2 files changed, 9 insertions(+), 14 deletions(-) diff --git a/crates/story/src/image_story.rs b/crates/story/src/image_story.rs index a31c7dfc..3f4aef93 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, svg_img, v_flex, SvgImg}; +use ui::{h_flex, v_flex, SvgImg}; const GOOGLE_LOGO: &str = include_str!("./fixtures/google.svg"); const PIE_JSON: &str = include_str!("./fixtures/pie.json"); @@ -27,9 +27,9 @@ impl ImageStory { Self { focus_handle: cx.focus_handle(), - google_logo: svg_img().source(GOOGLE_LOGO.as_bytes(), px(300.), px(300.)), - pie_chart: svg_img().source(chart.svg().unwrap().as_bytes(), px(600.), px(400.)), - inbox_img: svg_img().source("icons/inbox.svg", px(300.), px(300.)), + google_logo: SvgImg::new().source(GOOGLE_LOGO.as_bytes(), px(300.), px(300.)), + pie_chart: SvgImg::new().source(chart.svg().unwrap().as_bytes(), px(600.), px(400.)), + inbox_img: SvgImg::new().source("icons/inbox.svg", px(24.), px(24.)), } } @@ -59,7 +59,7 @@ impl Render for ImageStory { .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.inbox_img.clone().w(px(24.)).h(px(24.))) .child(self.pie_chart.clone().size_full()) } } diff --git a/crates/ui/src/svg_img.rs b/crates/ui/src/svg_img.rs index bf820ff0..cb028cbf 100644 --- a/crates/ui/src/svg_img.rs +++ b/crates/ui/src/svg_img.rs @@ -16,6 +16,7 @@ use image::ImageBuffer; use crate::Assets; +const SCALE: f32 = 2.; const FONT_PATH: &str = "fonts/NotoSans-Regular.ttf"; static OPTIONS: LazyLock = LazyLock::new(|| { let mut options = usvg::Options::default(); @@ -90,7 +91,6 @@ impl Asset for Image { source: Self::Source, cx: &mut AppContext, ) -> impl std::future::Future + Send + 'static { - let scale = 2.; let asset_source = cx.asset_source().clone(); async move { @@ -99,8 +99,8 @@ impl Asset for Image { return Err(usvg::Error::InvalidSize.into()); } let size = Size { - width: (size.width * 2).ceil(), - height: (size.height * scale).ceil(), + width: (size.width * SCALE).ceil(), + height: (size.height * SCALE).ceil(), }; let bytes = match source.source { @@ -124,7 +124,7 @@ impl Asset for Image { resvg::tiny_skia::Pixmap::new(size.width.0 as u32, size.height.0 as u32) .ok_or(usvg::Error::InvalidSize)?; - let transform = resvg::tiny_skia::Transform::from_scale(scale, scale); + let transform = resvg::tiny_skia::Transform::from_scale(SCALE, SCALE); resvg::render(&tree, transform, &mut pixmap.as_mut()); @@ -144,11 +144,6 @@ impl Asset for Image { } } -/// An SVG image element. -pub fn svg_img() -> SvgImg { - SvgImg::new() -} - pub struct SvgImg { interactivity: Interactivity, source: Option,