make mousewheel scroll instead of change selection

This commit is contained in:
Robin Appelman 2024-09-16 10:57:17 +02:00
commit 5e01154d24
3 changed files with 16 additions and 2 deletions

View file

@ -86,8 +86,8 @@ fn handle_events(page: UiPage, ui_state: &UiState) -> io::Result<Option<UiEvent>
}
Event::Mouse(mouse) => {
return Ok(match mouse.kind {
MouseEventKind::ScrollUp => Some(UiEvent::Up(1, false)),
MouseEventKind::ScrollDown => Some(UiEvent::Down(1, false)),
MouseEventKind::ScrollUp => Some(UiEvent::Scroll(-1)),
MouseEventKind::ScrollDown => Some(UiEvent::Scroll(1)),
MouseEventKind::Down(MouseButton::Left) => {
find_hit_row(mouse.row, ui_state).map(UiEvent::Enter)
}

View file

@ -259,6 +259,12 @@ impl<'a> UiState<'a> {
}
(true, state)
}
(mut state, UiEvent::Scroll(step)) => {
if let Some(table_state) = state.table_state_mut() {
table_state.scroll(step);
}
(true, state)
}
(mut state, UiEvent::SelectAt(selected)) => {
if let Some(table_state) = state.table_state_mut() {
table_state.select(selected);
@ -333,6 +339,7 @@ pub enum UiEvent {
Back,
Up(usize, bool),
Down(usize, bool),
Scroll(isize),
Errors,
Select,
#[allow(dead_code)]

View file

@ -100,6 +100,13 @@ impl ScrollbarTableState {
after
}
pub fn scroll(&mut self, step: isize) {
*self.table.offset_mut() = self.table.offset().saturating_add_signed(step);
let selected = self.selected().saturating_add_signed(step).min(self.count - 1);
self.table.select(Some(selected));
self.scrollbar = self.scrollbar.position(selected);
}
pub fn select(&mut self, selected: usize) {
self.table.select(Some(selected));
self.scrollbar = self.scrollbar.position(selected);