1
//! The wheel and the scrollbar parts: each moves the surface the pointer is on.
2

            
3
use crate::app::App;
4
use crate::event::{Intent, apply, apply_to_view, apply_to_window};
5
use crate::hits::Surface;
6
use crate::keymap;
7
use crate::window::WindowId;
8
use ratatui::layout::Position;
9
use tvui::BarPart;
10

            
11
/// One wheel step over `position`; nothing happens where nothing scrolls.
12
122
pub(super) fn step(app: &mut App, position: Position, up: bool) {
13
122
    let Some((_, target, window)) = app.hits.hit_at(position) else {
14
        return;
15
    };
16
122
    let Some(surface) = target.surface() else {
17
11
        return;
18
    };
19
111
    scroll(app, surface, window, line_intent(app, surface, window, up));
20
122
}
21

            
22
/// A click on a scrollbar part: an arrow steps a line, a page area a page, the thumb nothing.
23
85
pub(super) fn press_bar(app: &mut App, surface: Surface, part: BarPart) {
24
85
    let intent = match part {
25
12
        BarPart::Thumb => None,
26
33
        BarPart::Up | BarPart::Down => line_intent(app, surface, None, part == BarPart::Up),
27
40
        BarPart::PageUp | BarPart::PageDown => page_intent(app, surface, part == BarPart::PageDown),
28
    };
29
85
    scroll(app, surface, None, intent);
30
85
}
31

            
32
93
fn view_keys(app: &App, window: Option<WindowId>) -> keymap::KeyView<'_> {
33
93
    match window {
34
75
        Some(id) => keymap::in_window(app, id),
35
18
        None => keymap::in_view(app),
36
    }
37
93
}
38

            
39
/// The intent a line step stands for on `surface`. A list takes the view's own Up and Down,
40
/// so the mouse and the keys agree on every tab.
41
162
pub(super) fn line_intent(
42
162
    app: &App,
43
162
    surface: Surface,
44
162
    window: Option<WindowId>,
45
162
    up: bool,
46
162
) -> Option<Intent> {
47
162
    match (surface, up) {
48
93
        (Surface::List, up) => view_keys(app, window).bound(if up { "Up" } else { "Down" }),
49
19
        (Surface::Pane | Surface::Modal, true) => Some(Intent::ScrollLineUp),
50
29
        (Surface::Pane | Surface::Modal, false) => Some(Intent::ScrollLineDown),
51
9
        (Surface::Options, true) => Some(Intent::SelectPrev),
52
12
        (Surface::Options, false) => Some(Intent::SelectNext),
53
    }
54
162
}
55

            
56
/// The intent a page step stands for on `surface`; a list takes the view's own PageUp and
57
/// PageDown, which turn the server page on Transactions.
58
40
fn page_intent(app: &App, surface: Surface, down: bool) -> Option<Intent> {
59
40
    match (surface, down) {
60
        (Surface::List, down) => {
61
16
            keymap::in_view(app).bound(if down { "PageDown" } else { "PageUp" })
62
        }
63
6
        (Surface::Pane | Surface::Modal, true) => Some(Intent::ScrollPageDown),
64
6
        (Surface::Pane | Surface::Modal, false) => Some(Intent::ScrollPageUp),
65
9
        (Surface::Options, true) => Some(Intent::SelectPageDown),
66
3
        (Surface::Options, false) => Some(Intent::SelectPageUp),
67
    }
68
40
}
69

            
70
/// Applies `intent` to `surface` whatever holds the focus: the views take it straight, the
71
/// overlay and its drop-down are the focus whenever they are drawn.
72
196
fn scroll(app: &mut App, surface: Surface, window: Option<WindowId>, intent: Option<Intent>) {
73
196
    let Some(intent) = intent else {
74
12
        return;
75
    };
76
184
    match (surface, window) {
77
91
        (Surface::List | Surface::Pane, Some(id)) => apply_to_window(app, id, intent),
78
43
        (Surface::List | Surface::Pane, None) => apply_to_view(app, intent),
79
50
        (Surface::Modal | Surface::Options, _) => apply(app, intent),
80
    }
81
196
}