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
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
use std::collections::HashSet;

use taffy::geometry::{Rect, Size};
use taffy::layout::AvailableSpace;
use taffy::node::{Node, Taffy};
use taffy::style::{
    AlignItems, Dimension, FlexDirection, FlexWrap, JustifyContent, PositionType, Style,
};

use abstutil::CloneableAny;
use geom::{CornerRadii, Distance, Percent, Polygon};

use crate::widgets::containers::{Container, Nothing};
pub use crate::widgets::panel::{Panel, PanelBuilder, PanelDims};
use crate::{
    Button, Choice, Color, DeferDraw, Drawable, Dropdown, EventCtx, GeomBatch, GfxCtx, JustDraw,
    OutlineStyle, ScreenDims, ScreenPt, ScreenRectangle, Text, Toggle,
};

pub mod autocomplete;
pub mod button;
pub mod compare_times;
pub mod containers;
pub mod drag_drop;
pub mod dropdown;
pub mod fan_chart;
pub mod filler;
pub mod image;
pub mod just_draw;
pub mod line_plot;
pub mod menu;
mod panel;
pub mod persistent_split;
pub mod plots;
pub mod scatter_plot;
pub mod slider;
pub mod spinner;
pub mod stash;
pub mod table;
pub mod tabs;
pub mod text_box;
pub mod toggle;

pub const DEFAULT_CORNER_RADIUS: f64 = 5.0;

/// Create a new widget by implementing this trait. You can instantiate your widget by calling
/// `Widget::new(Box::new(instance of your new widget))`, which gives you the usual style options.
pub trait WidgetImpl: downcast_rs::Downcast {
    /// What width and height does the widget occupy? If this changes, be sure to set
    /// `redo_layout` to true in `event`.
    fn get_dims(&self) -> ScreenDims;
    /// Your widget's top left corner should be here. Handle mouse events and draw appropriately.
    fn set_pos(&mut self, top_left: ScreenPt);
    /// Your chance to react to an event. Any side effects outside of this widget are communicated
    /// through the output.
    fn event(&mut self, ctx: &mut EventCtx, output: &mut WidgetOutput);
    /// Draw the widget. Be sure to draw relative to the top-left specified by `set_pos`.
    fn draw(&self, g: &mut GfxCtx);
    /// If a new Panel is being created to replace an older one, all widgets have the chance to
    /// preserve state from the previous version.
    fn can_restore(&self) -> bool {
        false
    }
    /// Restore state from the previous version of this widget, with the same ID. Implementors must
    /// downcast.
    fn restore(&mut self, _: &mut EventCtx, _prev: &dyn WidgetImpl) {
        unreachable!()
    }
}

/// The result of a Panel handling an event
pub enum Outcome {
    /// An action was done
    Clicked(String),
    /// An action was done, with custom data. The caller must cast to the proper type.
    ClickCustom(Box<dyn CloneableAny>),
    /// A dropdown, checkbox, spinner, etc changed values. The name of the changed widget is
    /// returned, but not the value, since its type is generic.
    Changed(String),
    /// On a DragDrop widget, a member was clicked on and released. Its position may have changed.
    /// (name, orig_idx, new_idx)
    DragDropReleased(String, usize, usize),
    /// Some named widget currently holds focus
    Focused(String),
    /// Nothing happened
    Nothing,
}

impl Outcome {
    pub(crate) fn describe(&self) -> String {
        match self {
            Outcome::Clicked(x) => format!("Outcome::Clicked({x})"),
            Outcome::ClickCustom(_) => format!("Outcome::ClickCustom(???)"),
            Outcome::Changed(x) => format!("Outcome::Changed({x})"),
            Outcome::DragDropReleased(x, _, _) => format!("Outcome::DragDropReleased({x}, ...)"),
            Outcome::Focused(x) => format!("Outcome::Focused({x})"),
            Outcome::Nothing => format!("Outcome::Nothing"),
        }
    }
}

/// When an action happens through a button-like widget, what data is plumbed back?
pub enum ClickOutcome {
    Label(String),
    Custom(Box<dyn CloneableAny>),
}

pub struct WidgetOutput {
    /// This widget changed dimensions, so recalculate layout.
    pub redo_layout: bool,
    /// This widget produced an Outcome, and event handling should immediately stop. Most widgets
    /// shouldn't set this.
    pub outcome: Outcome,
}

impl WidgetOutput {
    pub fn new() -> WidgetOutput {
        WidgetOutput {
            redo_layout: false,
            outcome: Outcome::Nothing,
        }
    }
}

downcast_rs::impl_downcast!(WidgetImpl);

pub struct Widget {
    // TODO pub just for Container. Just move that here?
    pub(crate) widget: Box<dyn WidgetImpl>,
    layout: LayoutStyle,
    pub(crate) rect: ScreenRectangle,
    bg: Option<Drawable>,
    // to_geom forces this one to happen
    bg_batch: Option<GeomBatch>,
    id: Option<String>,
}

#[derive(Debug, Clone, Copy)]
pub enum CornerRounding {
    CornerRadii(CornerRadii),
    FullyRounded,
    NoRounding,
}

impl std::convert::From<f64> for CornerRounding {
    fn from(uniform: f64) -> Self {
        CornerRounding::CornerRadii(uniform.into())
    }
}

impl std::convert::From<CornerRadii> for CornerRounding {
    fn from(radii: CornerRadii) -> Self {
        CornerRounding::CornerRadii(radii)
    }
}

impl Default for CornerRounding {
    fn default() -> Self {
        CornerRounding::CornerRadii(CornerRadii::default())
    }
}

struct LayoutStyle {
    bg_color: Option<Color>,
    // (thickness, color)
    outline: Option<(f64, Color)>,
    corner_rounding: CornerRounding,
    style: Style,
}

// Layouting
// TODO Maybe I just want margin, not padding. And maybe more granular controls per side. And to
// apply margin to everything in a row or column.
// TODO Row and columns feel backwards when using them.
impl Widget {
    pub fn centered(mut self) -> Widget {
        self.layout.style.align_items = AlignItems::Center;
        self.layout.style.justify_content = JustifyContent::SpaceAround;
        self
    }

    pub fn centered_horiz(self) -> Widget {
        Widget::row(vec![self]).centered()
    }

    pub fn centered_vert(self) -> Widget {
        Widget::col(vec![self]).centered()
    }

    pub fn centered_cross(mut self) -> Widget {
        self.layout.style.align_items = AlignItems::Center;
        self
    }

    pub fn evenly_spaced(mut self) -> Widget {
        self.layout.style.justify_content = JustifyContent::SpaceBetween;
        self
    }

    pub fn fill_width(mut self) -> Widget {
        self.layout.style.size.width = Dimension::Percent(1.0);
        self
    }
    pub fn fill_height(mut self) -> Widget {
        self.layout.style.size.height = Dimension::Percent(1.0);
        self
    }

    /// This one is really weird. percent_width should be LESS than the max_size_percent given to
    /// the overall Panel, otherwise weird things happen.
    /// Only makes sense for rows/columns.
    pub fn flex_wrap(mut self, ctx: &EventCtx, width: Percent) -> Widget {
        self.layout.style.size = Size {
            width: Dimension::Points((ctx.canvas.window_width * width.inner()) as f32),
            height: Dimension::Undefined,
        };
        self.layout.style.flex_wrap = FlexWrap::Wrap;
        self.layout.style.justify_content = JustifyContent::SpaceAround;
        self
    }
    /// Like flex_wrap, but doesn't horizontally space out widgets on the same row.
    pub fn flex_wrap_no_inner_spacing(mut self, ctx: &EventCtx, width: Percent) -> Widget {
        self.layout.style.size = Size {
            width: Dimension::Points((ctx.canvas.window_width * width.inner()) as f32),
            height: Dimension::Undefined,
        };
        self.layout.style.flex_wrap = FlexWrap::Wrap;
        self
    }
    /// Only for rows/columns. Used to force table columns to line up.
    pub fn force_width(mut self, width: f64) -> Widget {
        self.layout.style.size.width = Dimension::Points(width as f32);
        self
    }
    pub fn force_width_window_pct(mut self, ctx: &EventCtx, width: Percent) -> Widget {
        self.layout.style.size.width =
            Dimension::Points((ctx.canvas.window_width * width.inner()) as f32);
        self
    }
    pub fn force_width_parent_pct(mut self, width: f64) -> Widget {
        self.layout.style.size.width = Dimension::Percent(width as f32);
        self
    }

    /// Needed for force_width.
    pub fn get_width_for_forcing(&self) -> f64 {
        self.widget.get_dims().width
    }

    pub fn bg(mut self, color: Color) -> Widget {
        self.layout.bg_color = Some(color);
        self
    }

    /// Callers have to adjust padding too, probably
    pub fn outline(mut self, style: OutlineStyle) -> Widget {
        self.layout.outline = Some(style);
        self
    }

    pub fn corner_rounding<R: Into<CornerRounding>>(mut self, value: R) -> Widget {
        self.layout.corner_rounding = value.into();
        self
    }

    /// Things like padding don't work on many widgets, so just make a convenient way to wrap in a
    /// row/column first
    pub fn container(self) -> Widget {
        Widget::row(vec![self])
    }

    pub fn section(self, ctx: &EventCtx) -> Widget {
        self.bg(ctx.style().section_bg)
            .padding(16)
            .outline(ctx.style().section_outline)
    }

    pub fn tab_body(self, ctx: &EventCtx) -> Widget {
        let mut tab_body =
            self.bg(ctx.style().section_bg)
                .padding(16)
                .corner_rounding(CornerRadii {
                    top_left: 0.0,
                    top_right: DEFAULT_CORNER_RADIUS,
                    bottom_left: DEFAULT_CORNER_RADIUS,
                    bottom_right: DEFAULT_CORNER_RADIUS,
                });

        // really short tab bodies look out of place in the panels
        tab_body.layout.style.min_size.height = Dimension::Points(200.0);
        tab_body
    }

    // TODO Maybe panic if we call this on a non-container
    pub fn padding<I: Into<EdgeInsets>>(mut self, insets: I) -> Widget {
        let insets = insets.into();
        self.layout.style.padding = Rect::from(insets);
        self
    }

    pub fn padding_top(mut self, pixels: usize) -> Widget {
        self.layout.style.padding.top = Dimension::Points(pixels as f32);
        self
    }

    pub fn padding_left(mut self, pixels: usize) -> Widget {
        self.layout.style.padding.left = Dimension::Points(pixels as f32);
        self
    }

    pub fn padding_bottom(mut self, pixels: usize) -> Widget {
        self.layout.style.padding.bottom = Dimension::Points(pixels as f32);
        self
    }

    pub fn padding_right(mut self, pixels: usize) -> Widget {
        self.layout.style.padding.right = Dimension::Points(pixels as f32);
        self
    }

    pub fn margin<I: Into<EdgeInsets>>(mut self, insets: I) -> Widget {
        let insets = insets.into();
        self.layout.style.margin = Rect::from(insets);
        self
    }

    pub fn margin_above(mut self, pixels: usize) -> Widget {
        self.layout.style.margin.top = Dimension::Points(pixels as f32);
        self
    }
    pub fn margin_below(mut self, pixels: usize) -> Widget {
        self.layout.style.margin.bottom = Dimension::Points(pixels as f32);
        self
    }
    pub fn margin_left(mut self, pixels: usize) -> Widget {
        self.layout.style.margin.left = Dimension::Points(pixels as f32);
        self
    }
    pub fn margin_right(mut self, pixels: usize) -> Widget {
        self.layout.style.margin.right = Dimension::Points(pixels as f32);
        self
    }
    pub fn margin_horiz(mut self, pixels: usize) -> Widget {
        self.layout.style.margin.left = Dimension::Points(pixels as f32);
        self.layout.style.margin.right = Dimension::Points(pixels as f32);
        self
    }
    pub fn margin_vert(mut self, pixels: usize) -> Widget {
        self.layout.style.margin.top = Dimension::Points(pixels as f32);
        self.layout.style.margin.bottom = Dimension::Points(pixels as f32);
        self
    }

    pub fn align_left(mut self) -> Widget {
        self.layout.style.margin.right = Dimension::Auto;
        self
    }
    pub fn align_right(mut self) -> Widget {
        self.layout.style.margin = Rect {
            left: Dimension::Auto,
            right: Dimension::Undefined,
            top: Dimension::Undefined,
            bottom: Dimension::Undefined,
        };
        self
    }
    pub fn align_bottom(mut self) -> Widget {
        self.layout.style.margin = Rect {
            left: Dimension::Undefined,
            right: Dimension::Undefined,
            top: Dimension::Auto,
            bottom: Dimension::Undefined,
        };
        self
    }
    /// This doesn't count against the entire container
    pub fn align_vert_center(mut self) -> Widget {
        self.layout.style.margin = Rect {
            left: Dimension::Undefined,
            right: Dimension::Undefined,
            top: Dimension::Auto,
            bottom: Dimension::Auto,
        };
        self
    }

    fn abs(mut self, x: f64, y: f64) -> Widget {
        self.layout.style.position_type = PositionType::Absolute;
        self.layout.style.position = Rect {
            left: Dimension::Points(x as f32),
            right: Dimension::Undefined,
            top: Dimension::Points(y as f32),
            bottom: Dimension::Undefined,
        };
        self
    }

    pub fn named<I: Into<String>>(mut self, id: I) -> Widget {
        self.id = Some(id.into());
        self
    }

    /// If the argument is true, don't actually create this widget. May be more readable than an
    /// if/else block.
    pub fn hide(self, x: bool) -> Widget {
        if x {
            Widget::nothing()
        } else {
            self
        }
    }
}

// Convenient?? constructors
impl Widget {
    pub(crate) fn new(widget: Box<dyn WidgetImpl>) -> Widget {
        Widget {
            widget,
            layout: LayoutStyle {
                bg_color: None,
                outline: None,
                corner_rounding: CornerRounding::from(DEFAULT_CORNER_RADIUS),
                style: Style {
                    ..Default::default()
                },
            },
            rect: ScreenRectangle::placeholder(),
            bg: None,
            bg_batch: None,
            id: None,
        }
    }

    // TODO Change this to Dropdown::widget
    pub fn dropdown<T: 'static + PartialEq + Clone + std::fmt::Debug, I: AsRef<str>>(
        ctx: &EventCtx,
        label: I,
        default_value: T,
        choices: Vec<Choice<T>>,
    ) -> Widget {
        let label = label.as_ref();
        Widget::new(Box::new(Dropdown::new(
            ctx,
            label,
            default_value,
            choices,
            false,
        )))
        .named(label)
    }

    /// Creates a row with the specified widgets. No margins or other layouting is applied.
    pub fn custom_row(widgets: Vec<Widget>) -> Widget {
        Widget::new(Box::new(Container::new(true, widgets)))
    }

    /// Creates a row with the specified widgets. Every member gets a default horizontal margin.
    pub fn row(widgets: Vec<Widget>) -> Widget {
        Widget::evenly_spaced_row(10, widgets)
    }

    /// Creates a row with the specified widgets, with a `spacing` sized margin between members
    pub fn evenly_spaced_row(spacing: usize, widgets: Vec<Widget>) -> Widget {
        let mut new = Vec::new();
        let len = widgets.len();
        // TODO Time for that is_last iterator?
        for (idx, w) in widgets.into_iter().enumerate() {
            if idx == len - 1 {
                new.push(w);
            } else {
                new.push(w.margin_right(spacing));
            }
        }
        Widget::new(Box::new(Container::new(true, new)))
    }

    /// Creates a column with the specified widgets. No margins or other layouting is applied.
    pub fn custom_col(widgets: Vec<Widget>) -> Widget {
        Widget::new(Box::new(Container::new(false, widgets)))
    }

    /// Creates a column with the specified widgets, with a `spacing` sized margin between members
    pub fn evenly_spaced_col(spacing: usize, widgets: Vec<Widget>) -> Widget {
        let mut new = Vec::new();
        let len = widgets.len();
        // TODO Time for that is_last iterator?
        for (idx, w) in widgets.into_iter().enumerate() {
            if idx == len - 1 {
                new.push(w);
            } else {
                new.push(w.margin_below(spacing));
            }
        }
        Widget::new(Box::new(Container::new(false, new)))
    }

    /// Creates a column with the specified widgets. Every member gets a default vertical margin.
    pub fn col(widgets: Vec<Widget>) -> Widget {
        Self::evenly_spaced_col(10, widgets)
    }

    pub fn nothing() -> Widget {
        Widget::new(Box::new(Nothing {}))
    }

    // Also returns the hitbox of the entire widget
    pub fn into_geom(
        mut self,
        ctx: &EventCtx,
        exact_pct_width: Option<f64>,
    ) -> (GeomBatch, Polygon) {
        if let Some(w) = exact_pct_width {
            // TODO 35 is a sad magic number. By default, Panels have padding of 16, so assuming
            // this geometry is going in one of those, it makes sense to subtract 32. But that still
            // caused some scrolling in a test, so snip away a few more pixels.
            self.layout.style.min_size.width =
                Dimension::Points((w * ctx.canvas.window_width) as f32 - 35.0);
        }

        // Pretend we're in a Panel and basically copy recompute_layout
        {
            let mut taffy = Taffy::new();
            let root = taffy
                .new_with_children(
                    Style {
                        ..Default::default()
                    },
                    &[],
                )
                .unwrap();

            let mut nodes = vec![];
            self.get_flexbox(root, &mut taffy, &mut nodes);
            nodes.reverse();

            let container_size = Size {
                width: AvailableSpace::MaxContent,
                height: AvailableSpace::MaxContent,
            };
            taffy.compute_layout(root, container_size).unwrap();

            self.apply_flexbox(&taffy, &mut nodes, 0.0, 0.0, (0.0, 0.0), ctx, true, true);
            assert!(nodes.is_empty());
        }

        // Now build one big batch from all of the geometry, which now has the correct top left
        // position.
        let hitbox = self.rect.to_polygon();
        let mut batch = GeomBatch::new();
        self.consume_geometry(&mut batch);
        batch.autocrop_dims = false;
        (batch, hitbox)
    }

    pub fn horiz_separator(ctx: &EventCtx, pct_container_width: f64) -> Widget {
        GeomBatch::from(vec![(Color::CLEAR, Polygon::rectangle(0.1, 2.0))])
            .into_widget(ctx)
            .container()
            .bg(ctx.style().section_outline.1)
            .force_width_parent_pct(pct_container_width)
            .centered_horiz()
    }

    pub fn vert_separator(ctx: &EventCtx, height_px: f64) -> Widget {
        GeomBatch::from(vec![(
            ctx.style().section_outline.1,
            Polygon::rectangle(2.0, height_px),
        )])
        .into_widget(ctx)
    }

    // TODO Clean up other uses
    pub fn vertical_separator(ctx: &EventCtx) -> Widget {
        let thickness = 3.0;
        GeomBatch::from(vec![(Color::CLEAR, Polygon::rectangle(thickness, 0.1))])
            .into_widget(ctx)
            .container()
            .bg(ctx.style().section_outline.1)
    }

    pub fn placeholder(ctx: &EventCtx, label: &str) -> Widget {
        Text::new().into_widget(ctx).named(label)
    }
}

// Internals
impl Widget {
    pub(crate) fn draw(&self, g: &mut GfxCtx) {
        // Don't draw these yet; clipping is still in effect.
        if self.id == Some("horiz scrollbar".to_string())
            || self.id == Some("vert scrollbar".to_string())
        {
            return;
        }

        if let Some(ref bg) = self.bg {
            g.redraw_at(ScreenPt::new(self.rect.x1, self.rect.y1), bg);
        }

        self.widget.draw(g);
    }

    // Populate a flattened list of Nodes, matching the traversal order
    fn get_flexbox(&self, parent: Node, taffy: &mut Taffy, nodes: &mut Vec<Node>) {
        let mut style = self.layout.style;
        if let Some(container) = self.widget.downcast_ref::<Container>() {
            style.flex_direction = if container.is_row {
                FlexDirection::Row
            } else {
                FlexDirection::Column
            };
            let node = taffy.new_with_children(style, &[]).unwrap();
            nodes.push(node);
            for widget in &container.members {
                widget.get_flexbox(node, taffy, nodes);
            }
            taffy.add_child(parent, node).unwrap();
        } else {
            style.size = Size {
                width: Dimension::Points(self.widget.get_dims().width as f32),
                height: Dimension::Points(self.widget.get_dims().height as f32),
            };
            let node = taffy.new_with_children(style, &[]).unwrap();
            taffy.add_child(parent, node).unwrap();
            nodes.push(node);
        }
    }

    // TODO Clean up argument passing
    fn apply_flexbox(
        &mut self,
        taffy: &Taffy,
        nodes: &mut Vec<Node>,
        dx: f64,
        dy: f64,
        scroll_offset: (f64, f64),
        ctx: &EventCtx,
        recompute_layout: bool,
        defer_draw: bool,
    ) {
        let result = taffy.layout(nodes.pop().unwrap()).unwrap();
        let x: f64 = result.location.x.into();
        let y: f64 = result.location.y.into();
        let width: f64 = result.size.width.into();
        let height: f64 = result.size.height.into();
        // Don't scroll the scrollbars
        let top_left = if self.id == Some("horiz scrollbar".to_string())
            || self.id == Some("vert scrollbar".to_string())
        {
            ScreenPt::new(x, y)
        } else {
            ScreenPt::new(x + dx - scroll_offset.0, y + dy - scroll_offset.1)
        };
        self.rect = ScreenRectangle::top_left(top_left, ScreenDims::new(width, height));

        // Assume widgets don't dynamically change, so we just upload the background once.
        if (self.bg.is_none() || recompute_layout)
            && (self.layout.bg_color.is_some() || self.layout.outline.is_some())
        {
            let mut batch = GeomBatch::new();
            if let Some(color) = self.layout.bg_color {
                batch.push(
                    color,
                    match self.layout.corner_rounding {
                        CornerRounding::CornerRadii(corner_radii) => {
                            Polygon::rounded_rectangle(width, height, corner_radii)
                        }
                        CornerRounding::FullyRounded => Polygon::pill(width, height),
                        CornerRounding::NoRounding => Polygon::rectangle(width, height),
                    },
                );
            }

            if let Some((thickness, color)) = self.layout.outline {
                batch.push(
                    color,
                    match self.layout.corner_rounding {
                        CornerRounding::CornerRadii(corner_radii) => {
                            Polygon::rounded_rectangle(width, height, corner_radii)
                        }
                        CornerRounding::FullyRounded => Polygon::pill(width, height),
                        CornerRounding::NoRounding => Polygon::rectangle(width, height),
                    }
                    .to_outline(Distance::meters(thickness)),
                );
            }
            if defer_draw {
                self.bg_batch = Some(batch);
            } else {
                self.bg = Some(ctx.upload(batch));
            }
        }

        if let Some(container) = self.widget.downcast_mut::<Container>() {
            // layout() doesn't return absolute position; it's relative to the container.
            for widget in &mut container.members {
                widget.apply_flexbox(
                    taffy,
                    nodes,
                    x + dx,
                    y + dy,
                    scroll_offset,
                    ctx,
                    recompute_layout,
                    defer_draw,
                );
            }
        } else {
            self.widget.set_pos(top_left);
        }
    }

    fn get_all_click_actions(&self, actions: &mut HashSet<String>) {
        if let Some(btn) = self.widget.downcast_ref::<Button>() {
            if btn.is_enabled() {
                if actions.contains(&btn.action) {
                    panic!("Two buttons in one Panel both use action {}", btn.action);
                }
                actions.insert(btn.action.clone());
            }
        } else if let Some(container) = self.widget.downcast_ref::<Container>() {
            for w in &container.members {
                w.get_all_click_actions(actions);
            }
        }
    }

    fn currently_hovering(&self) -> Option<&String> {
        if let Some(btn) = self.widget.downcast_ref::<Button>() {
            if btn.hovering {
                return Some(&btn.action);
            }
        } else if let Some(checkbox) = self.widget.downcast_ref::<Toggle>() {
            if checkbox.btn.hovering {
                return Some(&checkbox.btn.action);
            }
        } else if let Some(container) = self.widget.downcast_ref::<Container>() {
            for w in &container.members {
                if let Some(a) = w.currently_hovering() {
                    return Some(a);
                }
            }
        }
        None
    }

    fn restore(&mut self, ctx: &mut EventCtx, prev: &Panel) {
        if let Some(container) = self.widget.downcast_mut::<Container>() {
            for w in &mut container.members {
                w.restore(ctx, prev);
            }
        } else if self.widget.can_restore() {
            if let Some(other) = prev.maybe_find_widget(self.id.as_ref().unwrap()) {
                self.widget.restore(ctx, other.widget.as_ref());
            }
        }
    }

    fn consume_geometry(mut self, batch: &mut GeomBatch) {
        if let Some(bg) = self.bg_batch.take() {
            batch.append(bg.translate(self.rect.x1, self.rect.y1));
        }

        if self.widget.is::<Container>() {
            // downcast() consumes, so we have to do the is() check first
            if let Ok(container) = self.widget.downcast::<Container>() {
                for w in container.members {
                    w.consume_geometry(batch);
                }
            }
        } else if let Ok(defer) = self.widget.downcast::<DeferDraw>() {
            batch.append(defer.batch.translate(defer.top_left.x, defer.top_left.y));
        } else {
            panic!("to_geom called on a widget tree that has something interactive");
        }
    }

    fn find(&self, name: &str) -> Option<&Widget> {
        if self.id == Some(name.to_string()) {
            return Some(self);
        }

        if let Some(container) = self.widget.downcast_ref::<Container>() {
            for widget in &container.members {
                if let Some(w) = widget.find(name) {
                    return Some(w);
                }
            }
        }

        None
    }
    fn find_mut(&mut self, name: &str) -> Option<&mut Widget> {
        if self.id == Some(name.to_string()) {
            return Some(self);
        }

        if let Some(container) = self.widget.downcast_mut::<Container>() {
            for widget in &mut container.members {
                if let Some(w) = widget.find_mut(name) {
                    return Some(w);
                }
            }
        }

        None
    }

    fn take(&mut self, name: &str) -> Option<Widget> {
        if self.id == Some(name.to_string()) {
            panic!("Can't take({}), it's a top-level widget", name);
        }

        if let Some(container) = self.widget.downcast_mut::<Container>() {
            let mut members = Vec::new();
            let mut found = None;
            for mut widget in container.members.drain(..) {
                if widget.id == Some(name.to_string()) {
                    found = Some(widget);
                } else if let Some(w) = widget.take(name) {
                    found = Some(w);
                    members.push(widget);
                } else {
                    members.push(widget);
                }
            }
            found
        } else {
            None
        }
    }

    pub(crate) fn take_just_draw(self) -> JustDraw {
        *self.widget.downcast::<JustDraw>().ok().unwrap()
    }
}

#[derive(Copy, Clone, Debug, Default, PartialEq)]
pub struct EdgeInsets {
    pub top: f64,
    pub left: f64,
    pub bottom: f64,
    pub right: f64,
}

impl EdgeInsets {
    pub fn zero() -> Self {
        EdgeInsets {
            top: 0.0,
            left: 0.0,
            bottom: 0.0,
            right: 0.0,
        }
    }

    pub fn uniform(inset: f64) -> Self {
        EdgeInsets {
            top: inset,
            left: inset,
            bottom: inset,
            right: inset,
        }
    }
}

impl From<usize> for EdgeInsets {
    fn from(uniform_size: usize) -> EdgeInsets {
        EdgeInsets::uniform(uniform_size as f64)
    }
}

impl From<f64> for EdgeInsets {
    fn from(uniform_size: f64) -> EdgeInsets {
        EdgeInsets::uniform(uniform_size)
    }
}

impl From<EdgeInsets> for Rect<Dimension> {
    fn from(insets: EdgeInsets) -> Rect<Dimension> {
        Rect {
            left: Dimension::Points(insets.left as f32),
            right: Dimension::Points(insets.right as f32),
            top: Dimension::Points(insets.top as f32),
            bottom: Dimension::Points(insets.bottom as f32),
        }
    }
}