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 order = layout.order;
 53        let bounds = layout.bounds;
 54
 55        style.paint(order, bounds, cx);
 56
 57        // if let Some(uri) = &self.uri {
 58        //     let image_future = cx.image_cache.get(uri.clone());
 59        //     if let Some(data) = image_future
 60        //         .clone()
 61        //         .now_or_never()
 62        //         .and_then(ResultExt::log_err)
 63        //     {
 64        //         let rem_size = cx.rem_size();
 65        //         cx.scene().push_image(scene::Image {
 66        //             bounds,
 67        //             border: gpui::Border {
 68        //                 color: style.border_color.unwrap_or_default().into(),
 69        //                 top: style.border_widths.top.to_pixels(rem_size),
 70        //                 right: style.border_widths.right.to_pixels(rem_size),
 71        //                 bottom: style.border_widths.bottom.to_pixels(rem_size),
 72        //                 left: style.border_widths.left.to_pixels(rem_size),
 73        //             },
 74        //             corner_radii: style.corner_radii.to_gpui(bounds.size(), rem_size),
 75        //             grayscale: false,
 76        //             data,
 77        //         })
 78        //     } else {
 79        //         cx.spawn(|this, mut cx| async move {
 80        //             if image_future.await.log_err().is_some() {
 81        //                 this.update(&mut cx, |_, cx| cx.notify()).ok();
 82        //             }
 83        //         })
 84        //         .detach();
 85        //     }
 86        // }
 87        Ok(())
 88    }
 89}
 90
 91impl<S> Styled for Img<S> {
 92    type Style = Style;
 93
 94    fn style_cascade(&mut self) -> &mut RefinementCascade<Self::Style> {
 95        &mut self.style
 96    }
 97
 98    fn declared_style(&mut self) -> &mut <Self::Style as refineable::Refineable>::Refinement {
 99        self.style.base()
100    }
101}
102
103impl<S> StyleHelpers for Img<S> {}