nuit_core/compose/view/layout/
overlay.rs

1use nuit_derive::Bind;
2
3use crate::{Alignment, Context, Event, Id, IdPath, IdentifyExt, Node, View};
4
5/// A view that lays out its children on top of each other.
6#[derive(Debug, Clone, PartialEq, Eq, Bind)]
7pub struct Overlay<T, O> {
8    wrapped: T,
9    alignment: Alignment,
10    overlayed: O,
11}
12
13impl<T, O> Overlay<T, O> {
14    pub fn new(wrapped: T, alignment: Alignment, overlayed: O) -> Self {
15        Self {
16            wrapped,
17            alignment,
18            overlayed,
19        }
20    }
21}
22
23impl<T, O> View for Overlay<T, O> where T: View, O: View {
24    fn fire(&self, event: &Event, event_path: &IdPath, context: &Context) {
25        if let Some(head) = event_path.head() {
26            match head {
27                Id::Index(0) => self.wrapped.fire(event, &event_path.tail(), &context.child(0)),
28                Id::Index(1) => self.overlayed.fire(event, &event_path.tail(), &context.child(1)),
29                i => panic!("Cannot fire event for child id {} on Overlay which only has two childs", i)
30            }
31        }
32    }
33
34    fn render(&self, context: &Context) -> Node {
35        Node::Overlay {
36            wrapped: Box::new(self.wrapped.render(&context.child(0)).identify(0)),
37            alignment: self.alignment,
38            overlayed: Box::new(self.overlayed.render(&context.child(1)).identify(1)),
39        }
40    }
41}