leftwm_core/models/
size.rs

1use serde::{Deserialize, Serialize};
2
3/// Helper enum to represent a size which can be
4/// an absolute pixel value or a relative percentage value
5#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Copy)]
6#[serde(untagged)]
7pub enum Size {
8    Pixel(i32),
9    Ratio(f32),
10}
11
12impl Size {
13    /// Turn the size into an absolute value.
14    ///
15    /// The pixel value will be returned as is, the ratio value will be multiplied by the provided
16    /// `whole` to calculate the absolute value.
17    #[must_use]
18    pub fn into_absolute(self, whole: i32) -> i32 {
19        match self {
20            Size::Pixel(x) => x,
21            Size::Ratio(x) => (whole as f32 * x).floor() as i32,
22        }
23    }
24}