rat_widget/layout/
layout_edit.rs

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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
//!
//! Calculate the layout for an edit-mask with lots of label/widget pairs.
//!

use crate::layout::GenericLayout;
use ratatui::layout::{Flex, Rect, Size};
use std::borrow::Cow;
use std::cmp::{max, min};

/// Constraint data for [layout_edit]
#[allow(variant_size_differences)]
#[derive(Debug)]
pub enum EditConstraint {
    /// Label by sample.
    Label(Cow<'static, str>),
    /// Label by width.
    /// __unit: cols__
    LabelWidth(u16),
    /// Label by height+width.
    /// __unit: cols, rows__
    LabelRows(u16, u16),
    /// Label occupying the full row.
    /// This is added with its own index.
    TitleLabel(Cow<'static, str>),
    /// Label occupying the full row, but rendering only part of it.
    /// This is added with its own index.
    /// __unit: cols__
    TitleLabelWidth(u16),
    /// Label occupying multiple full rows.
    /// This is added with its own index.
    /// __unit: rows__
    TitleLabelRows(u16),
    /// Widget aligned with the label.
    /// __unit: cols__
    Widget(u16),
    /// Widget aligned with the label.
    /// __unit: cols, rows__
    WidgetRows(u16, u16),
    /// Empty space.
    /// This is not a widget, just some spacing.
    Empty,
    /// Empty space.
    /// This is not a widget, just some spacing.
    /// __unit: rows__
    EmptyRows(u16),
    /// Widget aligned with the left margin.
    /// __unit: cols__
    LineWidget(u16),
    /// Widget aligned with the left margin.
    /// __unit: cols, rows__
    LineWidgetRows(u16, u16),
}

/// Layout for an edit mask with lots of label+widget pairs.
///
/// This neatly aligns labels and widgets in one column.
/// Use the edit constraints to define the layout.
///
/// This returns a [GenericLayout] with indexed widgets.
///
/// If the space runs out during layout, everything
/// gets stuffed in the last row, regardless.
#[allow(clippy::comparison_chain)]
pub fn layout_edit(
    area: Size,
    constraints: &[EditConstraint],
    mut spacing: u16,
    flex: Flex,
) -> GenericLayout<usize> {
    let mut max_label = 0;
    let mut max_widget = 0;

    // find max
    for l in constraints.iter() {
        match l {
            EditConstraint::Label(s) => max_label = max(max_label, s.len() as u16),
            EditConstraint::LabelWidth(w) => max_label = max(max_label, *w),
            EditConstraint::LabelRows(w, _) => max_label = max(max_label, *w),
            EditConstraint::TitleLabel(_) => {}
            EditConstraint::TitleLabelWidth(_) => {}
            EditConstraint::TitleLabelRows(_) => {}
            EditConstraint::Widget(w) => max_widget = max(max_widget, *w),
            EditConstraint::WidgetRows(w, _) => max_widget = max(max_widget, *w),
            EditConstraint::LineWidget(_) => {}
            EditConstraint::LineWidgetRows(_, _) => {}
            EditConstraint::Empty => {}
            EditConstraint::EmptyRows(_) => {}
        }
    }

    let mut gen_layout = GenericLayout::new();

    // cut excess
    if max_label + spacing + max_widget > area.width {
        let mut reduce = max_label + spacing + max_widget - area.width;

        if spacing > reduce {
            spacing -= reduce;
            reduce = 0;
        } else {
            reduce -= spacing;
            spacing = 0;
        }
        if max_label > 5 {
            if max_label - 5 > reduce {
                max_label -= reduce;
                reduce = 0;
            } else {
                reduce -= max_label - 5;
                max_label = 5;
            }
        }
        if max_widget > 5 {
            if max_widget - 5 > reduce {
                max_widget -= reduce;
                reduce = 0;
            } else {
                reduce -= max_widget - 5;
                max_widget = 5;
            }
        }
        if max_label > reduce {
            max_label -= reduce;
            reduce = 0;
        } else {
            reduce -= max_label;
            max_label = 0;
        }
        if max_widget > reduce {
            max_widget -= reduce;
            // reduce = 0;
        } else {
            // reduce -= max_widget;
            max_widget = 0;
        }
    }

    let label_x;
    let widget_x;

    match flex {
        Flex::Legacy => {
            label_x = 0;
            widget_x = label_x + spacing + max_label;
        }
        Flex::Start => {
            label_x = 0;
            widget_x = label_x + spacing + max_label;
        }
        Flex::End => {
            widget_x = area.width - max_widget;
            label_x = widget_x - spacing - max_label;
        }
        Flex::Center => {
            let rest = area.width - max_label - max_widget - spacing;
            label_x = rest / 2;
            widget_x = label_x + spacing + max_label;
        }
        Flex::SpaceAround => {
            let rest = area.width - max_label - max_widget - spacing;
            label_x = rest / 2;
            widget_x = label_x + spacing + max_label;
        }
        Flex::SpaceBetween => {
            let rest = area.width - max_label - max_widget;
            label_x = rest / 3;
            widget_x = label_x + rest / 3 + max_label;
        }
    }
    let total = max_label + spacing + max_widget;

    let mut n = 0;
    let mut y = 0;

    let mut label_area = Rect::default();
    let mut label_text = None;

    let mut rest_height = if area.height > 0 { area.height - 1 } else { 0 };

    for l in constraints.iter() {
        let height;

        // self
        match l {
            EditConstraint::Label(s) => {
                height = 0;
                label_area = Rect::new(label_x, y, max_label, min(1, rest_height));
                label_text = Some(s.clone());
            }
            EditConstraint::LabelWidth(_) => {
                height = 0;
                label_area = Rect::new(label_x, y, max_label, min(1, rest_height));
                label_text = None;
            }
            EditConstraint::LabelRows(_, h) => {
                height = 0;
                label_area = Rect::new(label_x, y, max_label, min(1, min(*h, rest_height)));
                label_text = None;
            }
            EditConstraint::TitleLabel(s) => {
                height = min(1, rest_height);

                label_area = Rect::new(label_x, y, total, height);
                gen_layout.add(n, Rect::default(), Some(s.clone()), label_area);

                n += 1;
                label_area = Rect::default();
                label_text = None;
            }
            EditConstraint::TitleLabelWidth(w) => {
                height = min(1, rest_height);

                label_area = Rect::new(label_x, y, min(*w, max_label), height);
                gen_layout.add(n, Rect::default(), None, label_area);

                n += 1;
                label_area = Rect::default();
                label_text = None;
            }
            EditConstraint::TitleLabelRows(h) => {
                height = min(*h, rest_height);

                label_area = Rect::new(label_x, y, total, height);
                gen_layout.add(n, Rect::default(), None, label_area);

                n += 1;
                label_area = Rect::default();
                label_text = None;
            }
            EditConstraint::Widget(w) => {
                let w_height = min(1, rest_height);
                height = max(label_area.height, w_height);

                let area = Rect::new(widget_x, y, min(*w, max_widget), w_height);
                gen_layout.add(n, area, label_text.take(), label_area);

                n += 1;
                label_area = Rect::default();
            }
            EditConstraint::WidgetRows(w, h) => {
                let w_height = min(*h, rest_height);
                height = max(label_area.height, w_height);

                let area = Rect::new(widget_x, y, min(*w, max_widget), w_height);
                gen_layout.add(n, area, label_text.take(), label_area);

                n += 1;
                label_area = Rect::default();
            }
            EditConstraint::LineWidget(w) => {
                height = min(1, rest_height);

                let area = Rect::new(label_x, y, min(*w, total), height);
                gen_layout.add(n, area, label_text.take(), label_area);

                n += 1;
                label_area = Rect::default();
            }
            EditConstraint::LineWidgetRows(w, h) => {
                height = min(*h, rest_height);

                let area = Rect::new(label_x, y, min(*w, total), height);
                gen_layout.add(n, area, label_text.take(), label_area);

                n += 1;
                label_area = Rect::default();
            }
            EditConstraint::Empty => {
                height = min(1, rest_height);

                label_text = None;
                label_area = Rect::default();
            }
            EditConstraint::EmptyRows(h) => {
                height = min(*h, rest_height);

                label_text = None;
                label_area = Rect::default();
            }
        }

        y += height;
        rest_height -= height;
    }

    gen_layout
}