Lines
97.92 %
Functions
100 %
Branches
//! The wheel and the scrollbar parts: each moves the surface the pointer is on.
use crate::app::App;
use crate::event::{Intent, apply, apply_to_view, apply_to_window};
use crate::hits::Surface;
use crate::keymap;
use crate::window::WindowId;
use ratatui::layout::Position;
use tvui::BarPart;
/// One wheel step over `position`; nothing happens where nothing scrolls.
pub(super) fn step(app: &mut App, position: Position, up: bool) {
let Some((_, target, window)) = app.hits.hit_at(position) else {
return;
};
let Some(surface) = target.surface() else {
scroll(app, surface, window, line_intent(app, surface, window, up));
}
/// A click on a scrollbar part: an arrow steps a line, a page area a page, the thumb nothing.
pub(super) fn press_bar(app: &mut App, surface: Surface, part: BarPart) {
let intent = match part {
BarPart::Thumb => None,
BarPart::Up | BarPart::Down => line_intent(app, surface, None, part == BarPart::Up),
BarPart::PageUp | BarPart::PageDown => page_intent(app, surface, part == BarPart::PageDown),
scroll(app, surface, None, intent);
fn view_keys(app: &App, window: Option<WindowId>) -> keymap::KeyView<'_> {
match window {
Some(id) => keymap::in_window(app, id),
None => keymap::in_view(app),
/// The intent a line step stands for on `surface`. A list takes the view's own Up and Down,
/// so the mouse and the keys agree on every tab.
pub(super) fn line_intent(
app: &App,
surface: Surface,
window: Option<WindowId>,
up: bool,
) -> Option<Intent> {
match (surface, up) {
(Surface::List, up) => view_keys(app, window).bound(if up { "Up" } else { "Down" }),
(Surface::Pane | Surface::Modal, true) => Some(Intent::ScrollLineUp),
(Surface::Pane | Surface::Modal, false) => Some(Intent::ScrollLineDown),
(Surface::Options, true) => Some(Intent::SelectPrev),
(Surface::Options, false) => Some(Intent::SelectNext),
/// The intent a page step stands for on `surface`; a list takes the view's own PageUp and
/// PageDown, which turn the server page on Transactions.
fn page_intent(app: &App, surface: Surface, down: bool) -> Option<Intent> {
match (surface, down) {
(Surface::List, down) => {
keymap::in_view(app).bound(if down { "PageDown" } else { "PageUp" })
(Surface::Pane | Surface::Modal, true) => Some(Intent::ScrollPageDown),
(Surface::Pane | Surface::Modal, false) => Some(Intent::ScrollPageUp),
(Surface::Options, true) => Some(Intent::SelectPageDown),
(Surface::Options, false) => Some(Intent::SelectPageUp),
/// Applies `intent` to `surface` whatever holds the focus: the views take it straight, the
/// overlay and its drop-down are the focus whenever they are drawn.
fn scroll(app: &mut App, surface: Surface, window: Option<WindowId>, intent: Option<Intent>) {
let Some(intent) = intent else {
match (surface, window) {
(Surface::List | Surface::Pane, Some(id)) => apply_to_window(app, id, intent),
(Surface::List | Surface::Pane, None) => apply_to_view(app, intent),
(Surface::Modal | Surface::Options, _) => apply(app, intent),