Skip to main content

ferroui_base/layout/
min_max.rs

1use super::Layoutable;
2
3/// The effective minimum and maximum size of a layoutable, combining its
4/// explicit size with its min/max constraints.
5#[derive(Clone, Copy, Debug)]
6pub struct MinMax {
7    pub min_width: f64,
8    pub max_width: f64,
9    pub min_height: f64,
10    pub max_height: f64,
11}
12
13impl MinMax {
14    pub fn new(e: &Layoutable) -> Self {
15        let (min_width, max_width) = Self::calc_min_max(e.width(), e.min_width(), e.max_width());
16        let (min_height, max_height) = Self::calc_min_max(e.height(), e.min_height(), e.max_height());
17        Self { min_width, max_width, min_height, max_height }
18    }
19
20    fn calc_min_max(value: f64, min: f64, max: f64) -> (f64, f64) {
21        let (v0, v1) = if value.is_nan() { (0.0, f64::INFINITY) } else { (value, value) };
22        let max = Self::clamp_unchecked(v1, min, max);
23        let min = Self::clamp_unchecked(v0, min, max);
24        (min, max)
25    }
26
27    // Not `f64::clamp`: it's possible for min to be greater than max here.
28    #[inline]
29    fn clamp_unchecked(mut value: f64, min: f64, max: f64) -> f64 {
30        if value > max {
31            value = max;
32        }
33        if value < min {
34            value = min;
35        }
36        value
37    }
38}