summaryrefslogtreecommitdiffstats
path: root/wgpu/src/renderer/image.rs
blob: 0e312706a4f9df9dab9ce8f98494987e631277c5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
use crate::{Primitive, Renderer};
use iced_native::{image, Image, Layout, Length, MouseCursor, Node, Style};

impl image::Renderer for Renderer {
    fn node(&self, image: &Image) -> Node {
        let (width, height) = self.image_pipeline.dimensions(&image.path);

        let aspect_ratio = width as f32 / height as f32;

        let mut style = Style::default().align_self(image.align_self);

        // TODO: Deal with additional cases
        style = match (image.width, image.height) {
            (Length::Units(width), _) => style.width(image.width).height(
                Length::Units((width as f32 / aspect_ratio).round() as u16),
            ),
            (_, _) => style
                .width(Length::Units(width as u16))
                .height(Length::Units(height as u16)),
        };

        Node::new(style)
    }

    fn draw(&mut self, image: &Image, layout: Layout<'_>) -> Self::Output {
        (
            Primitive::Image {
                path: image.path.clone(),
                bounds: layout.bounds(),
            },
            MouseCursor::OutOfBounds,
        )
    }
}