fix(ui): ListView decrement selected on remove

When removing an element, it is important to check whether it was the
last element in the list and decrement the selected index if so.
This commit is contained in:
Thomas Frans
2023-05-11 19:20:15 +02:00
committed by Henrik Friedrichsen
parent aeff120e67
commit e9f34873b4

View File

@@ -188,9 +188,17 @@ impl<I: ListItem + Clone> ListView<I> {
}
}
pub fn remove(&self, index: usize) {
/// Remove the item at `index` from the list.
///
/// # Panics
///
/// Panics if `index` is out of bounds.
pub fn remove(&mut self, index: usize) {
let mut c = self.content.write().unwrap();
c.remove(index);
if self.selected >= c.len() {
self.selected = self.selected.saturating_sub(1);
}
}
}