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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use figures::units::UPx;
use figures::Size;
use kludgine::Color;

use crate::context::{GraphicsContext, LayoutContext};
use crate::styles::components::PrimaryColor;
use crate::styles::{DynamicComponent, IntoDynamicComponentValue};
use crate::value::{IntoValue, Value};
use crate::widget::Widget;
use crate::ConstraintLimit;

/// A widget that occupies space, optionally filling it with a color.
#[derive(Debug, Clone)]
pub struct Space {
    color: Value<ColorSource>,
}

impl Default for Space {
    fn default() -> Self {
        Self::clear()
    }
}

impl Space {
    /// Returns a widget that draws nothing.
    #[must_use]
    pub const fn clear() -> Self {
        Self {
            color: Value::Constant(ColorSource::Color(Color::CLEAR_BLACK)),
        }
    }

    /// Returns a widget that fills its space with `color`.
    #[must_use]
    pub fn colored(color: impl IntoValue<Color>) -> Self {
        Self {
            color: color
                .into_value()
                .map_each(|color| ColorSource::Color(*color)),
        }
    }

    /// Returns a spacer that fills itself with `dynamic`'s color.
    pub fn dynamic(dynamic: impl IntoDynamicComponentValue) -> Self {
        Self {
            color: dynamic
                .into_dynamic_component()
                .map_each(|component| ColorSource::Dynamic(component.clone())),
        }
    }

    /// Returns a spacer that fills itself with the value of [`PrimaryColor`].
    #[must_use]
    pub fn primary() -> Self {
        Self::dynamic(PrimaryColor)
    }
}

impl Widget for Space {
    fn redraw(&mut self, context: &mut GraphicsContext<'_, '_, '_, '_>) {
        let source = self.color.get_tracking_redraw(context);
        let color = match source {
            ColorSource::Color(color) => color,
            ColorSource::Dynamic(component) => component
                .resolve(context)
                .and_then(|component| Color::try_from(component).ok())
                .unwrap_or(Color::CLEAR_BLACK),
        };
        context.fill(color);
    }

    fn layout(
        &mut self,
        available_space: Size<ConstraintLimit>,
        _context: &mut LayoutContext<'_, '_, '_, '_>,
    ) -> Size<UPx> {
        available_space.map(ConstraintLimit::min)
    }
}

#[derive(Debug, PartialEq, Clone)]
enum ColorSource {
    Color(Color),
    Dynamic(DynamicComponent),
}