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 initialize(
 52        &mut self,
 53        _view_state: &mut V,
 54        element_state: Option<Self::ElementState>,
 55        cx: &mut ViewContext<V>,
 56    ) -> Self::ElementState {
 57        self.interactivity.initialize(element_state, cx)
 58    }
 59
 60    fn layout(
 61        &mut self,
 62        _view_state: &mut V,
 63        element_state: &mut Self::ElementState,
 64        cx: &mut ViewContext<V>,
 65    ) -> LayoutId {
 66        self.interactivity.layout(element_state, cx, |style, cx| {
 67            cx.request_layout(&style, None)
 68        })
 69    }
 70
 71    fn paint(
 72        &mut self,
 73        bounds: Bounds<Pixels>,
 74        _view_state: &mut V,
 75        element_state: &mut Self::ElementState,
 76        cx: &mut ViewContext<V>,
 77    ) {
 78        self.interactivity.paint(
 79            bounds,
 80            bounds.size,
 81            element_state,
 82            cx,
 83            |style, _scroll_offset, cx| {
 84                let corner_radii = style.corner_radii;
 85
 86                if let Some(uri) = self.uri.clone() {
 87                    // eprintln!(">>> image_cache.get({uri}");
 88                    let image_future = cx.image_cache.get(uri.clone());
 89                    // eprintln!("<<< image_cache.get({uri}");
 90                    if let Some(data) = image_future
 91                        .clone()
 92                        .now_or_never()
 93                        .and_then(ResultExt::log_err)
 94                    {
 95                        let corner_radii = corner_radii.to_pixels(bounds.size, cx.rem_size());
 96                        cx.with_z_index(1, |cx| {
 97                            cx.paint_image(bounds, corner_radii, data, self.grayscale)
 98                                .log_err()
 99                        });
100                    } else {
101                        cx.spawn(|_, mut cx| async move {
102                            if image_future.await.log_err().is_some() {
103                                cx.on_next_frame(|cx| cx.notify());
104                            }
105                        })
106                        .detach()
107                    }
108                }
109            },
110        )
111    }
112}
113
114impl<V> Styled for Img<V> {
115    fn style(&mut self) -> &mut StyleRefinement {
116        &mut self.interactivity.base_style
117    }
118}
119
120impl<V> InteractiveComponent<V> for Img<V> {
121    fn interactivity(&mut self) -> &mut Interactivity<V> {
122        &mut self.interactivity
123    }
124}