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() { fn main() {
let mut siv = Cursive::new(); 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("Name", EditView::new().min_length(10))
.child("Email", .child("Email",
LinearLayout::horizontal() LinearLayout::horizontal()
@ -27,7 +30,8 @@ fn main() {
})) }))
.delimiter() .delimiter()
.child("Age", .child("Age",
SelectView::new().popup() SelectView::new()
.popup()
.item_str("0-18") .item_str("0-18")
.item_str("19-30") .item_str("19-30")
.item_str("31-40") .item_str("31-40")
@ -36,9 +40,7 @@ fn main() {
for i in 0..50 { for i in 0..50 {
list.add_child(&format!("Item {}", i), EditView::new()); list.add_child(&format!("Item {}", i), EditView::new());
} }
})) })));
.title("Please fill out this form")
.button("Ok", |s| s.quit()));
siv.run(); 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, // 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_layer(Dialog::new(TextView::new(&content)) siv.add_layer(Dialog::new(TextView::new(&content))
.h_align(HAlign::Center) .h_align(HAlign::Center)
.button("Quit", |s| s.quit())); .button("Quit", |s| s.quit()));
// Show a popup on top of the view. // Show a popup on top of the view.
siv.add_layer(Dialog::new(TextView::new("Try resizing the terminal!\n(Press 'q' to quit \ siv.add_layer(Dialog::info("Try resizing the terminal!\n(Press 'q' to \
when you're done.)")) quit when you're done.)"));
.dismiss_button("Ok"));
siv.run(); siv.run();
} }

View File

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

View File

@ -2,11 +2,12 @@ use std::cmp::max;
use std::any::Any; use std::any::Any;
use Cursive; use Cursive;
use With;
use direction::Direction; use direction::Direction;
use align::*; use align::*;
use event::*; use event::*;
use theme::ColorStyle; use theme::ColorStyle;
use view::{Selector, TextView, View}; use view::{DummyView, Selector, TextView, View};
use view::{Button, SizedView}; use view::{Button, SizedView};
use vec::{Vec2, Vec4}; use vec::{Vec2, Vec4};
use Printer; use Printer;
@ -43,7 +44,14 @@ pub struct Dialog {
} }
impl 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 { pub fn new<V: View + 'static>(view: V) -> Self {
Dialog { Dialog {
content: Box::new(view), 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. /// Sets the content for this dialog.
/// ///
/// Previous content will be dropped. /// 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. /// Cache around a one-dimensional layout result.
/// ///
/// This is not a View, but something to help you if you create your own Views. /// This is not a View, but something to help you if you create your own Views.