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
use crate::{Alignment, Bind, Context, Event, Id, IdPath, IdentifyExt, Node, View};

/// A view that lays out its children on top of each other.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Overlay<T, O> {
    wrapped: T,
    alignment: Alignment,
    overlayed: O,
}

impl<T, O> Overlay<T, O> {
    pub fn new(wrapped: T, alignment: Alignment, overlayed: O) -> Self {
        Self {
            wrapped,
            alignment,
            overlayed,
        }
    }
}

impl<T, O> Bind for Overlay<T, O> where T: Bind, O: Bind {}

impl<T, O> View for Overlay<T, O> where T: View, O: View {
    fn fire(&self, event: &Event, id_path: &IdPath) {
        if let Some(head) = id_path.head() {
            match head {
                Id::Index(0) => self.wrapped.fire(event, &id_path.tail()),
                Id::Index(1) => self.overlayed.fire(event, &id_path.tail()),
                i => panic!("Cannot fire event for child id {} on Overlay which only has two childs", i)
            }
        }
    }

    fn render(&self, context: &Context) -> Node {
        Node::Overlay {
            wrapped: Box::new(self.wrapped.render(&context.child(0)).identify(0)),
            alignment: self.alignment,
            overlayed: Box::new(self.overlayed.render(&context.child(1)).identify(1)),
        }
    }
}