2019-10-05 19:22:51 +02:00
|
|
|
use crate::{Primitive, Renderer};
|
2019-11-10 06:05:20 +01:00
|
|
|
use iced_native::{image, layout, Image, Layout, Length, MouseCursor, Size};
|
2019-10-05 19:22:51 +02:00
|
|
|
|
2019-10-22 23:20:24 +02:00
|
|
|
impl image::Renderer for Renderer {
|
2019-11-10 06:05:20 +01:00
|
|
|
fn layout(&self, image: &Image, limits: &layout::Limits) -> layout::Node {
|
|
|
|
|
let (width, height) = self.image_pipeline.dimensions(&image.path);
|
|
|
|
|
|
|
|
|
|
let aspect_ratio = width as f32 / height as f32;
|
|
|
|
|
|
|
|
|
|
// TODO: Deal with additional cases
|
|
|
|
|
let (width, height) = match (image.width, image.height) {
|
|
|
|
|
(Length::Units(width), _) => (
|
|
|
|
|
image.width,
|
|
|
|
|
Length::Units((width as f32 / aspect_ratio).round() as u16),
|
|
|
|
|
),
|
|
|
|
|
(_, _) => {
|
|
|
|
|
(Length::Units(width as u16), Length::Units(height as u16))
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let mut size = limits.width(width).height(height).resolve(Size::ZERO);
|
|
|
|
|
|
|
|
|
|
size.height = size.width / aspect_ratio;
|
|
|
|
|
|
|
|
|
|
layout::Node::new(size)
|
2019-10-05 19:22:51 +02:00
|
|
|
}
|
|
|
|
|
|
2019-11-10 06:05:20 +01:00
|
|
|
fn draw(&mut self, image: &Image, layout: Layout<'_>) -> Self::Output {
|
2019-10-23 01:21:23 +02:00
|
|
|
(
|
|
|
|
|
Primitive::Image {
|
|
|
|
|
path: image.path.clone(),
|
|
|
|
|
bounds: layout.bounds(),
|
|
|
|
|
},
|
|
|
|
|
MouseCursor::OutOfBounds,
|
|
|
|
|
)
|
2019-10-05 19:22:51 +02:00
|
|
|
}
|
|
|
|
|
}
|