veloren_voxygen/ui/ice/renderer/widget/
checkbox.rs

1use super::super::{super::Rotation, IcedRenderer, Primitive, style};
2use iced::{Rectangle, checkbox, mouse};
3
4impl checkbox::Renderer for IcedRenderer {
5    // TODO: what if this gets large enough to not be copied around?
6    type Style = style::checkbox::Style;
7
8    const DEFAULT_SIZE: u16 = 20;
9    const DEFAULT_SPACING: u16 = 15;
10
11    fn draw(
12        &mut self,
13        bounds: Rectangle,
14        is_checked: bool,
15        is_mouse_over: bool,
16        (label, _): Self::Output,
17        style: &Self::Style,
18    ) -> Self::Output {
19        let default_rect = || Primitive::Rectangle {
20            bounds,
21            linear_color: vek::Rgba::broadcast(1.0),
22        };
23
24        let background_image = match (is_checked, is_mouse_over) {
25            (true, true) => style.bg_hover_checked(),
26            (true, false) => style.bg_checked(),
27            (false, true) => style.bg_hover(),
28            (false, false) => style.bg_default(),
29        };
30
31        let background = background_image
32            .map(|image| Primitive::Image {
33                handle: (image, Rotation::None),
34                bounds,
35                color: vek::Rgba::broadcast(255),
36                source_rect: None,
37            })
38            .unwrap_or_else(default_rect);
39
40        (
41            Primitive::Group {
42                primitives: if is_checked {
43                    let check = style
44                        .check()
45                        .map(|image| Primitive::Image {
46                            handle: (image, Rotation::None),
47                            bounds,
48                            color: vek::Rgba::broadcast(255),
49                            source_rect: None,
50                        })
51                        .unwrap_or_else(default_rect);
52
53                    vec![background, check, label]
54                } else {
55                    vec![background, label]
56                },
57            },
58            if is_mouse_over {
59                mouse::Interaction::Pointer
60            } else {
61                mouse::Interaction::default()
62            },
63        )
64    }
65}