svg_img: Reduce ImageSource creation (#728)

This commit is contained in:
Floyd Wang 2025-03-21 17:22:12 +08:00 committed by GitHub
parent 393e7e8f85
commit 9d2a70be48
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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<Pixels>,
}
@ -152,7 +152,7 @@ impl Asset for Image {
pub struct SvgImg {
interactivity: Interactivity,
source: Option<SvgSource>,
source: Option<ImageSource>,
size: Size<Pixels>,
}
@ -178,10 +178,18 @@ impl SvgImg {
width: impl Into<Pixels>,
height: impl Into<Pixels>,
) -> 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::<Image>(&ImageSource { source, size }, cx) {
match window.use_asset::<Image>(&source, cx) {
Some(Ok(data)) => Some(data),
_ => None,
}