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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
use std::collections::{BTreeSet, HashMap};

use abstutil::prettyprint_usize;
use geom::{Duration, Polygon, Time};
use sim::{TripEndpoint, TripID, TripMode};
use widgetry::table::{Col, Filter, Table};
use widgetry::{
    Color, EventCtx, Filler, GeomBatch, GfxCtx, Line, Outcome, Panel, Stash, State, TabController,
    Text, Toggle, Widget,
};

use super::generic_trip_table::{open_trip_transition, preview_trip};
use super::selector::RectangularSelector;
use super::DashTab;
use crate::app::{App, Transition};
use crate::common::{checkbox_per_mode, cmp_duration_shorter, color_for_mode};

pub struct TripTable {
    tab: DashTab,
    table_tabs: TabController,
    panel: Panel,
    finished_trips_table: Table<App, FinishedTrip, Filters>,
    cancelled_trips_table: Table<App, CancelledTrip, Filters>,
    unfinished_trips_table: Table<App, UnfinishedTrip, Filters>,
    recompute_filters: bool,
}

impl TripTable {
    pub fn new(ctx: &mut EventCtx, app: &App) -> Self {
        let mut tabs = TabController::new("trips_tabs");

        let (finished, unfinished) = app.primary.sim.num_trips();
        let mut cancelled = 0;
        // TODO Can we avoid iterating through this again?
        for (_, _, _, maybe_dt) in &app.primary.sim.get_analytics().finished_trips {
            if maybe_dt.is_none() {
                cancelled += 1;
            }
        }
        let total = finished + cancelled + unfinished;

        let percent = |x: usize| -> f64 {
            if total > 0 {
                (x as f64) / (total as f64) * 100.0
            } else {
                0.0
            }
        };

        let finished_trips_btn = ctx
            .style()
            .btn_tab
            .text(format!(
                "Finished Trips: {} ({:.1}%)",
                prettyprint_usize(finished),
                percent(finished),
            ))
            .tooltip("Finished Trips");

        let finished_trips_table = make_table_finished_trips(app);
        let finished_trips_content = Widget::col(vec![
            finished_trips_table.render(ctx, app),
            Filler::square_width(ctx, 0.15)
                .named("preview")
                .centered_horiz(),
        ]);
        tabs.push_tab(finished_trips_btn, finished_trips_content);

        let cancelled_trips_table = make_table_cancelled_trips(app);
        let cancelled_trips_btn = ctx
            .style()
            .btn_tab
            .text(format!("Cancelled Trips: {}", prettyprint_usize(cancelled)))
            .tooltip("Cancelled Trips");
        let cancelled_trips_content = Widget::col(vec![
            cancelled_trips_table.render(ctx, app),
            Filler::square_width(ctx, 0.15)
                .named("preview")
                .centered_horiz(),
        ]);
        tabs.push_tab(cancelled_trips_btn, cancelled_trips_content);

        let unfinished_trips_table = make_table_unfinished_trips(app);
        let unfinished_trips_btn = ctx
            .style()
            .btn_tab
            .text(format!(
                "Unfinished Trips: {} ({:.1}%)",
                prettyprint_usize(unfinished),
                percent(unfinished)
            ))
            .tooltip("Unfinished Trips");
        let unfinished_trips_content = Widget::col(vec![
            unfinished_trips_table.render(ctx, app),
            Filler::square_width(ctx, 0.15)
                .named("preview")
                .centered_horiz(),
        ]);
        tabs.push_tab(unfinished_trips_btn, unfinished_trips_content);

        let panel = Panel::new_builder(Widget::col(vec![
            DashTab::TripTable.picker(ctx, app),
            tabs.build_widget(ctx),
        ]))
        .exact_size_percent(90, 90)
        .build(ctx);

        Self {
            tab: DashTab::TripTable,
            table_tabs: tabs,
            panel,
            finished_trips_table,
            cancelled_trips_table,
            unfinished_trips_table,
            recompute_filters: false,
        }
    }
}

impl State<App> for TripTable {
    fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition {
        match self.panel.event(ctx) {
            Outcome::Clicked(x) => {
                if self.table_tabs.active_tab_idx() == 0 && self.finished_trips_table.clicked(&x) {
                    self.finished_trips_table
                        .replace_render(ctx, app, &mut self.panel);
                } else if self.table_tabs.active_tab_idx() == 1
                    && self.cancelled_trips_table.clicked(&x)
                {
                    self.cancelled_trips_table
                        .replace_render(ctx, app, &mut self.panel);
                } else if self.table_tabs.active_tab_idx() == 2
                    && self.unfinished_trips_table.clicked(&x)
                {
                    self.unfinished_trips_table
                        .replace_render(ctx, app, &mut self.panel);
                } else if let Ok(idx) = x.parse::<usize>() {
                    return open_trip_transition(app, idx);
                } else if x == "close" {
                    return Transition::Pop;
                } else if self.table_tabs.handle_action(ctx, &x, &mut self.panel) {
                    // if true, tabs handled the action
                } else if x == "filter starts" {
                    // Set the recompute_filters bit, so we re-apply the filters when the selector
                    // state is done.
                    self.recompute_filters = true;
                    return Transition::Push(RectangularSelector::new_state(
                        ctx,
                        self.panel.stash("starts_in"),
                    ));
                } else if x == "filter ends" {
                    self.recompute_filters = true;
                    return Transition::Push(RectangularSelector::new_state(
                        ctx,
                        self.panel.stash("ends_in"),
                    ));
                } else {
                    unreachable!("unhandled action: {}", x)
                }
            }
            Outcome::Changed(_) => {
                if let Some(t) = self.tab.transition(ctx, app, &self.panel) {
                    return t;
                }

                self.recompute_filters = true;
            }
            _ => {}
        }

        if self.recompute_filters {
            self.recompute_filters = false;
            match self.table_tabs.active_tab_idx() {
                0 => {
                    self.finished_trips_table.panel_changed(&self.panel);
                    self.finished_trips_table
                        .replace_render(ctx, app, &mut self.panel);
                }
                1 => {
                    self.cancelled_trips_table.panel_changed(&self.panel);
                    self.cancelled_trips_table
                        .replace_render(ctx, app, &mut self.panel);
                }
                2 => {
                    self.unfinished_trips_table.panel_changed(&self.panel);
                    self.unfinished_trips_table
                        .replace_render(ctx, app, &mut self.panel);
                }
                _ => unreachable!(),
            }
        }

        Transition::Keep
    }

    fn draw(&self, g: &mut GfxCtx, app: &App) {
        self.panel.draw(g);
        let mut batch = GeomBatch::new();
        if self.panel.has_widget("starts_in") {
            if let Some(p) = self.panel.clone_stashed("starts_in") {
                batch.push(Color::RED.alpha(0.5), p);
            }
            if let Some(p) = self.panel.clone_stashed("ends_in") {
                batch.push(Color::BLUE.alpha(0.5), p);
            }
        }
        preview_trip(g, app, &self.panel, batch);
    }
}

struct FinishedTrip {
    id: TripID,
    mode: TripMode,
    modified: bool,
    capped: bool,
    start: TripEndpoint,
    end: TripEndpoint,
    departure: Time,
    duration_after: Duration,
    duration_before: Duration,
    waiting: Duration,
    percent_waiting: usize,
}

struct CancelledTrip {
    id: TripID,
    mode: TripMode,
    departure: Time,
    start: TripEndpoint,
    end: TripEndpoint,
    duration_before: Duration,
    reason: String,
}

struct UnfinishedTrip {
    id: TripID,
    mode: TripMode,
    departure: Time,
    duration_before: Duration,
    // TODO Estimated wait time?
}

struct Filters {
    modes: BTreeSet<TripMode>,
    off_map_starts: bool,
    off_map_ends: bool,
    starts_in: Option<Polygon>,
    ends_in: Option<Polygon>,
    unmodified_trips: bool,
    modified_trips: bool,
    uncapped_trips: bool,
    capped_trips: bool,
}

fn produce_raw_data(app: &App) -> (Vec<FinishedTrip>, Vec<CancelledTrip>) {
    let mut finished = Vec::new();
    let mut cancelled = Vec::new();

    // Only make one pass through prebaked data
    let trip_times_before = if app.has_prebaked().is_some() {
        let mut times = HashMap::new();
        for (_, id, _, maybe_dt) in &app.prebaked().finished_trips {
            if let Some(dt) = maybe_dt {
                times.insert(*id, *dt);
            }
        }
        Some(times)
    } else {
        None
    };

    let sim = &app.primary.sim;
    for (_, id, mode, maybe_duration_after) in &sim.get_analytics().finished_trips {
        let trip = sim.trip_info(*id);
        let duration_before = if let Some(ref times) = trip_times_before {
            times.get(id).cloned()
        } else {
            Some(Duration::ZERO)
        };

        if maybe_duration_after.is_none() || duration_before.is_none() {
            let reason = trip.cancellation_reason.clone().unwrap_or_else(|| {
                "trip succeeded now, but not before the current proposal".to_string()
            });
            cancelled.push(CancelledTrip {
                id: *id,
                mode: *mode,
                departure: trip.departure,
                start: trip.start,
                end: trip.end,
                duration_before: duration_before.unwrap_or(Duration::ZERO),
                reason,
            });
            continue;
        };

        let (_, waiting, _) = sim.finished_trip_details(*id).unwrap();

        let duration_after = maybe_duration_after.unwrap();
        finished.push(FinishedTrip {
            id: *id,
            mode: *mode,
            departure: trip.departure,
            modified: trip.modified,
            capped: trip.capped,
            start: trip.start,
            end: trip.end,
            duration_after,
            duration_before: duration_before.unwrap(),
            waiting,
            percent_waiting: (100.0 * waiting / duration_after) as usize,
        });
    }

    (finished, cancelled)
}

fn make_table_finished_trips(app: &App) -> Table<App, FinishedTrip, Filters> {
    let (finished, _) = produce_raw_data(app);
    let any_congestion_caps = app
        .primary
        .map
        .all_zones()
        .iter()
        .any(|z| z.restrictions.cap_vehicles_per_hour.is_some());
    let filter: Filter<App, FinishedTrip, Filters> = Filter {
        state: Filters {
            modes: TripMode::all().into_iter().collect(),
            off_map_starts: true,
            off_map_ends: true,
            starts_in: None,
            ends_in: None,
            unmodified_trips: true,
            modified_trips: true,
            uncapped_trips: true,
            capped_trips: true,
        },
        to_controls: Box::new(move |ctx, app, state| {
            Widget::col(vec![
                checkbox_per_mode(ctx, app, &state.modes),
                Widget::row(vec![
                    Toggle::switch(ctx, "starting off-map", None, state.off_map_starts),
                    Toggle::switch(ctx, "ending off-map", None, state.off_map_ends),
                    ctx.style().btn_plain.text("filter starts").build_def(ctx),
                    ctx.style().btn_plain.text("filter ends").build_def(ctx),
                    Stash::new_widget("starts_in", state.starts_in.clone()),
                    Stash::new_widget("ends_in", state.ends_in.clone()),
                    if app.primary.has_modified_trips {
                        Toggle::switch(
                            ctx,
                            "trips unmodified by experiment",
                            None,
                            state.unmodified_trips,
                        )
                    } else {
                        Widget::nothing()
                    },
                    if app.primary.has_modified_trips {
                        Toggle::switch(
                            ctx,
                            "trips modified by experiment",
                            None,
                            state.modified_trips,
                        )
                    } else {
                        Widget::nothing()
                    },
                    if any_congestion_caps {
                        Toggle::switch(
                            ctx,
                            "trips not affected by congestion caps",
                            None,
                            state.uncapped_trips,
                        )
                    } else {
                        Widget::nothing()
                    },
                    if any_congestion_caps {
                        Toggle::switch(
                            ctx,
                            "trips affected by congestion caps",
                            None,
                            state.capped_trips,
                        )
                    } else {
                        Widget::nothing()
                    },
                ]),
            ])
        }),
        from_controls: Box::new(|panel| {
            let mut modes = BTreeSet::new();
            for m in TripMode::all() {
                if panel.is_checked(m.ongoing_verb()) {
                    modes.insert(m);
                }
            }
            Filters {
                modes,
                off_map_starts: panel.is_checked("starting off-map"),
                off_map_ends: panel.is_checked("ending off-map"),
                starts_in: panel.clone_stashed("starts_in"),
                ends_in: panel.clone_stashed("ends_in"),
                unmodified_trips: panel
                    .maybe_is_checked("trips unmodified by experiment")
                    .unwrap_or(true),
                modified_trips: panel
                    .maybe_is_checked("trips modified by experiment")
                    .unwrap_or(true),
                uncapped_trips: panel
                    .maybe_is_checked("trips not affected by congestion caps")
                    .unwrap_or(true),
                capped_trips: panel
                    .maybe_is_checked("trips affected by congestion caps")
                    .unwrap_or(true),
            }
        }),
        apply: Box::new(|state, x, app| {
            if !state.modes.contains(&x.mode) {
                return false;
            }
            if !state.off_map_starts && matches!(x.start, TripEndpoint::Border(_)) {
                return false;
            }
            if !state.off_map_ends && matches!(x.end, TripEndpoint::Border(_)) {
                return false;
            }
            if let Some(ref polygon) = state.starts_in {
                if !polygon.contains_pt(x.start.pt(&app.primary.map)) {
                    return false;
                }
            }
            if let Some(ref polygon) = state.ends_in {
                if !polygon.contains_pt(x.end.pt(&app.primary.map)) {
                    return false;
                }
            }
            if !state.unmodified_trips && !x.modified {
                return false;
            }
            if !state.modified_trips && x.modified {
                return false;
            }
            if !state.uncapped_trips && !x.capped {
                return false;
            }
            if !state.capped_trips && x.capped {
                return false;
            }
            true
        }),
    };

    let mut table = Table::new(
        "finished_trips_table",
        finished,
        Box::new(|x| x.id.0.to_string()),
        "Percent waiting",
        filter,
    );
    table.static_col("Trip ID", Box::new(|x| x.id.0.to_string()));
    if app.primary.has_modified_trips {
        table.static_col(
            "Modified",
            Box::new(|x| {
                if x.modified {
                    "Yes".to_string()
                } else {
                    "No".to_string()
                }
            }),
        );
    }
    if any_congestion_caps {
        table.static_col(
            "Capped",
            Box::new(|x| {
                if x.capped {
                    "Yes".to_string()
                } else {
                    "No".to_string()
                }
            }),
        );
    }
    table.column(
        "Type",
        Box::new(|ctx, app, x| {
            Text::from(Line(x.mode.ongoing_verb()).fg(color_for_mode(app, x.mode))).render(ctx)
        }),
        Col::Static,
    );
    table.column(
        "Departure",
        Box::new(|ctx, _, x| Text::from(x.departure.ampm_tostring()).render(ctx)),
        Col::Sortable(Box::new(|rows| rows.sort_by_key(|x| x.departure))),
    );
    table.column(
        "Duration",
        Box::new(|ctx, app, x| Text::from(x.duration_after.to_string(&app.opts.units)).render(ctx)),
        Col::Sortable(Box::new(|rows| rows.sort_by_key(|x| x.duration_after))),
    );

    if app.has_prebaked().is_some() {
        table.column(
            "Comparison",
            Box::new(|ctx, app, x| {
                Text::from_all(cmp_duration_shorter(
                    app,
                    x.duration_after,
                    x.duration_before,
                ))
                .render(ctx)
            }),
            Col::Sortable(Box::new(|rows| {
                rows.sort_by_key(|x| x.duration_after - x.duration_before)
            })),
        );
        table.column(
            "Normalized",
            Box::new(|ctx, _, x| {
                Text::from(match x.duration_after.cmp(&x.duration_before) {
                    std::cmp::Ordering::Equal => "same".to_string(),
                    std::cmp::Ordering::Less => {
                        format!(
                            "{}% faster",
                            (100.0 * (1.0 - (x.duration_after / x.duration_before))) as usize
                        )
                    }
                    std::cmp::Ordering::Greater => {
                        format!(
                            "{}% slower ",
                            (100.0 * ((x.duration_after / x.duration_before) - 1.0)) as usize
                        )
                    }
                })
                .render(ctx)
            }),
            Col::Sortable(Box::new(|rows| {
                rows.sort_by_key(|x| (100.0 * (x.duration_after / x.duration_before)) as isize)
            })),
        );
    }

    table.column(
        "Time spent waiting",
        Box::new(|ctx, app, x| Text::from(x.waiting.to_string(&app.opts.units)).render(ctx)),
        Col::Sortable(Box::new(|rows| rows.sort_by_key(|x| x.waiting))),
    );
    table.column(
        "Percent waiting",
        Box::new(|ctx, _, x| Text::from(x.percent_waiting.to_string()).render(ctx)),
        Col::Sortable(Box::new(|rows| rows.sort_by_key(|x| x.percent_waiting))),
    );

    table
}

fn make_table_cancelled_trips(app: &App) -> Table<App, CancelledTrip, Filters> {
    let (_, cancelled) = produce_raw_data(app);
    // Reuse the same filters, but ignore modified and capped trips
    let filter: Filter<App, CancelledTrip, Filters> = Filter {
        state: Filters {
            modes: TripMode::all().into_iter().collect(),
            off_map_starts: true,
            off_map_ends: true,
            starts_in: None,
            ends_in: None,
            unmodified_trips: true,
            modified_trips: true,
            uncapped_trips: true,
            capped_trips: true,
        },
        to_controls: Box::new(move |ctx, app, state| {
            Widget::col(vec![
                checkbox_per_mode(ctx, app, &state.modes),
                Widget::row(vec![
                    Toggle::switch(ctx, "starting off-map", None, state.off_map_starts),
                    Toggle::switch(ctx, "ending off-map", None, state.off_map_ends),
                ]),
            ])
        }),
        from_controls: Box::new(|panel| {
            let mut modes = BTreeSet::new();
            for m in TripMode::all() {
                if panel.is_checked(m.ongoing_verb()) {
                    modes.insert(m);
                }
            }
            Filters {
                modes,
                off_map_starts: panel.is_checked("starting off-map"),
                off_map_ends: panel.is_checked("ending off-map"),
                starts_in: None,
                ends_in: None,
                unmodified_trips: true,
                modified_trips: true,
                uncapped_trips: true,
                capped_trips: true,
            }
        }),
        apply: Box::new(|state, x, app| {
            if !state.modes.contains(&x.mode) {
                return false;
            }
            if !state.off_map_starts && matches!(x.start, TripEndpoint::Border(_)) {
                return false;
            }
            if !state.off_map_ends && matches!(x.end, TripEndpoint::Border(_)) {
                return false;
            }
            if let Some(ref polygon) = state.starts_in {
                if !polygon.contains_pt(x.start.pt(&app.primary.map)) {
                    return false;
                }
            }
            if let Some(ref polygon) = state.ends_in {
                if !polygon.contains_pt(x.end.pt(&app.primary.map)) {
                    return false;
                }
            }
            true
        }),
    };

    let mut table = Table::new(
        "cancelled_trips_table",
        cancelled,
        Box::new(|x| x.id.0.to_string()),
        "Departure",
        filter,
    );
    table.static_col("Trip ID", Box::new(|x| x.id.0.to_string()));
    table.column(
        "Type",
        Box::new(|ctx, app, x| {
            Text::from(Line(x.mode.ongoing_verb()).fg(color_for_mode(app, x.mode))).render(ctx)
        }),
        Col::Static,
    );
    table.column(
        "Departure",
        Box::new(|ctx, _, x| Text::from(x.departure.ampm_tostring()).render(ctx)),
        Col::Sortable(Box::new(|rows| rows.sort_by_key(|x| x.departure))),
    );
    if app.has_prebaked().is_some() {
        table.column(
            "Estimated duration",
            Box::new(|ctx, app, x| {
                Text::from(x.duration_before.to_string(&app.opts.units)).render(ctx)
            }),
            Col::Sortable(Box::new(|rows| rows.sort_by_key(|x| x.duration_before))),
        );
    }
    table.static_col("Reason", Box::new(|x| x.reason.clone()));

    table
}

fn make_table_unfinished_trips(app: &App) -> Table<App, UnfinishedTrip, Filters> {
    // Only make one pass through prebaked data
    let trip_times_before = if app.has_prebaked().is_some() {
        let mut times = HashMap::new();
        for (_, id, _, maybe_dt) in &app.prebaked().finished_trips {
            if let Some(dt) = maybe_dt {
                times.insert(*id, *dt);
            }
        }
        Some(times)
    } else {
        None
    };
    let mut unfinished = Vec::new();
    for (id, trip) in app.primary.sim.all_trip_info() {
        if app.primary.sim.finished_trip_details(id).is_none() {
            let duration_before = trip_times_before
                .as_ref()
                .and_then(|times| times.get(&id))
                .cloned()
                .unwrap_or(Duration::ZERO);
            unfinished.push(UnfinishedTrip {
                id,
                mode: trip.mode,
                departure: trip.departure,
                duration_before,
            });
        }
    }

    // Reuse the same filters, but ignore modified and capped trips
    let filter: Filter<App, UnfinishedTrip, Filters> = Filter {
        state: Filters {
            modes: TripMode::all().into_iter().collect(),
            off_map_starts: true,
            off_map_ends: true,
            starts_in: None,
            ends_in: None,
            unmodified_trips: true,
            modified_trips: true,
            uncapped_trips: true,
            capped_trips: true,
        },
        to_controls: Box::new(move |ctx, app, state| checkbox_per_mode(ctx, app, &state.modes)),
        from_controls: Box::new(|panel| {
            let mut modes = BTreeSet::new();
            for m in TripMode::all() {
                if panel.is_checked(m.ongoing_verb()) {
                    modes.insert(m);
                }
            }
            Filters {
                modes,
                off_map_starts: true,
                off_map_ends: true,
                starts_in: None,
                ends_in: None,
                unmodified_trips: true,
                modified_trips: true,
                uncapped_trips: true,
                capped_trips: true,
            }
        }),
        apply: Box::new(|state, x, _| {
            if !state.modes.contains(&x.mode) {
                return false;
            }
            true
        }),
    };

    let mut table = Table::new(
        "unfinished_trips_table",
        unfinished,
        Box::new(|x| x.id.0.to_string()),
        "Departure",
        filter,
    );
    table.static_col("Trip ID", Box::new(|x| x.id.0.to_string()));
    table.column(
        "Type",
        Box::new(|ctx, app, x| {
            Text::from(Line(x.mode.ongoing_verb()).fg(color_for_mode(app, x.mode))).render(ctx)
        }),
        Col::Static,
    );
    table.column(
        "Departure",
        Box::new(|ctx, _, x| Text::from(x.departure.ampm_tostring()).render(ctx)),
        Col::Sortable(Box::new(|rows| rows.sort_by_key(|x| x.departure))),
    );
    if app.has_prebaked().is_some() {
        table.column(
            "Estimated duration",
            Box::new(|ctx, app, x| {
                Text::from(x.duration_before.to_string(&app.opts.units)).render(ctx)
            }),
            Col::Sortable(Box::new(|rows| rows.sort_by_key(|x| x.duration_before))),
        );
    }

    table
}