2016-10-02 22:22:29 +00:00
|
|
|
|
2015-05-16 00:56:38 +00:00
|
|
|
|
2016-03-15 22:37:57 +00:00
|
|
|
use Cursive;
|
2016-07-11 01:27:26 +00:00
|
|
|
use event::{Callback, Event, EventResult};
|
2016-10-02 22:22:29 +00:00
|
|
|
use std::collections::HashMap;
|
2016-07-28 23:36:01 +00:00
|
|
|
use view::{View, ViewWrapper};
|
2017-06-12 23:39:12 +00:00
|
|
|
use With;
|
2015-05-16 00:56:38 +00:00
|
|
|
|
|
|
|
/// A simple wrapper view that catches some ignored event from its child.
|
|
|
|
///
|
2016-07-21 05:08:06 +00:00
|
|
|
/// If the event doesn't have a corresponding callback, it will stay ignored.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2016-09-29 05:45:27 +00:00
|
|
|
/// # use cursive::event;;
|
2017-06-12 23:39:12 +00:00
|
|
|
/// # use cursive::views::{OnEventView, TextView};
|
|
|
|
/// let view = OnEventView::new(TextView::new("This view has an event!"))
|
|
|
|
/// .on_event('q', |s| s.quit())
|
|
|
|
/// .on_event(event::Key::Esc, |s| s.quit());
|
2016-07-21 05:08:06 +00:00
|
|
|
/// ```
|
2017-06-12 23:39:12 +00:00
|
|
|
pub struct OnEventView<T: View> {
|
2016-09-20 00:11:00 +00:00
|
|
|
content: T,
|
2016-07-02 22:02:42 +00:00
|
|
|
callbacks: HashMap<Event, Callback>,
|
2015-05-16 00:56:38 +00:00
|
|
|
}
|
|
|
|
|
2017-06-12 23:39:12 +00:00
|
|
|
impl<T: View> OnEventView<T> {
|
|
|
|
/// Wraps the given view in a new OnEventView.
|
2016-09-20 00:11:00 +00:00
|
|
|
pub fn new(view: T) -> Self {
|
2017-06-12 23:39:12 +00:00
|
|
|
OnEventView {
|
2016-09-20 00:11:00 +00:00
|
|
|
content: view,
|
2015-05-16 00:56:38 +00:00
|
|
|
callbacks: HashMap::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-12 23:39:12 +00:00
|
|
|
/// Registers a callback when the given event is ignored by the child.
|
|
|
|
///
|
|
|
|
/// Chainable variant.
|
|
|
|
pub fn on_event<F, E: Into<Event>>(self, event: E, cb: F) -> Self
|
2015-05-24 00:07:22 +00:00
|
|
|
where F: Fn(&mut Cursive) + 'static
|
2015-05-16 00:56:38 +00:00
|
|
|
{
|
2017-06-12 23:39:12 +00:00
|
|
|
self.with(|s| s.set_on_event(event, cb))
|
|
|
|
}
|
2015-05-16 00:56:38 +00:00
|
|
|
|
2017-06-12 23:39:12 +00:00
|
|
|
/// Registers a callback when the given event is ignored by the child.
|
|
|
|
pub fn set_on_event<F, E: Into<Event>>(&mut self, event: E, cb: F)
|
|
|
|
where F: Fn(&mut Cursive) + 'static
|
|
|
|
{
|
|
|
|
self.callbacks.insert(event.into(), Callback::from_fn(cb));
|
2015-05-16 00:56:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-12 23:39:12 +00:00
|
|
|
impl<T: View> ViewWrapper for OnEventView<T> {
|
2016-09-20 00:11:00 +00:00
|
|
|
wrap_impl!(self.content: T);
|
2015-05-19 22:54:11 +00:00
|
|
|
|
2015-05-28 01:04:33 +00:00
|
|
|
fn wrap_on_event(&mut self, event: Event) -> EventResult {
|
2017-03-25 18:01:25 +00:00
|
|
|
match self.content.on_event(event.clone()) {
|
2016-06-25 23:36:22 +00:00
|
|
|
EventResult::Ignored => {
|
|
|
|
match self.callbacks.get(&event) {
|
|
|
|
None => EventResult::Ignored,
|
|
|
|
Some(cb) => EventResult::Consumed(Some(cb.clone())),
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 22:54:11 +00:00
|
|
|
res => res,
|
2015-05-16 00:56:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|