ListView: use checked subtraction between focus and scroll index

This commit is contained in:
Alexandre Bury 2018-02-11 18:26:02 -08:00
parent bbc3d3d5b7
commit 8f3441bc3c

View File

@ -294,6 +294,7 @@ impl View for ListView {
.unwrap_or(0); .unwrap_or(0);
if self.children.len() > req.y { if self.children.len() > req.y {
// Include a scroll bar
Vec2::new(label_width + 1 + view_size + 2, req.y) Vec2::new(label_width + 1 + view_size + 2, req.y)
} else { } else {
Vec2::new(label_width + 1 + view_size, self.children.len()) Vec2::new(label_width + 1 + view_size, self.children.len())
@ -372,13 +373,18 @@ impl View for ListView {
// Send the event to the focused child. // Send the event to the focused child.
let labels_width = self.labels_width(); let labels_width = self.labels_width();
if let ListChild::Row(_, ref mut view) = self.children[self.focus] { if let ListChild::Row(_, ref mut view) = self.children[self.focus] {
let y = self.focus - self.scrollbase.start_line;
// If self.focus < self.scrollbase.start_line, it means the focus is not
// in view. Something's fishy, so don't send the event.
if let Some(y) = self.focus.checked_sub(self.scrollbase.start_line) {
let offset = (labels_width + 1, y); let offset = (labels_width + 1, y);
let result = view.on_event(event.relativized(offset)); let result = view.on_event(event.relativized(offset));
if result.is_consumed() { if result.is_consumed() {
return result; return result;
} }
} }
}
// If the child ignored this event, change the focus. // If the child ignored this event, change the focus.
match event { match event {