1use crate::{
2 AbsoluteLength, AnyElement, AnyImageCache, App, Asset, AssetLogger, Bounds, DefiniteLength,
3 Element, ElementId, Entity, GlobalElementId, Hitbox, Image, ImageCache, InspectorElementId,
4 InteractiveElement, Interactivity, IntoElement, LayoutId, Length, ObjectFit, Pixels,
5 RenderImage, Resource, SMOOTH_SVG_SCALE_FACTOR, SharedString, SharedUri, StyleRefinement,
6 Styled, SvgSize, Task, Window, px, swap_rgba_pa_to_bgra,
7};
8use anyhow::{Context as _, Result};
9
10use futures::{AsyncReadExt, Future};
11use image::{
12 AnimationDecoder, DynamicImage, Frame, ImageBuffer, ImageError, ImageFormat, Rgba,
13 codecs::{gif::GifDecoder, webp::WebPDecoder},
14};
15use smallvec::SmallVec;
16use std::{
17 fs,
18 io::{self, Cursor},
19 ops::{Deref, DerefMut},
20 path::{Path, PathBuf},
21 str::FromStr,
22 sync::Arc,
23 time::{Duration, Instant},
24};
25use thiserror::Error;
26use util::ResultExt;
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 http_client::Uri::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 the object fit for the image.
164 fn with_fallback(mut self, fallback: impl Fn() -> AnyElement + 'static) -> Self {
165 self.image_style().fallback = Some(Box::new(fallback));
166 self
167 }
168
169 /// Set the object fit for the image.
170 fn with_loading(mut self, loading: impl Fn() -> AnyElement + 'static) -> Self {
171 self.image_style().loading = Some(Box::new(loading));
172 self
173 }
174}
175
176impl StyledImage for Img {
177 fn image_style(&mut self) -> &mut ImageStyle {
178 &mut self.style
179 }
180}
181
182impl StyledImage for Stateful<Img> {
183 fn image_style(&mut self) -> &mut ImageStyle {
184 &mut self.element.style
185 }
186}
187
188/// An image element.
189pub struct Img {
190 interactivity: Interactivity,
191 source: ImageSource,
192 style: ImageStyle,
193 image_cache: Option<AnyImageCache>,
194}
195
196/// Create a new image element.
197#[track_caller]
198pub fn img(source: impl Into<ImageSource>) -> Img {
199 Img {
200 interactivity: Interactivity::new(),
201 source: source.into(),
202 style: ImageStyle::default(),
203 image_cache: None,
204 }
205}
206
207impl Img {
208 /// A list of all format extensions currently supported by this img element
209 pub fn extensions() -> &'static [&'static str] {
210 // This is the list in [image::ImageFormat::from_extension] + `svg`
211 &[
212 "avif", "jpg", "jpeg", "png", "gif", "webp", "tif", "tiff", "tga", "dds", "bmp", "ico",
213 "hdr", "exr", "pbm", "pam", "ppm", "pgm", "ff", "farbfeld", "qoi", "svg",
214 ]
215 }
216
217 /// Sets the image cache for the current node.
218 ///
219 /// If the `image_cache` is not explicitly provided, the function will determine the image cache by:
220 ///
221 /// 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.
222 /// 2. If no ancestor node contains an `ImageCacheElement`, the global image cache will be used as a fallback.
223 ///
224 /// This mechanism provides a flexible way to manage image caching, allowing precise control when needed,
225 /// while ensuring a default behavior when no cache is explicitly specified.
226 #[inline]
227 pub fn image_cache<I: ImageCache>(self, image_cache: &Entity<I>) -> Self {
228 Self {
229 image_cache: Some(image_cache.clone().into()),
230 ..self
231 }
232 }
233}
234
235impl Deref for Stateful<Img> {
236 type Target = Img;
237
238 fn deref(&self) -> &Self::Target {
239 &self.element
240 }
241}
242
243impl DerefMut for Stateful<Img> {
244 fn deref_mut(&mut self) -> &mut Self::Target {
245 &mut self.element
246 }
247}
248
249/// The image state between frames
250struct ImgState {
251 frame_index: usize,
252 last_frame_time: Option<Instant>,
253 started_loading: Option<(Instant, Task<()>)>,
254}
255
256/// The image layout state between frames
257pub struct ImgLayoutState {
258 frame_index: usize,
259 replacement: Option<AnyElement>,
260}
261
262impl Element for Img {
263 type RequestLayoutState = ImgLayoutState;
264 type PrepaintState = Option<Hitbox>;
265
266 fn id(&self) -> Option<ElementId> {
267 self.interactivity.element_id.clone()
268 }
269
270 fn source_location(&self) -> Option<&'static core::panic::Location<'static>> {
271 self.interactivity.source_location()
272 }
273
274 fn request_layout(
275 &mut self,
276 global_id: Option<&GlobalElementId>,
277 inspector_id: Option<&InspectorElementId>,
278 window: &mut Window,
279 cx: &mut App,
280 ) -> (LayoutId, Self::RequestLayoutState) {
281 let mut layout_state = ImgLayoutState {
282 frame_index: 0,
283 replacement: None,
284 };
285
286 window.with_optional_element_state(global_id, |state, window| {
287 let mut state = state.map(|state| {
288 state.unwrap_or(ImgState {
289 frame_index: 0,
290 last_frame_time: None,
291 started_loading: None,
292 })
293 });
294
295 let frame_index = state.as_ref().map(|state| state.frame_index).unwrap_or(0);
296
297 let layout_id = self.interactivity.request_layout(
298 global_id,
299 inspector_id,
300 window,
301 cx,
302 |mut style, window, cx| {
303 let mut replacement_id = None;
304
305 match self.source.use_data(
306 self.image_cache
307 .clone()
308 .or_else(|| window.image_cache_stack.last().cloned()),
309 window,
310 cx,
311 ) {
312 Some(Ok(data)) => {
313 if let Some(state) = &mut state {
314 let frame_count = data.frame_count();
315 if frame_count > 1 {
316 let current_time = Instant::now();
317 if let Some(last_frame_time) = state.last_frame_time {
318 let elapsed = current_time - last_frame_time;
319 let frame_duration =
320 Duration::from(data.delay(state.frame_index));
321
322 if elapsed >= frame_duration {
323 state.frame_index =
324 (state.frame_index + 1) % frame_count;
325 state.last_frame_time =
326 Some(current_time - (elapsed - frame_duration));
327 }
328 } else {
329 state.last_frame_time = Some(current_time);
330 }
331 }
332 state.started_loading = None;
333 }
334
335 let image_size = data.size(frame_index);
336 style.aspect_ratio =
337 Some(image_size.width.0 as f32 / image_size.height.0 as f32);
338
339 if let Length::Auto = style.size.width {
340 style.size.width = match style.size.height {
341 Length::Definite(DefiniteLength::Absolute(
342 AbsoluteLength::Pixels(height),
343 )) => Length::Definite(
344 px(image_size.width.0 as f32 * height.0
345 / image_size.height.0 as f32)
346 .into(),
347 ),
348 _ => Length::Definite(px(image_size.width.0 as f32).into()),
349 };
350 }
351
352 if let Length::Auto = style.size.height {
353 style.size.height = match style.size.width {
354 Length::Definite(DefiniteLength::Absolute(
355 AbsoluteLength::Pixels(width),
356 )) => Length::Definite(
357 px(image_size.height.0 as f32 * width.0
358 / image_size.width.0 as f32)
359 .into(),
360 ),
361 _ => Length::Definite(px(image_size.height.0 as f32).into()),
362 };
363 }
364
365 if global_id.is_some() && data.frame_count() > 1 {
366 window.request_animation_frame();
367 }
368 }
369 Some(_err) => {
370 if let Some(fallback) = self.style.fallback.as_ref() {
371 let mut element = fallback();
372 replacement_id = Some(element.request_layout(window, cx));
373 layout_state.replacement = Some(element);
374 }
375 if let Some(state) = &mut state {
376 state.started_loading = None;
377 }
378 }
379 None => {
380 if let Some(state) = &mut state {
381 if let Some((started_loading, _)) = state.started_loading {
382 if started_loading.elapsed() > LOADING_DELAY
383 && let Some(loading) = self.style.loading.as_ref()
384 {
385 let mut element = loading();
386 replacement_id = Some(element.request_layout(window, cx));
387 layout_state.replacement = Some(element);
388 }
389 } else {
390 let current_view = window.current_view();
391 let task = window.spawn(cx, async move |cx| {
392 cx.background_executor().timer(LOADING_DELAY).await;
393 cx.update(move |_, cx| {
394 cx.notify(current_view);
395 })
396 .ok();
397 });
398 state.started_loading = Some((Instant::now(), task));
399 }
400 }
401 }
402 }
403
404 window.request_layout(style, replacement_id, cx)
405 },
406 );
407
408 layout_state.frame_index = frame_index;
409
410 ((layout_id, layout_state), state)
411 })
412 }
413
414 fn prepaint(
415 &mut self,
416 global_id: Option<&GlobalElementId>,
417 inspector_id: Option<&InspectorElementId>,
418 bounds: Bounds<Pixels>,
419 request_layout: &mut Self::RequestLayoutState,
420 window: &mut Window,
421 cx: &mut App,
422 ) -> Self::PrepaintState {
423 self.interactivity.prepaint(
424 global_id,
425 inspector_id,
426 bounds,
427 bounds.size,
428 window,
429 cx,
430 |_, _, hitbox, window, cx| {
431 if let Some(replacement) = &mut request_layout.replacement {
432 replacement.prepaint(window, cx);
433 }
434
435 hitbox
436 },
437 )
438 }
439
440 fn paint(
441 &mut self,
442 global_id: Option<&GlobalElementId>,
443 inspector_id: Option<&InspectorElementId>,
444 bounds: Bounds<Pixels>,
445 layout_state: &mut Self::RequestLayoutState,
446 hitbox: &mut Self::PrepaintState,
447 window: &mut Window,
448 cx: &mut App,
449 ) {
450 let source = self.source.clone();
451 self.interactivity.paint(
452 global_id,
453 inspector_id,
454 bounds,
455 hitbox.as_ref(),
456 window,
457 cx,
458 |style, window, cx| {
459 if let Some(Ok(data)) = source.use_data(
460 self.image_cache
461 .clone()
462 .or_else(|| window.image_cache_stack.last().cloned()),
463 window,
464 cx,
465 ) {
466 let new_bounds = self
467 .style
468 .object_fit
469 .get_bounds(bounds, data.size(layout_state.frame_index));
470 let corner_radii = style
471 .corner_radii
472 .to_pixels(window.rem_size())
473 .clamp_radii_for_quad_size(new_bounds.size);
474 window
475 .paint_image(
476 new_bounds,
477 corner_radii,
478 data,
479 layout_state.frame_index,
480 self.style.grayscale,
481 )
482 .log_err();
483 } else if let Some(replacement) = &mut layout_state.replacement {
484 replacement.paint(window, cx);
485 }
486 },
487 )
488 }
489}
490
491impl Styled for Img {
492 fn style(&mut self) -> &mut StyleRefinement {
493 &mut self.interactivity.base_style
494 }
495}
496
497impl InteractiveElement for Img {
498 fn interactivity(&mut self) -> &mut Interactivity {
499 &mut self.interactivity
500 }
501}
502
503impl IntoElement for Img {
504 type Element = Self;
505
506 fn into_element(self) -> Self::Element {
507 self
508 }
509}
510
511impl StatefulInteractiveElement for Img {}
512
513impl ImageSource {
514 pub(crate) fn use_data(
515 &self,
516 cache: Option<AnyImageCache>,
517 window: &mut Window,
518 cx: &mut App,
519 ) -> Option<Result<Arc<RenderImage>, ImageCacheError>> {
520 match self {
521 ImageSource::Resource(resource) => {
522 if let Some(cache) = cache {
523 cache.load(resource, window, cx)
524 } else {
525 window.use_asset::<ImgResourceLoader>(resource, cx)
526 }
527 }
528 ImageSource::Custom(loading_fn) => loading_fn(window, cx),
529 ImageSource::Render(data) => Some(Ok(data.to_owned())),
530 ImageSource::Image(data) => window.use_asset::<AssetLogger<ImageDecoder>>(data, cx),
531 }
532 }
533
534 pub(crate) fn get_data(
535 &self,
536 cache: Option<AnyImageCache>,
537 window: &mut Window,
538 cx: &mut App,
539 ) -> Option<Result<Arc<RenderImage>, ImageCacheError>> {
540 match self {
541 ImageSource::Resource(resource) => {
542 if let Some(cache) = cache {
543 cache.load(resource, window, cx)
544 } else {
545 window.get_asset::<ImgResourceLoader>(resource, cx)
546 }
547 }
548 ImageSource::Custom(loading_fn) => loading_fn(window, cx),
549 ImageSource::Render(data) => Some(Ok(data.to_owned())),
550 ImageSource::Image(data) => window.get_asset::<AssetLogger<ImageDecoder>>(data, cx),
551 }
552 }
553
554 /// Remove this image source from the asset system
555 pub fn remove_asset(&self, cx: &mut App) {
556 match self {
557 ImageSource::Resource(resource) => {
558 cx.remove_asset::<ImgResourceLoader>(resource);
559 }
560 ImageSource::Custom(_) | ImageSource::Render(_) => {}
561 ImageSource::Image(data) => cx.remove_asset::<AssetLogger<ImageDecoder>>(data),
562 }
563 }
564}
565
566#[derive(Clone)]
567enum ImageDecoder {}
568
569impl Asset for ImageDecoder {
570 type Source = Arc<Image>;
571 type Output = Result<Arc<RenderImage>, ImageCacheError>;
572
573 fn load(
574 source: Self::Source,
575 cx: &mut App,
576 ) -> impl Future<Output = Self::Output> + Send + 'static {
577 let renderer = cx.svg_renderer();
578 async move { source.to_image_data(renderer).map_err(Into::into) }
579 }
580}
581
582/// An image loader for the GPUI asset system
583#[derive(Clone)]
584pub enum ImageAssetLoader {}
585
586impl Asset for ImageAssetLoader {
587 type Source = Resource;
588 type Output = Result<Arc<RenderImage>, ImageCacheError>;
589
590 fn load(
591 source: Self::Source,
592 cx: &mut App,
593 ) -> impl Future<Output = Self::Output> + Send + 'static {
594 let client = cx.http_client();
595 // TODO: Can we make SVGs always rescale?
596 // let scale_factor = cx.scale_factor();
597 let svg_renderer = cx.svg_renderer();
598 let asset_source = cx.asset_source().clone();
599 async move {
600 let bytes = match source.clone() {
601 Resource::Path(uri) => fs::read(uri.as_ref())?,
602 Resource::Uri(uri) => {
603 let mut response = client
604 .get(uri.as_ref(), ().into(), true)
605 .await
606 .with_context(|| format!("loading image asset from {uri:?}"))?;
607 let mut body = Vec::new();
608 response.body_mut().read_to_end(&mut body).await?;
609 if !response.status().is_success() {
610 let mut body = String::from_utf8_lossy(&body).into_owned();
611 let first_line = body.lines().next().unwrap_or("").trim_end();
612 body.truncate(first_line.len());
613 return Err(ImageCacheError::BadStatus {
614 uri,
615 status: response.status(),
616 body,
617 });
618 }
619 body
620 }
621 Resource::Embedded(path) => {
622 let data = asset_source.load(&path).ok().flatten();
623 if let Some(data) = data {
624 data.to_vec()
625 } else {
626 return Err(ImageCacheError::Asset(
627 format!("Embedded resource not found: {}", path).into(),
628 ));
629 }
630 }
631 };
632
633 let data = if let Ok(format) = image::guess_format(&bytes) {
634 let data = match format {
635 ImageFormat::Gif => {
636 let decoder = GifDecoder::new(Cursor::new(&bytes))?;
637 let mut frames = SmallVec::new();
638
639 for frame in decoder.into_frames() {
640 let mut frame = frame?;
641 // Convert from RGBA to BGRA.
642 for pixel in frame.buffer_mut().chunks_exact_mut(4) {
643 pixel.swap(0, 2);
644 }
645 frames.push(frame);
646 }
647
648 frames
649 }
650 ImageFormat::WebP => {
651 let mut decoder = WebPDecoder::new(Cursor::new(&bytes))?;
652
653 if decoder.has_animation() {
654 let _ = decoder.set_background_color(Rgba([0, 0, 0, 0]));
655 let mut frames = SmallVec::new();
656
657 for frame in decoder.into_frames() {
658 let mut frame = frame?;
659 // Convert from RGBA to BGRA.
660 for pixel in frame.buffer_mut().chunks_exact_mut(4) {
661 pixel.swap(0, 2);
662 }
663 frames.push(frame);
664 }
665
666 frames
667 } else {
668 let mut data = DynamicImage::from_decoder(decoder)?.into_rgba8();
669
670 // Convert from RGBA to BGRA.
671 for pixel in data.chunks_exact_mut(4) {
672 pixel.swap(0, 2);
673 }
674
675 SmallVec::from_elem(Frame::new(data), 1)
676 }
677 }
678 _ => {
679 let mut data =
680 image::load_from_memory_with_format(&bytes, format)?.into_rgba8();
681
682 // Convert from RGBA to BGRA.
683 for pixel in data.chunks_exact_mut(4) {
684 pixel.swap(0, 2);
685 }
686
687 SmallVec::from_elem(Frame::new(data), 1)
688 }
689 };
690
691 RenderImage::new(data)
692 } else {
693 let pixmap =
694 // TODO: Can we make svgs always rescale?
695 svg_renderer.render_pixmap(&bytes, SvgSize::ScaleFactor(SMOOTH_SVG_SCALE_FACTOR))?;
696
697 let mut buffer =
698 ImageBuffer::from_raw(pixmap.width(), pixmap.height(), pixmap.take()).unwrap();
699
700 for pixel in buffer.chunks_exact_mut(4) {
701 swap_rgba_pa_to_bgra(pixel);
702 }
703
704 RenderImage::new(SmallVec::from_elem(Frame::new(buffer), 1))
705 };
706
707 Ok(Arc::new(data))
708 }
709 }
710}
711
712/// An error that can occur when interacting with the image cache.
713#[derive(Debug, Error, Clone)]
714pub enum ImageCacheError {
715 /// Some other kind of error occurred
716 #[error("error: {0}")]
717 Other(#[from] Arc<anyhow::Error>),
718 /// An error that occurred while reading the image from disk.
719 #[error("IO error: {0}")]
720 Io(Arc<std::io::Error>),
721 /// An error that occurred while processing an image.
722 #[error("unexpected http status for {uri}: {status}, body: {body}")]
723 BadStatus {
724 /// The URI of the image.
725 uri: SharedUri,
726 /// The HTTP status code.
727 status: http_client::StatusCode,
728 /// The HTTP response body.
729 body: String,
730 },
731 /// An error that occurred while processing an asset.
732 #[error("asset error: {0}")]
733 Asset(SharedString),
734 /// An error that occurred while processing an image.
735 #[error("image error: {0}")]
736 Image(Arc<ImageError>),
737 /// An error that occurred while processing an SVG.
738 #[error("svg error: {0}")]
739 Usvg(Arc<usvg::Error>),
740}
741
742impl From<anyhow::Error> for ImageCacheError {
743 fn from(value: anyhow::Error) -> Self {
744 Self::Other(Arc::new(value))
745 }
746}
747
748impl From<io::Error> for ImageCacheError {
749 fn from(value: io::Error) -> Self {
750 Self::Io(Arc::new(value))
751 }
752}
753
754impl From<usvg::Error> for ImageCacheError {
755 fn from(value: usvg::Error) -> Self {
756 Self::Usvg(Arc::new(value))
757 }
758}
759
760impl From<image::ImageError> for ImageCacheError {
761 fn from(value: image::ImageError) -> Self {
762 Self::Image(Arc::new(value))
763 }
764}