From fde10910515ab734f96de0a7dea704f7d7e0f43f Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Thu, 13 Oct 2016 11:57:10 -0700 Subject: [PATCH] Fix focus in ListView --- src/views/list_view.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/views/list_view.rs b/src/views/list_view.rs index 9790976..bfe9746 100644 --- a/src/views/list_view.rs +++ b/src/views/list_view.rs @@ -50,13 +50,15 @@ impl ListView { } /// Adds a view to the end of the list. - pub fn add_child(&mut self, label: &str, view: V) { + pub fn add_child(&mut self, label: &str, mut view: V) { + view.take_focus(direction::Direction::none()); self.children.push(Child::Row(label.to_string(), Box::new(view))); } /// Removes all children from this view. pub fn clear(&mut self) { self.children.clear(); + self.focus = 0; } /// Adds a view to the end of the list. @@ -140,6 +142,10 @@ fn try_focus((i, child): (usize, &mut Child), source: direction::Direction) impl View for ListView { fn draw(&self, printer: &Printer) { + if self.children.is_empty() { + return; + } + let offset = self.children .iter() .map(Child::label) @@ -200,6 +206,10 @@ impl View for ListView { } fn on_event(&mut self, event: Event) -> EventResult { + if self.children.is_empty() { + return EventResult::Ignored; + } + if let Child::Row(_, ref mut view) = self.children[self.focus] { let result = view.on_event(event); if result.is_consumed() {