map_gui/tools/
ui.rs

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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
//! Generic UI tools. Some of this should perhaps be lifted to widgetry.

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, PopupMsg};
use widgetry::{Color, EventCtx, GeomBatch, Line, State, Text, Toggle, Transition, Widget};

use crate::AppLike;

pub struct FilePicker;
type PickerOutput = (String, Vec<u8>);

impl FilePicker {
    // The callback gets the filename and file contents as bytes
    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<PickerOutput>>) -> 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<PickerOutput>>::new_state(
            ctx,
            Box::pin(async move {
                let mut builder = rfd::AsyncFileDialog::new();
                if let Some(dir) = start_dir {
                    builder = builder.set_directory(&dir);
                }
                // Can't get map() or and_then() to work with async
                let result = if let Some(handle) = builder.pick_file().await {
                    Some((handle.file_name(), handle.read().await))
                } else {
                    None
                };
                let wrap: Box<dyn Send + FnOnce(&A) -> Option<PickerOutput>> =
                    Box::new(move |_: &A| result);
                Ok(wrap)
            }),
            outer_progress_rx,
            inner_progress_rx,
            "Waiting for a file to be chosen",
            on_load,
        )
    }
}

pub struct FileSaver;

// TODO Lift to abstio, or just do AsRef<[u8]>
pub enum FileSaverContents {
    String(String),
    Bytes(Vec<u8>),
}

impl FileSaver {
    // The callback gets the filename
    pub fn new_state<A: 'static + AppLike>(
        ctx: &mut EventCtx,
        filename: String,
        start_dir: Option<String>,
        write: FileSaverContents,
        // TODO The double wrapped Result is silly, can't figure this out
        on_load: Box<dyn FnOnce(&mut EventCtx, &mut A, Result<Result<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, Result<String>>::new_state(
            ctx,
            Box::pin(async move {
                let mut builder = rfd::AsyncFileDialog::new().set_file_name(&filename);
                if let Some(dir) = start_dir {
                    builder = builder.set_directory(&dir);
                }

                #[cfg(not(target_arch = "wasm32"))]
                let result = if let Some(handle) = builder.save_file().await {
                    let path = handle.path().display().to_string();
                    // Both cases do AsRef<[u8]>
                    match write {
                        FileSaverContents::String(string) => fs_err::write(&path, string),
                        FileSaverContents::Bytes(bytes) => fs_err::write(&path, bytes),
                    }
                    .map(|_| path)
                    .map_err(|err| err.into())
                } else {
                    Err(anyhow!("no file chosen to save"))
                };

                #[cfg(target_arch = "wasm32")]
                let result = {
                    // Hide an unused warning
                    let _ = builder;

                    // TODO No file save dialog on WASM until
                    // https://developer.mozilla.org/en-US/docs/Web/API/Window/showSaveFilePicker
                    match write {
                        FileSaverContents::String(string) => {
                            abstio::write_file(filename.clone(), string)
                        }
                        FileSaverContents::Bytes(_bytes) => {
                            // We need to use write_file (which downloads a file), but encode
                            // binary data the right way
                            Err(anyhow!("writing binary files on web unsupported"))
                        }
                    }
                };

                let wrap: Box<dyn Send + FnOnce(&A) -> Result<String>> =
                    Box::new(move |_: &A| result);
                Ok(wrap)
            }),
            outer_progress_rx,
            inner_progress_rx,
            "Waiting for a file to be chosen",
            on_load,
        )
    }

    // Popup a success or failure message after
    pub fn with_default_messages<A: 'static + AppLike>(
        ctx: &mut EventCtx,
        filename: String,
        start_dir: Option<String>,
        write: FileSaverContents,
    ) -> Box<dyn State<A>> {
        Self::new_state(
            ctx,
            filename,
            start_dir,
            write,
            Box::new(|ctx, _, result| {
                Transition::Replace(match result {
                    Ok(Ok(path)) => PopupMsg::new_state(
                        ctx,
                        "File saved",
                        vec![format!("File saved to {path}")],
                    ),
                    Err(err) | Ok(Err(err)) => {
                        PopupMsg::new_state(ctx, "Save failed", vec![err.to_string()])
                    }
                })
            }),
        )
    }
}

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();
    // Background
    batch.push(
        bad_color,
        Polygon::rounded_rectangle(total_width, height, radius),
    );
    // Foreground
    if let Some(poly) = Polygon::maybe_rounded_rectangle(pct_green * total_width, height, radius) {
        batch.push(good_color, poly);
    }
    // Text
    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)
}

/// Shorter is better
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 => {}
    }
}

/// Shorter is better
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 => {}
    }
}

/// Less is better
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)
}