1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use std::cell::OnceCell;

use crate::views::{Bounds, Event, Output, Point, Size, View};

pub struct Spacer(pub(crate) OnceCell<Size>);

impl Spacer {
    #[inline(always)]
    pub fn fill() -> Self {
        Spacer(OnceCell::new()) // a flexible spacer has no size (yet)
    }

    #[inline(always)]
    pub fn fixed(width: f32, height: f32) -> Self {
        let spacer = Self::fill();
        spacer.0.set(Size::new(width, height)).ok();
        spacer
    }

    #[inline(always)]
    pub fn width(width: f32) -> Self {
        Spacer::fixed(width, 1.0)
    }

    #[inline(always)]
    pub fn height(height: f32) -> Self {
        Spacer::fixed(1.0, height)
    }

    #[inline(always)]
    pub fn empty() -> Self {
        Self::fixed(0.0, 0.0)
    }
}

#[allow(unused_variables)]
impl View for Spacer {
    #[inline]
    fn size(&self) -> Size {
        self.0.get().cloned().unwrap_or_default()
    }

    #[inline(always)]
    fn draw(&self, bounds: Bounds, onto: &mut impl Output) {}

    #[inline(always)]
    fn needs_layout(&self) -> bool {
        self.0.get().is_none()
    }

    #[inline]
    fn update_layout(&self, size: Size, _bounds: Bounds) {
        self.0.set(size).expect("size set twice")
    }
}