fix: do not move offset on unselect

This commit is contained in:
EdJoPaTo
2024-02-26 18:05:17 +01:00
parent d3386fe016
commit 15f05be46c
2 changed files with 21 additions and 16 deletions
+3
View File
@@ -143,6 +143,9 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> std::io::Res
KeyCode::Right => app.state.key_right(), KeyCode::Right => app.state.key_right(),
KeyCode::Down => app.state.key_down(&app.items), KeyCode::Down => app.state.key_down(&app.items),
KeyCode::Up => app.state.key_up(&app.items), KeyCode::Up => app.state.key_up(&app.items),
KeyCode::Esc => {
app.state.select(Vec::new());
}
KeyCode::Home => { KeyCode::Home => {
app.state.select_first(&app.items); app.state.select_first(&app.items);
} }
+9 -7
View File
@@ -201,20 +201,20 @@ where
} }
let available_height = area.height as usize; let available_height = area.height as usize;
let selected_index = if state.selected.is_empty() { let ensure_index_in_view =
0 if state.ensure_selected_in_view_on_next_render && !state.selected.is_empty() {
} else {
visible visible
.iter() .iter()
.position(|flattened| flattened.identifier == state.selected) .position(|flattened| flattened.identifier == state.selected)
.unwrap_or(0) } else {
None
}; };
// Ensure last line is still visible // Ensure last line is still visible
let mut start = state.offset.min(visible.len().saturating_sub(1)); let mut start = state.offset.min(visible.len().saturating_sub(1));
if state.ensure_selected_in_view_on_next_render { if let Some(ensure_index_in_view) = ensure_index_in_view {
start = start.min(selected_index); start = start.min(ensure_index_in_view);
} }
let mut end = start; let mut end = start;
@@ -231,7 +231,8 @@ where
end += 1; end += 1;
} }
while state.ensure_selected_in_view_on_next_render && selected_index >= end { if let Some(ensure_index_in_view) = ensure_index_in_view {
while ensure_index_in_view >= end {
height += visible[end].item.height(); height += visible[end].item.height();
end += 1; end += 1;
while height > available_height { while height > available_height {
@@ -239,6 +240,7 @@ where
start += 1; start += 1;
} }
} }
}
state.offset = start; state.offset = start;
state.ensure_selected_in_view_on_next_render = false; state.ensure_selected_in_view_on_next_render = false;