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
#![warn(missing_docs)]
use egui::{Id, Response, Ui, Widget};
use std::hash::Hash;
pub struct DropDownBox<
'a,
F: FnMut(&mut Ui, &str) -> Response,
V: AsRef<str>,
I: Iterator<Item = V>,
> {
buf: &'a mut String,
popup_id: Id,
display: F,
it: I,
}
impl<'a, F: FnMut(&mut Ui, &str) -> Response, V: AsRef<str>, I: Iterator<Item = V>>
DropDownBox<'a, F, V, I>
{
pub fn from_iter(
it: impl IntoIterator<IntoIter = I>,
id_source: impl Hash,
buf: &'a mut String,
display: F,
) -> Self {
Self {
popup_id: Id::new(id_source),
it: it.into_iter(),
display,
buf,
}
}
}
impl<'a, F: FnMut(&mut Ui, &str) -> Response, V: AsRef<str>, I: Iterator<Item = V>> Widget
for DropDownBox<'a, F, V, I>
{
fn ui(self, ui: &mut Ui) -> Response {
let Self {
popup_id,
buf,
it,
mut display,
} = self;
let mut r = ui.text_edit_singleline(buf);
if r.gained_focus() {
ui.memory().open_popup(popup_id);
}
let mut changed = false;
egui::popup_below_widget(ui, popup_id, &r, |ui| {
for var in it {
let text = var.as_ref();
if !buf.is_empty() && !text.contains(&*buf) {
continue;
}
if display(ui, text).clicked() {
*buf = text.to_owned();
changed = true;
ui.memory().close_popup();
}
}
});
if changed {
r.mark_changed();
}
r
}
}