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
use super::super::super::widget::image;
use iced::Color;
use vek::Rgba;

#[derive(Clone, Copy)]
struct Background {
    default: image::Handle,
    hover: image::Handle,
    press: image::Handle,
    color: Rgba<u8>,
}

impl Background {
    fn new(image: image::Handle) -> Self {
        Self {
            default: image,
            hover: image,
            press: image,
            color: Rgba::white(),
        }
    }
}
// TODO: consider a different place for this
// Note: for now all buttons have an image background
#[derive(Clone, Copy)]
pub struct Style {
    background: Option<Background>,
    enabled_text: Color,
    disabled_text: Color,
}

impl Style {
    pub fn new(image: image::Handle) -> Self {
        Self {
            background: Some(Background::new(image)),
            ..Default::default()
        }
    }

    #[must_use]
    pub fn hover_image(mut self, image: image::Handle) -> Self {
        self.background = Some(match self.background {
            Some(mut background) => {
                background.hover = image;
                background
            },
            None => Background::new(image),
        });
        self
    }

    #[must_use]
    pub fn press_image(mut self, image: image::Handle) -> Self {
        self.background = Some(match self.background {
            Some(mut background) => {
                background.press = image;
                background
            },
            None => Background::new(image),
        });
        self
    }

    // TODO: this needs to be refactored since the color isn't used if there is no
    // background
    #[must_use]
    pub fn image_color(mut self, color: Rgba<u8>) -> Self {
        if let Some(background) = &mut self.background {
            background.color = color;
        }
        self
    }

    #[must_use]
    pub fn text_color(mut self, color: Color) -> Self {
        self.enabled_text = color;
        self
    }

    #[must_use]
    pub fn disabled_text_color(mut self, color: Color) -> Self {
        self.disabled_text = color;
        self
    }

    pub fn disabled(&self) -> (Option<(image::Handle, Rgba<u8>)>, Color) {
        (
            self.background.as_ref().map(|b| (b.default, b.color)),
            self.disabled_text,
        )
    }

    pub fn pressed(&self) -> (Option<(image::Handle, Rgba<u8>)>, Color) {
        (
            self.background.as_ref().map(|b| (b.press, b.color)),
            self.enabled_text,
        )
    }

    pub fn hovered(&self) -> (Option<(image::Handle, Rgba<u8>)>, Color) {
        (
            self.background.as_ref().map(|b| (b.hover, b.color)),
            self.enabled_text,
        )
    }

    pub fn active(&self) -> (Option<(image::Handle, Rgba<u8>)>, Color) {
        (
            self.background.as_ref().map(|b| (b.default, b.color)),
            self.enabled_text,
        )
    }
}

impl Default for Style {
    fn default() -> Self {
        Self {
            background: None,
            enabled_text: Color::WHITE,
            disabled_text: Color::from_rgb(0.5, 0.5, 0.5),
        }
    }
}