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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
//! Houses the [`SizeConstraint`] widget

use std::ops::{Bound, RangeBounds};

use le::{Layout, MinimumNatural};

use crate::{
    le::layout::{Rect, Vec2},
    terminal::Buffer,
    Widget,
};

/// A widget which forces its child to be of a fixed size
pub struct SizeConstraint<
    W: RangeBounds<usize>,
    H: RangeBounds<usize>,
    T: Widget,
> {
    /// The child widget
    pub widget: T,
    /// The range of allowed heights
    pub width: W,
    /// The range of allowed heights
    pub height: H,
}

impl<W, H, T> SizeConstraint<W, H, T>
where
    W: RangeBounds<usize>,
    H: RangeBounds<usize>,
    T: Widget,
{
    /// Creates a new [`SizeConstraint`]
    /// See [`WidgetExt`](crate::prelude::WidgetExt::fixed_size) for
    /// multiple builder methods
    pub fn new(widget: T, width: W, height: H) -> Self {
        Self {
            widget,
            width,
            height,
        }
    }

    /// Clamps the width to our size constraints
    fn clamp_width(&self, width: usize) -> usize {
        let start = start_bound_n(&self.width);
        let end = end_bound_n(&self.width);
        width.clamp(start, end.max(start))
    }

    /// Clamps the height to our size constraints
    fn clamp_height(&self, height: usize) -> usize {
        let start = start_bound_n(&self.height);
        let end = end_bound_n(&self.height);

        height.clamp(start, end.max(start))
    }
}

/// Gets the start bound as a `usize`
fn start_bound_n<R: RangeBounds<usize>>(range: &R) -> usize {
    bound_n(&range.start_bound()).unwrap_or(0)
}

/// Gets the end bound as a `usize`
fn end_bound_n<R: RangeBounds<usize>>(range: &R) -> usize {
    bound_n(&range.end_bound()).unwrap_or(usize::MAX)
}

/// Gets the bound as an `Option<usize>`
fn bound_n(bound: &Bound<&usize>) -> Option<usize> {
    match bound {
        Bound::Excluded(x) => Some(x.saturating_sub(1)),
        Bound::Included(x) => Some(**x),
        Bound::Unbounded => None,
    }
}

impl<W, H, T> Layout for SizeConstraint<W, H, T>
where
    W: RangeBounds<usize>,
    H: RangeBounds<usize>,
    T: Widget,
{
    fn width_for_height(&self, height: usize) -> MinimumNatural<usize> {
        let height = self.clamp_height(height);

        MinimumNatural {
            minimum: self
                .clamp_width(self.widget.width_for_height(height).minimum),
            natural: self
                .clamp_width(self.widget.width_for_height(height).natural),
        }
    }

    fn height_for_width(&self, width: usize) -> MinimumNatural<usize> {
        let width = self.clamp_width(width);
        MinimumNatural {
            minimum: self
                .clamp_height(self.widget.height_for_width(width).minimum),
            natural: self
                .clamp_height(self.widget.height_for_width(width).natural),
        }
    }

    fn prefered_size(&self) -> MinimumNatural<Vec2> {
        // Get the widgets prefered size
        let size_bounds = (
            bound_n(&self.width.end_bound()),
            bound_n(&self.height.end_bound()),
        );
        let widget_size_og = if let (Some(size_x), Some(size_y)) = size_bounds {
            self.widget
                .prefered_size_of_container(Vec2::new(size_x, size_y))
        } else {
            self.widget.prefered_size()
        };
        let mut widget_size = widget_size_og;

        // Clamp there size
        widget_size.minimum.x = self.clamp_width(widget_size_og.minimum.x);
        widget_size.minimum.y = self.clamp_height(widget_size_og.minimum.y);

        widget_size.natural.x = self.clamp_width(widget_size_og.natural.x);
        widget_size.natural.y = self.clamp_height(widget_size_og.natural.y);

        // See if we can relayout their height-for-width
        // or width-for-height based of this new layout
        if widget_size.minimum.x != widget_size_og.minimum.x {
            widget_size.minimum.y = self.clamp_height(
                self.height_for_width(widget_size.minimum.x).minimum,
            );
        }
        if widget_size.minimum.y != widget_size_og.minimum.y {
            widget_size.minimum.x = self.clamp_width(
                self.width_for_height(widget_size.minimum.y).minimum,
            );
        }

        if widget_size.natural.x != widget_size_og.natural.x {
            widget_size.natural.y = self.clamp_height(
                self.height_for_width(widget_size.natural.x).natural,
            );
        }
        if widget_size.natural.y != widget_size_og.natural.y {
            widget_size.natural.x = self.clamp_width(
                self.width_for_height(widget_size.natural.y).natural,
            );
        }

        // Return
        widget_size
    }

    fn prefered_size_of_container(
        &self,
        container: Vec2,
    ) -> MinimumNatural<Vec2> {
        let container = Vec2::new(
            self.clamp_width(container.x),
            self.clamp_height(container.y),
        );
        let widget_size_og = self.widget.prefered_size_of_container(container);
        let mut widget_size = self.widget.prefered_size_of_container(container);

        // Clamp there size
        widget_size.minimum.x = self.clamp_width(widget_size_og.minimum.x);
        widget_size.minimum.y = self.clamp_height(widget_size_og.minimum.y);

        widget_size.natural.x = self.clamp_width(widget_size_og.natural.x);
        widget_size.natural.y = self.clamp_height(widget_size_og.natural.y);

        // See if we can relayout their height-for-width
        // or width-for-height based of this new layout
        if widget_size.minimum.x != widget_size_og.minimum.x {
            widget_size.minimum.y = self.clamp_height(
                self.height_for_width(widget_size.minimum.x).minimum,
            );
        }
        if widget_size.minimum.y != widget_size_og.minimum.y {
            widget_size.minimum.x = self.clamp_width(
                self.width_for_height(widget_size.minimum.y).minimum,
            );
        }

        if widget_size.natural.x != widget_size_og.natural.x {
            widget_size.natural.y = self.clamp_height(
                self.height_for_width(widget_size.natural.x).natural,
            );
        }
        if widget_size.natural.y != widget_size_og.natural.y {
            widget_size.natural.x = self.clamp_width(
                self.width_for_height(widget_size.natural.y).natural,
            );
        }

        // Return
        widget_size
    }
}

impl<W, H, T> Widget for SizeConstraint<W, H, T>
where
    W: RangeBounds<usize>,
    H: RangeBounds<usize>,
    T: Widget,
{
    fn render(&self, rect: Rect, buffer: &mut Buffer) {
        let mut rect = rect;

        if self.clamp_width(rect.size.x) != rect.size.x {
            rect.size.x =
                self.clamp_width(self.width_for_height(rect.size.y).natural);
        }
        if self.clamp_height(rect.size.y) != rect.size.y {
            rect.size.y =
                self.clamp_height(self.height_for_width(rect.size.x).natural);
        }

        self.widget.render(rect, buffer);
    }
}