use crate::{
element::{builders::View, Element},
ImageHandle,
};
pub struct Image {
handle: ImageHandle,
width: Option<f32>,
height: Option<f32>,
}
impl Image {
pub fn new(handle: ImageHandle) -> Self {
Self {
handle,
width: None,
height: None,
}
}
pub fn width(mut self, value: f32) -> Self {
self.width = Some(value);
self
}
pub fn height(mut self, value: f32) -> Self {
self.height = Some(value);
self
}
pub fn into_element(self) -> Element {
let mut view = View::new().image(self.handle);
if let Some(width) = self.width {
view = view.width(width);
}
if let Some(height) = self.height {
view = view.height(height);
}
view.into_element()
}
}
impl From<Image> for Element {
fn from(image: Image) -> Self {
image.into_element()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::asset::image_handle::ImageData;
use crate::element::style::Dimension;
use std::sync::Arc;
#[test]
fn image_widget_produces_view_with_image_paint() {
let handle = ImageHandle::from_arc(Arc::new(ImageData {
width: 2,
height: 2,
pixels: vec![255u8; 2 * 2 * 4],
}));
let expected = handle.clone();
let element = Image::new(handle).width(100.0).height(80.0).into_element();
let Element::View(view) = element else {
panic!("expected View element from Image");
};
assert_eq!(view.paint.image, Some(expected));
assert_eq!(view.style.width, Some(Dimension::Points(100.0)));
assert_eq!(view.style.height, Some(Dimension::Points(80.0)));
}
#[test]
fn image_widget_implements_into_element_api() {
let handle = ImageHandle::from_arc(Arc::new(ImageData {
width: 1,
height: 1,
pixels: vec![0u8; 4],
}));
let _ = Image::new(handle).into_element();
}
}