Skip to main content

laser_pdf/elements/
none.rs

1use crate::*;
2
3/// A null element that takes up no space.
4///
5/// It uses `None` as the size on both axes, meaning it will trigger collapsing in containers that
6/// support it, such as a `Column` with `collapse: true`. Collapsing means that for example the gaps
7/// before and after the element will be combined into one and if all elements in a container are
8/// collapsed, the container itself will also have a `None` size on the relevant axis.
9///
10/// This element is useful for conditional layouts where you may want to
11/// include an element or nothing at all based on some condition.
12pub struct NoneElement;
13
14impl Element for NoneElement {
15    fn first_location_usage(&self, _ctx: FirstLocationUsageCtx) -> FirstLocationUsage {
16        FirstLocationUsage::NoneHeight
17    }
18
19    fn measure(&self, _ctx: MeasureCtx) -> ElementSize {
20        ElementSize {
21            width: None,
22            height: None,
23        }
24    }
25
26    fn draw(&self, _ctx: DrawCtx) -> ElementSize {
27        ElementSize {
28            width: None,
29            height: None,
30        }
31    }
32}
33
34#[cfg(test)]
35mod tests {
36    use super::*;
37    use crate::test_utils::*;
38
39    #[test]
40    fn none_element() {
41        for output in ElementTestParams::default().run(&NoneElement) {
42            output.assert_size(ElementSize {
43                width: None,
44                height: None,
45            });
46
47            if let Some(b) = output.breakable {
48                b.assert_break_count(0)
49                    .assert_extra_location_min_height(None);
50            }
51        }
52    }
53}