rat_widget/pager/
pager.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
use crate::layout::GenericLayout;
use crate::pager::PagerStyle;
use ratatui::buffer::Buffer;
use ratatui::layout::{Alignment, Rect};
use ratatui::style::Style;
use ratatui::text::Line;
use ratatui::widgets::{StatefulWidget, Widget};
use std::borrow::Cow;
use std::cell::{RefCell, RefMut};
use std::hash::Hash;
use std::rc::Rc;

///
/// Renders widgets for one page of a [GenericLayout].
///
///
///
///
#[derive(Debug)]
pub struct Pager<W>
where
    W: Eq + Hash + Clone,
{
    layout: Rc<RefCell<GenericLayout<W>>>,
    page: usize,
    style: Style,
    label_style: Option<Style>,
    label_alignment: Option<Alignment>,
}

/// Rendering phase.
#[derive(Debug)]
pub struct PagerBuffer<'a, W>
where
    W: Eq + Hash + Clone,
{
    layout: Rc<RefCell<GenericLayout<W>>>,
    page_area: Rect,
    widget_area: Rect,
    buffer: Rc<RefCell<&'a mut Buffer>>,
    label_style: Option<Style>,
    label_alignment: Option<Alignment>,
}

impl<W> Clone for Pager<W>
where
    W: Eq + Hash + Clone,
{
    fn clone(&self) -> Self {
        Self {
            layout: self.layout.clone(),
            page: self.page,
            style: self.style,
            label_style: self.label_style,
            label_alignment: self.label_alignment,
        }
    }
}

impl<W> Default for Pager<W>
where
    W: Eq + Hash + Clone,
{
    fn default() -> Self {
        Self {
            layout: Default::default(),
            page: Default::default(),
            style: Default::default(),
            label_style: Default::default(),
            label_alignment: Default::default(),
        }
    }
}

impl<W> Pager<W>
where
    W: Eq + Hash + Clone,
{
    pub fn new() -> Self {
        Self::default()
    }

    /// Layout
    pub fn layout(mut self, layout: Rc<RefCell<GenericLayout<W>>>) -> Self {
        self.layout = layout;
        self
    }

    /// Display page.
    pub fn page(mut self, page: usize) -> Self {
        self.page = page;
        self
    }

    /// Base style.
    pub fn style(mut self, style: Style) -> Self {
        self.style = style;
        self
    }

    pub fn label_style(mut self, style: Style) -> Self {
        self.label_style = Some(style);
        self
    }

    pub fn label_alignment(mut self, alignment: Alignment) -> Self {
        self.label_alignment = Some(alignment);
        self
    }

    /// Set all styles.
    pub fn styles(mut self, styles: PagerStyle) -> Self {
        self.style = styles.style;
        if let Some(label) = styles.label_style {
            self.label_style = Some(label);
        }
        if let Some(alignment) = styles.label_alignment {
            self.label_alignment = Some(alignment);
        }
        self
    }

    /// Create the second stage.
    #[allow(clippy::needless_lifetimes)]
    pub fn into_buffer<'b>(
        self,
        area: Rect,
        buf: Rc<RefCell<&'b mut Buffer>>,
    ) -> PagerBuffer<'b, W> {
        let page_size = self.layout.borrow().page_size();
        let page_area = Rect::new(
            0,
            self.page as u16 * page_size.height,
            page_size.width,
            page_size.height,
        );

        PagerBuffer {
            layout: self.layout,
            page_area,
            widget_area: area,
            buffer: buf,
            label_style: self.label_style,
            label_alignment: self.label_alignment,
        }
    }
}

impl<'a, W> PagerBuffer<'a, W>
where
    W: Eq + Hash + Clone,
{
    /// Is the widget visible.
    #[inline]
    pub fn is_visible(&self, idx: usize) -> bool {
        let area = self.layout.borrow().widget(idx);
        self.page_area.intersects(area)
    }

    /// Is the label visible.
    #[inline]
    pub fn is_label_visible(&self, idx: usize) -> bool {
        let area = self.layout.borrow().widget(idx);
        self.page_area.intersects(area)
    }

    /// Get the widget index.
    #[inline(always)]
    pub fn widget_idx(&self, widget: W) -> Option<usize> {
        self.layout.borrow().try_index_of(widget)
    }

    /// Render a manual label.
    #[inline(always)]
    pub fn render_label<FN, WW>(&mut self, idx: usize, render_fn: FN) -> bool
    where
        FN: FnOnce(&Option<Cow<'static, str>>) -> WW,
        WW: Widget,
    {
        let Some(label_area) = self.locate_area(self.layout.borrow().label(idx)) else {
            return false;
        };
        let layout = self.layout.borrow();
        let label_str = layout.label_str(idx);

        let mut buffer = self.buffer.borrow_mut();
        render_fn(label_str).render(label_area, *buffer);

        true
    }

    /// Render the label with the set style and alignment.
    #[inline(always)]
    pub fn render_auto_label(&mut self, idx: usize) -> bool {
        let Some(label_area) = self.locate_area(self.layout.borrow().label(idx)) else {
            return false;
        };
        let layout = self.layout.borrow();
        let Some(label_str) = layout.label_str(idx) else {
            return false;
        };

        let mut buffer = self.buffer.borrow_mut();
        let mut label = Line::from(label_str.as_ref());
        if let Some(style) = self.label_style {
            label = label.style(style)
        };
        if let Some(align) = self.label_alignment {
            label = label.alignment(align);
        }
        label.render(label_area, *buffer);

        true
    }

    /// Render a stateless widget.
    #[inline(always)]
    pub fn render_widget<FN, WW>(&mut self, idx: usize, render_fn: FN) -> bool
    where
        FN: FnOnce() -> WW,
        WW: Widget,
    {
        let Some(widget_area) = self.locate_area(self.layout.borrow().widget(idx)) else {
            return false;
        };

        let mut buffer = self.buffer.borrow_mut();
        render_fn().render(widget_area, *buffer);
        true
    }

    /// Render a stateful widget.
    #[inline(always)]
    pub fn render<FN, WW, SS>(&mut self, idx: usize, render_fn: FN, state: &mut SS) -> bool
    where
        FN: FnOnce() -> WW,
        WW: StatefulWidget<State = SS>,
    {
        let Some(widget_area) = self.locate_area(self.layout.borrow().widget(idx)) else {
            return false;
        };

        let mut buffer = self.buffer.borrow_mut();
        render_fn().render(widget_area, *buffer, state);

        true
    }

    /// Render a stateful widget.
    #[inline(always)]
    pub fn render_opt<FN, WW, SS>(&mut self, idx: usize, render_fn: FN, state: &mut SS) -> bool
    where
        FN: FnOnce() -> Option<WW>,
        WW: StatefulWidget<State = SS>,
    {
        let Some(widget_area) = self.locate_area(self.layout.borrow().widget(idx)) else {
            return false;
        };

        let mut buffer = self.buffer.borrow_mut();
        let widget = render_fn();
        if let Some(widget) = widget {
            widget.render(widget_area, *buffer, state);
        }

        true
    }

    /// Render a stateful widget.
    #[inline(always)]
    #[allow(clippy::question_mark)]
    pub fn render2<FN, WW, SS, R>(&mut self, idx: usize, render_fn: FN, state: &mut SS) -> Option<R>
    where
        FN: FnOnce() -> (WW, R),
        WW: StatefulWidget<State = SS>,
    {
        let Some(widget_area) = self.locate_area(self.layout.borrow().widget(idx)) else {
            return None;
        };

        let mut buffer = self.buffer.borrow_mut();
        let (widget, remainder) = render_fn();
        widget.render(widget_area, *buffer, state);

        Some(remainder)
    }

    /// Render all blocks for the current page.
    pub fn render_block(&mut self) {
        for (idx, block_area) in self.layout.borrow().block_area_iter().enumerate() {
            if let Some(block_area) = self.locate_area(*block_area) {
                let mut buffer = self.buffer.borrow_mut();
                self.layout.borrow().block(idx).render(block_area, *buffer);
            }
        }
    }

    /// Relocate the widget area to screen coordinates.
    /// Returns None if the widget is not visible.
    /// This clips the area to page_area.
    #[inline]
    pub fn locate_widget(&self, idx: usize) -> Option<Rect> {
        self.locate_area(self.layout.borrow().widget(idx))
    }

    /// Relocate the label area to screen coordinates.
    /// Returns None if the widget is not visible.
    /// This clips the area to page_area.
    #[inline]
    pub fn locate_label(&self, idx: usize) -> Option<Rect> {
        self.locate_area(self.layout.borrow().label(idx))
    }

    /// Relocate an area from layout coordinates to screen coordinates.
    /// A result None indicates that the area is invisible.
    ///
    /// This will clip the area to the page_area.
    #[inline]
    pub fn locate_area(&self, area: Rect) -> Option<Rect> {
        let area = self.page_area.intersection(area);
        if area.is_empty() {
            None
        } else {
            Some(Rect::new(
                area.x - self.page_area.x + self.widget_area.x,
                area.y - self.page_area.y + self.widget_area.y,
                area.width,
                area.height,
            ))
        }
    }

    /// Get access to the buffer during rendering a page.
    #[inline]
    pub fn buffer<'b>(&'b mut self) -> RefMut<'b, &'a mut Buffer> {
        self.buffer.borrow_mut()
    }
}