use crossterm::event::{MouseButton, MouseEvent, MouseEventKind};
use ratatui::layout::{Position, Rect};
pub trait PickerRows {
fn last_rect(&self) -> Rect;
fn select_next(&mut self);
fn select_prev(&mut self);
fn row_index_at(&self, y: u16) -> Option<usize>;
fn select(&mut self, idx: usize);
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PickerMouse {
Stay,
Close,
Accept,
}
const WHEEL_ROWS: usize = 3;
pub fn answer_mouse(picker: &mut impl PickerRows, m: MouseEvent) -> PickerMouse {
let inside = picker.last_rect().contains(Position::new(m.column, m.row));
match m.kind {
MouseEventKind::ScrollDown => {
for _ in 0..WHEEL_ROWS {
picker.select_next();
}
PickerMouse::Stay
}
MouseEventKind::ScrollUp => {
for _ in 0..WHEEL_ROWS {
picker.select_prev();
}
PickerMouse::Stay
}
MouseEventKind::Down(MouseButton::Left) | MouseEventKind::Down(MouseButton::Right)
if !inside =>
{
PickerMouse::Close
}
MouseEventKind::Down(MouseButton::Left) => match picker.row_index_at(m.row) {
Some(idx) => {
picker.select(idx);
PickerMouse::Accept
}
None => PickerMouse::Stay,
},
_ => PickerMouse::Stay,
}
}
macro_rules! picker_rows {
($($picker:ty),* $(,)?) => {$(
impl PickerRows for $picker {
fn last_rect(&self) -> Rect {
self.last_rect
}
fn select_next(&mut self) {
<$picker>::select_next(self)
}
fn select_prev(&mut self) {
<$picker>::select_prev(self)
}
fn row_index_at(&self, y: u16) -> Option<usize> {
<$picker>::row_index_at(self, y)
}
fn select(&mut self, idx: usize) {
self.selected = idx;
}
}
)*};
}
picker_rows!(
crate::widgets::file_finder::FileFinder,
crate::widgets::command_palette::CommandPalette,
crate::widgets::symbol_picker::SymbolPicker,
crate::widgets::workspace_symbols::WorkspaceSymbolPicker,
crate::widgets::process_picker::ProcessPicker,
crate::widgets::zoxide_jump::ZoxideJump,
crate::widgets::branch_picker::BranchPicker,
crate::widgets::list_picker::ListPicker,
);
#[cfg(test)]
mod tests {
use super::*;
use crossterm::event::KeyModifiers;
struct Rows {
selected: usize,
}
const COUNT: usize = 10;
const LIST_TOP: u16 = 3;
impl PickerRows for Rows {
fn last_rect(&self) -> Rect {
Rect::new(10, 0, 40, 20)
}
fn select_next(&mut self) {
self.selected = (self.selected + 1).min(COUNT - 1);
}
fn select_prev(&mut self) {
self.selected = self.selected.saturating_sub(1);
}
fn row_index_at(&self, y: u16) -> Option<usize> {
let idx = usize::from(y.checked_sub(LIST_TOP)?);
(idx < COUNT).then_some(idx)
}
fn select(&mut self, idx: usize) {
self.selected = idx;
}
}
fn event(kind: MouseEventKind, column: u16, row: u16) -> MouseEvent {
MouseEvent {
kind,
column,
row,
modifiers: KeyModifiers::NONE,
}
}
#[test]
fn the_wheel_moves_the_selection_three_rows_and_stops_at_the_ends() {
let mut rows = Rows { selected: 0 };
let down = event(MouseEventKind::ScrollDown, 20, 5);
assert_eq!(answer_mouse(&mut rows, down), PickerMouse::Stay);
assert_eq!(rows.selected, 3);
for _ in 0..5 {
answer_mouse(&mut rows, down);
}
assert_eq!(rows.selected, COUNT - 1, "the last row stops the wheel");
let up = event(MouseEventKind::ScrollUp, 20, 5);
assert_eq!(answer_mouse(&mut rows, up), PickerMouse::Stay);
assert_eq!(rows.selected, COUNT - 4);
for _ in 0..5 {
answer_mouse(&mut rows, up);
}
assert_eq!(rows.selected, 0, "the first row stops the wheel");
}
#[test]
fn the_wheel_moves_the_selection_wherever_the_pointer_is() {
let mut rows = Rows { selected: 0 };
let outside = event(MouseEventKind::ScrollDown, 0, 30);
assert_eq!(answer_mouse(&mut rows, outside), PickerMouse::Stay);
assert_eq!(rows.selected, 3);
}
#[test]
fn a_left_or_right_press_outside_closes_the_picker() {
for button in [MouseButton::Left, MouseButton::Right] {
for (column, row) in [(9, 5), (50, 5), (20, 20), (0, 0)] {
let mut rows = Rows { selected: 2 };
let press = event(MouseEventKind::Down(button), column, row);
assert_eq!(
answer_mouse(&mut rows, press),
PickerMouse::Close,
"{button:?} press at {column},{row}"
);
assert_eq!(rows.selected, 2, "closing moves no selection");
}
}
}
#[test]
fn a_left_press_on_a_row_selects_and_takes_it() {
let mut rows = Rows { selected: 0 };
let press = event(MouseEventKind::Down(MouseButton::Left), 20, LIST_TOP + 4);
assert_eq!(answer_mouse(&mut rows, press), PickerMouse::Accept);
assert_eq!(rows.selected, 4);
}
#[test]
fn a_press_inside_but_on_no_row_asks_for_nothing() {
for row in [1, LIST_TOP + COUNT as u16] {
let mut rows = Rows { selected: 2 };
let press = event(MouseEventKind::Down(MouseButton::Left), 20, row);
assert_eq!(answer_mouse(&mut rows, press), PickerMouse::Stay);
assert_eq!(rows.selected, 2);
}
}
#[test]
fn a_right_press_inside_and_every_other_event_ask_for_nothing() {
for kind in [
MouseEventKind::Down(MouseButton::Right),
MouseEventKind::Down(MouseButton::Middle),
MouseEventKind::Up(MouseButton::Left),
MouseEventKind::Drag(MouseButton::Left),
MouseEventKind::Moved,
MouseEventKind::ScrollLeft,
MouseEventKind::ScrollRight,
] {
let mut rows = Rows { selected: 2 };
let press = event(kind, 20, LIST_TOP + 4);
assert_eq!(
answer_mouse(&mut rows, press),
PickerMouse::Stay,
"{kind:?}"
);
assert_eq!(rows.selected, 2, "{kind:?}");
}
}
}