torin/values/
visible_size.rs

1use crate::prelude::Length;
2
3/// Controls the percentage of the measured size that will actually be used in layout,
4/// regardless of the element's own size.
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6#[derive(PartialEq, Clone, Debug)]
7pub enum VisibleSize {
8    /// Use the full measured size. This is the default.
9    Full,
10    /// Only use a percentage of the measured size in layout.
11    InnerPercentage(Length),
12}
13
14impl Default for VisibleSize {
15    fn default() -> Self {
16        Self::Full
17    }
18}
19
20impl VisibleSize {
21    /// Use a [`Full`](VisibleSize::Full) visible size.
22    pub fn full() -> VisibleSize {
23        VisibleSize::Full
24    }
25
26    /// Use an [`InnerPercentage`](VisibleSize::InnerPercentage) visible size.
27    pub fn inner_percent(value: impl Into<f32>) -> VisibleSize {
28        VisibleSize::InnerPercentage(Length::new(value.into()))
29    }
30
31    pub fn pretty(&self) -> String {
32        match self {
33            Self::Full => "full".to_string(),
34            Self::InnerPercentage(p) => format!("{}%", p.get()),
35        }
36    }
37}