Allow multiple callbacks to be registered per event.

This commit is contained in:
Alexandre Bury 2018-01-11 18:47:44 +01:00
parent 2d62d2c91f
commit 0c24ed369b

View File

@ -25,7 +25,7 @@ pub type ScreenId = usize;
pub struct Cursive { pub struct Cursive {
theme: theme::Theme, theme: theme::Theme,
screens: Vec<views::StackView>, screens: Vec<views::StackView>,
global_callbacks: HashMap<Event, Callback>, global_callbacks: HashMap<Event, Vec<Callback>>,
menubar: views::Menubar, menubar: views::Menubar,
// Last layer sizes of the stack view. // Last layer sizes of the stack view.
@ -381,7 +381,9 @@ impl Cursive {
F: Fn(&mut Cursive) + 'static, F: Fn(&mut Cursive) + 'static,
{ {
self.global_callbacks self.global_callbacks
.insert(event.into(), Callback::from_fn(cb)); .entry(event.into())
.or_insert(Vec::new())
.push(Callback::from_fn(cb));
} }
/// Add a layer to the current screen. /// Add a layer to the current screen.
@ -420,12 +422,14 @@ impl Cursive {
// Handles a key event when it was ignored by the current view // Handles a key event when it was ignored by the current view
fn on_event(&mut self, event: Event) { fn on_event(&mut self, event: Event) {
let cb = match self.global_callbacks.get(&event) { let cb_list = match self.global_callbacks.get(&event) {
None => return, None => return,
Some(cb) => cb.clone(), Some(cb_list) => cb_list.clone(),
}; };
// Not from a view, so no viewpath here // Not from a view, so no viewpath here
cb(self); for cb in cb_list {
cb(self);
}
} }
/// Returns the size of the screen, in characters. /// Returns the size of the screen, in characters.