empty.rs

 1use crate::{
 2    geometry::{
 3        rect::RectF,
 4        vector::{vec2f, Vector2F},
 5    },
 6    json::{json, ToJson},
 7    DebugContext,
 8};
 9use crate::{Element, Event, EventContext, LayoutContext, PaintContext, SizeConstraint};
10
11pub struct Empty {
12    collapsed: bool,
13}
14
15impl Empty {
16    pub fn new() -> Self {
17        Self { collapsed: false }
18    }
19
20    pub fn collapsed(mut self) -> Self {
21        self.collapsed = true;
22        self
23    }
24}
25
26impl Element for Empty {
27    type LayoutState = ();
28    type PaintState = ();
29
30    fn layout(
31        &mut self,
32        constraint: SizeConstraint,
33        _: &mut LayoutContext,
34    ) -> (Vector2F, Self::LayoutState) {
35        let x = if constraint.max.x().is_finite() && !self.collapsed {
36            constraint.max.x()
37        } else {
38            constraint.min.x()
39        };
40        let y = if constraint.max.y().is_finite() && !self.collapsed {
41            constraint.max.y()
42        } else {
43            constraint.min.y()
44        };
45
46        (vec2f(x, y), ())
47    }
48
49    fn paint(
50        &mut self,
51        _: RectF,
52        _: RectF,
53        _: &mut Self::LayoutState,
54        _: &mut PaintContext,
55    ) -> Self::PaintState {
56    }
57
58    fn dispatch_event(
59        &mut self,
60        _: &Event,
61        _: RectF,
62        _: RectF,
63        _: &mut Self::LayoutState,
64        _: &mut Self::PaintState,
65        _: &mut EventContext,
66    ) -> bool {
67        false
68    }
69
70    fn debug(
71        &self,
72        bounds: RectF,
73        _: &Self::LayoutState,
74        _: &Self::PaintState,
75        _: &DebugContext,
76    ) -> serde_json::Value {
77        json!({
78            "type": "Empty",
79            "bounds": bounds.to_json(),
80        })
81    }
82}