img.rs

  1use crate::{
  2    AnyElement, AnyImageCache, App, Asset, AssetLogger, Bounds, DefiniteLength, Element, ElementId,
  3    Entity, GlobalElementId, Hitbox, Image, ImageCache, InspectorElementId, InteractiveElement,
  4    Interactivity, IntoElement, LayoutId, Length, ObjectFit, Pixels, RenderImage, Resource,
  5    SharedString, SharedUri, StyleRefinement, Styled, Task, Window, px,
  6};
  7use anyhow::Result;
  8
  9use futures::Future;
 10use gpui_util::ResultExt;
 11use image::{
 12    AnimationDecoder, DynamicImage, Frame, ImageError, ImageFormat, Rgba,
 13    codecs::{gif::GifDecoder, webp::WebPDecoder},
 14};
 15use scheduler::Instant;
 16use smallvec::SmallVec;
 17use std::{
 18    fs,
 19    io::{self, Cursor},
 20    ops::{Deref, DerefMut},
 21    path::{Path, PathBuf},
 22    str::FromStr,
 23    sync::Arc,
 24    time::Duration,
 25};
 26use thiserror::Error;
 27
 28use super::{Stateful, StatefulInteractiveElement};
 29
 30/// The delay before showing the loading state.
 31pub const LOADING_DELAY: Duration = Duration::from_millis(200);
 32
 33/// A type alias to the resource loader that the `img()` element uses.
 34///
 35/// Note: that this is only for Resources, like URLs or file paths.
 36/// Custom loaders, or external images will not use this asset loader
 37pub type ImgResourceLoader = AssetLogger<ImageAssetLoader>;
 38
 39/// A source of image content.
 40#[derive(Clone)]
 41pub enum ImageSource {
 42    /// The image content will be loaded from some resource location
 43    Resource(Resource),
 44    /// Cached image data
 45    Render(Arc<RenderImage>),
 46    /// Cached image data
 47    Image(Arc<Image>),
 48    /// A custom loading function to use
 49    Custom(Arc<dyn Fn(&mut Window, &mut App) -> Option<Result<Arc<RenderImage>, ImageCacheError>>>),
 50}
 51
 52fn is_uri(uri: &str) -> bool {
 53    url::Url::from_str(uri).is_ok()
 54}
 55
 56impl From<SharedUri> for ImageSource {
 57    fn from(value: SharedUri) -> Self {
 58        Self::Resource(Resource::Uri(value))
 59    }
 60}
 61
 62impl<'a> From<&'a str> for ImageSource {
 63    fn from(s: &'a str) -> Self {
 64        if is_uri(s) {
 65            Self::Resource(Resource::Uri(s.to_string().into()))
 66        } else {
 67            Self::Resource(Resource::Embedded(s.to_string().into()))
 68        }
 69    }
 70}
 71
 72impl From<String> for ImageSource {
 73    fn from(s: String) -> Self {
 74        if is_uri(&s) {
 75            Self::Resource(Resource::Uri(s.into()))
 76        } else {
 77            Self::Resource(Resource::Embedded(s.into()))
 78        }
 79    }
 80}
 81
 82impl From<SharedString> for ImageSource {
 83    fn from(s: SharedString) -> Self {
 84        s.as_ref().into()
 85    }
 86}
 87
 88impl From<&Path> for ImageSource {
 89    fn from(value: &Path) -> Self {
 90        Self::Resource(value.to_path_buf().into())
 91    }
 92}
 93
 94impl From<Arc<Path>> for ImageSource {
 95    fn from(value: Arc<Path>) -> Self {
 96        Self::Resource(value.into())
 97    }
 98}
 99
100impl From<PathBuf> for ImageSource {
101    fn from(value: PathBuf) -> Self {
102        Self::Resource(value.into())
103    }
104}
105
106impl From<Arc<RenderImage>> for ImageSource {
107    fn from(value: Arc<RenderImage>) -> Self {
108        Self::Render(value)
109    }
110}
111
112impl From<Arc<Image>> for ImageSource {
113    fn from(value: Arc<Image>) -> Self {
114        Self::Image(value)
115    }
116}
117
118impl<F> From<F> for ImageSource
119where
120    F: Fn(&mut Window, &mut App) -> Option<Result<Arc<RenderImage>, ImageCacheError>> + 'static,
121{
122    fn from(value: F) -> Self {
123        Self::Custom(Arc::new(value))
124    }
125}
126
127/// The style of an image element.
128pub struct ImageStyle {
129    grayscale: bool,
130    object_fit: ObjectFit,
131    loading: Option<Box<dyn Fn() -> AnyElement>>,
132    fallback: Option<Box<dyn Fn() -> AnyElement>>,
133}
134
135impl Default for ImageStyle {
136    fn default() -> Self {
137        Self {
138            grayscale: false,
139            object_fit: ObjectFit::Contain,
140            loading: None,
141            fallback: None,
142        }
143    }
144}
145
146/// Style an image element.
147pub trait StyledImage: Sized {
148    /// Get a mutable [ImageStyle] from the element.
149    fn image_style(&mut self) -> &mut ImageStyle;
150
151    /// Set the image to be displayed in grayscale.
152    fn grayscale(mut self, grayscale: bool) -> Self {
153        self.image_style().grayscale = grayscale;
154        self
155    }
156
157    /// Set the object fit for the image.
158    fn object_fit(mut self, object_fit: ObjectFit) -> Self {
159        self.image_style().object_fit = object_fit;
160        self
161    }
162
163    /// Set a fallback function that will be invoked to render an error view should
164    /// the image fail to load.
165    fn with_fallback(mut self, fallback: impl Fn() -> AnyElement + 'static) -> Self {
166        self.image_style().fallback = Some(Box::new(fallback));
167        self
168    }
169
170    /// Set a fallback function that will be invoked to render a view while the image
171    /// is still being loaded.
172    fn with_loading(mut self, loading: impl Fn() -> AnyElement + 'static) -> Self {
173        self.image_style().loading = Some(Box::new(loading));
174        self
175    }
176}
177
178impl StyledImage for Img {
179    fn image_style(&mut self) -> &mut ImageStyle {
180        &mut self.style
181    }
182}
183
184impl StyledImage for Stateful<Img> {
185    fn image_style(&mut self) -> &mut ImageStyle {
186        &mut self.element.style
187    }
188}
189
190/// An image element.
191pub struct Img {
192    interactivity: Interactivity,
193    source: ImageSource,
194    style: ImageStyle,
195    image_cache: Option<AnyImageCache>,
196}
197
198/// Create a new image element.
199#[track_caller]
200pub fn img(source: impl Into<ImageSource>) -> Img {
201    Img {
202        interactivity: Interactivity::new(),
203        source: source.into(),
204        style: ImageStyle::default(),
205        image_cache: None,
206    }
207}
208
209impl Img {
210    /// A list of all format extensions currently supported by this img element
211    pub fn extensions() -> &'static [&'static str] {
212        // This is the list in [image::ImageFormat::from_extension] + `svg`
213        &[
214            "avif", "jpg", "jpeg", "png", "gif", "webp", "tif", "tiff", "tga", "dds", "bmp", "ico",
215            "hdr", "exr", "pbm", "pam", "ppm", "pgm", "ff", "farbfeld", "qoi", "svg",
216        ]
217    }
218
219    /// Sets the image cache for the current node.
220    ///
221    /// If the `image_cache` is not explicitly provided, the function will determine the image cache by:
222    ///
223    /// 1. Checking if any ancestor node of the current node contains an `ImageCacheElement`, If such a node exists, the image cache specified by that ancestor will be used.
224    /// 2. If no ancestor node contains an `ImageCacheElement`, the global image cache will be used as a fallback.
225    ///
226    /// This mechanism provides a flexible way to manage image caching, allowing precise control when needed,
227    /// while ensuring a default behavior when no cache is explicitly specified.
228    #[inline]
229    pub fn image_cache<I: ImageCache>(self, image_cache: &Entity<I>) -> Self {
230        Self {
231            image_cache: Some(image_cache.clone().into()),
232            ..self
233        }
234    }
235}
236
237impl Deref for Stateful<Img> {
238    type Target = Img;
239
240    fn deref(&self) -> &Self::Target {
241        &self.element
242    }
243}
244
245impl DerefMut for Stateful<Img> {
246    fn deref_mut(&mut self) -> &mut Self::Target {
247        &mut self.element
248    }
249}
250
251/// The image state between frames
252struct ImgState {
253    frame_index: usize,
254    last_frame_time: Option<Instant>,
255    started_loading: Option<(Instant, Task<()>)>,
256}
257
258/// The image layout state between frames
259pub struct ImgLayoutState {
260    frame_index: usize,
261    replacement: Option<AnyElement>,
262}
263
264impl Element for Img {
265    type RequestLayoutState = ImgLayoutState;
266    type PrepaintState = Option<Hitbox>;
267
268    fn id(&self) -> Option<ElementId> {
269        self.interactivity.element_id.clone()
270    }
271
272    fn source_location(&self) -> Option<&'static core::panic::Location<'static>> {
273        self.interactivity.source_location()
274    }
275
276    fn request_layout(
277        &mut self,
278        global_id: Option<&GlobalElementId>,
279        inspector_id: Option<&InspectorElementId>,
280        window: &mut Window,
281        cx: &mut App,
282    ) -> (LayoutId, Self::RequestLayoutState) {
283        let mut layout_state = ImgLayoutState {
284            frame_index: 0,
285            replacement: None,
286        };
287
288        window.with_optional_element_state(global_id, |state, window| {
289            let mut state = state.map(|state| {
290                state.unwrap_or(ImgState {
291                    frame_index: 0,
292                    last_frame_time: None,
293                    started_loading: None,
294                })
295            });
296
297            let frame_index = state.as_ref().map(|state| state.frame_index).unwrap_or(0);
298
299            let layout_id = self.interactivity.request_layout(
300                global_id,
301                inspector_id,
302                window,
303                cx,
304                |mut style, window, cx| {
305                    let mut replacement_id = None;
306
307                    match self.source.use_data(
308                        self.image_cache
309                            .clone()
310                            .or_else(|| window.image_cache_stack.last().cloned()),
311                        window,
312                        cx,
313                    ) {
314                        Some(Ok(data)) => {
315                            if let Some(state) = &mut state {
316                                let frame_count = data.frame_count();
317                                if frame_count > 1 {
318                                    if window.is_window_active() {
319                                        let current_time = Instant::now();
320                                        if let Some(last_frame_time) = state.last_frame_time {
321                                            let elapsed = current_time - last_frame_time;
322                                            let frame_duration =
323                                                Duration::from(data.delay(state.frame_index));
324
325                                            if elapsed >= frame_duration {
326                                                state.frame_index =
327                                                    (state.frame_index + 1) % frame_count;
328                                                state.last_frame_time =
329                                                    Some(current_time - (elapsed - frame_duration));
330                                            }
331                                        } else {
332                                            state.last_frame_time = Some(current_time);
333                                        }
334                                    } else {
335                                        state.last_frame_time = None;
336                                    }
337                                }
338                                state.started_loading = None;
339                            }
340
341                            let image_size = data.render_size(frame_index);
342                            style.aspect_ratio = Some(image_size.width / image_size.height);
343
344                            if let Length::Auto = style.size.width {
345                                style.size.width = match style.size.height {
346                                    Length::Definite(DefiniteLength::Absolute(abs_length)) => {
347                                        let height_px = abs_length.to_pixels(window.rem_size());
348                                        Length::Definite(
349                                            px(image_size.width.0 * height_px.0
350                                                / image_size.height.0)
351                                            .into(),
352                                        )
353                                    }
354                                    _ => Length::Definite(image_size.width.into()),
355                                };
356                            }
357
358                            if let Length::Auto = style.size.height {
359                                style.size.height = match style.size.width {
360                                    Length::Definite(DefiniteLength::Absolute(abs_length)) => {
361                                        let width_px = abs_length.to_pixels(window.rem_size());
362                                        Length::Definite(
363                                            px(image_size.height.0 * width_px.0
364                                                / image_size.width.0)
365                                            .into(),
366                                        )
367                                    }
368                                    _ => Length::Definite(image_size.height.into()),
369                                };
370                            }
371
372                            if global_id.is_some()
373                                && data.frame_count() > 1
374                                && window.is_window_active()
375                            {
376                                window.request_animation_frame();
377                            }
378                        }
379                        Some(_err) => {
380                            if let Some(fallback) = self.style.fallback.as_ref() {
381                                let mut element = fallback();
382                                replacement_id = Some(element.request_layout(window, cx));
383                                layout_state.replacement = Some(element);
384                            }
385                            if let Some(state) = &mut state {
386                                state.started_loading = None;
387                            }
388                        }
389                        None => {
390                            if let Some(state) = &mut state {
391                                if let Some((started_loading, _)) = state.started_loading {
392                                    if started_loading.elapsed() > LOADING_DELAY
393                                        && let Some(loading) = self.style.loading.as_ref()
394                                    {
395                                        let mut element = loading();
396                                        replacement_id = Some(element.request_layout(window, cx));
397                                        layout_state.replacement = Some(element);
398                                    }
399                                } else {
400                                    let current_view = window.current_view();
401                                    let task = window.spawn(cx, async move |cx| {
402                                        cx.background_executor().timer(LOADING_DELAY).await;
403                                        cx.update(move |_, cx| {
404                                            cx.notify(current_view);
405                                        })
406                                        .ok();
407                                    });
408                                    state.started_loading = Some((Instant::now(), task));
409                                }
410                            }
411                        }
412                    }
413
414                    window.request_layout(style, replacement_id, cx)
415                },
416            );
417
418            layout_state.frame_index = frame_index;
419
420            ((layout_id, layout_state), state)
421        })
422    }
423
424    fn prepaint(
425        &mut self,
426        global_id: Option<&GlobalElementId>,
427        inspector_id: Option<&InspectorElementId>,
428        bounds: Bounds<Pixels>,
429        request_layout: &mut Self::RequestLayoutState,
430        window: &mut Window,
431        cx: &mut App,
432    ) -> Self::PrepaintState {
433        self.interactivity.prepaint(
434            global_id,
435            inspector_id,
436            bounds,
437            bounds.size,
438            window,
439            cx,
440            |_, _, hitbox, window, cx| {
441                if let Some(replacement) = &mut request_layout.replacement {
442                    replacement.prepaint(window, cx);
443                }
444
445                hitbox
446            },
447        )
448    }
449
450    fn paint(
451        &mut self,
452        global_id: Option<&GlobalElementId>,
453        inspector_id: Option<&InspectorElementId>,
454        bounds: Bounds<Pixels>,
455        layout_state: &mut Self::RequestLayoutState,
456        hitbox: &mut Self::PrepaintState,
457        window: &mut Window,
458        cx: &mut App,
459    ) {
460        let source = self.source.clone();
461        self.interactivity.paint(
462            global_id,
463            inspector_id,
464            bounds,
465            hitbox.as_ref(),
466            window,
467            cx,
468            |style, window, cx| {
469                if let Some(Ok(data)) = source.use_data(
470                    self.image_cache
471                        .clone()
472                        .or_else(|| window.image_cache_stack.last().cloned()),
473                    window,
474                    cx,
475                ) {
476                    if data.frame_count() == 0 {
477                        return;
478                    }
479                    let new_bounds = self
480                        .style
481                        .object_fit
482                        .get_bounds(bounds, data.size(layout_state.frame_index));
483                    let corner_radii = style
484                        .corner_radii
485                        .to_pixels(window.rem_size())
486                        .clamp_radii_for_quad_size(new_bounds.size);
487                    window
488                        .paint_image(
489                            new_bounds,
490                            corner_radii,
491                            data,
492                            layout_state.frame_index,
493                            self.style.grayscale,
494                        )
495                        .log_err();
496                } else if let Some(replacement) = &mut layout_state.replacement {
497                    replacement.paint(window, cx);
498                }
499            },
500        )
501    }
502}
503
504impl Styled for Img {
505    fn style(&mut self) -> &mut StyleRefinement {
506        &mut self.interactivity.base_style
507    }
508}
509
510impl InteractiveElement for Img {
511    fn interactivity(&mut self) -> &mut Interactivity {
512        &mut self.interactivity
513    }
514}
515
516impl IntoElement for Img {
517    type Element = Self;
518
519    fn into_element(self) -> Self::Element {
520        self
521    }
522}
523
524impl StatefulInteractiveElement for Img {}
525
526impl ImageSource {
527    pub(crate) fn use_data(
528        &self,
529        cache: Option<AnyImageCache>,
530        window: &mut Window,
531        cx: &mut App,
532    ) -> Option<Result<Arc<RenderImage>, ImageCacheError>> {
533        match self {
534            ImageSource::Resource(resource) => {
535                if let Some(cache) = cache {
536                    cache.load(resource, window, cx)
537                } else {
538                    window.use_asset::<ImgResourceLoader>(resource, cx)
539                }
540            }
541            ImageSource::Custom(loading_fn) => loading_fn(window, cx),
542            ImageSource::Render(data) => Some(Ok(data.to_owned())),
543            ImageSource::Image(data) => window.use_asset::<AssetLogger<ImageDecoder>>(data, cx),
544        }
545    }
546
547    pub(crate) fn get_data(
548        &self,
549        cache: Option<AnyImageCache>,
550        window: &mut Window,
551        cx: &mut App,
552    ) -> Option<Result<Arc<RenderImage>, ImageCacheError>> {
553        match self {
554            ImageSource::Resource(resource) => {
555                if let Some(cache) = cache {
556                    cache.load(resource, window, cx)
557                } else {
558                    window.get_asset::<ImgResourceLoader>(resource, cx)
559                }
560            }
561            ImageSource::Custom(loading_fn) => loading_fn(window, cx),
562            ImageSource::Render(data) => Some(Ok(data.to_owned())),
563            ImageSource::Image(data) => window.get_asset::<AssetLogger<ImageDecoder>>(data, cx),
564        }
565    }
566
567    /// Remove this image source from the asset system
568    pub fn remove_asset(&self, cx: &mut App) {
569        match self {
570            ImageSource::Resource(resource) => {
571                cx.remove_asset::<ImgResourceLoader>(resource);
572            }
573            ImageSource::Custom(_) | ImageSource::Render(_) => {}
574            ImageSource::Image(data) => cx.remove_asset::<AssetLogger<ImageDecoder>>(data),
575        }
576    }
577}
578
579#[derive(Clone)]
580enum ImageDecoder {}
581
582impl Asset for ImageDecoder {
583    type Source = Arc<Image>;
584    type Output = Result<Arc<RenderImage>, ImageCacheError>;
585
586    fn load(
587        source: Self::Source,
588        cx: &mut App,
589    ) -> impl Future<Output = Self::Output> + Send + 'static {
590        let renderer = cx.svg_renderer();
591        async move { source.to_image_data(renderer).map_err(Into::into) }
592    }
593}
594
595/// An image loader for the GPUI asset system
596#[derive(Clone)]
597pub enum ImageAssetLoader {}
598
599impl Asset for ImageAssetLoader {
600    type Source = Resource;
601    type Output = Result<Arc<RenderImage>, ImageCacheError>;
602
603    fn load(
604        source: Self::Source,
605        cx: &mut App,
606    ) -> impl Future<Output = Self::Output> + Send + 'static {
607        let client = cx.http_client();
608        // TODO: Can we make SVGs always rescale?
609        // let scale_factor = cx.scale_factor();
610        let svg_renderer = cx.svg_renderer();
611        let asset_source = cx.asset_source().clone();
612        async move {
613            let bytes = match source.clone() {
614                Resource::Path(uri) => fs::read(uri.as_ref())?,
615                Resource::Uri(uri) => {
616                    use anyhow::Context as _;
617                    use futures::AsyncReadExt as _;
618
619                    let mut response = client
620                        .get(uri.as_ref(), ().into(), true)
621                        .await
622                        .with_context(|| format!("loading image asset from {uri:?}"))?;
623                    let mut body = Vec::new();
624                    response.body_mut().read_to_end(&mut body).await?;
625                    if !response.status().is_success() {
626                        let mut body = String::from_utf8_lossy(&body).into_owned();
627                        let first_line = body.lines().next().unwrap_or("").trim_end();
628                        body.truncate(first_line.len());
629                        return Err(ImageCacheError::BadStatus {
630                            uri,
631                            status: response.status(),
632                            body,
633                        });
634                    }
635                    body
636                }
637                Resource::Embedded(path) => {
638                    let data = asset_source.load(&path).ok().flatten();
639                    if let Some(data) = data {
640                        data.to_vec()
641                    } else {
642                        return Err(ImageCacheError::Asset(
643                            format!("Embedded resource not found: {}", path).into(),
644                        ));
645                    }
646                }
647            };
648
649            if let Ok(format) = image::guess_format(&bytes) {
650                let data = match format {
651                    ImageFormat::Gif => {
652                        let decoder = GifDecoder::new(Cursor::new(&bytes))?;
653                        let mut frames = SmallVec::new();
654
655                        for frame in decoder.into_frames() {
656                            match frame {
657                                Ok(mut frame) => {
658                                    // Convert from RGBA to BGRA.
659                                    for pixel in frame.buffer_mut().chunks_exact_mut(4) {
660                                        pixel.swap(0, 2);
661                                    }
662                                    frames.push(frame);
663                                }
664                                Err(err) => {
665                                    log::debug!(
666                                        "Skipping GIF frame in {source:?} due to decode error: {err}"
667                                    );
668                                }
669                            }
670                        }
671
672                        if frames.is_empty() {
673                            return Err(ImageCacheError::Other(Arc::new(anyhow::anyhow!(
674                                "GIF could not be decoded: all frames failed ({source:?})"
675                            ))));
676                        }
677
678                        frames
679                    }
680                    ImageFormat::WebP => {
681                        let mut decoder = WebPDecoder::new(Cursor::new(&bytes))?;
682
683                        if decoder.has_animation() {
684                            let _ = decoder.set_background_color(Rgba([0, 0, 0, 0]));
685                            let mut frames = SmallVec::new();
686
687                            for frame in decoder.into_frames() {
688                                match frame {
689                                    Ok(mut frame) => {
690                                        // Convert from RGBA to BGRA.
691                                        for pixel in frame.buffer_mut().chunks_exact_mut(4) {
692                                            pixel.swap(0, 2);
693                                        }
694                                        frames.push(frame);
695                                    }
696                                    Err(err) => {
697                                        log::debug!(
698                                            "Skipping WebP frame in {source:?} due to decode error: {err}"
699                                        );
700                                    }
701                                }
702                            }
703
704                            if frames.is_empty() {
705                                return Err(ImageCacheError::Other(Arc::new(anyhow::anyhow!(
706                                    "WebP could not be decoded: all frames failed ({source:?})"
707                                ))));
708                            }
709
710                            frames
711                        } else {
712                            let mut data = DynamicImage::from_decoder(decoder)?.into_rgba8();
713
714                            // Convert from RGBA to BGRA.
715                            for pixel in data.chunks_exact_mut(4) {
716                                pixel.swap(0, 2);
717                            }
718
719                            SmallVec::from_elem(Frame::new(data), 1)
720                        }
721                    }
722                    _ => {
723                        let mut data =
724                            image::load_from_memory_with_format(&bytes, format)?.into_rgba8();
725
726                        // Convert from RGBA to BGRA.
727                        for pixel in data.chunks_exact_mut(4) {
728                            pixel.swap(0, 2);
729                        }
730
731                        SmallVec::from_elem(Frame::new(data), 1)
732                    }
733                };
734
735                Ok(Arc::new(RenderImage::new(data)))
736            } else {
737                svg_renderer
738                    .render_single_frame(&bytes, 1.0)
739                    .map_err(Into::into)
740            }
741        }
742    }
743}
744
745/// An error that can occur when interacting with the image cache.
746#[derive(Debug, Error, Clone)]
747pub enum ImageCacheError {
748    /// Some other kind of error occurred
749    #[error("error: {0}")]
750    Other(#[from] Arc<anyhow::Error>),
751    /// An error that occurred while reading the image from disk.
752    #[error("IO error: {0}")]
753    Io(Arc<std::io::Error>),
754    /// An error that occurred while processing an image.
755    #[error("unexpected http status for {uri}: {status}, body: {body}")]
756    BadStatus {
757        /// The URI of the image.
758        uri: SharedUri,
759        /// The HTTP status code.
760        status: http_client::StatusCode,
761        /// The HTTP response body.
762        body: String,
763    },
764    /// An error that occurred while processing an asset.
765    #[error("asset error: {0}")]
766    Asset(SharedString),
767    /// An error that occurred while processing an image.
768    #[error("image error: {0}")]
769    Image(Arc<ImageError>),
770    /// An error that occurred while processing an SVG.
771    #[error("svg error: {0}")]
772    Usvg(Arc<usvg::Error>),
773}
774
775impl From<anyhow::Error> for ImageCacheError {
776    fn from(value: anyhow::Error) -> Self {
777        Self::Other(Arc::new(value))
778    }
779}
780
781impl From<io::Error> for ImageCacheError {
782    fn from(value: io::Error) -> Self {
783        Self::Io(Arc::new(value))
784    }
785}
786
787impl From<usvg::Error> for ImageCacheError {
788    fn from(value: usvg::Error) -> Self {
789        Self::Usvg(Arc::new(value))
790    }
791}
792
793impl From<image::ImageError> for ImageCacheError {
794    fn from(value: image::ImageError) -> Self {
795        Self::Image(Arc::new(value))
796    }
797}
798
799#[cfg(test)]
800mod tests {
801    use super::*;
802    use crate::{TestAppContext, point, px, size};
803
804    #[gpui::test]
805    fn zero_frame_image_does_not_panic_on_paint(cx: &mut TestAppContext) {
806        let cx = cx.add_empty_window();
807        let image = Arc::new(RenderImage::new(SmallVec::new()));
808        cx.draw(point(px(0.), px(0.)), size(px(100.), px(100.)), |_, _| {
809            img(ImageSource::Render(image)).into_any_element()
810        });
811    }
812}