2015-05-16 00:56:38 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
2016-03-15 22:37:57 +00:00
|
|
|
use Cursive;
|
2016-06-28 05:10:59 +00:00
|
|
|
use event::{Callback, Event, EventResult, ToEvent};
|
2016-03-15 22:37:57 +00:00
|
|
|
use super::{View, ViewWrapper};
|
2015-05-16 00:56:38 +00:00
|
|
|
|
|
|
|
/// A simple wrapper view that catches some ignored event from its child.
|
|
|
|
///
|
|
|
|
/// Events ignored by its child without a callback will stay ignored.
|
|
|
|
pub struct KeyEventView {
|
|
|
|
content: Box<View>,
|
2016-07-02 22:02:42 +00:00
|
|
|
callbacks: HashMap<Event, Callback>,
|
2015-05-16 00:56:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl KeyEventView {
|
|
|
|
/// Wraps the given view in a new KeyEventView.
|
|
|
|
pub fn new<V: View + 'static>(view: V) -> Self {
|
|
|
|
KeyEventView {
|
|
|
|
content: Box::new(view),
|
|
|
|
callbacks: HashMap::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Registers a callback when the given key is ignored by the child.
|
2016-03-15 22:37:57 +00:00
|
|
|
pub fn register<F, E: ToEvent>(mut 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
|
|
|
{
|
2016-07-02 22:02:42 +00:00
|
|
|
self.callbacks.insert(event.to_event(), Rc::new(cb));
|
2015-05-16 00:56:38 +00:00
|
|
|
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-19 22:54:11 +00:00
|
|
|
impl ViewWrapper for KeyEventView {
|
2015-05-19 23:53:50 +00:00
|
|
|
wrap_impl!(self.content);
|
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 {
|
|
|
|
match self.content.on_event(event) {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|