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() { fn main() {
let mut siv = Cursive::new(); 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") .title("Enter your name")
.button("Ok", |s| { .button("Ok", |s| {
// When the button is clicked, read the text and print it in a new dialog.
let content = { let content = {
let name = s.find::<EditView>(&Selector::Id("edit")).unwrap().get_content(); let name = s.find::<EditView>(&Selector::Id("edit")).unwrap().get_content();
format!("Hello {}", name) format!("Hello {}", name)

View File

@ -1,7 +1,8 @@
use ncurses; use ncurses;
use color; use color;
use view::{View}; use vec::Vec2;
use view::{View,SizeRequest};
use event::EventResult; use event::EventResult;
use printer::Printer; use printer::Printer;
@ -10,29 +11,43 @@ pub struct EditView {
content: String, content: String,
cursor: usize, cursor: usize,
multiline: bool, multiline: bool,
min_length: usize,
} }
impl EditView { impl EditView {
/// Creates a new, empty edit view.
pub fn new() -> Self { pub fn new() -> Self {
EditView { EditView {
content: String::new(), content: String::new(),
cursor: 0, cursor: 0,
multiline: false, 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) { pub fn set_content<'a>(&mut self, content: &'a str) {
self.content = content.to_string(); self.content = content.to_string();
} }
/// Get the current text.
pub fn get_content(&self) -> &str { pub fn get_content(&self) -> &str {
&self.content &self.content
} }
/// Sets the current content to the given value. Convenient chainable method.
pub fn content<'a>(mut self, content: &'a str) -> Self { pub fn content<'a>(mut self, content: &'a str) -> Self {
self.set_content(content); self.set_content(content);
self 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> { 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 { fn take_focus(&mut self) -> bool {
true true
} }