Use IntoBoxedView as bound for LinearLayout

This commit is contained in:
Alexandre Bury 2019-11-04 13:45:15 -08:00
parent 0e4d4c5fbb
commit bf0fb488a1
2 changed files with 23 additions and 11 deletions

View File

@ -2,7 +2,7 @@ use crate::direction;
use crate::event::{AnyCb, Event, EventResult, Key}; use crate::event::{AnyCb, Event, EventResult, Key};
use crate::rect::Rect; use crate::rect::Rect;
use crate::vec::Vec2; use crate::vec::Vec2;
use crate::view::{Selector, SizeCache, View}; use crate::view::{IntoBoxedView, Selector, SizeCache, View};
use crate::Printer; use crate::Printer;
use crate::With; use crate::With;
use crate::XY; use crate::XY;
@ -155,14 +155,14 @@ impl LinearLayout {
/// Adds a child to the layout. /// Adds a child to the layout.
/// ///
/// Chainable variant. /// Chainable variant.
pub fn child<V: View + 'static>(self, view: V) -> Self { pub fn child<V: IntoBoxedView + 'static>(self, view: V) -> Self {
self.with(|s| s.add_child(view)) self.with(|s| s.add_child(view))
} }
/// Adds a child to the layout. /// Adds a child to the layout.
pub fn add_child<V: View + 'static>(&mut self, view: V) { pub fn add_child<V: IntoBoxedView + 'static>(&mut self, view: V) {
self.children.push(Child { self.children.push(Child {
view: Box::new(view), view: view.as_boxed_view(),
size: Vec2::zero(), size: Vec2::zero(),
weight: 0, weight: 0,
}); });
@ -174,11 +174,15 @@ impl LinearLayout {
/// # Panics /// # Panics
/// ///
/// Panics if `i > self.len()`. /// Panics if `i > self.len()`.
pub fn insert_child<V: View + 'static>(&mut self, i: usize, view: V) { pub fn insert_child<V: IntoBoxedView + 'static>(
&mut self,
i: usize,
view: V,
) {
self.children.insert( self.children.insert(
i, i,
Child { Child {
view: Box::new(view), view: view.as_boxed_view(),
size: Vec2::zero(), size: Vec2::zero(),
weight: 0, weight: 0,
}, },

View File

@ -2,7 +2,7 @@ use crate::direction;
use crate::event::{AnyCb, Callback, Event, EventResult, Key}; use crate::event::{AnyCb, Callback, Event, EventResult, Key};
use crate::rect::Rect; use crate::rect::Rect;
use crate::vec::Vec2; use crate::vec::Vec2;
use crate::view::{Selector, View}; use crate::view::{IntoBoxedView, Selector, View};
use crate::Cursive; use crate::Cursive;
use crate::Printer; use crate::Printer;
use crate::With; use crate::With;
@ -90,10 +90,14 @@ impl ListView {
} }
/// Adds a view to the end of the list. /// Adds a view to the end of the list.
pub fn add_child<V: View + 'static>(&mut self, label: &str, mut view: V) { pub fn add_child<V: IntoBoxedView + 'static>(
&mut self,
label: &str,
view: V,
) {
let mut view = view.as_boxed_view();
view.take_focus(direction::Direction::none()); view.take_focus(direction::Direction::none());
self.children self.children.push(ListChild::Row(label.to_string(), view));
.push(ListChild::Row(label.to_string(), Box::new(view)));
} }
/// Removes all children from this view. /// Removes all children from this view.
@ -105,7 +109,11 @@ impl ListView {
/// Adds a view to the end of the list. /// Adds a view to the end of the list.
/// ///
/// Chainable variant. /// Chainable variant.
pub fn child<V: View + 'static>(self, label: &str, view: V) -> Self { pub fn child<V: IntoBoxedView + 'static>(
self,
label: &str,
view: V,
) -> Self {
self.with(|s| s.add_child(label, view)) self.with(|s| s.add_child(label, view))
} }