vizia_core/views/
image.rs1use vizia_style::Url;
2
3use crate::prelude::*;
4
5pub struct Image {}
10
11impl Image {
12 pub fn new<T>(cx: &mut Context, img: impl Res<T> + 'static) -> Handle<'_, Self>
16 where
17 T: ToString + Clone + 'static,
18 {
19 let img = img.to_signal(cx).map(|img| {
20 let path = img.to_string();
21 BackgroundImage::Url(Url { url: path.into() })
22 });
23
24 let handle = Self {}.build(cx, |_| {});
25
26 handle.background_image(img)
27 }
28}
29
30impl View for Image {
31 fn element(&self) -> Option<&'static str> {
32 Some("image")
33 }
34}
35
36pub struct Svg {}
38
39impl Svg {
40 pub fn new<T>(cx: &mut Context, data: impl Res<T>) -> Handle<Self>
42 where
43 T: AsRef<[u8]> + 'static,
44 {
45 let svg_data = data.get_value(cx);
46 let h = format!("{:x}", fxhash::hash64(svg_data.as_ref()));
47 let mut handle = Self {}.build(cx, |_| {});
48 handle.context().load_svg(&h, svg_data.as_ref(), ImageRetentionPolicy::DropWhenNoObservers);
49 handle.background_image(format!("'{}'", h).as_str()).hoverable(false)
50 }
51}
52
53impl View for Svg {
54 fn element(&self) -> Option<&'static str> {
55 Some("svg")
56 }
57}