cursive/cursive-core/src/views/checkbox.rs

163 lines
3.9 KiB
Rust
Raw Normal View History

use crate::direction::Direction;
use crate::event::{Event, EventResult, Key, MouseButton, MouseEvent};
use crate::theme::ColorStyle;
use crate::view::View;
use crate::Cursive;
use crate::Printer;
2020-01-06 23:51:38 +00:00
use crate::Vec2;
use crate::With;
2019-03-01 00:04:14 +00:00
use std::rc::Rc;
2016-07-17 01:18:33 +00:00
2016-07-17 00:22:24 +00:00
/// Checkable box.
2019-10-07 23:14:35 +00:00
///
/// # Examples
///
/// ```
/// use cursive_core::views::Checkbox;
/// use cursive_core::traits::Identifiable;
2019-10-07 23:14:35 +00:00
///
2020-01-06 23:39:30 +00:00
/// let checkbox = Checkbox::new().checked().with_name("check");
2019-10-07 23:14:35 +00:00
/// ```
2016-07-17 00:22:24 +00:00
pub struct Checkbox {
checked: bool,
2016-07-17 01:36:54 +00:00
enabled: bool,
2016-07-17 01:18:33 +00:00
2019-02-28 23:55:02 +00:00
on_change: Option<Rc<dyn Fn(&mut Cursive, bool)>>,
2016-07-17 00:22:24 +00:00
}
2016-07-17 00:28:42 +00:00
new_default!(Checkbox);
2016-07-17 00:22:24 +00:00
impl Checkbox {
2016-10-02 21:55:19 +00:00
impl_enabled!(self.enabled);
2016-07-17 00:22:24 +00:00
/// Creates a new, unchecked checkbox.
pub fn new() -> Self {
2016-07-17 01:18:33 +00:00
Checkbox {
checked: false,
2016-07-17 01:36:54 +00:00
enabled: true,
2016-07-17 01:18:33 +00:00
on_change: None,
}
}
/// Sets a callback to be used when the state changes.
2017-10-12 22:10:09 +00:00
pub fn set_on_change<F: 'static + Fn(&mut Cursive, bool)>(
2019-07-30 23:08:05 +00:00
&mut self,
on_change: F,
2017-10-12 22:10:09 +00:00
) {
2016-07-17 01:18:33 +00:00
self.on_change = Some(Rc::new(on_change));
}
/// Sets a callback to be used when the state changes.
///
/// Chainable variant.
2017-10-12 22:10:09 +00:00
pub fn on_change<F: 'static + Fn(&mut Cursive, bool)>(
2019-07-30 23:08:05 +00:00
self,
on_change: F,
2017-10-12 22:10:09 +00:00
) -> Self {
2016-07-17 01:18:33 +00:00
self.with(|s| s.set_on_change(on_change))
2016-07-17 00:22:24 +00:00
}
/// Toggles the checkbox state.
2016-07-17 01:18:33 +00:00
pub fn toggle(&mut self) -> EventResult {
let checked = !self.checked;
self.set_checked(checked)
2016-07-17 00:22:24 +00:00
}
/// Check the checkbox.
2016-07-17 01:18:33 +00:00
pub fn check(&mut self) -> EventResult {
self.set_checked(true)
2016-07-17 00:22:24 +00:00
}
/// Check the checkbox.
///
/// Chainable variant.
pub fn checked(self) -> Self {
2017-10-12 22:10:09 +00:00
self.with(|s| {
s.check();
})
2016-07-17 00:22:24 +00:00
}
/// Returns `true` if the checkbox is checked.
2019-10-07 23:14:35 +00:00
///
/// # Examples
///
/// ```
/// use cursive_core::views::Checkbox;
2019-10-07 23:14:35 +00:00
///
/// let mut checkbox = Checkbox::new().checked();
/// assert!(checkbox.is_checked());
///
/// checkbox.uncheck();
/// assert!(!checkbox.is_checked());
/// ```
2016-07-17 00:22:24 +00:00
pub fn is_checked(&self) -> bool {
self.checked
}
/// Uncheck the checkbox.
2016-07-17 01:18:33 +00:00
pub fn uncheck(&mut self) -> EventResult {
self.set_checked(false)
2016-07-17 00:22:24 +00:00
}
/// Uncheck the checkbox.
///
/// Chainable variant.
pub fn unchecked(self) -> Self {
2017-10-12 22:10:09 +00:00
self.with(|s| {
s.uncheck();
})
2016-07-17 01:18:33 +00:00
}
/// Sets the checkbox state.
pub fn set_checked(&mut self, checked: bool) -> EventResult {
self.checked = checked;
if let Some(ref on_change) = self.on_change {
2017-10-13 18:22:02 +00:00
let on_change = Rc::clone(on_change);
2016-07-17 01:18:33 +00:00
EventResult::with_cb(move |s| on_change(s, checked))
} else {
EventResult::Consumed(None)
}
2016-07-17 00:22:24 +00:00
}
2016-07-17 01:36:54 +00:00
2019-02-28 23:55:02 +00:00
fn draw_internal(&self, printer: &Printer<'_, '_>) {
2016-07-17 01:36:54 +00:00
printer.print((0, 0), "[ ]");
if self.checked {
printer.print((1, 0), "X");
}
}
2016-07-17 00:22:24 +00:00
}
impl View for Checkbox {
fn required_size(&mut self, _: Vec2) -> Vec2 {
2016-07-17 00:22:24 +00:00
Vec2::new(3, 1)
}
fn take_focus(&mut self, _: Direction) -> bool {
2016-07-17 01:36:54 +00:00
self.enabled
2016-07-17 00:22:24 +00:00
}
2019-02-28 23:55:02 +00:00
fn draw(&self, printer: &Printer<'_, '_>) {
2018-11-09 22:08:43 +00:00
if self.enabled && printer.enabled {
2017-12-30 22:03:42 +00:00
printer.with_selection(printer.focused, |printer| {
self.draw_internal(printer)
});
2016-07-17 01:36:54 +00:00
} else {
printer.with_color(ColorStyle::secondary(), |printer| {
2017-12-30 22:03:42 +00:00
self.draw_internal(printer)
});
2016-07-17 01:36:54 +00:00
}
2016-07-17 00:22:24 +00:00
}
fn on_event(&mut self, event: Event) -> EventResult {
match event {
2017-10-12 22:10:09 +00:00
Event::Key(Key::Enter) | Event::Char(' ') => self.toggle(),
Event::Mouse {
event: MouseEvent::Release(MouseButton::Left),
position,
offset,
2018-11-09 22:08:43 +00:00
} if position.fits_in_rect(offset, (3, 1)) => self.toggle(),
2016-07-17 01:18:33 +00:00
_ => EventResult::Ignored,
2016-07-17 00:22:24 +00:00
}
}
}