mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-23 17:35:00 +00:00
Merge pull request #299 from agavrilov/radiogroup_callback
Add on_change callback to `RadioGroup`
This commit is contained in:
commit
a6d9b46bf3
@ -5,11 +5,14 @@ use std::rc::Rc;
|
|||||||
use theme::ColorStyle;
|
use theme::ColorStyle;
|
||||||
use vec::Vec2;
|
use vec::Vec2;
|
||||||
use view::View;
|
use view::View;
|
||||||
|
use Cursive;
|
||||||
use {Printer, With};
|
use {Printer, With};
|
||||||
|
|
||||||
struct SharedState<T> {
|
struct SharedState<T> {
|
||||||
selection: usize,
|
selection: usize,
|
||||||
values: Vec<Rc<T>>,
|
values: Vec<Rc<T>>,
|
||||||
|
|
||||||
|
on_change: Option<Rc<Fn(&mut Cursive, &T)>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> SharedState<T> {
|
impl<T> SharedState<T> {
|
||||||
@ -31,19 +34,20 @@ pub struct RadioGroup<T> {
|
|||||||
state: Rc<RefCell<SharedState<T>>>,
|
state: Rc<RefCell<SharedState<T>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Default for RadioGroup<T> {
|
impl<T: 'static> Default for RadioGroup<T> {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self::new()
|
Self::new()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> RadioGroup<T> {
|
impl<T: 'static> RadioGroup<T> {
|
||||||
/// Creates an empty group for radio buttons.
|
/// Creates an empty group for radio buttons.
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
RadioGroup {
|
RadioGroup {
|
||||||
state: Rc::new(RefCell::new(SharedState {
|
state: Rc::new(RefCell::new(SharedState {
|
||||||
selection: 0,
|
selection: 0,
|
||||||
values: Vec::new(),
|
values: Vec::new(),
|
||||||
|
on_change: None,
|
||||||
})),
|
})),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -70,6 +74,22 @@ impl<T> RadioGroup<T> {
|
|||||||
pub fn selection(&self) -> Rc<T> {
|
pub fn selection(&self) -> Rc<T> {
|
||||||
self.state.borrow().selection()
|
self.state.borrow().selection()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Sets a callback to be used when the selection changes.
|
||||||
|
pub fn set_on_change<F: 'static + Fn(&mut Cursive, &T)>(
|
||||||
|
&mut self, on_change: F,
|
||||||
|
) {
|
||||||
|
self.state.borrow_mut().on_change = Some(Rc::new(on_change));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets a callback to be used when the selection changes.
|
||||||
|
///
|
||||||
|
/// Chainable variant.
|
||||||
|
pub fn on_change<F: 'static + Fn(&mut Cursive, &T)>(
|
||||||
|
self, on_change: F,
|
||||||
|
) -> Self {
|
||||||
|
self.with(|s| s.set_on_change(on_change))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RadioGroup<String> {
|
impl RadioGroup<String> {
|
||||||
@ -100,7 +120,7 @@ pub struct RadioButton<T> {
|
|||||||
label: String,
|
label: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> RadioButton<T> {
|
impl<T: 'static> RadioButton<T> {
|
||||||
impl_enabled!(self.enabled);
|
impl_enabled!(self.enabled);
|
||||||
|
|
||||||
fn new(
|
fn new(
|
||||||
@ -120,15 +140,26 @@ impl<T> RadioButton<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Selects this button, un-selecting any other in the same group.
|
/// Selects this button, un-selecting any other in the same group.
|
||||||
pub fn select(&mut self) {
|
pub fn select(&mut self) -> EventResult {
|
||||||
self.state.borrow_mut().selection = self.id;
|
let mut state = self.state.borrow_mut();
|
||||||
|
state.selection = self.id;
|
||||||
|
if let Some(ref on_change) = state.on_change {
|
||||||
|
let on_change = Rc::clone(on_change);
|
||||||
|
let value = state.selection();
|
||||||
|
EventResult::with_cb(move |s| on_change(s, &value))
|
||||||
|
} else {
|
||||||
|
EventResult::Consumed(None)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Selects this button, un-selecting any other in the same group.
|
/// Selects this button, un-selecting any other in the same group.
|
||||||
///
|
///
|
||||||
/// Chainable variant.
|
/// Chainable variant.
|
||||||
pub fn selected(self) -> Self {
|
pub fn selected(self) -> Self {
|
||||||
self.with(Self::select)
|
self.with(|s| {
|
||||||
|
// Ignore the potential callback here
|
||||||
|
s.select();
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn draw_internal(&self, printer: &Printer) {
|
fn draw_internal(&self, printer: &Printer) {
|
||||||
@ -177,16 +208,14 @@ impl<T: 'static> View for RadioButton<T> {
|
|||||||
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::Char(' ') => {
|
Event::Key(Key::Enter) | Event::Char(' ') => {
|
||||||
self.select();
|
self.select()
|
||||||
EventResult::Consumed(None)
|
|
||||||
}
|
}
|
||||||
Event::Mouse {
|
Event::Mouse {
|
||||||
event: MouseEvent::Release(MouseButton::Left),
|
event: MouseEvent::Release(MouseButton::Left),
|
||||||
position,
|
position,
|
||||||
offset,
|
offset,
|
||||||
} if position.fits_in_rect(offset, self.req_size()) => {
|
} if position.fits_in_rect(offset, self.req_size()) => {
|
||||||
self.select();
|
self.select()
|
||||||
EventResult::Consumed(None)
|
|
||||||
}
|
}
|
||||||
_ => EventResult::Ignored,
|
_ => EventResult::Ignored,
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user