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
use crate::event::ScrollOutcome;
use crate::{Scroll, ScrollState, ScrollbarPolicy};
use rat_event::{ct_event, flow, HandleEvent, MouseOnly};
use ratatui::buffer::Buffer;
use ratatui::layout::{Position, Rect};
use ratatui::widgets::{Block, ScrollbarOrientation, StatefulWidget, Widget};

///
/// Utility widget for layout/rendering the combined block and scrollbars.
///
/// It layouts the scrollbars on top of the border and leaves
/// the corners free if necessary.
///
#[derive(Debug, Default, Clone)]
pub struct ScrollArea<'a> {
    block: Option<Block<'a>>,
    h_scroll: Option<Scroll<'a>>,
    v_scroll: Option<Scroll<'a>>,
}

/// Temporary state for ScrollArea.
///
/// This state is not meant to keep, it just packages the widgets state
/// for use by ScrollArea.
#[derive(Debug)]
pub struct ScrollAreaState<'a> {
    pub area: Rect,
    pub h_scroll: Option<&'a mut ScrollState>,
    pub v_scroll: Option<&'a mut ScrollState>,
}

impl<'a> ScrollArea<'a> {
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets the block.
    pub fn block(mut self, block: Option<Block<'a>>) -> Self {
        self.block = block;
        self
    }

    /// Sets the horizontal scroll.
    pub fn h_scroll(mut self, scroll: Option<Scroll<'a>>) -> Self {
        self.h_scroll = scroll;
        self
    }

    /// Sets the vertical scroll.
    pub fn v_scroll(mut self, scroll: Option<Scroll<'a>>) -> Self {
        self.v_scroll = scroll;
        self
    }
}

impl<'a> ScrollArea<'a> {
    /// Calculates the inner area for the widget.
    ///
    /// __Panic__
    /// Panics if the orientation doesn't match,
    /// h_scroll doesn't accept ScrollBarOrientation::Vertical* and
    /// v_scroll doesn't accept ScrollBarOrientation::Horizontal*.
    ///
    /// Panics if the state doesn't contain the necessary scroll-states.
    pub fn inner(&self, area: Rect, state: ScrollAreaState<'_>) -> Rect {
        self.layout(area, &state).0
    }
}

impl<'a> ScrollArea<'a> {
    /// Calculate the layout for the given scrollbars.
    /// This prevents overlaps in the corners, if both scrollbars are
    /// visible, and tries to fit in the given block.
    ///
    /// Returns (inner, h_area, v_area).
    ///
    /// __Panic__
    /// Panics if the orientation doesn't match,
    /// h_scroll doesn't accept ScrollBarOrientation::Vertical* and
    /// v_scroll doesn't accept ScrollBarOrientation::Horizontal*.
    ///
    /// Panics if the state doesn't contain the necessary scroll-states.
    fn layout(&self, area: Rect, state: &ScrollAreaState<'_>) -> (Rect, Rect, Rect) {
        let mut inner = area;

        if let Some(block) = &self.block {
            inner = block.inner(area);
        }

        if let Some(h_scroll) = &self.h_scroll {
            let state = state.h_scroll.as_deref().expect("h_scroll state");
            let show = match h_scroll.get_policy() {
                ScrollbarPolicy::Always => true,
                ScrollbarPolicy::Minimize => true,
                ScrollbarPolicy::Collapse => state.max_offset > 0,
            };
            if show {
                match h_scroll.get_orientation() {
                    ScrollbarOrientation::VerticalRight => {
                        unimplemented!(
                        "ScrollbarOrientation::VerticalRight not supported for horizontal scrolling."
                    );
                    }
                    ScrollbarOrientation::VerticalLeft => {
                        unimplemented!(
                            "ScrollbarOrientation::VerticalLeft not supported for horizontal scrolling."
                        );
                    }
                    ScrollbarOrientation::HorizontalBottom => {
                        if inner.bottom() == area.bottom() {
                            inner.height = inner.height.saturating_sub(1);
                        }
                    }
                    ScrollbarOrientation::HorizontalTop => {
                        if inner.top() == area.top() {
                            inner.y += 1;
                            inner.height = inner.height.saturating_sub(1);
                        }
                    }
                }
            }
        }

        if let Some(v_scroll) = &self.v_scroll {
            let state = state.v_scroll.as_deref().expect("v_scroll state");
            let show = match v_scroll.get_policy() {
                ScrollbarPolicy::Always => true,
                ScrollbarPolicy::Minimize => true,
                ScrollbarPolicy::Collapse => state.max_offset > 0,
            };
            if show {
                match v_scroll.get_orientation() {
                    ScrollbarOrientation::VerticalRight => {
                        if inner.right() == area.right() {
                            inner.width = inner.width.saturating_sub(1);
                        }
                    }
                    ScrollbarOrientation::VerticalLeft => {
                        if inner.left() == area.left() {
                            inner.x += 1;
                            inner.width = inner.width.saturating_sub(1);
                        }
                    }
                    ScrollbarOrientation::HorizontalBottom => {
                        unimplemented!(
                            "ScrollbarOrientation::HorizontalBottom not supported for vertical scrolling."
                        );
                    }
                    ScrollbarOrientation::HorizontalTop => {
                        unimplemented!(
                            "ScrollbarOrientation::HorizontalTop not supported for vertical scrolling."
                        );
                    }
                }
            }
        }

        // horizontal
        let h_area = if let Some(h_scroll) = &self.h_scroll {
            let state = state.h_scroll.as_deref().expect("h_scroll state");
            let show = match h_scroll.get_policy() {
                ScrollbarPolicy::Always => true,
                ScrollbarPolicy::Minimize => true,
                ScrollbarPolicy::Collapse => state.max_offset > 0,
            };
            if show {
                match h_scroll.get_orientation() {
                    ScrollbarOrientation::HorizontalBottom => Rect::new(
                        inner.x + h_scroll.get_start_margin(),
                        area.bottom().saturating_sub(1),
                        inner.width.saturating_sub(
                            h_scroll.get_start_margin() + h_scroll.get_end_margin(),
                        ),
                        if area.height > 0 { 1 } else { 0 },
                    ),
                    ScrollbarOrientation::HorizontalTop => Rect::new(
                        inner.x + h_scroll.get_start_margin(),
                        area.y,
                        inner.width.saturating_sub(
                            h_scroll.get_start_margin() + h_scroll.get_end_margin(),
                        ),
                        if area.height > 0 { 1 } else { 0 },
                    ),
                    _ => unreachable!(),
                }
            } else {
                Rect::new(area.x, area.y, 0, 0)
            }
        } else {
            Rect::new(area.x, area.y, 0, 0)
        };

        // vertical
        let v_area = if let Some(v_scroll) = &self.v_scroll {
            let state = state.v_scroll.as_deref().expect("v_scroll state");
            let show = match v_scroll.get_policy() {
                ScrollbarPolicy::Always => true,
                ScrollbarPolicy::Minimize => true,
                ScrollbarPolicy::Collapse => state.max_offset > 0,
            };
            if show {
                match v_scroll.get_orientation() {
                    ScrollbarOrientation::VerticalRight => Rect::new(
                        area.right().saturating_sub(1),
                        inner.y + v_scroll.get_start_margin(),
                        if area.width > 0 { 1 } else { 0 },
                        inner.height.saturating_sub(
                            v_scroll.get_start_margin() + v_scroll.get_end_margin(),
                        ),
                    ),
                    ScrollbarOrientation::VerticalLeft => Rect::new(
                        area.x,
                        inner.y + v_scroll.get_start_margin(),
                        if area.width > 0 { 1 } else { 0 },
                        inner.height.saturating_sub(
                            v_scroll.get_start_margin() + v_scroll.get_end_margin(),
                        ),
                    ),
                    _ => unreachable!(),
                }
            } else {
                Rect::new(area.x, area.y, 0, 0)
            }
        } else {
            Rect::new(area.x, area.y, 0, 0)
        };

        (inner, h_area, v_area)
    }
}

impl<'a> StatefulWidget for ScrollArea<'a> {
    type State = ScrollAreaState<'a>;

    fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
        let (_inner, h_scroll, v_scroll) = self.layout(area, state);

        if let Some(block) = self.block {
            block.render(area, buf);
        }
        if let Some(h) = self.h_scroll {
            h.render(
                h_scroll,
                buf,
                state.h_scroll.as_deref_mut().expect("h_scroll state"),
            );
        }
        if let Some(v) = self.v_scroll {
            v.render(
                v_scroll,
                buf,
                state.v_scroll.as_deref_mut().expect("v_scroll state"),
            )
        }
    }
}

///
/// Handle scrolling for the whole area spanned by the two scroll-states.
///
impl<'a> HandleEvent<crossterm::event::Event, MouseOnly, ScrollOutcome> for ScrollAreaState<'a> {
    fn handle(&mut self, event: &crossterm::event::Event, _qualifier: MouseOnly) -> ScrollOutcome {
        let area = self.area;

        if let Some(h_scroll) = &mut self.h_scroll {
            flow!(match event {
                // right scroll with ALT down. shift doesn't work?
                ct_event!(scroll ALT down for column, row) => {
                    if area.contains(Position::new(*column, *row)) {
                        ScrollOutcome::Right(h_scroll.scroll_by())
                    } else {
                        ScrollOutcome::Continue
                    }
                }
                // left scroll with ALT up. shift doesn't work?
                ct_event!(scroll ALT up for column, row) => {
                    if area.contains(Position::new(*column, *row)) {
                        ScrollOutcome::Left(h_scroll.scroll_by())
                    } else {
                        ScrollOutcome::Continue
                    }
                }
                _ => ScrollOutcome::Continue,
            });
            flow!(h_scroll.handle(event, MouseOnly));
        }
        if let Some(v_scroll) = &mut self.v_scroll {
            flow!(match event {
                ct_event!(scroll down for column, row) => {
                    if area.contains(Position::new(*column, *row)) {
                        ScrollOutcome::Down(v_scroll.scroll_by())
                    } else {
                        ScrollOutcome::Continue
                    }
                }
                ct_event!(scroll up for column, row) => {
                    if area.contains(Position::new(*column, *row)) {
                        ScrollOutcome::Up(v_scroll.scroll_by())
                    } else {
                        ScrollOutcome::Continue
                    }
                }
                _ => ScrollOutcome::Continue,
            });
            flow!(v_scroll.handle(event, MouseOnly));
        }

        ScrollOutcome::Continue
    }
}