From 760cf11d9249bf6da319605eda22bd8e94b802f9 Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Sun, 26 Aug 2018 12:13:12 -0700 Subject: [PATCH] Invalidate cache on mutable child access --- src/views/linear_layout.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/views/linear_layout.rs b/src/views/linear_layout.rs index 7720b51..e5722e1 100644 --- a/src/views/linear_layout.rs +++ b/src/views/linear_layout.rs @@ -182,6 +182,8 @@ impl LinearLayout { /// Returns a mutable reference to a child. pub fn get_child_mut(&mut self, i: usize) -> Option<&mut View> { + // Anything could happen to the child view, so bust the cache. + self.invalidate(); self.children.get_mut(i).map(|child| &mut *child.view) } @@ -190,13 +192,20 @@ impl LinearLayout { /// If `i` is within bounds, the removed child will be returned. pub fn remove_child(&mut self, i: usize) -> Option> { if i < self.children.len() { + // Any alteration means we should invalidate the cache. self.invalidate(); - if self.focus > i || (self.focus != 0 && self.focus == self.children.len() - 1) { + // Keep the same view focused. + if self.focus > i + || (self.focus != 0 && self.focus == self.children.len() - 1) + { self.focus -= 1; } + + // Return the wrapped view Some(self.children.remove(i).view) } else { + // This includes empty list None } }