mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-23 17:35:00 +00:00
Add checkbox view
This commit is contained in:
parent
5ee8706582
commit
d4afdf024f
@ -2,7 +2,7 @@ extern crate cursive;
|
||||
|
||||
use cursive::Cursive;
|
||||
use cursive::With;
|
||||
use cursive::view::{ListView, Dialog, EditView, TextView, LinearLayout};
|
||||
use cursive::view::{ListView, Checkbox, Dialog, EditView, TextView, LinearLayout};
|
||||
|
||||
fn main() {
|
||||
let mut siv = Cursive::new();
|
||||
@ -13,6 +13,7 @@ fn main() {
|
||||
.child(EditView::new().min_length(15))
|
||||
.child(TextView::new("@"))
|
||||
.child(EditView::new().min_length(10)))
|
||||
.child("Receive spam?", Checkbox::new())
|
||||
.delimiter()
|
||||
.with(|list| {
|
||||
for i in 0..50 {
|
||||
|
86
src/view/checkbox.rs
Normal file
86
src/view/checkbox.rs
Normal file
@ -0,0 +1,86 @@
|
||||
use With;
|
||||
use Printer;
|
||||
use vec::Vec2;
|
||||
use view::View;
|
||||
use event::{Event, EventResult, Key};
|
||||
use direction::Direction;
|
||||
|
||||
|
||||
/// Checkable box.
|
||||
#[derive(Debug)]
|
||||
pub struct Checkbox {
|
||||
checked: bool,
|
||||
}
|
||||
|
||||
impl Checkbox {
|
||||
/// Creates a new, unchecked checkbox.
|
||||
pub fn new() -> Self {
|
||||
Checkbox {
|
||||
checked: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Toggles the checkbox state.
|
||||
pub fn toggle(&mut self) {
|
||||
self.checked = !self.checked;
|
||||
}
|
||||
|
||||
/// Check the checkbox.
|
||||
pub fn check(&mut self) {
|
||||
self.checked = true;
|
||||
}
|
||||
|
||||
/// Check the checkbox.
|
||||
///
|
||||
/// Chainable variant.
|
||||
pub fn checked(self) -> Self {
|
||||
self.with(Self::check)
|
||||
}
|
||||
|
||||
/// Returns `true` if the checkbox is checked.
|
||||
pub fn is_checked(&self) -> bool {
|
||||
self.checked
|
||||
}
|
||||
|
||||
/// Uncheck the checkbox.
|
||||
pub fn uncheck(&mut self) {
|
||||
self.checked = false;
|
||||
}
|
||||
|
||||
/// Uncheck the checkbox.
|
||||
///
|
||||
/// Chainable variant.
|
||||
pub fn unchecked(self) -> Self {
|
||||
self.with(Self::uncheck)
|
||||
}
|
||||
}
|
||||
|
||||
impl View for Checkbox {
|
||||
fn get_min_size(&mut self, _: Vec2) -> Vec2 {
|
||||
Vec2::new(3, 1)
|
||||
}
|
||||
|
||||
fn take_focus(&mut self, _: Direction) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn draw(&self, printer: &Printer) {
|
||||
printer.with_selection(printer.focused, |printer| {
|
||||
printer.print((0,0), "[ ]");
|
||||
if self.checked {
|
||||
printer.print((1, 0), "X");
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
fn on_event(&mut self, event: Event) -> EventResult {
|
||||
match event {
|
||||
Event::Key(Key::Enter) |
|
||||
Event::Char(' ') => self.toggle(),
|
||||
_ => return EventResult::Ignored,
|
||||
}
|
||||
|
||||
EventResult::Consumed(None)
|
||||
}
|
||||
}
|
@ -48,6 +48,7 @@ mod scroll;
|
||||
// Views
|
||||
mod box_view;
|
||||
mod button;
|
||||
mod checkbox;
|
||||
mod dialog;
|
||||
mod edit_view;
|
||||
mod full_view;
|
||||
@ -79,6 +80,7 @@ pub use self::scroll::ScrollBase;
|
||||
pub use self::id_view::IdView;
|
||||
pub use self::box_view::BoxView;
|
||||
pub use self::button::Button;
|
||||
pub use self::checkbox::Checkbox;
|
||||
pub use self::dialog::Dialog;
|
||||
pub use self::edit_view::EditView;
|
||||
pub use self::full_view::FullView;
|
||||
|
Loading…
Reference in New Issue
Block a user