Add Dialog::empty and Dialog::content

Allows to set the content after the title, for when it's more
readable.
This commit is contained in:
Alexandre Bury 2016-07-25 20:43:01 -07:00
parent 7283e7f3af
commit 5dd20db676
5 changed files with 46 additions and 19 deletions

View File

@ -5,7 +5,10 @@ use cursive::prelude::*;
fn main() {
let mut siv = Cursive::new();
siv.add_layer(Dialog::new(ListView::new()
siv.add_layer(Dialog::empty()
.title("Please fill out this form")
.button("Ok", |s| s.quit())
.content(ListView::new()
.child("Name", EditView::new().min_length(10))
.child("Email",
LinearLayout::horizontal()
@ -27,7 +30,8 @@ fn main() {
}))
.delimiter()
.child("Age",
SelectView::new().popup()
SelectView::new()
.popup()
.item_str("0-18")
.item_str("19-30")
.item_str("31-40")
@ -36,9 +40,7 @@ fn main() {
for i in 0..50 {
list.add_child(&format!("Item {}", i), EditView::new());
}
}))
.title("Please fill out this form")
.button("Ok", |s| s.quit()));
})));
siv.run();
}

View File

@ -15,12 +15,11 @@ fn main() {
// The text is too long to fit on a line, so the view will wrap lines,
// and will adapt to the terminal size.
siv.add_layer(Dialog::new(TextView::new(&content))
.h_align(HAlign::Center)
.button("Quit", |s| s.quit()));
.h_align(HAlign::Center)
.button("Quit", |s| s.quit()));
// Show a popup on top of the view.
siv.add_layer(Dialog::new(TextView::new("Try resizing the terminal!\n(Press 'q' to quit \
when you're done.)"))
.dismiss_button("Ok"));
siv.add_layer(Dialog::info("Try resizing the terminal!\n(Press 'q' to \
quit when you're done.)"));
siv.run();
}

View File

@ -10,12 +10,15 @@ use std::sync::atomic::{AtomicUsize, Ordering};
fn main() {
let mut siv = Cursive::new();
siv.add_layer(Dialog::new(Button::new("Start", |s| {
siv.add_layer(Dialog::empty()
.title("Progress bar example")
.padding((0, 0, 1, 1))
.content(Button::new("Start", |s| {
// These two values will allow us to communicate.
let value = Arc::new(AtomicUsize::new(0));
let cb = Arc::new(Mutex::new(None));
let n_max = 1000;
let n_max = 100;
s.pop_layer();
s.add_layer(Panel::new(FullView::full_width(ProgressBar::new()
@ -26,21 +29,19 @@ fn main() {
// Spawn a thread to process things in the background.
thread::spawn(move || {
for _ in 0..n_max {
thread::sleep(Duration::from_millis(3));
thread::sleep(Duration::from_millis(20));
value.fetch_add(1, Ordering::Relaxed);
}
*cb.lock().unwrap() = Some(Box::new(move |s| {
s.pop_layer();
s.add_layer(Dialog::new(TextView::new("Phew, that was \
a lot of work!"))
s.add_layer(Dialog::empty()
.title("Work done!")
.content(TextView::new("Phew, that was some work!"))
.button("Sure!", |s| s.quit()));
}));
});
}))
.title("Progress bar example")
.padding((0, 0, 1, 1))
.with_id("dialog"));
siv.set_fps(30);

View File

@ -2,11 +2,12 @@ use std::cmp::max;
use std::any::Any;
use Cursive;
use With;
use direction::Direction;
use align::*;
use event::*;
use theme::ColorStyle;
use view::{Selector, TextView, View};
use view::{DummyView, Selector, TextView, View};
use view::{Button, SizedView};
use vec::{Vec2, Vec4};
use Printer;
@ -43,7 +44,14 @@ pub struct Dialog {
}
impl Dialog {
/// Creates a new Dialog with the given content.
/// Creates a new `Dialog` with empty content.
///
/// You should probably call `content()` next.
pub fn empty() -> Self {
Self::new(DummyView)
}
/// Creates a new `Dialog` with the given content.
pub fn new<V: View + 'static>(view: V) -> Self {
Dialog {
content: Box::new(view),
@ -56,6 +64,13 @@ impl Dialog {
}
}
/// Sets the content for this dialog.
///
/// Chainable variant.
pub fn content<V: View + 'static>(self, view: V) -> Self {
self.with(|s| s.set_content(view))
}
/// Sets the content for this dialog.
///
/// Previous content will be dropped.

View File

@ -161,6 +161,16 @@ pub trait View {
}
}
/// Dummy view.
///
/// Doesn't print anything. Minimal size is (1,1).
pub struct DummyView;
impl View for DummyView {
fn draw(&self, _: &Printer) {}
}
/// Cache around a one-dimensional layout result.
///
/// This is not a View, but something to help you if you create your own Views.