Do not accept any return value for callbacks

It got added so we could keep using `s.pop_layer()` in single-line callbacks.
It was confusing, and the return value was ignored anyway.
This commit is contained in:
Alexandre Bury 2018-03-16 15:50:56 -07:00
parent acb73765e9
commit 9bc723ab4a
10 changed files with 32 additions and 29 deletions

View File

@ -63,7 +63,7 @@ fn show_options(siv: &mut Cursive) {
new_game(s, *option); new_game(s, *option);
}), }),
) )
.button("Back", |s| s.pop_layer()), .dismiss_button("Back"),
); );
} }

View File

@ -41,8 +41,10 @@ fn main() {
).unwrap(); ).unwrap();
find(s, &text); find(s, &text);
}) })
.button("Cancel", |s| s.pop_layer()), .dismiss_button("Cancel"),
).on_event(Event::Key(Key::Esc), |s| s.pop_layer()), ).on_event(Event::Key(Key::Esc), |s| {
s.pop_layer();
}),
) )
}); });

View File

@ -26,9 +26,9 @@ pub struct Callback(Rc<Box<Fn(&mut Cursive)>>);
impl Callback { impl Callback {
/// Wraps the given function into a `Callback` object. /// Wraps the given function into a `Callback` object.
pub fn from_fn<F, R>(f: F) -> Self pub fn from_fn<F>(f: F) -> Self
where where
F: 'static + Fn(&mut Cursive) -> R, F: 'static + Fn(&mut Cursive),
{ {
Callback(Rc::new(Box::new(move |siv| { Callback(Rc::new(Box::new(move |siv| {
f(siv); f(siv);
@ -77,9 +77,9 @@ pub enum EventResult {
impl EventResult { impl EventResult {
/// Convenient method to create `Consumed(Some(f))` /// Convenient method to create `Consumed(Some(f))`
pub fn with_cb<F, R>(f: F) -> Self pub fn with_cb<F>(f: F) -> Self
where where
F: 'static + Fn(&mut Cursive) -> R, F: 'static + Fn(&mut Cursive),
{ {
EventResult::Consumed(Some(Callback::from_fn(f))) EventResult::Consumed(Some(Callback::from_fn(f)))
} }

View File

@ -43,4 +43,3 @@ impl<T: View> AnyView for T {
self self
} }
} }

View File

@ -63,6 +63,6 @@ pub use self::position::{Offset, Position};
pub use self::scroll::{ScrollBase, ScrollStrategy}; pub use self::scroll::{ScrollBase, ScrollStrategy};
pub use self::size_cache::SizeCache; pub use self::size_cache::SizeCache;
pub use self::size_constraint::SizeConstraint; pub use self::size_constraint::SizeConstraint;
pub use self::view::View;
pub use self::view_path::ViewPath; pub use self::view_path::ViewPath;
pub use self::view_wrapper::ViewWrapper; pub use self::view_wrapper::ViewWrapper;
pub use self::view::View;

View File

@ -5,7 +5,6 @@ use std::any::Any;
use vec::Vec2; use vec::Vec2;
use view::{AnyView, Selector}; use view::{AnyView, Selector};
/// Main trait defining a view behaviour. /// Main trait defining a view behaviour.
/// ///
/// This is what you should implement to define a custom View. /// This is what you should implement to define a custom View.
@ -86,4 +85,3 @@ pub trait View: Any + AnyView {
false false
} }
} }

View File

@ -25,9 +25,9 @@ pub struct Button {
impl Button { impl Button {
/// Creates a new button with the given content and callback. /// Creates a new button with the given content and callback.
pub fn new<F, R, S>(label: S, cb: F) -> Self pub fn new<F, S>(label: S, cb: F) -> Self
where where
F: 'static + Fn(&mut Cursive) -> R, F: 'static + Fn(&mut Cursive),
S: Into<String>, S: Into<String>,
{ {
let label = label.into(); let label = label.into();
@ -35,9 +35,9 @@ impl Button {
} }
/// Creates a new button without angle brackets. /// Creates a new button without angle brackets.
pub fn new_raw<F, R, S: Into<String>>(label: S, cb: F) -> Self pub fn new_raw<F, S: Into<String>>(label: S, cb: F) -> Self
where where
F: 'static + Fn(&mut Cursive) -> R, F: 'static + Fn(&mut Cursive),
{ {
Button { Button {
label: label.into(), label: label.into(),

View File

@ -30,9 +30,9 @@ struct ChildButton {
} }
impl ChildButton { impl ChildButton {
pub fn new<F, R, S: Into<String>>(label: S, cb: F) -> Self pub fn new<F, S: Into<String>>(label: S, cb: F) -> Self
where where
F: 'static + Fn(&mut Cursive) -> R, F: 'static + Fn(&mut Cursive),
{ {
ChildButton { ChildButton {
button: SizedView::new(Button::new(label, cb)), button: SizedView::new(Button::new(label, cb)),
@ -151,9 +151,9 @@ impl Dialog {
/// Adds a button to the dialog with the given label and callback. /// Adds a button to the dialog with the given label and callback.
/// ///
/// Consumes and returns self for easy chaining. /// Consumes and returns self for easy chaining.
pub fn button<F, R, S: Into<String>>(mut self, label: S, cb: F) -> Self pub fn button<F, S: Into<String>>(mut self, label: S, cb: F) -> Self
where where
F: 'static + Fn(&mut Cursive) -> R, F: 'static + Fn(&mut Cursive),
{ {
self.buttons.push(ChildButton::new(label, cb)); self.buttons.push(ChildButton::new(label, cb));
@ -184,7 +184,9 @@ impl Dialog {
/// Shortcut method to add a button that will dismiss the dialog. /// Shortcut method to add a button that will dismiss the dialog.
pub fn dismiss_button<S: Into<String>>(self, label: S) -> Self { pub fn dismiss_button<S: Into<String>>(self, label: S) -> Self {
self.button(label, |s| s.pop_layer()) self.button(label, |s| {
s.pop_layer();
})
} }
/// Sets the title of the dialog. /// Sets the title of the dialog.

View File

@ -161,7 +161,9 @@ impl MenuPopup {
action_cb.clone()(s); action_cb.clone()(s);
} }
}, },
)).on_event(Key::Left, |s| s.pop_layer()), )).on_event(Key::Left, |s| {
s.pop_layer();
}),
); );
}) })
} }

View File

@ -80,10 +80,10 @@ impl<T: View> OnEventView<T> {
/// Registers a callback when the given event is ignored by the child. /// Registers a callback when the given event is ignored by the child.
/// ///
/// Chainable variant. /// Chainable variant.
pub fn on_event<F, R, E>(self, event: E, cb: F) -> Self pub fn on_event<F, E>(self, event: E, cb: F) -> Self
where where
E: Into<Event>, E: Into<Event>,
F: 'static + Fn(&mut Cursive) -> R, F: 'static + Fn(&mut Cursive),
{ {
self.with(|s| s.set_on_event(event, cb)) self.with(|s| s.set_on_event(event, cb))
} }
@ -93,10 +93,10 @@ impl<T: View> OnEventView<T> {
/// The child will never receive this event. /// The child will never receive this event.
/// ///
/// Chainable variant. /// Chainable variant.
pub fn on_pre_event<F, R, E>(self, event: E, cb: F) -> Self pub fn on_pre_event<F, E>(self, event: E, cb: F) -> Self
where where
E: Into<Event>, E: Into<Event>,
F: 'static + Fn(&mut Cursive) -> R, F: 'static + Fn(&mut Cursive),
{ {
self.with(|s| s.set_on_pre_event(event, cb)) self.with(|s| s.set_on_pre_event(event, cb))
} }
@ -135,10 +135,10 @@ impl<T: View> OnEventView<T> {
} }
/// Registers a callback when the given event is ignored by the child. /// Registers a callback when the given event is ignored by the child.
pub fn set_on_event<F, R, E>(&mut self, event: E, cb: F) pub fn set_on_event<F, E>(&mut self, event: E, cb: F)
where where
E: Into<Event>, E: Into<Event>,
F: Fn(&mut Cursive) -> R + 'static, F: Fn(&mut Cursive) + 'static,
{ {
let cb = Callback::from_fn(cb); let cb = Callback::from_fn(cb);
let action = let action =
@ -150,10 +150,10 @@ impl<T: View> OnEventView<T> {
/// Registers a callback when the given event is received. /// Registers a callback when the given event is received.
/// ///
/// The child will never receive this event. /// The child will never receive this event.
pub fn set_on_pre_event<F, R, E>(&mut self, event: E, cb: F) pub fn set_on_pre_event<F, E>(&mut self, event: E, cb: F)
where where
E: Into<Event>, E: Into<Event>,
F: 'static + Fn(&mut Cursive) -> R, F: 'static + Fn(&mut Cursive),
{ {
let cb = Callback::from_fn(cb); let cb = Callback::from_fn(cb);
// We want to clone the Callback every time we call the closure // We want to clone the Callback every time we call the closure