Fix example and add doc tests

This commit is contained in:
Alexandre Bury 2018-11-09 10:54:57 -08:00
parent d9d34b4350
commit b60c69ca63
2 changed files with 49 additions and 2 deletions

View File

@ -28,10 +28,11 @@ fn main() {
// Let's override the `j` and `k` keys for navigation // Let's override the `j` and `k` keys for navigation
let select = OnEventView::new(select) let select = OnEventView::new(select)
.on_pre_event_inner('k', |s| { .on_pre_event_inner('k', |s, _| {
s.select_up(1); s.select_up(1);
Some(EventResult::Consumed(None)) Some(EventResult::Consumed(None))
}).on_pre_event_inner('j', |s| { })
.on_pre_event_inner('j', |s, _| {
s.select_down(1); s.select_down(1);
Some(EventResult::Consumed(None)) Some(EventResult::Consumed(None))
}); });

View File

@ -77,6 +77,22 @@ 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.
///
/// # Examples
///
///
/// ```rust
/// # use cursive::views::{OnEventView, DummyView};
/// # use cursive::event::{Key, EventTrigger};
/// let view = OnEventView::new(DummyView)
/// .on_event('q', |s| s.quit())
/// .on_event(Key::Esc, |s| {
/// s.pop_layer();
/// })
/// .on_event(EventTrigger::mouse(), |s| {
/// s.add_layer(DummyView);
/// });
/// ```
pub fn on_event<F, E>(self, trigger: E, cb: F) -> Self pub fn on_event<F, E>(self, trigger: E, cb: F) -> Self
where where
E: Into<EventTrigger>, E: Into<EventTrigger>,
@ -118,11 +134,36 @@ 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.
/// ///
/// This is an advanced method to get more control.
/// [`Self::on_event()`] may be easier to use.
///
/// If the child view ignores the event, `cb` will be called with the /// If the child view ignores the event, `cb` will be called with the
/// child view as argument. /// child view as argument.
/// If the result is not `None`, it will be processed as well. /// If the result is not `None`, it will be processed as well.
/// ///
/// Chainable variant. /// Chainable variant.
///
/// # Examples
///
/// ```rust
/// # use cursive::views::{DummyView, OnEventView};
/// # use cursive::event::{Event, EventTrigger, MouseEvent, EventResult};
/// let view = OnEventView::new(DummyView)
/// .on_event_inner(
/// EventTrigger::mouse(),
/// |d: &mut DummyView, e: &Event| {
/// if let &Event::Mouse { event: MouseEvent::Press(_), .. } = e {
/// // Do something on mouse press
/// Some(EventResult::with_cb(|s| {
/// s.pop_layer();
/// }))
/// } else {
/// // Otherwise, don't do anything
/// None
/// }
/// }
/// );
/// ```
pub fn on_event_inner<F, E>(self, trigger: E, cb: F) -> Self pub fn on_event_inner<F, E>(self, trigger: E, cb: F) -> Self
where where
E: Into<EventTrigger>, E: Into<EventTrigger>,
@ -203,6 +244,11 @@ impl<T: View> OnEventView<T> {
)); ));
} }
/// Remove any callbacks defined for this view.
pub fn clear_callbacks(&mut self) {
self.callbacks.clear();
}
inner_getters!(self.view: T); inner_getters!(self.view: T);
} }