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, 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 rect_for_text_range(
63        &self,
64        _: Range<usize>,
65        _: RectF,
66        _: RectF,
67        _: &Self::LayoutState,
68        _: &Self::PaintState,
69        _: &MeasurementContext,
70    ) -> Option<RectF> {
71        None
72    }
73
74    fn debug(
75        &self,
76        bounds: RectF,
77        _: &Self::LayoutState,
78        _: &Self::PaintState,
79        _: &DebugContext,
80    ) -> serde_json::Value {
81        json!({
82            "type": "Empty",
83            "bounds": bounds.to_json(),
84        })
85    }
86}