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
use crate::{
Choice, DrawBaselayer, EventCtx, GfxCtx, Line, Menu, Outcome, Panel, State, Transition, Widget,
};
pub struct ChooseSomething<A, T> {
panel: Panel,
cb: Option<Box<dyn FnOnce(T, &mut EventCtx, &mut A) -> Transition<A>>>,
}
impl<A: 'static, T: 'static> ChooseSomething<A, T> {
pub fn new_state<I: Into<String>>(
ctx: &mut EventCtx,
query: I,
choices: Vec<Choice<T>>,
cb: Box<dyn FnOnce(T, &mut EventCtx, &mut A) -> Transition<A>>,
) -> Box<dyn State<A>> {
Box::new(ChooseSomething {
panel: Panel::new_builder(Widget::col(vec![
Widget::row(vec![
Line(query).small_heading().into_widget(ctx),
ctx.style().btn_close_widget(ctx),
]),
Menu::widget(ctx, choices).named("menu"),
]))
.build(ctx),
cb: Some(cb),
})
}
}
impl<A: 'static, T: 'static> State<A> for ChooseSomething<A, T> {
fn event(&mut self, ctx: &mut EventCtx, app: &mut A) -> Transition<A> {
match self.panel.event(ctx) {
Outcome::Clicked(x) => match x.as_ref() {
"close" => Transition::Pop,
_ => {
let data = self.panel.take_menu_choice::<T>("menu");
(self.cb.take().unwrap())(data, ctx, app)
}
},
_ => {
if ctx.normal_left_click() && ctx.canvas.get_cursor_in_screen_space().is_none() {
return Transition::Pop;
}
Transition::Keep
}
}
}
fn draw_baselayer(&self) -> DrawBaselayer {
DrawBaselayer::PreviousState
}
fn draw(&self, g: &mut GfxCtx, _: &A) {
super::grey_out_map(g);
self.panel.draw(g);
}
}