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
use crate::{EventCtx, GfxCtx, Outcome, ScreenDims, ScreenPt, Widget, WidgetImpl, WidgetOutput};

pub struct Nothing {}

impl WidgetImpl for Nothing {
    fn get_dims(&self) -> ScreenDims {
        unreachable!()
    }

    fn set_pos(&mut self, _top_left: ScreenPt) {
        unreachable!()
    }

    fn event(&mut self, _: &mut EventCtx, _: &mut WidgetOutput) {
        unreachable!()
    }
    fn draw(&self, _g: &mut GfxCtx) {
        unreachable!()
    }
}

pub struct Container {
    // false means column
    pub is_row: bool,
    pub members: Vec<Widget>,
}

impl Container {
    pub fn new(is_row: bool, mut members: Vec<Widget>) -> Container {
        members.retain(|w| !w.widget.is::<Nothing>());
        Container { is_row, members }
    }
}

impl WidgetImpl for Container {
    fn get_dims(&self) -> ScreenDims {
        // TODO This impl isn't correct, but it works for the one use case of
        // get_width_for_forcing.
        let mut width: f64 = 0.0;
        for x in &self.members {
            width = width.max(x.get_width_for_forcing());
        }
        ScreenDims::new(width, 0.0)
    }
    fn set_pos(&mut self, _top_left: ScreenPt) {
        unreachable!()
    }

    fn event(&mut self, ctx: &mut EventCtx, output: &mut WidgetOutput) {
        for w in &mut self.members {
            if let Some(id) = ctx.focus_owned_by.as_ref() {
                // Container is the only place that needs to actually enforce focus. If a Panel
                // consists of only one top-level widget, then there's nothing else to conflict
                // with focus. And in the common case, we have a tree of Containers, with
                // non-Container leaves.
                if w.id.as_ref() != Some(id) && !w.widget.is::<Container>() {
                    continue;
                }
            }
            w.widget.event(ctx, output);
            // If the widget produced an outcome, short-circuit.
            if !matches!(output.outcome, Outcome::Nothing) {
                return;
            }
        }
    }

    fn draw(&self, g: &mut GfxCtx) {
        for w in &self.members {
            w.draw(g);
        }
    }
}