Skip to content

Commit

Permalink
fix: do not move offset on unselect
Browse files Browse the repository at this point in the history
  • Loading branch information
EdJoPaTo committed Feb 26, 2024
1 parent d3386fe commit 15f05be
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
3 changes: 3 additions & 0 deletions examples/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::Down => app.state.key_down(&app.items),
KeyCode::Up => app.state.key_up(&app.items),
KeyCode::Esc => {
app.state.select(Vec::new());
}
KeyCode::Home => {
app.state.select_first(&app.items);
}
Expand Down
34 changes: 18 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,20 +201,20 @@ where
}
let available_height = area.height as usize;

let selected_index = if state.selected.is_empty() {
0
} else {
visible
.iter()
.position(|flattened| flattened.identifier == state.selected)
.unwrap_or(0)
};
let ensure_index_in_view =
if state.ensure_selected_in_view_on_next_render && !state.selected.is_empty() {
visible
.iter()
.position(|flattened| flattened.identifier == state.selected)
} else {
None
};

// Ensure last line is still visible
let mut start = state.offset.min(visible.len().saturating_sub(1));

if state.ensure_selected_in_view_on_next_render {
start = start.min(selected_index);
if let Some(ensure_index_in_view) = ensure_index_in_view {
start = start.min(ensure_index_in_view);
}

let mut end = start;
Expand All @@ -231,12 +231,14 @@ where
end += 1;
}

while state.ensure_selected_in_view_on_next_render && selected_index >= end {
height += visible[end].item.height();
end += 1;
while height > available_height {
height = height.saturating_sub(visible[start].item.height());
start += 1;
if let Some(ensure_index_in_view) = ensure_index_in_view {
while ensure_index_in_view >= end {
height += visible[end].item.height();
end += 1;
while height > available_height {
height = height.saturating_sub(visible[start].item.height());
start += 1;
}
}
}

Expand Down

0 comments on commit 15f05be

Please sign in to comment.