vizia_core/views/
image.rs
1use vizia_style::Url;
2
3use crate::prelude::*;
4
5pub struct Image {}
7
8impl Image {
9 pub fn new<T: ToString>(cx: &mut Context, img: impl Res<T>) -> Handle<'_, Self> {
11 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
23pub struct Svg {}
25
26impl Svg {
27 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}