img.rs

  1use crate::{Element, Layout, LayoutId, Result, Style, StyleHelpers, Styled};
  2use refineable::RefinementCascade;
  3use std::marker::PhantomData;
  4use util::arc_cow::ArcCow;
  5
  6pub struct Img<S> {
  7    style: RefinementCascade<Style>,
  8    uri: Option<ArcCow<'static, str>>,
  9    state_type: PhantomData<S>,
 10}
 11
 12pub fn img<S>() -> Img<S> {
 13    Img {
 14        style: RefinementCascade::default(),
 15        uri: None,
 16        state_type: PhantomData,
 17    }
 18}
 19
 20impl<S> Img<S> {
 21    pub fn uri(mut self, uri: impl Into<ArcCow<'static, str>>) -> Self {
 22        self.uri = Some(uri.into());
 23        self
 24    }
 25}
 26
 27impl<S: 'static> Element for Img<S> {
 28    type State = S;
 29    type FrameState = ();
 30
 31    fn layout(
 32        &mut self,
 33        _: &mut Self::State,
 34        cx: &mut crate::ViewContext<Self::State>,
 35    ) -> anyhow::Result<(LayoutId, Self::FrameState)>
 36    where
 37        Self: Sized,
 38    {
 39        let style = self.computed_style();
 40        let layout_id = cx.request_layout(style, [])?;
 41        Ok((layout_id, ()))
 42    }
 43
 44    fn paint(
 45        &mut self,
 46        layout: Layout,
 47        _: &mut Self::State,
 48        _: &mut Self::FrameState,
 49        cx: &mut crate::ViewContext<Self::State>,
 50    ) -> Result<()> {
 51        let style = self.computed_style();
 52        let bounds = layout.bounds;
 53
 54        style.paint_background(bounds, cx);
 55
 56        // if let Some(uri) = &self.uri {
 57        //     let image_future = cx.image_cache.get(uri.clone());
 58        //     if let Some(data) = image_future
 59        //         .clone()
 60        //         .now_or_never()
 61        //         .and_then(ResultExt::log_err)
 62        //     {
 63        //         let rem_size = cx.rem_size();
 64        //         cx.scene().push_image(scene::Image {
 65        //             bounds,
 66        //             border: gpui::Border {
 67        //                 color: style.border_color.unwrap_or_default().into(),
 68        //                 top: style.border_widths.top.to_pixels(rem_size),
 69        //                 right: style.border_widths.right.to_pixels(rem_size),
 70        //                 bottom: style.border_widths.bottom.to_pixels(rem_size),
 71        //                 left: style.border_widths.left.to_pixels(rem_size),
 72        //             },
 73        //             corner_radii: style.corner_radii.to_gpui(bounds.size(), rem_size),
 74        //             grayscale: false,
 75        //             data,
 76        //         })
 77        //     } else {
 78        //         cx.spawn(|this, mut cx| async move {
 79        //             if image_future.await.log_err().is_some() {
 80        //                 this.update(&mut cx, |_, cx| cx.notify()).ok();
 81        //             }
 82        //         })
 83        //         .detach();
 84        //     }
 85        // }
 86        Ok(())
 87    }
 88}
 89
 90impl<S> Styled for Img<S> {
 91    type Style = Style;
 92
 93    fn style_cascade(&mut self) -> &mut RefinementCascade<Self::Style> {
 94        &mut self.style
 95    }
 96
 97    fn declared_style(&mut self) -> &mut <Self::Style as refineable::Refineable>::Refinement {
 98        self.style.base()
 99    }
100}
101
102impl<S> StyleHelpers for Img<S> {}