overlay.rs

 1use crate::{
 2    geometry::{rect::RectF, vector::Vector2F},
 3    DebugContext, Element, ElementBox, Event, EventContext, LayoutContext, PaintContext,
 4    SizeConstraint,
 5};
 6
 7pub struct Overlay {
 8    child: ElementBox,
 9}
10
11impl Overlay {
12    pub fn new(child: ElementBox) -> Self {
13        Self { child }
14    }
15}
16
17impl Element for Overlay {
18    type LayoutState = Vector2F;
19    type PaintState = ();
20
21    fn layout(
22        &mut self,
23        constraint: SizeConstraint,
24        cx: &mut LayoutContext,
25    ) -> (Vector2F, Self::LayoutState) {
26        let size = self.child.layout(constraint, cx);
27        (Vector2F::zero(), size)
28    }
29
30    fn paint(
31        &mut self,
32        bounds: RectF,
33        _: RectF,
34        size: &mut Self::LayoutState,
35        cx: &mut PaintContext,
36    ) {
37        let bounds = RectF::new(bounds.origin(), *size);
38        cx.scene.push_stacking_context(None);
39        self.child.paint(bounds.origin(), bounds, cx);
40        cx.scene.pop_stacking_context();
41    }
42
43    fn dispatch_event(
44        &mut self,
45        event: &Event,
46        _: RectF,
47        _: &mut Self::LayoutState,
48        _: &mut Self::PaintState,
49        cx: &mut EventContext,
50    ) -> bool {
51        self.child.dispatch_event(event, cx)
52    }
53
54    fn debug(
55        &self,
56        _: RectF,
57        _: &Self::LayoutState,
58        _: &Self::PaintState,
59        cx: &DebugContext,
60    ) -> serde_json::Value {
61        self.child.debug(cx)
62    }
63}