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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
use super::super::graphic;
use iced::{layout, Element, Hasher, Layout, Length, Point, Rectangle, Widget};
use std::hash::Hash;
use vek::Rgba;

// TODO: consider iced's approach to images and caching image data
// Also `Graphic` might be a better name for this is it wasn't already in use
// elsewhere

pub type Handle = graphic::Id;

pub struct Image {
    handle: Handle,
    width: Length,
    height: Length,
    fix_aspect_ratio: bool,
    color: Rgba<u8>,
}

impl Image {
    pub fn new(handle: Handle) -> Self {
        let width = Length::Fill;
        let height = Length::Fill;
        Self {
            handle,
            width,
            height,
            fix_aspect_ratio: false,
            color: Rgba::broadcast(255),
        }
    }

    #[must_use]
    pub fn width(mut self, width: Length) -> Self {
        self.width = width;
        self
    }

    #[must_use]
    pub fn height(mut self, height: Length) -> Self {
        self.height = height;
        self
    }

    #[must_use]
    pub fn fix_aspect_ratio(mut self) -> Self {
        self.fix_aspect_ratio = true;
        self
    }

    #[must_use]
    pub fn color(mut self, color: Rgba<u8>) -> Self {
        self.color = color;
        self
    }
}

impl<M, R> Widget<M, R> for Image
where
    R: Renderer,
{
    fn width(&self) -> Length { self.width }

    fn height(&self) -> Length { self.height }

    fn layout(&self, renderer: &R, limits: &layout::Limits) -> layout::Node {
        let mut size = limits.width(self.width).height(self.height).max();

        if self.fix_aspect_ratio {
            let aspect_ratio = {
                let (w, h) = renderer.dimensions(self.handle);
                w as f32 / h as f32
            };

            let max_aspect_ratio = size.width / size.height;

            if max_aspect_ratio > aspect_ratio {
                size.width = size.height * aspect_ratio;
            } else {
                size.height = size.width / aspect_ratio;
            }
        }

        layout::Node::new(size)
    }

    fn draw(
        &self,
        renderer: &mut R,
        _defaults: &R::Defaults,
        layout: Layout<'_>,
        _cursor_position: Point,
        _viewport: &Rectangle,
    ) -> R::Output {
        renderer.draw(self.handle, self.color, layout)
    }

    fn hash_layout(&self, state: &mut Hasher) {
        struct Marker;
        std::any::TypeId::of::<Marker>().hash(state);

        self.width.hash(state);
        self.height.hash(state);
        self.fix_aspect_ratio.hash(state);
        // TODO: also depends on dims but we have no way to access
    }
}

pub trait Renderer: iced::Renderer {
    fn dimensions(&self, handle: Handle) -> (u32, u32);
    fn draw(&mut self, handle: Handle, color: Rgba<u8>, layout: Layout<'_>) -> Self::Output;
}

impl<'a, M, R> From<Image> for Element<'a, M, R>
where
    R: Renderer,
{
    fn from(image: Image) -> Element<'a, M, R> { Element::new(image) }
}

impl<R> super::background_container::Background<R> for Image
where
    R: Renderer,
{
    fn width(&self) -> Length { self.width }

    fn height(&self) -> Length { self.height }

    fn aspect_ratio_fixed(&self) -> bool { self.fix_aspect_ratio }

    fn pixel_dims(&self, renderer: &R) -> (u16, u16) {
        let (w, h) = renderer.dimensions(self.handle);
        (w as u16, h as u16)
    }

    fn draw(
        &self,
        renderer: &mut R,
        _defaults: &R::Defaults,
        layout: Layout<'_>,
        _cursor_position: Point,
        _viewport: &Rectangle,
    ) -> R::Output {
        renderer.draw(self.handle, self.color, layout)
    }
}