empty.rs

 1use std::ops::Range;
 2
 3use crate::{
 4    geometry::{
 5        rect::RectF,
 6        vector::{vec2f, Vector2F},
 7    },
 8    json::{json, ToJson},
 9    LayoutContext, PaintContext, SceneBuilder, View, 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: View> 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 LayoutContext<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        _: &mut SceneBuilder,
56        _: RectF,
57        _: RectF,
58        _: &mut Self::LayoutState,
59        _: &mut V,
60        _: &mut PaintContext<V>,
61    ) -> Self::PaintState {
62    }
63
64    fn rect_for_text_range(
65        &self,
66        _: Range<usize>,
67        _: RectF,
68        _: RectF,
69        _: &Self::LayoutState,
70        _: &Self::PaintState,
71        _: &V,
72        _: &ViewContext<V>,
73    ) -> Option<RectF> {
74        None
75    }
76
77    fn debug(
78        &self,
79        bounds: RectF,
80        _: &Self::LayoutState,
81        _: &Self::PaintState,
82        _: &V,
83        _: &ViewContext<V>,
84    ) -> serde_json::Value {
85        json!({
86            "type": "Empty",
87            "bounds": bounds.to_json(),
88        })
89    }
90}