rat_widget/layout/
layout_edit2.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
//!
//! Calculate the layout for an edit-mask with lots of label/widget pairs.
//!

use crate::layout::LabelWidget::{Lbl, Wdg};
use crate::layout::StructuredLayout;
use ratatui::layout::Rect;
use std::cmp::{max, min};
use std::ops::{Index, IndexMut};

/// [layout_edit] returns pairs of areas as `[label, widget]`.
///
/// This type can be used to index into the array.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LabelWidget {
    Lbl,
    Wdg,
}

impl LabelWidget {
    pub fn size() -> usize {
        2
    }
}

impl Index<LabelWidget> for [Rect] {
    type Output = Rect;

    fn index(&self, index: LabelWidget) -> &Self::Output {
        match index {
            Lbl => &self[0],
            Wdg => &self[1],
        }
    }
}

impl IndexMut<LabelWidget> for [Rect] {
    fn index_mut(&mut self, index: LabelWidget) -> &mut Self::Output {
        match index {
            Lbl => &mut self[0],
            Wdg => &mut self[1],
        }
    }
}

/// Constraint data for [layout_edit]
#[allow(variant_size_differences)]
#[derive(Debug)]
pub enum EditConstraint<'a> {
    /// Label by sample
    Label(&'a str),
    /// Label by width. (cols)
    LabelWidth(u16),
    /// Label by height+width. ( cols, rows).
    LabelRows(u16, u16),
    /// Label occupying the full row.
    TitleLabel,
    /// Label occupying the full row, but rendering only part of it. (cols)
    TitleLabelWidth(u16),
    /// Label occupying multiple full rows. (rows)
    TitleLabelRows(u16),
    /// Widget aligned with the label. (cols)
    Widget(u16),
    /// Widget aligned with the label. (cols, rows)
    WidgetRows(u16, u16),
    /// Empty line. Only increase the line counter.
    Empty,
    /// Empty lines. (rows). Only increase the line counter.
    EmptyRows(u16),
    /// Widget aligned with the left margin. (cols)
    LineWidget(u16),
    /// Widget aligned with the left margin. (cols, rows)
    LineWidgetRows(u16, u16),
    /// Add a page break marker.
    Break,
}

/// 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 [StructuredLayout] with pairs of [label area, widget area]
/// for each index.
#[allow(clippy::comparison_chain)]
pub fn layout_edit(area: Rect, constraints: &[EditConstraint<'_>]) -> StructuredLayout {
    let mut max_label = 0;
    let mut max_widget = 0;
    let mut space = 1;

    // 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(_) => {}
            EditConstraint::Break => {}
        }
    }

    let mut ll = StructuredLayout::new(2);

    // area.width is a constraint too
    if max_label + space + max_widget < area.width {
        space = area.width - max_label - max_widget;
    } else if max_label + space + max_widget > area.width {
        let mut reduce = max_label + space + max_widget - area.width;

        if space > reduce {
            space -= reduce;
            reduce = 0;
        } else {
            reduce -= space;
            space = 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 mut x = area.x;
    let mut y = area.y;
    let total = max_label + space + max_widget;
    let mut rest_height = if area.height > 0 { area.height - 1 } else { 0 }; //todo: verify the '-1' somehow??
    let mut height = min(1, rest_height);

    let mut cur: [Rect; 2] = Default::default();

    for l in constraints.iter() {
        // break before
        match l {
            EditConstraint::LineWidget(_) | EditConstraint::LineWidgetRows(_, _) => {
                if x != area.x {
                    // result.add(cur);
                    // cur = Default::default();

                    x = area.x;
                    y += height;
                    rest_height -= height;
                    height = min(1, rest_height);
                }
            }
            EditConstraint::TitleLabel
            | EditConstraint::TitleLabelWidth(_)
            | EditConstraint::TitleLabelRows(_) => {
                if x != area.x {
                    ll.add(&cur);
                    cur = Default::default();

                    x = area.x;
                    y += height;
                    rest_height -= height;
                    height = min(1, rest_height);
                }
            }
            EditConstraint::Label(_)
            | EditConstraint::LabelWidth(_)
            | EditConstraint::LabelRows(_, _)
            | EditConstraint::Widget(_)
            | EditConstraint::WidgetRows(_, _)
            | EditConstraint::Empty
            | EditConstraint::EmptyRows(_) => {}
            EditConstraint::Break => {}
        }

        // self
        match l {
            EditConstraint::Label(s) => {
                cur[Lbl] = Rect::new(x, y, min(s.len() as u16, max_label), min(1, rest_height))
            }
            EditConstraint::LabelWidth(w) => {
                cur[Lbl] = Rect::new(x, y, min(*w, max_label), min(1, rest_height))
            }
            EditConstraint::LabelRows(w, h) => {
                cur[Lbl] = Rect::new(x, y, min(*w, max_label), min(1, *h))
            }
            EditConstraint::TitleLabel => {
                cur[Lbl] = Rect::new(x, y, total, min(1, rest_height));
                ll.add(&cur);
                cur = Default::default();
            }
            EditConstraint::TitleLabelWidth(w) => {
                cur[Lbl] = Rect::new(x, y, min(*w, max_label), min(1, rest_height));
                ll.add(&cur);
                cur = Default::default();
            }
            EditConstraint::TitleLabelRows(h) => {
                cur[Lbl] = Rect::new(x, y, total, min(*h, rest_height));
                ll.add(&cur);
                cur = Default::default();
            }
            EditConstraint::Widget(w) => {
                cur[Wdg] = Rect::new(x, y, min(*w, max_widget), min(1, rest_height));
                ll.add(&cur);
                cur = Default::default();
            }
            EditConstraint::WidgetRows(w, h) => {
                cur[Wdg] = Rect::new(x, y, min(*w, max_widget), min(*h, rest_height));
                ll.add(&cur);
                cur = Default::default();
            }
            EditConstraint::LineWidget(w) => {
                cur[Wdg] = Rect::new(x, y, min(*w, max_widget), min(1, rest_height));
                ll.add(&cur);
                cur = Default::default();
            }
            EditConstraint::LineWidgetRows(w, h) => {
                cur[Wdg] = Rect::new(x, y, min(*w, max_widget), min(*h, rest_height));
                ll.add(&cur);
                cur = Default::default();
            }
            EditConstraint::Empty => {}
            EditConstraint::EmptyRows(_) => {}
            EditConstraint::Break => ll.break_before_row(y),
        }

        // row-height
        match l {
            EditConstraint::Label(_)
            | EditConstraint::LabelWidth(_)
            | EditConstraint::TitleLabel
            | EditConstraint::TitleLabelWidth(_)
            | EditConstraint::Widget(_)
            | EditConstraint::Empty
            | EditConstraint::LineWidget(_) => {
                height = min(max(height, 1), rest_height);
            }
            EditConstraint::LabelRows(_, h)
            | EditConstraint::TitleLabelRows(h)
            | EditConstraint::WidgetRows(_, h)
            | EditConstraint::EmptyRows(h)
            | EditConstraint::LineWidgetRows(_, h) => {
                height = min(max(height, *h), rest_height);
            }
            EditConstraint::Break => {}
        }

        // break after
        match l {
            EditConstraint::Label(_)
            | EditConstraint::LabelWidth(_)
            | EditConstraint::LabelRows(_, _) => {
                x += max_label + space;
            }
            EditConstraint::TitleLabel
            | EditConstraint::TitleLabelWidth(_)
            | EditConstraint::TitleLabelRows(_) => {
                x = area.x;
                y += height;
                rest_height -= height;
                height = min(1, rest_height);
            }
            EditConstraint::Widget(_)
            | EditConstraint::WidgetRows(_, _)
            | EditConstraint::Empty
            | EditConstraint::EmptyRows(_)
            | EditConstraint::LineWidget(_)
            | EditConstraint::LineWidgetRows(_, _) => {
                x = area.x;
                y += height;
                rest_height -= height;
                height = min(1, rest_height);
            }
            EditConstraint::Break => {}
        };
    }

    ll
}