empty.rs

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