Skip to main content

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(Default, PartialEq, Clone, Debug)]
7pub enum VisibleSize {
8    /// Use the full measured size. This is the default.
9    #[default]
10    Full,
11    /// Only use a percentage of the measured size in layout.
12    InnerPercentage(Length),
13}
14
15impl VisibleSize {
16    /// Use a [`Full`](VisibleSize::Full) visible size.
17    pub fn full() -> VisibleSize {
18        VisibleSize::Full
19    }
20
21    /// Use an [`InnerPercentage`](VisibleSize::InnerPercentage) visible size.
22    pub fn inner_percent(value: impl Into<f32>) -> VisibleSize {
23        VisibleSize::InnerPercentage(Length::new(value.into()))
24    }
25
26    pub fn pretty(&self) -> String {
27        match self {
28            Self::Full => "full".to_string(),
29            Self::InnerPercentage(p) => format!("{}%", p.get()),
30        }
31    }
32}