From 9b3b16f64dbcce1920f18252daa19b07ba3ad14d Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Thu, 18 Oct 2018 13:36:17 -0700 Subject: [PATCH] Fix important_size for SelectView and ListView --- src/rect.rs | 13 +++++++++---- src/view/view.rs | 2 +- src/views/list_view.rs | 15 +++++++++------ src/views/select_view.rs | 6 ++++-- 4 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/rect.rs b/src/rect.rs index 6918546c4..8a92af7d6 100644 --- a/src/rect.rs +++ b/src/rect.rs @@ -3,8 +3,6 @@ use std::ops::Add; use vec::Vec2; /// A non-empty rectangle on the 2D grid. -/// -/// #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct Rect { /// Top-left corner, inclusive @@ -38,7 +36,7 @@ where impl Rect { /// Creates a new `Rect` with the given position and size. /// - /// `size` must be non-zero in each axis. + /// The minimum size will `(1, 1)`. pub fn from_size(top_left: U, size: V) -> Self where U: Into, @@ -46,7 +44,6 @@ impl Rect { { let size = size.into(); let top_left = top_left.into(); - assert!(size > Vec2::zero()); let bottom_right = top_left + size.saturating_sub((1, 1)); @@ -119,21 +116,29 @@ impl Rect { } /// Returns the top-left corner. + /// + /// This is inclusive. pub fn top_left(self) -> Vec2 { self.top_left } /// Returns the bottom-right corner. + /// + /// This is inclusive. pub fn bottom_right(self) -> Vec2 { self.bottom_right } /// Returns the top-right corner. + /// + /// This is inclusive. pub fn top_right(self) -> Vec2 { Vec2::new(self.right(), self.top()) } /// Returns the bottom-left corner. + /// + /// This is inclusive. pub fn bottom_left(self) -> Vec2 { Vec2::new(self.left(), self.bottom()) } diff --git a/src/view/view.rs b/src/view/view.rs index 2c3254dff..3f8fb287b 100644 --- a/src/view/view.rs +++ b/src/view/view.rs @@ -106,6 +106,6 @@ pub trait View: Any + AnyView { /// /// Default implementation return the entire view. fn important_area(&self, view_size: Vec2) -> Rect { - Rect::from_corners((0, 0), view_size) + Rect::from_size((0, 0), view_size) } } diff --git a/src/views/list_view.rs b/src/views/list_view.rs index 656c1d9a4..cfd0c6396 100644 --- a/src/views/list_view.rs +++ b/src/views/list_view.rs @@ -239,11 +239,13 @@ fn try_focus( ) -> Option { match *child { ListChild::Delimiter => None, - ListChild::Row(_, ref mut view) => if view.take_focus(source) { - Some(i) - } else { - None - }, + ListChild::Row(_, ref mut view) => { + if view.take_focus(source) { + Some(i) + } else { + None + } + } } } @@ -409,7 +411,8 @@ impl View for ListView { let area = match self.children[self.focus] { ListChild::Row(_, ref view) => { - let available = Vec2::new(size.x - labels_width - 1, 1); + let available = + Vec2::new(size.x.saturating_sub(labels_width + 1), 1); view.important_area(available) + (labels_width, 0) } ListChild::Delimiter => Rect::from_size((0, 0), (size.x, 1)), diff --git a/src/views/select_view.rs b/src/views/select_view.rs index 856952645..08a39f2a6 100644 --- a/src/views/select_view.rs +++ b/src/views/select_view.rs @@ -510,7 +510,8 @@ impl SelectView { .checked_sub(offset) .map(|position| { position < self.last_size && position.y < self.len() - }).unwrap_or(false) => + }) + .unwrap_or(false) => { self.focus.set(position.y - offset.y) } @@ -523,7 +524,8 @@ impl SelectView { .checked_sub(offset) .map(|position| { position < self.last_size && position.y == self.focus() - }).unwrap_or(false) => + }) + .unwrap_or(false) => { return self.submit(); }