diff --git a/examples/edit.rs b/examples/edit.rs index 955163d..27243aa 100644 --- a/examples/edit.rs +++ b/examples/edit.rs @@ -6,23 +6,33 @@ fn main() { let mut siv = Cursive::new(); // Create a dialog with an edit text and a button. + // The user can either hit the button, + // or press Enter on the edit text. siv.add_layer(Dialog::empty() - .title("Enter your name") - .padding((1, 1, 1, 0)) - .content(EditView::new() - .min_length(20) - .on_submit(|s, name| { - if name.is_empty() { - s.add_layer(Dialog::new(TextView::new("Please enter a name!")) - .dismiss_button("Ok")); - } else { - let content = format!("Hello {}!", name); - s.pop_layer(); - s.add_layer(Dialog::new(TextView::new(&content)) - .button("Quit", |s| s.quit())); - } - })) - ); + .title("Enter your name") + .padding((1, 1, 1, 0)) + .content(EditView::new() + .min_length(20) + .on_submit(show_popup) + .with_id("name")) + .button("Ok", |s| { + let name = s.find_id::("name") + .unwrap() + .get_content(); + show_popup(s, &name); + })); siv.run(); } + +fn show_popup(s: &mut Cursive, name: &str) { + if name.is_empty() { + s.add_layer(Dialog::info("Please enter a name!")); + } else { + let content = format!("Hello {}!", name); + s.pop_layer(); + s.add_layer(Dialog::new(TextView::new(content)) + .button("Quit", |s| s.quit())); + } + +} diff --git a/src/views/button.rs b/src/views/button.rs index 3aa2b93..4eb74e2 100644 --- a/src/views/button.rs +++ b/src/views/button.rs @@ -26,11 +26,11 @@ pub struct Button { impl Button { /// Creates a new button with the given content and callback. - pub fn new(label: &str, cb: F) -> Self + pub fn new>(label: S, cb: F) -> Self where F: Fn(&mut Cursive) + 'static { Button { - label: label.to_string(), + label: label.into(), callback: Callback::from_fn(cb), enabled: true, } diff --git a/src/views/dialog.rs b/src/views/dialog.rs index 93e784c..ebc0abc 100644 --- a/src/views/dialog.rs +++ b/src/views/dialog.rs @@ -81,14 +81,14 @@ impl Dialog { /// Convenient method to create an infobox. /// /// It will contain the given text and a `Ok` dismiss button. - pub fn info(text: &str) -> Self { + pub fn info>(text: S) -> Self { Self::new(TextView::new(text)).dismiss_button("Ok") } /// Adds a button to the dialog with the given label and callback. /// /// Consumes and returns self for easy chaining. - pub fn button(mut self, label: &str, cb: F) -> Self + pub fn button>(mut self, label: S, cb: F) -> Self where F: Fn(&mut Cursive) + 'static { self.buttons.push(SizedView::new(Button::new(label, cb))); @@ -113,14 +113,14 @@ impl Dialog { } /// Shortcut method to add a button that will dismiss the dialog. - pub fn dismiss_button(self, label: &str) -> Self { + pub fn dismiss_button>(self, label: S) -> Self { self.button(label, |s| s.screen_mut().pop_layer()) } /// Sets the title of the dialog. /// If not empty, it will be visible at the top. - pub fn title(mut self, label: &str) -> Self { - self.title = label.to_string(); + pub fn title>(mut self, label: S) -> Self { + self.title = label.into(); self } diff --git a/src/views/edit_view.rs b/src/views/edit_view.rs index 66626a8..48c0666 100644 --- a/src/views/edit_view.rs +++ b/src/views/edit_view.rs @@ -167,8 +167,8 @@ impl EditView { } /// Get the current text. - pub fn get_content(&self) -> &str { - &&self.content + pub fn get_content(&self) -> Rc { + self.content.clone() } /// Sets the current content to the given value. @@ -231,7 +231,7 @@ impl View for EditView { if self.secret { printer.print_hline((0, 0), width, "*"); } else { - printer.print((0, 0), self.get_content()); + printer.print((0, 0), &self.content); } printer.print_hline((width, 0), printer.size.x - width, diff --git a/src/views/text_view.rs b/src/views/text_view.rs index f0e3052..9716496 100644 --- a/src/views/text_view.rs +++ b/src/views/text_view.rs @@ -30,20 +30,19 @@ pub struct TextView { } // If the last character is a newline, strip it. -fn strip_last_newline(content: &str) -> &str { - if !content.is_empty() && content.chars().last().unwrap() == '\n' { - &content[..content.len() - 1] - } else { - content +fn strip_last_newline(content: &mut String) { + if content.ends_with('\n') { + content.pop().unwrap(); } } impl TextView { /// Creates a new TextView with the given content. - pub fn new(content: &str) -> Self { - let content = strip_last_newline(content); + pub fn new>(content: S) -> Self { + let mut content = content.into(); + strip_last_newline(&mut content); TextView { - content: content.to_string(), + content: content, rows: Vec::new(), scrollable: true, scrollbase: ScrollBase::new(), @@ -99,9 +98,10 @@ impl TextView { } /// Replace the text in this view. - pub fn set_content(&mut self, content: &str) { - let content = strip_last_newline(content); - self.content = content.to_string(); + pub fn set_content>(&mut self, content: S) { + let mut content = content.into(); + strip_last_newline(&mut content); + self.content = content; self.invalidate(); }