mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-24 01:46:31 +00:00
Add mouse support to checkbox
This commit is contained in:
parent
2fed1f3ff4
commit
75ad6315e1
@ -2,8 +2,7 @@ use Cursive;
|
|||||||
use Printer;
|
use Printer;
|
||||||
use With;
|
use With;
|
||||||
use direction::Direction;
|
use direction::Direction;
|
||||||
use event::{Event, EventResult, Key};
|
use event::{Event, EventResult, Key, MouseButton, MouseEvent};
|
||||||
|
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use theme::ColorStyle;
|
use theme::ColorStyle;
|
||||||
use vec::Vec2;
|
use vec::Vec2;
|
||||||
@ -33,16 +32,18 @@ impl Checkbox {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Sets a callback to be used when the state changes.
|
/// Sets a callback to be used when the state changes.
|
||||||
pub fn set_on_change<F: 'static + Fn(&mut Cursive, bool)>(&mut self,
|
pub fn set_on_change<F: 'static + Fn(&mut Cursive, bool)>(
|
||||||
on_change: F) {
|
&mut self, on_change: F
|
||||||
|
) {
|
||||||
self.on_change = Some(Rc::new(on_change));
|
self.on_change = Some(Rc::new(on_change));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets a callback to be used when the state changes.
|
/// Sets a callback to be used when the state changes.
|
||||||
///
|
///
|
||||||
/// Chainable variant.
|
/// Chainable variant.
|
||||||
pub fn on_change<F: 'static + Fn(&mut Cursive, bool)>(self, on_change: F)
|
pub fn on_change<F: 'static + Fn(&mut Cursive, bool)>(
|
||||||
-> Self {
|
self, on_change: F
|
||||||
|
) -> Self {
|
||||||
self.with(|s| s.set_on_change(on_change))
|
self.with(|s| s.set_on_change(on_change))
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,7 +62,9 @@ impl Checkbox {
|
|||||||
///
|
///
|
||||||
/// Chainable variant.
|
/// Chainable variant.
|
||||||
pub fn checked(self) -> Self {
|
pub fn checked(self) -> Self {
|
||||||
self.with(|s| { s.check(); })
|
self.with(|s| {
|
||||||
|
s.check();
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns `true` if the checkbox is checked.
|
/// Returns `true` if the checkbox is checked.
|
||||||
@ -78,7 +81,9 @@ impl Checkbox {
|
|||||||
///
|
///
|
||||||
/// Chainable variant.
|
/// Chainable variant.
|
||||||
pub fn unchecked(self) -> Self {
|
pub fn unchecked(self) -> Self {
|
||||||
self.with(|s| { s.uncheck(); })
|
self.with(|s| {
|
||||||
|
s.uncheck();
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the checkbox state.
|
/// Sets the checkbox state.
|
||||||
@ -111,19 +116,29 @@ impl View for Checkbox {
|
|||||||
|
|
||||||
fn draw(&self, printer: &Printer) {
|
fn draw(&self, printer: &Printer) {
|
||||||
if self.enabled {
|
if self.enabled {
|
||||||
printer.with_selection(printer.focused,
|
printer.with_selection(
|
||||||
|printer| self.draw_internal(printer));
|
printer.focused,
|
||||||
|
|printer| self.draw_internal(printer),
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
printer.with_color(ColorStyle::Secondary,
|
printer.with_color(
|
||||||
|printer| self.draw_internal(printer));
|
ColorStyle::Secondary,
|
||||||
|
|printer| self.draw_internal(printer),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_event(&mut self, event: Event) -> EventResult {
|
fn on_event(&mut self, event: Event) -> EventResult {
|
||||||
match event {
|
match event {
|
||||||
Event::Key(Key::Enter) |
|
Event::Key(Key::Enter) | Event::Char(' ') => self.toggle(),
|
||||||
Event::Char(' ') => self.toggle(),
|
Event::Mouse {
|
||||||
|
event: MouseEvent::Release(MouseButton::Left),
|
||||||
|
position,
|
||||||
|
offset,
|
||||||
|
} if position.fits_in_rect(offset, (3, 1)) =>
|
||||||
|
{
|
||||||
|
self.toggle()
|
||||||
|
}
|
||||||
_ => EventResult::Ignored,
|
_ => EventResult::Ignored,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user