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 = BackgroundImage::Url(Url { url: img.get(cx).to_string().into() });
13        Self {}.build(cx, |_| {}).background_image(img)
14    }
15}
16
17impl View for Image {
18    fn element(&self) -> Option<&'static str> {
19        Some("image")
20    }
21}
22
23/// A view which presents an SVG image.
24pub struct Svg {}
25
26impl Svg {
27    /// Creates a new [Svg] view.
28    pub fn new<T>(cx: &mut Context, data: impl Res<T>) -> Handle<Self>
29    where
30        T: AsRef<[u8]> + 'static,
31    {
32        Self {}.build(cx, |_| {}).bind(data, |mut handle, data| {
33            let svg_data = data.get(&handle);
34            let h = format!("{:x}", fxhash::hash64(svg_data.as_ref()));
35
36            handle.context().load_svg(
37                &h,
38                svg_data.as_ref(),
39                ImageRetentionPolicy::DropWhenNoObservers,
40            );
41            handle.background_image(format!("'{}'", h).as_str()).hoverable(false);
42        })
43    }
44}
45
46impl View for Svg {
47    fn element(&self) -> Option<&'static str> {
48        Some("svg")
49    }
50}