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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
use super::super::{super::Rotation, style, IcedRenderer, Primitive};
use common::util::srgba_to_linear;
use iced::{container, Element, Layout, Point, Rectangle};
use style::container::Border;
use vek::Rgba;

// TODO: move to style
const BORDER_SIZE: u16 = 8;

impl container::Renderer for IcedRenderer {
    type Style = style::container::Style;

    fn draw<M>(
        &mut self,
        defaults: &Self::Defaults,
        bounds: Rectangle,
        cursor_position: Point,
        viewport: &Rectangle,
        style_sheet: &Self::Style,
        content: &Element<'_, M, Self>,
        content_layout: Layout<'_>,
    ) -> Self::Output {
        let (content, mouse_interaction) =
            content.draw(self, defaults, content_layout, cursor_position, viewport);

        let prim = match style_sheet {
            Self::Style::Image(handle, color) => {
                let background = Primitive::Image {
                    handle: (*handle, Rotation::None),
                    bounds,
                    color: *color,
                    source_rect: None,
                };

                Primitive::Group {
                    primitives: vec![background, content],
                }
            },
            Self::Style::Color(color, border) => {
                let linear_color = srgba_to_linear(color.map(|e| e as f32 / 255.0));

                let primitives = match border {
                    Border::None => {
                        let background = Primitive::Rectangle {
                            bounds,
                            linear_color,
                        };

                        vec![background, content]
                    },
                    Border::DoubleCornerless { inner, outer } => {
                        let border_size = f32::from(BORDER_SIZE)
                            .min(bounds.width / 4.0)
                            .min(bounds.height / 4.0);

                        let center = Primitive::Rectangle {
                            bounds: Rectangle {
                                x: bounds.x + border_size * 2.0,
                                y: bounds.y + border_size * 2.0,
                                width: bounds.width - border_size * 4.0,
                                height: bounds.height - border_size * 4.0,
                            },
                            linear_color,
                        };

                        let linear_color = srgba_to_linear(outer.map(|e| e as f32 / 255.0));
                        let top = Primitive::Rectangle {
                            bounds: Rectangle {
                                x: bounds.x + border_size,
                                y: bounds.y,
                                width: bounds.width - border_size * 2.0,
                                height: border_size,
                            },
                            linear_color,
                        };
                        let bottom = Primitive::Rectangle {
                            bounds: Rectangle {
                                x: bounds.x + border_size,
                                y: bounds.y + bounds.height - border_size,
                                width: bounds.width - border_size * 2.0,
                                height: border_size,
                            },
                            linear_color,
                        };
                        let left = Primitive::Rectangle {
                            bounds: Rectangle {
                                x: bounds.x,
                                y: bounds.y + border_size,
                                width: border_size,
                                height: bounds.height - border_size * 2.0,
                            },
                            linear_color,
                        };
                        let right = Primitive::Rectangle {
                            bounds: Rectangle {
                                x: bounds.x + bounds.width - border_size,
                                y: bounds.y + border_size,
                                width: border_size,
                                height: bounds.height - border_size * 2.0,
                            },
                            linear_color,
                        };

                        let linear_color = srgba_to_linear(inner.map(|e| e as f32 / 255.0));
                        let top_inner = Primitive::Rectangle {
                            bounds: Rectangle {
                                x: bounds.x + border_size,
                                y: bounds.y + border_size,
                                width: bounds.width - border_size * 2.0,
                                height: border_size,
                            },
                            linear_color,
                        };
                        let bottom_inner = Primitive::Rectangle {
                            bounds: Rectangle {
                                x: bounds.x + border_size,
                                y: bounds.y + bounds.height - border_size * 2.0,
                                width: bounds.width - border_size * 2.0,
                                height: border_size,
                            },
                            linear_color,
                        };
                        let left_inner = Primitive::Rectangle {
                            bounds: Rectangle {
                                x: bounds.x + border_size,
                                y: bounds.y + border_size * 2.0,
                                width: border_size,
                                height: bounds.height - border_size * 4.0,
                            },
                            linear_color,
                        };
                        let right_inner = Primitive::Rectangle {
                            bounds: Rectangle {
                                x: bounds.x + bounds.width - border_size * 2.0,
                                y: bounds.y + border_size * 2.0,
                                width: border_size,
                                height: bounds.height - border_size * 4.0,
                            },
                            linear_color,
                        };

                        vec![
                            center,
                            top,
                            bottom,
                            left,
                            right,
                            top_inner,
                            bottom_inner,
                            left_inner,
                            right_inner,
                            content,
                        ]
                    },
                    Border::Image { corner, edge } => {
                        let border_size = f32::from(BORDER_SIZE)
                            .min(bounds.width / 4.0)
                            .min(bounds.height / 4.0);

                        let center = Primitive::Rectangle {
                            bounds: Rectangle {
                                x: bounds.x + border_size,
                                y: bounds.y + border_size,
                                width: bounds.width - border_size * 2.0,
                                height: bounds.height - border_size * 2.0,
                            },
                            linear_color,
                        };

                        let color = Rgba::white();

                        let tl_corner = Primitive::Image {
                            handle: (*corner, Rotation::None),
                            bounds: Rectangle {
                                x: bounds.x,
                                y: bounds.y,
                                width: border_size,
                                height: border_size,
                            },
                            color,
                            source_rect: None,
                        };

                        let tr_corner = Primitive::Image {
                            handle: (*corner, Rotation::Cw90),
                            bounds: Rectangle {
                                x: bounds.x + bounds.width - border_size,
                                y: bounds.y,
                                width: border_size,
                                height: border_size,
                            },
                            color,
                            source_rect: None,
                        };

                        let bl_corner = Primitive::Image {
                            handle: (*corner, Rotation::Cw270),
                            bounds: Rectangle {
                                x: bounds.x,
                                y: bounds.y + bounds.height - border_size,
                                width: border_size,
                                height: border_size,
                            },
                            color,
                            source_rect: None,
                        };

                        let br_corner = Primitive::Image {
                            handle: (*corner, Rotation::Cw180),
                            bounds: Rectangle {
                                x: bounds.x + bounds.width - border_size,
                                y: bounds.y + bounds.height - border_size,
                                width: border_size,
                                height: border_size,
                            },
                            color,
                            source_rect: None,
                        };

                        let top_edge = Primitive::Image {
                            handle: (*edge, Rotation::None),
                            bounds: Rectangle {
                                x: bounds.x + border_size,
                                y: bounds.y,
                                width: bounds.width - 2.0 * border_size,
                                height: border_size,
                            },
                            color,
                            source_rect: None,
                        };

                        let bottom_edge = Primitive::Image {
                            handle: (*edge, Rotation::Cw180),
                            bounds: Rectangle {
                                x: bounds.x + border_size,
                                y: bounds.y + bounds.height - border_size,
                                width: bounds.width - 2.0 * border_size,
                                height: border_size,
                            },
                            color,
                            source_rect: None,
                        };

                        let left_edge = Primitive::Image {
                            handle: (*edge, Rotation::Cw270),
                            bounds: Rectangle {
                                x: bounds.x,
                                y: bounds.y + border_size,
                                width: border_size,
                                height: bounds.height - 2.0 * border_size,
                            },
                            color,
                            source_rect: None,
                        };

                        let right_edge = Primitive::Image {
                            handle: (*edge, Rotation::Cw90),
                            bounds: Rectangle {
                                x: bounds.x + bounds.width - border_size,
                                y: bounds.y + border_size,
                                width: border_size,
                                height: bounds.height - 2.0 * border_size,
                            },
                            color,
                            source_rect: None,
                        };

                        // Is this worth it as opposed to using a giant image? (Probably)
                        vec![
                            center,
                            tl_corner,
                            tr_corner,
                            bl_corner,
                            br_corner,
                            top_edge,
                            bottom_edge,
                            left_edge,
                            right_edge,
                            content,
                        ]
                    },
                };

                Primitive::Group { primitives }
            },
            Self::Style::None => content,
        };

        (prim, mouse_interaction)
    }
}