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
use crate::{
    math::{vec2, Rect, Vec2},
    ui::{ElementState, Id, Layout, Ui, UiContent},
};

pub struct ComboBox<'a, 'b, 'c> {
    id: Id,
    label: &'a str,
    variants: &'b [&'c str],
    ratio: f32,
}

impl<'a, 'b, 'c> ComboBox<'a, 'b, 'c> {
    pub fn new(id: Id, variants: &'b [&'c str]) -> ComboBox<'a, 'b, 'c> {
        ComboBox {
            id,
            label: "",
            variants,
            ratio: 0.5,
        }
    }

    pub fn label<'x>(self, label: &'x str) -> ComboBox<'x, 'b, 'c> {
        ComboBox {
            id: self.id,
            variants: self.variants,
            label,
            ratio: self.ratio,
        }
    }

    pub fn ratio(self, ratio: f32) -> Self {
        Self { ratio, ..self }
    }
    pub fn ui(self, ui: &mut Ui, data: &mut usize) -> usize {
        let mut context = ui.get_active_window_context();

        let line_height = context.style.label_style.font_size;

        let size = vec2(
            context.window.cursor.area.w - context.style.margin * 2. - context.window.cursor.ident,
            (line_height as f32 + 4.).max(19.),
        );

        let combobox_area_w = size.x * self.ratio - 15.;

        let pos = context.window.cursor.fit(size, Layout::Vertical);

        let active_area_w = size.x * self.ratio;

        let text_measures = {
            let font = &mut *context.style.label_style.font.lock().unwrap();
            let font_size = context.style.label_style.font_size;

            context
                .window
                .painter
                .label_size(self.label, None, font, font_size)
        };

        let clickable_rect = Rect::new(pos.x, pos.y, active_area_w, size.y);

        let (hovered, _) = context.register_click_intention(clickable_rect);

        let state = context
            .storage_any
            .get_or_default::<bool>(hash!(self.id, "combobox_state"));

        if context.window.was_active == false {
            *state = false;
        }

        context.window.painter.draw_element_background(
            &context.style.combobox_style,
            pos,
            vec2(combobox_area_w, size.y),
            ElementState {
                focused: context.focused,
                hovered,
                clicked: hovered && context.input.is_mouse_down,
                ..Default::default()
            },
        );

        context.window.painter.draw_element_content(
            &context.style.label_style,
            pos,
            vec2(combobox_area_w, size.y),
            &UiContent::Label((&*self.variants[*data]).into()),
            ElementState {
                focused: context.focused,
                hovered,
                clicked: hovered && context.input.is_mouse_down,
                selected: false,
            },
        );

        {
            context.window.painter.draw_element_label(
                &context.style.label_style,
                Vec2::new(pos.x + size.x * self.ratio, pos.y),
                self.label,
                ElementState {
                    focused: context.focused,
                    ..Default::default()
                },
            );
        }

        let modal_size = Vec2::new(active_area_w, self.variants.len() as f32 * size.y);
        let modal_rect = Rect::new(pos.x, pos.y + size.y, modal_size.x, modal_size.y);

        if *state == false && context.focused && hovered && context.input.click_down {
            *state = true;
        } else if *state
            && (context.input.escape
                || context.input.enter
                || (modal_rect.contains(context.input.mouse_position) == false
                    && context.input.click_down))
        {
            *state = false;
        }

        if *state {
            let context = ui.begin_modal(
                hash!("combobox", self.id),
                pos + Vec2::new(0., 20.),
                modal_size,
            );

            let state = context
                .storage_any
                .get_or_default::<bool>(hash!(self.id, "combobox_state"));

            for (i, variant) in self.variants.iter().enumerate() {
                let rect = Rect::new(
                    pos.x + 5.0,
                    pos.y + i as f32 * size.y + size.y,
                    active_area_w - 5.0,
                    size.y,
                );
                let hovered = rect.contains(context.input.mouse_position);

                let color = context.style.combobox_style.color(ElementState {
                    focused: context.focused,
                    hovered,
                    clicked: hovered && context.input.is_mouse_down,
                    selected: false,
                });

                context.window.painter.draw_rect(
                    rect, //context.style.combobox_variant_border(hovered, *data == i),
                    color,
                    // context
                    //     .style
                    //     .combobox_variant_background(hovered, *data == i),
                    color,
                );

                let font = &mut *context.style.label_style.font.lock().unwrap();
                let font_size = context.style.label_style.font_size;

                context.window.painter.draw_label(
                    variant,
                    Vec2::new(
                        pos.x + 7.,
                        pos.y + i as f32 * size.y + size.y + 2.0 + text_measures.offset_y,
                    ),
                    context.style.combobox_style.text_color,
                    font,
                    font_size,
                );

                if hovered && context.input.click_up {
                    *data = i;
                    *state = false;
                }
            }
            ui.end_modal();
        }

        *data
    }
}

impl Ui {
    pub fn combo_box<'a>(
        &mut self,
        id: Id,
        label: &str,
        variants: &[&str],
        data: impl Into<Option<&'a mut usize>>,
    ) -> usize {
        if let Some(r) = data.into() {
            ComboBox::new(id, variants).label(label).ui(self, r)
        } else {
            let data_id = hash!(id, "selected_variant");
            let mut selected_variant = { *self.get_any(data_id) };

            ComboBox::new(id, variants)
                .label(label)
                .ui(self, &mut selected_variant);

            *self.get_any(data_id) = selected_variant;

            selected_variant
        }
    }
}