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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use crate::{Length, Padding, Size};

/// A set of size constraints for layouting.
#[derive(Debug, Clone, Copy)]
pub struct Limits {
    min: Size,
    max: Size,
    fill: Size,
}

impl Limits {
    /// No limits
    pub const NONE: Limits = Limits {
        min: Size::ZERO,
        max: Size::INFINITY,
        fill: Size::INFINITY,
    };

    /// Creates new [`Limits`] with the given minimum and maximum [`Size`].
    pub const fn new(min: Size, max: Size) -> Limits {
        Limits {
            min,
            max,
            fill: Size::INFINITY,
        }
    }

    /// Returns the minimum [`Size`] of the [`Limits`].
    pub fn min(&self) -> Size {
        self.min
    }

    /// Returns the maximum [`Size`] of the [`Limits`].
    pub fn max(&self) -> Size {
        self.max
    }

    /// Returns the fill [`Size`] of the [`Limits`].
    pub fn fill(&self) -> Size {
        self.fill
    }

    /// Applies a width constraint to the current [`Limits`].
    pub fn width(mut self, width: Length) -> Limits {
        match width {
            Length::Shrink => {
                self.fill.width = self.min.width;
            }
            Length::Fill | Length::FillPortion(_) => {
                self.fill.width = self.fill.width.min(self.max.width);
            }
            Length::Units(units) => {
                let new_width =
                    (units as f32).min(self.max.width).max(self.min.width);

                self.min.width = new_width;
                self.max.width = new_width;
                self.fill.width = new_width;
            }
        }

        self
    }

    /// Applies a height constraint to the current [`Limits`].
    pub fn height(mut self, height: Length) -> Limits {
        match height {
            Length::Shrink => {
                self.fill.height = self.min.height;
            }
            Length::Fill | Length::FillPortion(_) => {
                self.fill.height = self.fill.height.min(self.max.height);
            }
            Length::Units(units) => {
                let new_height =
                    (units as f32).min(self.max.height).max(self.min.height);

                self.min.height = new_height;
                self.max.height = new_height;
                self.fill.height = new_height;
            }
        }

        self
    }

    /// Applies a minimum width constraint to the current [`Limits`].
    pub fn min_width(mut self, min_width: u32) -> Limits {
        self.min.width =
            self.min.width.max(min_width as f32).min(self.max.width);

        self
    }

    /// Applies a maximum width constraint to the current [`Limits`].
    pub fn max_width(mut self, max_width: u32) -> Limits {
        self.max.width =
            self.max.width.min(max_width as f32).max(self.min.width);

        self
    }

    /// Applies a minimum height constraint to the current [`Limits`].
    pub fn min_height(mut self, min_height: u32) -> Limits {
        self.min.height =
            self.min.height.max(min_height as f32).min(self.max.height);

        self
    }

    /// Applies a maximum height constraint to the current [`Limits`].
    pub fn max_height(mut self, max_height: u32) -> Limits {
        self.max.height =
            self.max.height.min(max_height as f32).max(self.min.height);

        self
    }

    /// Shrinks the current [`Limits`] to account for the given padding.
    pub fn pad(&self, padding: Padding) -> Limits {
        self.shrink(Size::new(
            padding.horizontal() as f32,
            padding.vertical() as f32,
        ))
    }

    /// Shrinks the current [`Limits`] by the given [`Size`].
    pub fn shrink(&self, size: Size) -> Limits {
        let min = Size::new(
            (self.min().width - size.width).max(0.0),
            (self.min().height - size.height).max(0.0),
        );

        let max = Size::new(
            (self.max().width - size.width).max(0.0),
            (self.max().height - size.height).max(0.0),
        );

        let fill = Size::new(
            (self.fill.width - size.width).max(0.0),
            (self.fill.height - size.height).max(0.0),
        );

        Limits { min, max, fill }
    }

    /// Removes the minimum width constraint for the current [`Limits`].
    pub fn loose(&self) -> Limits {
        Limits {
            min: Size::ZERO,
            max: self.max,
            fill: self.fill,
        }
    }

    /// Computes the resulting [`Size`] that fits the [`Limits`] given the
    /// intrinsic size of some content.
    pub fn resolve(&self, intrinsic_size: Size) -> Size {
        Size::new(
            intrinsic_size
                .width
                .min(self.max.width)
                .max(self.fill.width),
            intrinsic_size
                .height
                .min(self.max.height)
                .max(self.fill.height),
        )
    }
}