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
use abstutil::MultiMap;
use crate::{
Choice, EventCtx, GfxCtx, Menu, Outcome, ScreenDims, ScreenPt, TextBox, Widget, WidgetImpl,
WidgetOutput,
};
const NUM_SEARCH_RESULTS: usize = 10;
pub struct Autocomplete<T: Clone> {
choices: Vec<(String, Vec<T>)>,
tb: TextBox,
menu: Menu<()>,
current_line: String,
chosen_values: Option<Vec<T>>,
}
impl<T: 'static + Clone + Ord> Autocomplete<T> {
pub fn new(ctx: &mut EventCtx, raw_choices: Vec<(String, T)>) -> Widget {
let mut grouped: MultiMap<String, T> = MultiMap::new();
for (name, data) in raw_choices {
grouped.insert(name, data);
}
let choices: Vec<(String, Vec<T>)> = grouped
.consume()
.into_iter()
.map(|(k, v)| (k, v.into_iter().collect()))
.collect();
let mut a = Autocomplete {
choices,
tb: TextBox::new(ctx, 50, String::new(), true),
menu: Menu::<()>::new(ctx, Vec::new()).take_menu(),
current_line: String::new(),
chosen_values: None,
};
a.recalc_menu(ctx);
Widget::new(Box::new(a))
}
}
impl<T: 'static + Clone> Autocomplete<T> {
pub fn final_value(&self) -> Option<Vec<T>> {
self.chosen_values.clone()
}
fn recalc_menu(&mut self, ctx: &mut EventCtx) {
let mut choices = vec![Choice::new(
format!("anything matching \"{}\"", self.current_line),
(),
)];
let query = self.current_line.to_ascii_lowercase();
for (name, _) in &self.choices {
if name.to_ascii_lowercase().contains(&query) {
choices.push(Choice::new(name, ()));
}
if choices.len() == NUM_SEARCH_RESULTS {
break;
}
}
if choices.len() == 2 {
choices.remove(0);
}
self.menu = Menu::new(ctx, choices).take_menu();
}
}
impl<T: 'static + Clone> WidgetImpl for Autocomplete<T> {
fn get_dims(&self) -> ScreenDims {
let d1 = self.tb.get_dims();
let d2 = self.menu.get_dims();
ScreenDims::new(d1.width.max(d2.width), d1.height + d2.height)
}
fn set_pos(&mut self, top_left: ScreenPt) {
self.tb.set_pos(top_left);
self.menu.set_pos(ScreenPt::new(
top_left.x,
top_left.y + self.tb.get_dims().height,
));
}
fn event(&mut self, ctx: &mut EventCtx, output: &mut WidgetOutput) {
self.tb.event(ctx, output);
if self.tb.get_line() != self.current_line {
self.current_line = self.tb.get_line();
self.recalc_menu(ctx);
output.redo_layout = true;
} else {
let mut tmp_output = WidgetOutput::new();
self.menu.event(ctx, &mut tmp_output);
if let Outcome::Clicked(ref choice) = tmp_output.outcome {
if choice.starts_with("anything matching") {
let query = self.current_line.to_ascii_lowercase();
let mut matches = Vec::new();
for (name, choices) in &self.choices {
if name.to_ascii_lowercase().contains(&query) {
matches.extend(choices.clone());
}
}
self.chosen_values = Some(matches);
} else {
self.chosen_values = Some(
self.choices
.iter()
.find(|(name, _)| name == choice)
.unwrap()
.1
.clone(),
);
}
}
}
}
fn draw(&self, g: &mut GfxCtx) {
self.tb.draw(g);
self.menu.draw(g);
}
}