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