From 9d2a70be48c164f802bde62f7181f995ade47bc3 Mon Sep 17 00:00:00 2001 From: Floyd Wang Date: Fri, 21 Mar 2025 17:22:12 +0800 Subject: [PATCH] svg_img: Reduce `ImageSource` creation (#728) --- crates/ui/src/svg_img.rs | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/crates/ui/src/svg_img.rs b/crates/ui/src/svg_img.rs index 536f7bb2..7f5e2e2e 100644 --- a/crates/ui/src/svg_img.rs +++ b/crates/ui/src/svg_img.rs @@ -68,10 +68,10 @@ impl Clone for SvgImg { } } -enum Image {} +pub enum Image {} #[derive(Debug, Clone)] -struct ImageSource { +pub struct ImageSource { source: SvgSource, size: Size, } @@ -152,7 +152,7 @@ impl Asset for Image { pub struct SvgImg { interactivity: Interactivity, - source: Option, + source: Option, size: Size, } @@ -178,10 +178,18 @@ impl SvgImg { width: impl Into, height: impl Into, ) -> Self { - self.source = Some(source.into()); - self.size = size(width.into(), height.into()); + let size = size(width.into(), height.into()); + self.size = size; + self.source = Some(ImageSource { + source: source.into(), + size, + }); self } + + pub fn get_source(&self) -> Option<&ImageSource> { + self.source.as_ref() + } } impl IntoElement for SvgImg { @@ -213,9 +221,8 @@ impl Element for SvgImg { }); let source = self.source.clone(); - let size = self.size; let data = if let Some(source) = source { - match window.use_asset::(&ImageSource { source, size }, cx) { + match window.use_asset::(&source, cx) { Some(Ok(data)) => Some(data), _ => None, }