rat_widget/layout/
structured_layout.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
use crate::pager::AreaHandle;
use ratatui::layout::{Position, Rect};
use std::cell::Cell;
use std::ops::{Index, IndexMut};

/// Container for the areas coming out of a layout function.
///
/// This is more or less a `Vec<Rect>`, but it takes a _stride_
/// as parameter and treats N Rects as one unit.
///
/// This way it can add some structure to the list and
/// express something like 'the label area for the 5th item'.
///
/// As a second feature it returns a handle for each item,
/// which can be used to retrieve the item later.
///
/// ```rust
/// # use rat_widget::layout::StructuredLayout;
/// # use std::ops::Index;
/// # use rat_widget::checkbox::{Checkbox, CheckboxState};
/// # use ratatui::prelude::*;
/// pub enum LW {
///     Label,
///     Widget
/// }
/// #    impl Index<LW> for [Rect] {
/// #        type Output = Rect;
/// #
/// #        fn index(&self, index: LW) -> &Self::Output {
/// #            match index {
/// #                LW::Label => &self[0],
/// #                LW::Widget => &self[1]
/// #            }
/// #        }
/// #    }
/// #
/// #    impl LW {
/// #        pub fn count() -> usize {
/// #            2
/// #        }
/// #    }
/// #
/// # let mut buf = Buffer::default();
/// # let mut state = CheckboxState::default();
/// # use LW::*;
///
/// let mut l = StructuredLayout::new(LW::count());
///
/// // ... some layout calculations ...
/// let w0 = l.add(&[
///         Rect::new(0,0,5,1),
///         Rect::new(6,0,15,1)
/// ]);
///
/// // ... something entirely else ...
///
/// Span::from("label")
///     .render(l[w0][Label],&mut buf);
///
/// Checkbox::new()
///     .text("Check this out")
///     .render(l[w0][Widget], &mut buf, &mut state);
///
/// ```
///
#[derive(Debug, Clone)]
pub struct StructuredLayout {
    // reference area.
    area: Rect,
    // bounding box for all areas
    bounds: Cell<Option<Rect>>,
    // stride within areas
    stride: usize,
    // list of areas
    areas: Vec<Rect>,
    // manual breaks
    row_breaks: Vec<u16>,
}

impl Default for StructuredLayout {
    fn default() -> Self {
        Self {
            area: Default::default(),
            bounds: Cell::new(None),
            stride: 1, // non standard
            areas: vec![],
            row_breaks: vec![],
        }
    }
}

impl StructuredLayout {
    /// New layout with the given stride.
    pub fn new(stride: usize) -> Self {
        Self {
            stride,
            ..Default::default()
        }
    }

    /// Original area for which this layout has been calculated.
    /// Can be used to invalidate a layout if the area changes.
    pub fn area(&self) -> Rect {
        self.area
    }

    /// Original area for which this layout has been calculated.
    /// Can be used to invalidate a layout if the area changes.
    pub fn set_area(&mut self, area: Rect) {
        self.area = area;
    }

    /// Change detection.
    pub fn width_change(&self, width: u16) -> bool {
        self.area.width != width
    }

    /// Change detection.
    pub fn height_change(&self, height: u16) -> bool {
        self.area.height != height
    }

    /// Change detection.
    pub fn pos_change(&self, pos: Position) -> bool {
        self.area.as_position() != pos
    }

    /// Add the areas for one item.
    ///
    /// You can refer
    /// Returns a handle to access the item later.
    /// You can always use a simple index too.
    pub fn add(&mut self, areay: &[Rect]) -> AreaHandle {
        assert_eq!(self.stride, areay.len());

        // invalidate
        self.bounds.set(None);

        let h = AreaHandle(self.areas.len());

        for a in areay {
            self.areas.push(*a);
        }

        h
    }

    /// Add a manual break after the given position.
    ///
    /// __See__
    /// [SinglePager](crate::pager::SinglePager) and
    /// [DualPager](crate::pager::DualPager) who can work with this.
    /// Other widgets may simply ignore this.
    pub fn break_after_row(&mut self, y: u16) {
        // invalidate
        self.bounds.set(None);
        self.row_breaks.push(y + 1);
    }

    /// Add a manual break before the given position.
    ///
    /// __See__
    /// [SinglePager](crate::pager::SinglePager) and
    /// [DualPager](crate::pager::DualPager) who can work with this.
    /// Other widgets may simply ignore this.
    pub fn break_before_row(&mut self, y: u16) {
        // invalidate
        self.bounds.set(None);
        self.row_breaks.push(y);
    }

    /// Return the manual page breaks.
    pub fn row_breaks(&self) -> &[u16] {
        self.row_breaks.as_slice()
    }

    /// Return the manual page breaks.
    pub fn row_breaks_mut(&mut self) -> &mut [u16] {
        self.row_breaks.as_mut_slice()
    }

    /// Sort and dedup the row-breaks.
    pub fn sort_row_breaks_desc(&mut self) {
        self.row_breaks.sort_by(|a, b| b.cmp(a));
        self.row_breaks.dedup();
    }

    /// Number of areas.
    pub fn len(&self) -> usize {
        self.areas.len()
    }

    /// Any areas?
    pub fn is_empty(&self) -> bool {
        self.areas.is_empty()
    }

    /// Stride per item.
    pub fn stride(&self) -> usize {
        self.stride
    }

    /// All areas. If you want to access a specific item you
    /// must use the stride to calculate the offset.
    pub fn as_slice(&self) -> &[Rect] {
        self.areas.as_slice()
    }

    /// All areas. If you want to access a specific item you
    /// must use the stride to calculate the offset.
    pub fn as_mut_slice(&mut self) -> &mut [Rect] {
        self.areas.as_mut_slice()
    }

    /// Iterator over all areas.
    pub fn iter(&self) -> impl Iterator<Item = &'_ Rect> {
        self.areas.iter()
    }

    /// Iterator over all areas chunked by stride.
    pub fn chunked(&self) -> impl Iterator<Item = &[Rect]> {
        self.areas.chunks(self.stride)
    }

    /// Calculate the bounding box for all areas.
    pub fn bounds(&self) -> Rect {
        if let Some(bounds) = self.bounds.get() {
            return bounds;
        }

        let mut bounds;
        'fine: {
            for v in &self.areas {
                if !v.is_empty() {
                    bounds = *v;
                    break 'fine;
                }
            }
            bounds = Rect::new(self.area.x, self.area.y, 0, 0);
        }

        for v in &self.areas {
            if !v.is_empty() {
                bounds = bounds.union(*v);
            }
        }

        self.bounds.set(Some(bounds));

        bounds
    }
}

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

    fn index(&self, index: usize) -> &Self::Output {
        &self.areas[index * self.stride..(index + 1) * self.stride]
    }
}

impl IndexMut<usize> for StructuredLayout {
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        &mut self.areas[index * self.stride..(index + 1) * self.stride]
    }
}

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

    fn index(&self, index: AreaHandle) -> &Self::Output {
        &self.areas[index.0 * self.stride..(index.0 + 1) * self.stride]
    }
}

impl IndexMut<AreaHandle> for StructuredLayout {
    fn index_mut(&mut self, index: AreaHandle) -> &mut Self::Output {
        &mut self.areas[index.0 * self.stride..(index.0 + 1) * self.stride]
    }
}