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.render_size(frame_index);
336 style.aspect_ratio = Some(image_size.width / image_size.height);
337
338 if let Length::Auto = style.size.width {
339 style.size.width = match style.size.height {
340 Length::Definite(DefiniteLength::Absolute(
341 AbsoluteLength::Pixels(height),
342 )) => Length::Definite(
343 px(image_size.width.0 * height.0 / image_size.height.0)
344 .into(),
345 ),
346 _ => Length::Definite(image_size.width.into()),
347 };
348 }
349
350 if let Length::Auto = style.size.height {
351 style.size.height = match style.size.width {
352 Length::Definite(DefiniteLength::Absolute(
353 AbsoluteLength::Pixels(width),
354 )) => Length::Definite(
355 px(image_size.height.0 * width.0 / image_size.width.0)
356 .into(),
357 ),
358 _ => Length::Definite(image_size.height.into()),
359 };
360 }
361
362 if global_id.is_some() && data.frame_count() > 1 {
363 window.request_animation_frame();
364 }
365 }
366 Some(_err) => {
367 if let Some(fallback) = self.style.fallback.as_ref() {
368 let mut element = fallback();
369 replacement_id = Some(element.request_layout(window, cx));
370 layout_state.replacement = Some(element);
371 }
372 if let Some(state) = &mut state {
373 state.started_loading = None;
374 }
375 }
376 None => {
377 if let Some(state) = &mut state {
378 if let Some((started_loading, _)) = state.started_loading {
379 if started_loading.elapsed() > LOADING_DELAY
380 && let Some(loading) = self.style.loading.as_ref()
381 {
382 let mut element = loading();
383 replacement_id = Some(element.request_layout(window, cx));
384 layout_state.replacement = Some(element);
385 }
386 } else {
387 let current_view = window.current_view();
388 let task = window.spawn(cx, async move |cx| {
389 cx.background_executor().timer(LOADING_DELAY).await;
390 cx.update(move |_, cx| {
391 cx.notify(current_view);
392 })
393 .ok();
394 });
395 state.started_loading = Some((Instant::now(), task));
396 }
397 }
398 }
399 }
400
401 window.request_layout(style, replacement_id, cx)
402 },
403 );
404
405 layout_state.frame_index = frame_index;
406
407 ((layout_id, layout_state), state)
408 })
409 }
410
411 fn prepaint(
412 &mut self,
413 global_id: Option<&GlobalElementId>,
414 inspector_id: Option<&InspectorElementId>,
415 bounds: Bounds<Pixels>,
416 request_layout: &mut Self::RequestLayoutState,
417 window: &mut Window,
418 cx: &mut App,
419 ) -> Self::PrepaintState {
420 self.interactivity.prepaint(
421 global_id,
422 inspector_id,
423 bounds,
424 bounds.size,
425 window,
426 cx,
427 |_, _, hitbox, window, cx| {
428 if let Some(replacement) = &mut request_layout.replacement {
429 replacement.prepaint(window, cx);
430 }
431
432 hitbox
433 },
434 )
435 }
436
437 fn paint(
438 &mut self,
439 global_id: Option<&GlobalElementId>,
440 inspector_id: Option<&InspectorElementId>,
441 bounds: Bounds<Pixels>,
442 layout_state: &mut Self::RequestLayoutState,
443 hitbox: &mut Self::PrepaintState,
444 window: &mut Window,
445 cx: &mut App,
446 ) {
447 let source = self.source.clone();
448 self.interactivity.paint(
449 global_id,
450 inspector_id,
451 bounds,
452 hitbox.as_ref(),
453 window,
454 cx,
455 |style, window, cx| {
456 if let Some(Ok(data)) = source.use_data(
457 self.image_cache
458 .clone()
459 .or_else(|| window.image_cache_stack.last().cloned()),
460 window,
461 cx,
462 ) {
463 let new_bounds = self
464 .style
465 .object_fit
466 .get_bounds(bounds, data.size(layout_state.frame_index));
467 let corner_radii = style
468 .corner_radii
469 .to_pixels(window.rem_size())
470 .clamp_radii_for_quad_size(new_bounds.size);
471 window
472 .paint_image(
473 new_bounds,
474 corner_radii,
475 data,
476 layout_state.frame_index,
477 self.style.grayscale,
478 )
479 .log_err();
480 } else if let Some(replacement) = &mut layout_state.replacement {
481 replacement.paint(window, cx);
482 }
483 },
484 )
485 }
486}
487
488impl Styled for Img {
489 fn style(&mut self) -> &mut StyleRefinement {
490 &mut self.interactivity.base_style
491 }
492}
493
494impl InteractiveElement for Img {
495 fn interactivity(&mut self) -> &mut Interactivity {
496 &mut self.interactivity
497 }
498}
499
500impl IntoElement for Img {
501 type Element = Self;
502
503 fn into_element(self) -> Self::Element {
504 self
505 }
506}
507
508impl StatefulInteractiveElement for Img {}
509
510impl ImageSource {
511 pub(crate) fn use_data(
512 &self,
513 cache: Option<AnyImageCache>,
514 window: &mut Window,
515 cx: &mut App,
516 ) -> Option<Result<Arc<RenderImage>, ImageCacheError>> {
517 match self {
518 ImageSource::Resource(resource) => {
519 if let Some(cache) = cache {
520 cache.load(resource, window, cx)
521 } else {
522 window.use_asset::<ImgResourceLoader>(resource, cx)
523 }
524 }
525 ImageSource::Custom(loading_fn) => loading_fn(window, cx),
526 ImageSource::Render(data) => Some(Ok(data.to_owned())),
527 ImageSource::Image(data) => window.use_asset::<AssetLogger<ImageDecoder>>(data, cx),
528 }
529 }
530
531 pub(crate) fn get_data(
532 &self,
533 cache: Option<AnyImageCache>,
534 window: &mut Window,
535 cx: &mut App,
536 ) -> Option<Result<Arc<RenderImage>, ImageCacheError>> {
537 match self {
538 ImageSource::Resource(resource) => {
539 if let Some(cache) = cache {
540 cache.load(resource, window, cx)
541 } else {
542 window.get_asset::<ImgResourceLoader>(resource, cx)
543 }
544 }
545 ImageSource::Custom(loading_fn) => loading_fn(window, cx),
546 ImageSource::Render(data) => Some(Ok(data.to_owned())),
547 ImageSource::Image(data) => window.get_asset::<AssetLogger<ImageDecoder>>(data, cx),
548 }
549 }
550
551 /// Remove this image source from the asset system
552 pub fn remove_asset(&self, cx: &mut App) {
553 match self {
554 ImageSource::Resource(resource) => {
555 cx.remove_asset::<ImgResourceLoader>(resource);
556 }
557 ImageSource::Custom(_) | ImageSource::Render(_) => {}
558 ImageSource::Image(data) => cx.remove_asset::<AssetLogger<ImageDecoder>>(data),
559 }
560 }
561}
562
563#[derive(Clone)]
564enum ImageDecoder {}
565
566impl Asset for ImageDecoder {
567 type Source = Arc<Image>;
568 type Output = Result<Arc<RenderImage>, ImageCacheError>;
569
570 fn load(
571 source: Self::Source,
572 cx: &mut App,
573 ) -> impl Future<Output = Self::Output> + Send + 'static {
574 let renderer = cx.svg_renderer();
575 async move { source.to_image_data(renderer).map_err(Into::into) }
576 }
577}
578
579/// An image loader for the GPUI asset system
580#[derive(Clone)]
581pub enum ImageAssetLoader {}
582
583impl Asset for ImageAssetLoader {
584 type Source = Resource;
585 type Output = Result<Arc<RenderImage>, ImageCacheError>;
586
587 fn load(
588 source: Self::Source,
589 cx: &mut App,
590 ) -> impl Future<Output = Self::Output> + Send + 'static {
591 let client = cx.http_client();
592 // TODO: Can we make SVGs always rescale?
593 // let scale_factor = cx.scale_factor();
594 let svg_renderer = cx.svg_renderer();
595 let asset_source = cx.asset_source().clone();
596 async move {
597 let bytes = match source.clone() {
598 Resource::Path(uri) => fs::read(uri.as_ref())?,
599 Resource::Uri(uri) => {
600 let mut response = client
601 .get(uri.as_ref(), ().into(), true)
602 .await
603 .with_context(|| format!("loading image asset from {uri:?}"))?;
604 let mut body = Vec::new();
605 response.body_mut().read_to_end(&mut body).await?;
606 if !response.status().is_success() {
607 let mut body = String::from_utf8_lossy(&body).into_owned();
608 let first_line = body.lines().next().unwrap_or("").trim_end();
609 body.truncate(first_line.len());
610 return Err(ImageCacheError::BadStatus {
611 uri,
612 status: response.status(),
613 body,
614 });
615 }
616 body
617 }
618 Resource::Embedded(path) => {
619 let data = asset_source.load(&path).ok().flatten();
620 if let Some(data) = data {
621 data.to_vec()
622 } else {
623 return Err(ImageCacheError::Asset(
624 format!("Embedded resource not found: {}", path).into(),
625 ));
626 }
627 }
628 };
629
630 let data = if let Ok(format) = image::guess_format(&bytes) {
631 let data = match format {
632 ImageFormat::Gif => {
633 let decoder = GifDecoder::new(Cursor::new(&bytes))?;
634 let mut frames = SmallVec::new();
635
636 for frame in decoder.into_frames() {
637 let mut frame = frame?;
638 // Convert from RGBA to BGRA.
639 for pixel in frame.buffer_mut().chunks_exact_mut(4) {
640 pixel.swap(0, 2);
641 }
642 frames.push(frame);
643 }
644
645 frames
646 }
647 ImageFormat::WebP => {
648 let mut decoder = WebPDecoder::new(Cursor::new(&bytes))?;
649
650 if decoder.has_animation() {
651 let _ = decoder.set_background_color(Rgba([0, 0, 0, 0]));
652 let mut frames = SmallVec::new();
653
654 for frame in decoder.into_frames() {
655 let mut frame = frame?;
656 // Convert from RGBA to BGRA.
657 for pixel in frame.buffer_mut().chunks_exact_mut(4) {
658 pixel.swap(0, 2);
659 }
660 frames.push(frame);
661 }
662
663 frames
664 } else {
665 let mut data = DynamicImage::from_decoder(decoder)?.into_rgba8();
666
667 // Convert from RGBA to BGRA.
668 for pixel in data.chunks_exact_mut(4) {
669 pixel.swap(0, 2);
670 }
671
672 SmallVec::from_elem(Frame::new(data), 1)
673 }
674 }
675 _ => {
676 let mut data =
677 image::load_from_memory_with_format(&bytes, format)?.into_rgba8();
678
679 // Convert from RGBA to BGRA.
680 for pixel in data.chunks_exact_mut(4) {
681 pixel.swap(0, 2);
682 }
683
684 SmallVec::from_elem(Frame::new(data), 1)
685 }
686 };
687
688 RenderImage::new(data)
689 } else {
690 let pixmap =
691 // TODO: Can we make svgs always rescale?
692 svg_renderer.render_pixmap(&bytes, SvgSize::ScaleFactor(SMOOTH_SVG_SCALE_FACTOR))?;
693
694 let mut buffer =
695 ImageBuffer::from_raw(pixmap.width(), pixmap.height(), pixmap.take()).unwrap();
696
697 for pixel in buffer.chunks_exact_mut(4) {
698 swap_rgba_pa_to_bgra(pixel);
699 }
700
701 let mut image = RenderImage::new(SmallVec::from_elem(Frame::new(buffer), 1));
702 image.scale_factor = SMOOTH_SVG_SCALE_FACTOR;
703 image
704 };
705
706 Ok(Arc::new(data))
707 }
708 }
709}
710
711/// An error that can occur when interacting with the image cache.
712#[derive(Debug, Error, Clone)]
713pub enum ImageCacheError {
714 /// Some other kind of error occurred
715 #[error("error: {0}")]
716 Other(#[from] Arc<anyhow::Error>),
717 /// An error that occurred while reading the image from disk.
718 #[error("IO error: {0}")]
719 Io(Arc<std::io::Error>),
720 /// An error that occurred while processing an image.
721 #[error("unexpected http status for {uri}: {status}, body: {body}")]
722 BadStatus {
723 /// The URI of the image.
724 uri: SharedUri,
725 /// The HTTP status code.
726 status: http_client::StatusCode,
727 /// The HTTP response body.
728 body: String,
729 },
730 /// An error that occurred while processing an asset.
731 #[error("asset error: {0}")]
732 Asset(SharedString),
733 /// An error that occurred while processing an image.
734 #[error("image error: {0}")]
735 Image(Arc<ImageError>),
736 /// An error that occurred while processing an SVG.
737 #[error("svg error: {0}")]
738 Usvg(Arc<usvg::Error>),
739}
740
741impl From<anyhow::Error> for ImageCacheError {
742 fn from(value: anyhow::Error) -> Self {
743 Self::Other(Arc::new(value))
744 }
745}
746
747impl From<io::Error> for ImageCacheError {
748 fn from(value: io::Error) -> Self {
749 Self::Io(Arc::new(value))
750 }
751}
752
753impl From<usvg::Error> for ImageCacheError {
754 fn from(value: usvg::Error) -> Self {
755 Self::Usvg(Arc::new(value))
756 }
757}
758
759impl From<image::ImageError> for ImageCacheError {
760 fn from(value: image::ImageError) -> Self {
761 Self::Image(Arc::new(value))
762 }
763}