EditView can now have a minimum size

This commit is contained in:
Alexandre Bury 2015-05-26 16:11:22 -07:00
parent e8fcaabd6c
commit 3460f8123d
2 changed files with 23 additions and 2 deletions

View File

@ -6,9 +6,11 @@ use cursive::view::{Dialog,IdView,EditView,Selector,TextView};
fn main() {
let mut siv = Cursive::new();
siv.add_layer(Dialog::new(IdView::new("edit", EditView::new()))
// Create a dialog with an edit text and a button.
siv.add_layer(Dialog::new(IdView::new("edit", EditView::new().min_length(20)))
.title("Enter your name")
.button("Ok", |s| {
// When the button is clicked, read the text and print it in a new dialog.
let content = {
let name = s.find::<EditView>(&Selector::Id("edit")).unwrap().get_content();
format!("Hello {}", name)

View File

@ -1,7 +1,8 @@
use ncurses;
use color;
use view::{View};
use vec::Vec2;
use view::{View,SizeRequest};
use event::EventResult;
use printer::Printer;
@ -10,29 +11,43 @@ pub struct EditView {
content: String,
cursor: usize,
multiline: bool,
min_length: usize,
}
impl EditView {
/// Creates a new, empty edit view.
pub fn new() -> Self {
EditView {
content: String::new(),
cursor: 0,
multiline: false,
min_length: 1,
}
}
/// Replace the entire content of the view with the given one.
pub fn set_content<'a>(&mut self, content: &'a str) {
self.content = content.to_string();
}
/// Get the current text.
pub fn get_content(&self) -> &str {
&self.content
}
/// Sets the current content to the given value. Convenient chainable method.
pub fn content<'a>(mut self, content: &'a str) -> Self {
self.set_content(content);
self
}
/// Sets the minimum length for this view.
/// (This applies to the layout, not the content.)
pub fn min_length(mut self, min_length: usize) -> Self {
self.min_length = min_length;
self
}
}
fn read_char(ch: i32) -> Option<char> {
@ -52,6 +67,10 @@ impl View for EditView {
});
}
fn get_min_size(&self, req: SizeRequest) -> Vec2 {
Vec2::new(self.min_length, 1)
}
fn take_focus(&mut self) -> bool {
true
}