From e9f34873b4511d25dd42a0bf336c6f2ec2894470 Mon Sep 17 00:00:00 2001 From: Thomas Frans Date: Thu, 11 May 2023 19:20:15 +0200 Subject: [PATCH] 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. --- src/ui/listview.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/ui/listview.rs b/src/ui/listview.rs index 593cbab..07f7966 100644 --- a/src/ui/listview.rs +++ b/src/ui/listview.rs @@ -188,9 +188,17 @@ impl ListView { } } - 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); + } } }