torin/values/
content.rs

1#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
2#[derive(PartialEq, Clone, Debug, Default)]
3pub enum Content {
4    /// Default layout, children are stacked along the direction axis.
5    #[default]
6    Normal,
7    /// Resize children to evenly fit the available space along the direction axis.
8    Fit,
9    /// Let children use [`Size::Flex`](crate::size::Size::Flex) to grow proportionally to fill the available space.
10    Flex,
11    /// Wrap children to the next line or column when they exceed the available space,
12    /// with an optional gap between wrapped lines.
13    Wrap { wrap_spacing: Option<f32> },
14}
15
16impl Content {
17    /// Use a [`Normal`](Content::Normal) content.
18    pub fn normal() -> Content {
19        Content::Normal
20    }
21
22    /// Use a [`Fit`](Content::Fit) content.
23    pub fn fit() -> Content {
24        Content::Fit
25    }
26
27    /// Use a [`Flex`](Content::Flex) content.
28    pub fn flex() -> Content {
29        Content::Flex
30    }
31
32    /// Use a [`Wrap`](Content::Wrap) content with no spacing.
33    pub fn wrap() -> Content {
34        Content::Wrap { wrap_spacing: None }
35    }
36
37    /// Use a [`Wrap`](Content::Wrap) content with the given spacing.
38    pub fn wrap_spacing(spacing: f32) -> Content {
39        Content::Wrap {
40            wrap_spacing: Some(spacing),
41        }
42    }
43
44    pub fn is_fit(&self) -> bool {
45        self == &Self::Fit
46    }
47
48    pub fn is_flex(&self) -> bool {
49        self == &Self::Flex
50    }
51
52    pub fn is_wrap(&self) -> bool {
53        matches!(self, Self::Wrap { .. })
54    }
55
56    pub fn allows_alignments(&self) -> bool {
57        matches!(self, Self::Normal | Self::Flex | Self::Fit)
58    }
59}
60
61impl Content {
62    pub fn pretty(&self) -> String {
63        match self {
64            Self::Normal => "normal".to_owned(),
65            Self::Fit => "fit".to_owned(),
66            Self::Flex => "flex".to_owned(),
67            Self::Wrap { .. } => "wrap".to_owned(),
68        }
69    }
70}