Add Scrollable trait

This commit is contained in:
Alexandre Bury 2018-06-26 17:40:15 -07:00
parent 88fb6e7c54
commit a47537ec2c
6 changed files with 33 additions and 20 deletions

View File

@ -2,7 +2,7 @@ extern crate cursive;
use cursive::align::HAlign; use cursive::align::HAlign;
use cursive::traits::*; use cursive::traits::*;
use cursive::views::{Dialog, DummyView, LinearLayout, ScrollView, TextView}; use cursive::views::{Dialog, DummyView, LinearLayout, TextView};
use cursive::Cursive; use cursive::Cursive;
// This example uses a LinearLayout to stick multiple views next to each other. // This example uses a LinearLayout to stick multiple views next to each other.
@ -25,9 +25,9 @@ fn main() {
// Disabling scrollable means the view cannot shrink. // Disabling scrollable means the view cannot shrink.
.child(TextView::new(text)) .child(TextView::new(text))
// The other views will share the remaining space. // The other views will share the remaining space.
.child(ScrollView::new(TextView::new(text))) .child(TextView::new(text).scrollable())
.child(ScrollView::new(TextView::new(text))) .child(TextView::new(text).scrollable())
.child(ScrollView::new(TextView::new(text))) .child(TextView::new(text).scrollable())
.fixed_width(30), .fixed_width(30),
).button("Quit", |s| s.quit()) ).button("Quit", |s| s.quit())
.h_align(HAlign::Center), .h_align(HAlign::Center),

View File

@ -1,7 +1,8 @@
extern crate cursive; extern crate cursive;
use cursive::align::HAlign; use cursive::align::HAlign;
use cursive::views::{Dialog, Panel, ScrollView, TextView}; use cursive::view::Scrollable;
use cursive::views::{Dialog, Panel, TextView};
use cursive::Cursive; use cursive::Cursive;
fn main() { fn main() {
@ -16,7 +17,7 @@ fn main() {
// The text is too long to fit on a line, so the view will wrap lines, // The text is too long to fit on a line, so the view will wrap lines,
// and will adapt to the terminal size. // and will adapt to the terminal size.
siv.add_fullscreen_layer( siv.add_fullscreen_layer(
Dialog::around(Panel::new(ScrollView::new(TextView::new(content)))) Dialog::around(Panel::new(TextView::new(content).scrollable()))
.title("Unicode and wide-character support") .title("Unicode and wide-character support")
// This is the alignment for the button // This is the alignment for the button
.h_align(HAlign::Center) .h_align(HAlign::Center)

View File

@ -1,7 +1,8 @@
extern crate cursive; extern crate cursive;
use cursive::traits::Boxable; use cursive::traits::Boxable;
use cursive::views::{Button, Canvas, Dialog, LinearLayout, ScrollView}; use cursive::view::Scrollable;
use cursive::views::{Button, Canvas, Dialog, LinearLayout};
use cursive::Printer; use cursive::Printer;
fn main() { fn main() {
@ -9,18 +10,12 @@ fn main() {
siv.add_layer( siv.add_layer(
Dialog::around( Dialog::around(
ScrollView::new( LinearLayout::vertical()
LinearLayout::vertical() .child(Button::new("Foo", |s| s.add_layer(Dialog::info("Ah"))))
.child(Button::new("Foo", |s| { .child(Canvas::new(()).with_draw(draw).fixed_size((120, 40)))
s.add_layer(Dialog::info("Ah")) .child(Button::new("Bar", |s| s.add_layer(Dialog::info("Uh"))))
})) .scrollable()
.child( .scroll_x(true),
Canvas::new(()).with_draw(draw).fixed_size((120, 40)),
)
.child(Button::new("Bar", |s| {
s.add_layer(Dialog::info("Uh"))
})),
).scroll_x(true),
).fixed_size((60, 30)), ).fixed_size((60, 30)),
); );

View File

@ -9,6 +9,7 @@
//! ``` //! ```
#[doc(no_inline)] #[doc(no_inline)]
pub use view::{Boxable, Finder, Identifiable, View}; pub use view::{Boxable, Finder, Identifiable, Scrollable, View};
#[doc(no_inline)] #[doc(no_inline)]
pub use With; pub use With;

View File

@ -52,6 +52,7 @@ mod view_path;
mod boxable; mod boxable;
mod identifiable; mod identifiable;
mod scroll; mod scroll;
mod scrollable;
mod into_boxed_view; mod into_boxed_view;
@ -63,6 +64,7 @@ pub use self::into_boxed_view::IntoBoxedView;
pub use self::margins::Margins; pub use self::margins::Margins;
pub use self::position::{Offset, Position}; pub use self::position::{Offset, Position};
pub use self::scroll::{ScrollBase, ScrollStrategy}; pub use self::scroll::{ScrollBase, ScrollStrategy};
pub use self::scrollable::Scrollable;
pub use self::size_cache::SizeCache; pub use self::size_cache::SizeCache;
pub use self::size_constraint::SizeConstraint; pub use self::size_constraint::SizeConstraint;
pub use self::view::View; pub use self::view::View;

14
src/view/scrollable.rs Normal file
View File

@ -0,0 +1,14 @@
use view::View;
use views::ScrollView;
/// Makes a view wrappable in a [`ScrollView`].
///
/// [`ScrollView`]: ::views::ScrollView
pub trait Scrollable: View + Sized {
/// Wraps `self` in a `ScrollView`.
fn scrollable(self) -> ScrollView<Self> {
ScrollView::new(self)
}
}
impl<T: View> Scrollable for T {}