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
use std::cmp::Ordering;
use std::collections::BTreeSet;
use anyhow::Result;
use abstutil::prettyprint_usize;
use geom::{Distance, Duration, Polygon};
use synthpop::TripMode;
use widgetry::tools::FutureLoader;
use widgetry::{Color, EventCtx, GeomBatch, Line, State, Text, Toggle, Transition, Widget};
use crate::AppLike;
pub struct FilePicker;
impl FilePicker {
pub fn new_state<A: 'static + AppLike>(
ctx: &mut EventCtx,
start_dir: Option<String>,
on_load: Box<dyn FnOnce(&mut EventCtx, &mut A, Result<Option<String>>) -> Transition<A>>,
) -> Box<dyn State<A>> {
let (_, outer_progress_rx) = futures_channel::mpsc::channel(1);
let (_, inner_progress_rx) = futures_channel::mpsc::channel(1);
FutureLoader::<A, Option<String>>::new_state(
ctx,
Box::pin(async move {
let mut builder = rfd::AsyncFileDialog::new();
if let Some(dir) = start_dir {
builder = builder.set_directory(&dir);
}
let result = builder.pick_file().await.map(|x| {
#[cfg(not(target_arch = "wasm32"))]
{
x.path().display().to_string()
}
#[cfg(target_arch = "wasm32")]
{
format!("TODO rfd on wasm: {:?}", x)
}
});
let wrap: Box<dyn Send + FnOnce(&A) -> Option<String>> =
Box::new(move |_: &A| result);
Ok(wrap)
}),
outer_progress_rx,
inner_progress_rx,
"Waiting for a file to be chosen",
on_load,
)
}
}
pub fn percentage_bar(ctx: &EventCtx, txt: Text, pct_green: f64) -> Widget {
let bad_color = Color::RED;
let good_color = Color::GREEN;
let total_width = 450.0;
let height = 32.0;
let radius = 4.0;
let mut batch = GeomBatch::new();
batch.push(
bad_color,
Polygon::rounded_rectangle(total_width, height, radius),
);
if let Some(poly) = Polygon::maybe_rounded_rectangle(pct_green * total_width, height, radius) {
batch.push(good_color, poly);
}
let label = txt.render_autocropped(ctx);
let dims = label.get_dims();
batch.append(label.translate(10.0, height / 2.0 - dims.height / 2.0));
batch.into_widget(ctx)
}
pub fn cmp_dist(txt: &mut Text, app: &dyn AppLike, dist: Distance, shorter: &str, longer: &str) {
match dist.cmp(&Distance::ZERO) {
Ordering::Less => {
txt.add_line(
Line(format!(
"{} {}",
(-dist).to_string(&app.opts().units),
shorter
))
.fg(Color::GREEN),
);
}
Ordering::Greater => {
txt.add_line(
Line(format!("{} {}", dist.to_string(&app.opts().units), longer)).fg(Color::RED),
);
}
Ordering::Equal => {}
}
}
pub fn cmp_duration(
txt: &mut Text,
app: &dyn AppLike,
duration: Duration,
shorter: &str,
longer: &str,
) {
match duration.cmp(&Duration::ZERO) {
Ordering::Less => {
txt.add_line(
Line(format!(
"{} {}",
(-duration).to_string(&app.opts().units),
shorter
))
.fg(Color::GREEN),
);
}
Ordering::Greater => {
txt.add_line(
Line(format!(
"{} {}",
duration.to_string(&app.opts().units),
longer
))
.fg(Color::RED),
);
}
Ordering::Equal => {}
}
}
pub fn cmp_count(txt: &mut Text, before: usize, after: usize) {
match after.cmp(&before) {
std::cmp::Ordering::Equal => {
txt.add_line(Line("same"));
}
std::cmp::Ordering::Less => {
txt.add_appended(vec![
Line(prettyprint_usize(before - after)).fg(Color::GREEN),
Line(" less"),
]);
}
std::cmp::Ordering::Greater => {
txt.add_appended(vec![
Line(prettyprint_usize(after - before)).fg(Color::RED),
Line(" more"),
]);
}
}
}
pub fn color_for_mode(app: &dyn AppLike, m: TripMode) -> Color {
match m {
TripMode::Walk => app.cs().unzoomed_pedestrian,
TripMode::Bike => app.cs().unzoomed_bike,
TripMode::Transit => app.cs().unzoomed_bus,
TripMode::Drive => app.cs().unzoomed_car,
}
}
pub fn checkbox_per_mode(
ctx: &mut EventCtx,
app: &dyn AppLike,
current_state: &BTreeSet<TripMode>,
) -> Widget {
let mut filters = Vec::new();
for m in TripMode::all() {
filters.push(
Toggle::colored_checkbox(
ctx,
m.ongoing_verb(),
color_for_mode(app, m),
current_state.contains(&m),
)
.margin_right(24),
);
}
Widget::custom_row(filters)
}