Skip to main content

vizia_core/views/
image.rs

1use vizia_style::Url;
2
3use crate::prelude::*;
4
5/// A view which presents an image.
6pub struct Image {}
7
8impl Image {
9    /// Creates a new [Image] view.
10    pub fn new<T: ToString>(cx: &mut Context, img: impl Res<T>) -> Handle<'_, Self> {
11        // TODO: Make this reactive
12        let img = img.get_value(cx);
13        let img = BackgroundImage::Url(Url { url: img.to_string().into() });
14        Self {}.build(cx, |_| {}).background_image(img)
15    }
16}
17
18impl View for Image {
19    fn element(&self) -> Option<&'static str> {
20        Some("image")
21    }
22}
23
24/// A view which presents an SVG image.
25pub struct Svg {}
26
27impl Svg {
28    /// Creates a new [Svg] view.
29    pub fn new<T>(cx: &mut Context, data: impl Res<T>) -> Handle<Self>
30    where
31        T: AsRef<[u8]> + 'static,
32    {
33        let svg_data = data.get_value(cx);
34        let h = format!("{:x}", fxhash::hash64(svg_data.as_ref()));
35        let mut handle = Self {}.build(cx, |_| {});
36        handle.context().load_svg(&h, svg_data.as_ref(), ImageRetentionPolicy::DropWhenNoObservers);
37        handle.background_image(format!("'{}'", h).as_str()).hoverable(false)
38    }
39}
40
41impl View for Svg {
42    fn element(&self) -> Option<&'static str> {
43        Some("svg")
44    }
45}