nuit_core/utils/
frame.rs

1use serde::{Serialize, Deserialize};
2
3/// Geometry for a view.
4#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
5#[serde(rename_all = "camelCase", rename_all_fields = "camelCase")]
6pub enum Frame {
7    Constrained {
8        min_width: Option<f64>,
9        ideal_width: Option<f64>,
10        max_width: Option<f64>,
11        min_height: Option<f64>,
12        ideal_height: Option<f64>,
13        max_height: Option<f64>,
14    },
15    Exact {
16        width: Option<f64>,
17        height: Option<f64>,
18    },
19}
20
21impl Frame {
22    pub fn with_width(width: impl Into<f64>) -> Self {
23        Self::Exact { width: Some(width.into()), height: None }
24    }
25
26    pub fn with_height(height: impl Into<f64>) -> Self {
27        Self::Exact { width: None, height: Some(height.into()) }
28    }
29
30    pub fn exact(width: impl Into<f64>, height: impl Into<f64>) -> Self {
31        Self::Exact { width: Some(width.into()), height: Some(height.into()) }
32    }
33}
34
35macro_rules! impl_exact_from {
36    ($($tys:ty),*) => {
37        $(impl From<$tys> for Frame {
38            fn from(value: $tys) -> Self {
39                Self::exact(value as f64, value as f64)
40            }
41        })*
42    };
43}
44
45impl_exact_from!(
46    u8, u16, u32, u64, u128, usize,
47    i8, i16, i32, i64, i128, isize,
48    f32, f64
49);
50
51impl<W, H> From<(W, H)> for Frame where W: Into<f64>, H: Into<f64> {
52    fn from((width, height): (W, H)) -> Self {
53        Self::exact(width, height)
54    }
55}