empty.rs

 1use std::ops::Range;
 2
 3use crate::{
 4    geometry::{
 5        rect::RectF,
 6        vector::{vec2f, Vector2F},
 7    },
 8    json::{json, ToJson},
 9    ViewContext,
10};
11use crate::{Element, SizeConstraint};
12
13#[derive(Default)]
14pub struct Empty {
15    collapsed: bool,
16}
17
18impl Empty {
19    pub fn new() -> Self {
20        Self::default()
21    }
22
23    pub fn collapsed(mut self) -> Self {
24        self.collapsed = true;
25        self
26    }
27}
28
29impl<V: 'static> Element<V> for Empty {
30    type LayoutState = ();
31    type PaintState = ();
32
33    fn layout(
34        &mut self,
35        constraint: SizeConstraint,
36        _: &mut V,
37        _: &mut ViewContext<V>,
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 V,
59        _: &mut ViewContext<V>,
60    ) -> Self::PaintState {
61    }
62
63    fn rect_for_text_range(
64        &self,
65        _: Range<usize>,
66        _: RectF,
67        _: RectF,
68        _: &Self::LayoutState,
69        _: &Self::PaintState,
70        _: &V,
71        _: &ViewContext<V>,
72    ) -> Option<RectF> {
73        None
74    }
75
76    fn debug(
77        &self,
78        bounds: RectF,
79        _: &Self::LayoutState,
80        _: &Self::PaintState,
81        _: &V,
82        _: &ViewContext<V>,
83    ) -> serde_json::Value {
84        json!({
85            "type": "Empty",
86            "bounds": bounds.to_json(),
87        })
88    }
89}