From d330ecd656687bc508cf1854ebd1c0e4d07511e4 Mon Sep 17 00:00:00 2001 From: TomJGooding <101601846+TomJGooding@users.noreply.github.com> Date: Mon, 28 Oct 2024 18:58:02 +0000 Subject: [PATCH] handle unchanged index without always_update --- src/textual/widgets/_list_view.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/textual/widgets/_list_view.py b/src/textual/widgets/_list_view.py index afe0620156..a2ebbacc3e 100644 --- a/src/textual/widgets/_list_view.py +++ b/src/textual/widgets/_list_view.py @@ -268,10 +268,16 @@ def pop(self, index: Optional[int] = None) -> AwaitComplete: async def do_pop(): await item_to_remove.remove() if self.index is not None: - if normalized_index == self.index: + if normalized_index < self.index: + self.index -= 1 + elif normalized_index == self.index: + old_index = self.index + # Force a re-validation of the index self.index = self.index - elif normalized_index < self.index: - self.index = self.index - 1 + # If the index hasn't changed, the watcher won't be called + # but we need to update the highlighted item + if old_index == self.index: + self.watch_index(old_index, self.index) return AwaitComplete(do_pop()) @@ -299,7 +305,13 @@ async def do_remove_items(): if removed_before_highlighted: self.index -= removed_before_highlighted elif self.index in normalized_indices: + old_index = self.index + # Force a re-validation of the index self.index = self.index + # If the index hasn't changed, the watcher won't be called + # but we need to update the highlighted item + if old_index == self.index: + self.watch_index(old_index, self.index) return AwaitComplete(do_remove_items())