Add LinearLayout::set_focus_index

This commit is contained in:
Alexandre Bury 2019-10-14 10:03:02 -07:00
parent de0d76f796
commit aa74e691e1

View File

@ -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;