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
use conrod_core::{
    image,
    widget::{self, button},
    widget_ids, Color, Positionable, Rect, Sizeable, Widget, WidgetCommon,
};

#[derive(Clone, WidgetCommon)]
pub struct ToggleButton {
    #[conrod(common_builder)]
    common: widget::CommonBuilder,
    value: bool,
    f_image: button::Image,
    t_image: button::Image,
}

widget_ids! {
    struct Ids {
        button,
    }
}

pub struct State {
    ids: Ids,
}

impl ToggleButton {
    pub fn new(value: bool, f_image_id: image::Id, t_image_id: image::Id) -> Self {
        ToggleButton {
            common: widget::CommonBuilder::default(),
            value,
            f_image: button::Image {
                image_id: f_image_id,
                hover_image_id: None,
                press_image_id: None,
                src_rect: None,
                color: button::ImageColor::None,
            },
            t_image: button::Image {
                image_id: t_image_id,
                hover_image_id: None,
                press_image_id: None,
                src_rect: None,
                color: button::ImageColor::None,
            },
        }
    }

    #[must_use]
    pub fn source_rectangle(mut self, rect: Rect) -> Self {
        self.f_image.src_rect = Some(rect);
        self.t_image.src_rect = Some(rect);
        self
    }

    #[must_use]
    pub fn image_colors(mut self, f_color: Color, t_color: Color) -> Self {
        self.f_image.color = button::ImageColor::Normal(f_color);
        self.t_image.color = button::ImageColor::Normal(t_color);
        self
    }

    #[must_use]
    pub fn image_color_with_feedback(mut self, f_color: Color, t_color: Color) -> Self {
        self.f_image.color = button::ImageColor::WithFeedback(f_color);
        self.t_image.color = button::ImageColor::WithFeedback(t_color);
        self
    }

    #[must_use]
    pub fn hover_images(mut self, f_id: image::Id, t_id: image::Id) -> Self {
        self.f_image.hover_image_id = Some(f_id);
        self.t_image.hover_image_id = Some(t_id);
        self
    }

    #[must_use]
    pub fn press_images(mut self, f_id: image::Id, t_id: image::Id) -> Self {
        self.f_image.press_image_id = Some(f_id);
        self.t_image.press_image_id = Some(t_id);
        self
    }
}

impl Widget for ToggleButton {
    type Event = bool;
    type State = State;
    type Style = ();

    fn init_state(&self, id_gen: widget::id::Generator) -> Self::State {
        State {
            ids: Ids::new(id_gen),
        }
    }

    fn style(&self) -> Self::Style {}

    fn update(self, args: widget::UpdateArgs<Self>) -> Self::Event {
        let widget::UpdateArgs {
            id,
            state,
            ui,
            rect,
            ..
        } = args;
        let ToggleButton {
            mut value,
            f_image,
            t_image,
            ..
        } = self;
        // Check if the button was clicked.
        // (Can't use `.set().was_clicked()` because we are changing the image after
        // setting the widget, which causes flickering since it takes a frame to
        // change after the mouse button is lifted).
        if ui.widget_input(state.ids.button).clicks().left().count() % 2 == 1 {
            value = !value;
        }
        let image = if value { t_image } else { f_image };
        let (x, y, w, h) = rect.x_y_w_h();
        // Button
        let mut button = button::Button::image(image.image_id)
            .x_y(x, y)
            .w_h(w, h)
            .parent(id);
        button.show = image;
        button.set(state.ids.button, ui);

        value
    }
}