img.rs

  1use crate::{
  2    AnyElement, BorrowWindow, Bounds, Component, Element, InteractiveComponent,
  3    InteractiveElementState, Interactivity, LayoutId, Pixels, SharedString, StyleRefinement,
  4    Styled, ViewContext,
  5};
  6use futures::FutureExt;
  7use util::ResultExt;
  8
  9pub struct Img<V: 'static> {
 10    interactivity: Interactivity<V>,
 11    uri: Option<SharedString>,
 12    grayscale: bool,
 13}
 14
 15pub fn img<V: 'static>() -> Img<V> {
 16    Img {
 17        interactivity: Interactivity::default(),
 18        uri: None,
 19        grayscale: false,
 20    }
 21}
 22
 23impl<V> Img<V>
 24where
 25    V: 'static,
 26{
 27    pub fn uri(mut self, uri: impl Into<SharedString>) -> Self {
 28        self.uri = Some(uri.into());
 29        self
 30    }
 31
 32    pub fn grayscale(mut self, grayscale: bool) -> Self {
 33        self.grayscale = grayscale;
 34        self
 35    }
 36}
 37
 38impl<V> Component<V> for Img<V> {
 39    fn render(self) -> AnyElement<V> {
 40        AnyElement::new(self)
 41    }
 42}
 43
 44impl<V> Element<V> for Img<V> {
 45    type ElementState = InteractiveElementState;
 46
 47    fn element_id(&self) -> Option<crate::ElementId> {
 48        self.interactivity.element_id.clone()
 49    }
 50
 51    fn layout(
 52        &mut self,
 53        _view_state: &mut V,
 54        element_state: Option<Self::ElementState>,
 55        cx: &mut ViewContext<V>,
 56    ) -> (LayoutId, Self::ElementState) {
 57        self.interactivity.layout(element_state, cx, |style, cx| {
 58            cx.request_layout(&style, None)
 59        })
 60    }
 61
 62    fn paint(
 63        &mut self,
 64        bounds: Bounds<Pixels>,
 65        _view_state: &mut V,
 66        element_state: &mut Self::ElementState,
 67        cx: &mut ViewContext<V>,
 68    ) {
 69        self.interactivity.paint(
 70            bounds,
 71            bounds.size,
 72            element_state,
 73            cx,
 74            |style, _scroll_offset, cx| {
 75                let corner_radii = style.corner_radii;
 76
 77                if let Some(uri) = self.uri.clone() {
 78                    // eprintln!(">>> image_cache.get({uri}");
 79                    let image_future = cx.image_cache.get(uri.clone());
 80                    // eprintln!("<<< image_cache.get({uri}");
 81                    if let Some(data) = image_future
 82                        .clone()
 83                        .now_or_never()
 84                        .and_then(|result| result.ok())
 85                    {
 86                        let corner_radii = corner_radii.to_pixels(bounds.size, cx.rem_size());
 87                        cx.with_z_index(1, |cx| {
 88                            cx.paint_image(bounds, corner_radii, data, self.grayscale)
 89                                .log_err()
 90                        });
 91                    } else {
 92                        cx.spawn(|_, mut cx| async move {
 93                            if image_future.await.ok().is_some() {
 94                                cx.on_next_frame(|cx| cx.notify());
 95                            }
 96                        })
 97                        .detach()
 98                    }
 99                }
100            },
101        )
102    }
103}
104
105impl<V> Styled for Img<V> {
106    fn style(&mut self) -> &mut StyleRefinement {
107        &mut self.interactivity.base_style
108    }
109}
110
111impl<V> InteractiveComponent<V> for Img<V> {
112    fn interactivity(&mut self) -> &mut Interactivity<V> {
113        &mut self.interactivity
114    }
115}