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
use geom::Pt2D;
use crate::{
Choice, EventCtx, GfxCtx, Key, Line, Outcome, ScreenDims, ScreenPt, ScreenRectangle, Style,
Text, Widget, WidgetImpl, WidgetOutput,
};
pub struct Menu<T> {
choices: Vec<Choice<T>>,
current_idx: usize,
pub(crate) top_left: ScreenPt,
dims: ScreenDims,
}
impl<T: 'static> Menu<T> {
pub fn widget(ctx: &EventCtx, choices: Vec<Choice<T>>) -> Widget {
Widget::new(Box::new(Self::new(ctx, choices)))
}
pub fn new(ctx: &EventCtx, choices: Vec<Choice<T>>) -> Self {
let mut m = Menu {
choices,
current_idx: 0,
top_left: ScreenPt::new(0.0, 0.0),
dims: ScreenDims::new(0.0, 0.0),
};
m.dims = m.calculate_txt(ctx.style()).dims(&ctx.prerender.assets);
m
}
pub fn take_current_choice(&mut self) -> T {
self.choices.remove(self.current_idx).data
}
fn calculate_txt(&self, style: &Style) -> Text {
let mut txt = Text::new();
for (idx, choice) in self.choices.iter().enumerate() {
let is_hovered = idx == self.current_idx;
let mut text_color = if is_hovered {
choice.fg.unwrap_or(style.btn_solid.fg)
} else {
choice.fg.unwrap_or(style.text_fg_color)
};
if choice.active {
if let Some(ref key) = choice.hotkey {
txt.add_appended(vec![
Line(key.describe()).fg(style.text_hotkey_color),
Line(format!(" - {}", choice.label)).fg(text_color),
]);
} else {
txt.add(Line(&choice.label).fg(text_color))
}
} else {
text_color = text_color.alpha(0.8);
if let Some(ref key) = choice.hotkey {
txt.add(Line(format!("{} - {}", key.describe(), choice.label)).fg(text_color));
} else {
txt.add(Line(&choice.label).fg(text_color));
}
}
if choice.tooltip.is_some() {
txt.append(Line(" (!)"));
}
if is_hovered {
txt.highlight_last_line(style.btn_solid.bg);
}
}
txt
}
}
impl<T: 'static> WidgetImpl for Menu<T> {
fn get_dims(&self) -> ScreenDims {
self.dims
}
fn set_pos(&mut self, top_left: ScreenPt) {
self.top_left = top_left;
}
fn event(&mut self, ctx: &mut EventCtx, output: &mut WidgetOutput) {
if self.choices.is_empty() {
return;
}
if ctx.redo_mouseover() {
if let Some(cursor) = ctx.canvas.get_cursor_in_screen_space() {
let mut top_left = self.top_left;
for idx in 0..self.choices.len() {
let rect = ScreenRectangle {
x1: top_left.x,
y1: top_left.y,
x2: top_left.x + self.dims.width,
y2: top_left.y + ctx.default_line_height(),
};
if rect.contains(cursor) && self.choices[idx].active {
self.current_idx = idx;
break;
}
top_left.y += ctx.default_line_height();
}
}
}
{
let choice = &self.choices[self.current_idx];
if ctx.normal_left_click() {
let mut top_left = self.top_left;
top_left.y += ctx.default_line_height() * (self.current_idx as f64);
let rect = ScreenRectangle {
x1: top_left.x,
y1: top_left.y,
x2: top_left.x + self.dims.width,
y2: top_left.y + ctx.default_line_height(),
};
if let Some(pt) = ctx.canvas.get_cursor_in_screen_space() {
if rect.contains(pt) && choice.active {
output.outcome = Outcome::Clicked(choice.label.clone());
return;
}
}
ctx.input.unconsume_event();
}
}
for (idx, choice) in self.choices.iter().enumerate() {
if !choice.active {
continue;
}
if ctx.input.pressed(choice.hotkey.clone()) {
self.current_idx = idx;
output.outcome = Outcome::Clicked(choice.label.clone());
return;
}
}
if ctx.input.pressed(Key::Enter) {
let choice = &self.choices[self.current_idx];
if choice.active {
output.outcome = Outcome::Clicked(choice.label.clone());
return;
} else {
return;
}
} else if ctx.input.pressed(Key::UpArrow) {
if self.current_idx > 0 {
self.current_idx -= 1;
}
} else if ctx.input.pressed(Key::DownArrow) {
if self.current_idx < self.choices.len() - 1 {
self.current_idx += 1;
}
}
}
fn draw(&self, g: &mut GfxCtx) {
if self.choices.is_empty() {
return;
}
let draw = g.upload(self.calculate_txt(g.style()).render(g));
g.fork(
Pt2D::new(0.0, 0.0),
self.top_left,
1.0,
Some(crate::drawing::MENU_Z),
);
g.redraw(&draw);
g.unfork();
if let Some(ref info) = self.choices[self.current_idx].tooltip {
let mut top_left = self.top_left;
top_left.y += g.default_line_height() * (self.current_idx as f64);
let rect = ScreenRectangle {
x1: top_left.x,
y1: top_left.y,
x2: top_left.x + self.dims.width,
y2: top_left.y + g.default_line_height(),
};
if let Some(pt) = g.canvas.get_cursor_in_screen_space() {
if rect.contains(pt) {
g.draw_mouse_tooltip(
Text::from(Line(info))
.inner_wrap_to_pct(0.3 * g.canvas.window_width, &g.prerender.assets),
);
}
}
}
}
}