vizia_core 0.4.0

Core components of vizia
use vizia_style::Url;

use crate::prelude::*;

/// A view which presents an image.
pub struct Image {}

impl Image {
    /// Creates a new [Image] view.
    pub fn new<T: ToString>(cx: &mut Context, img: impl Res<T>) -> Handle<'_, Self> {
        // TODO: Make this reactive
        let img = img.get_value(cx);
        let img = BackgroundImage::Url(Url { url: img.to_string().into() });
        Self {}.build(cx, |_| {}).background_image(img)
    }
}

impl View for Image {
    fn element(&self) -> Option<&'static str> {
        Some("image")
    }
}

/// A view which presents an SVG image.
pub struct Svg {}

impl Svg {
    /// Creates a new [Svg] view.
    pub fn new<T>(cx: &mut Context, data: impl Res<T>) -> Handle<Self>
    where
        T: AsRef<[u8]> + 'static,
    {
        let svg_data = data.get_value(cx);
        let h = format!("{:x}", fxhash::hash64(svg_data.as_ref()));
        let mut handle = Self {}.build(cx, |_| {});
        handle.context().load_svg(&h, svg_data.as_ref(), ImageRetentionPolicy::DropWhenNoObservers);
        handle.background_image(format!("'{}'", h).as_str()).hoverable(false)
    }
}

impl View for Svg {
    fn element(&self) -> Option<&'static str> {
        Some("svg")
    }
}