asdf_overlay_common/size.rs
1//! Size-related types and utilities.
2
3use bincode::{Decode, Encode};
4
5/// A length that can be specified as either a percentage of a container size or an absolute length.
6#[derive(Debug, Decode, Encode, Clone, Copy, PartialEq)]
7pub enum PercentLength {
8 /// A percentage length relative to a base size.
9 ///
10 /// For example, `0.5` is 50% of the base size.
11 Percent(f32),
12
13 /// An absolute length. Usually in pixels.
14 ///
15 /// For example, `100.0` is 100 pixels.
16 Length(f32),
17}
18
19impl PercentLength {
20 pub const ZERO: Self = Self::Length(0.0);
21
22 /// Resolves the [`PercentLength`] to an absolute length based on the given container size.
23 pub const fn resolve(self, container_size: f32) -> f32 {
24 match self {
25 Self::Percent(percent) => container_size * percent,
26 Self::Length(length) => length,
27 }
28 }
29}
30
31impl Default for PercentLength {
32 fn default() -> Self {
33 Self::ZERO
34 }
35}