1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use serde::{Serialize, Deserialize};

/// Geometry for a view.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", rename_all_fields = "camelCase")]
pub enum Frame {
    Constrained {
        min_width: Option<f64>,
        ideal_width: Option<f64>,
        max_width: Option<f64>,
        min_height: Option<f64>,
        ideal_height: Option<f64>,
        max_height: Option<f64>,
    },
    Exact {
        width: Option<f64>,
        height: Option<f64>,
    },
}

impl Frame {
    pub fn width(width: impl Into<f64>) -> Self {
        Self::Exact { width: Some(width.into()), height: None }
    }

    pub fn height(height: impl Into<f64>) -> Self {
        Self::Exact { width: None, height: Some(height.into()) }
    }

    pub fn exact(width: impl Into<f64>, height: impl Into<f64>) -> Self {
        Self::Exact { width: Some(width.into()), height: Some(height.into()) }
    }
}

impl From<i32> for Frame {
    fn from(value: i32) -> Self {
        Self::exact(value, value)
    }
}

impl From<f64> for Frame {
    fn from(value: f64) -> Self {
        Self::exact(value, value)
    }
}

impl<W, H> From<(W, H)> for Frame where W: Into<f64>, H: Into<f64> {
    fn from((width, height): (W, H)) -> Self {
        Self::exact(width, height)
    }
}