From aa74e691e1f84a66e01fe8c613e68d57f536f047 Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Mon, 14 Oct 2019 10:03:02 -0700 Subject: [PATCH] Add LinearLayout::set_focus_index --- src/views/linear_layout.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/views/linear_layout.rs b/src/views/linear_layout.rs index 7cc2ab0..af45501 100644 --- a/src/views/linear_layout.rs +++ b/src/views/linear_layout.rs @@ -207,6 +207,24 @@ impl LinearLayout { self.focus } + /// Attemps to set the focus on the given child. + /// + /// Returns `Err(())` if `index >= self.len()`, or if the view at the + /// given index does not accept focus. + pub fn set_focus_index(&mut self, index: usize) -> Result<(), ()> { + if self + .children + .get_mut(index) + .map(|child| child.view.take_focus(direction::Direction::none())) + .unwrap_or(false) + { + self.focus = index; + Ok(()) + } else { + Err(()) + } + } + // Invalidate the view, to request a layout next time fn invalidate(&mut self) { self.cache = None;