From bf0fb488a1e3bcfd7a43d04577c26e394e258989 Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Mon, 4 Nov 2019 13:45:15 -0800 Subject: [PATCH] Use IntoBoxedView as bound for LinearLayout --- src/views/linear_layout.rs | 16 ++++++++++------ src/views/list_view.rs | 18 +++++++++++++----- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/views/linear_layout.rs b/src/views/linear_layout.rs index 4d78d01..5fe2fc4 100644 --- a/src/views/linear_layout.rs +++ b/src/views/linear_layout.rs @@ -2,7 +2,7 @@ use crate::direction; use crate::event::{AnyCb, Event, EventResult, Key}; use crate::rect::Rect; use crate::vec::Vec2; -use crate::view::{Selector, SizeCache, View}; +use crate::view::{IntoBoxedView, Selector, SizeCache, View}; use crate::Printer; use crate::With; use crate::XY; @@ -155,14 +155,14 @@ impl LinearLayout { /// Adds a child to the layout. /// /// Chainable variant. - pub fn child(self, view: V) -> Self { + pub fn child(self, view: V) -> Self { self.with(|s| s.add_child(view)) } /// Adds a child to the layout. - pub fn add_child(&mut self, view: V) { + pub fn add_child(&mut self, view: V) { self.children.push(Child { - view: Box::new(view), + view: view.as_boxed_view(), size: Vec2::zero(), weight: 0, }); @@ -174,11 +174,15 @@ impl LinearLayout { /// # Panics /// /// Panics if `i > self.len()`. - pub fn insert_child(&mut self, i: usize, view: V) { + pub fn insert_child( + &mut self, + i: usize, + view: V, + ) { self.children.insert( i, Child { - view: Box::new(view), + view: view.as_boxed_view(), size: Vec2::zero(), weight: 0, }, diff --git a/src/views/list_view.rs b/src/views/list_view.rs index 465e4f7..dc78275 100644 --- a/src/views/list_view.rs +++ b/src/views/list_view.rs @@ -2,7 +2,7 @@ use crate::direction; use crate::event::{AnyCb, Callback, Event, EventResult, Key}; use crate::rect::Rect; use crate::vec::Vec2; -use crate::view::{Selector, View}; +use crate::view::{IntoBoxedView, Selector, View}; use crate::Cursive; use crate::Printer; use crate::With; @@ -90,10 +90,14 @@ impl ListView { } /// Adds a view to the end of the list. - pub fn add_child(&mut self, label: &str, mut view: V) { + pub fn add_child( + &mut self, + label: &str, + view: V, + ) { + let mut view = view.as_boxed_view(); view.take_focus(direction::Direction::none()); - self.children - .push(ListChild::Row(label.to_string(), Box::new(view))); + self.children.push(ListChild::Row(label.to_string(), view)); } /// Removes all children from this view. @@ -105,7 +109,11 @@ impl ListView { /// Adds a view to the end of the list. /// /// Chainable variant. - pub fn child(self, label: &str, view: V) -> Self { + pub fn child( + self, + label: &str, + view: V, + ) -> Self { self.with(|s| s.add_child(label, view)) }