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(&mut self, bounds: RectF, size: &mut Self::LayoutState, cx: &mut PaintContext) {
31        let bounds = RectF::new(bounds.origin(), *size);
32        cx.scene.push_stacking_context(None);
33        self.child.paint(bounds.origin(), cx);
34        cx.scene.pop_stacking_context();
35    }
36
37    fn dispatch_event(
38        &mut self,
39        event: &Event,
40        _: RectF,
41        _: &mut Self::LayoutState,
42        _: &mut Self::PaintState,
43        cx: &mut EventContext,
44    ) -> bool {
45        self.child.dispatch_event(event, cx)
46    }
47
48    fn debug(
49        &self,
50        _: RectF,
51        _: &Self::LayoutState,
52        _: &Self::PaintState,
53        cx: &DebugContext,
54    ) -> serde_json::Value {
55        self.child.debug(cx)
56    }
57}