From b60c69ca635186217c52ebf6b0019b42c1f135a5 Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Fri, 9 Nov 2018 10:54:57 -0800 Subject: [PATCH] Fix example and add doc tests --- examples/select.rs | 5 +++-- src/views/on_event_view.rs | 46 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/examples/select.rs b/examples/select.rs index 5a0dde9..3090cec 100644 --- a/examples/select.rs +++ b/examples/select.rs @@ -28,10 +28,11 @@ fn main() { // Let's override the `j` and `k` keys for navigation let select = OnEventView::new(select) - .on_pre_event_inner('k', |s| { + .on_pre_event_inner('k', |s, _| { s.select_up(1); Some(EventResult::Consumed(None)) - }).on_pre_event_inner('j', |s| { + }) + .on_pre_event_inner('j', |s, _| { s.select_down(1); Some(EventResult::Consumed(None)) }); diff --git a/src/views/on_event_view.rs b/src/views/on_event_view.rs index 1094e33..da4926b 100644 --- a/src/views/on_event_view.rs +++ b/src/views/on_event_view.rs @@ -77,6 +77,22 @@ impl OnEventView { /// Registers a callback when the given event is ignored by the child. /// /// 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(self, trigger: E, cb: F) -> Self where E: Into, @@ -118,11 +134,36 @@ impl OnEventView { /// 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 /// child view as argument. /// If the result is not `None`, it will be processed as well. /// /// 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(self, trigger: E, cb: F) -> Self where E: Into, @@ -203,6 +244,11 @@ impl OnEventView { )); } + /// Remove any callbacks defined for this view. + pub fn clear_callbacks(&mut self) { + self.callbacks.clear(); + } + inner_getters!(self.view: T); }