Commit 907836c4 authored by Ralf's avatar Ralf
Browse files

add a version of handle_events that does not take a timeout

parent 8fb5e5cd
use std::time::Duration; use std::time::Duration;
use chan::{Receiver, Sender}; use chan::{Receiver, Sender, async};
use util::timeout_chan; use util::timeout_chan;
use input::Event; use input::Event;
...@@ -21,13 +21,33 @@ pub struct Environment { ...@@ -21,13 +21,33 @@ pub struct Environment {
} }
macro_rules! handle_events { macro_rules! handle_events {
( $env:expr, $( $x:expr ),* ) => {
{ $env.handle_events(vec![$( $x, )*] ) }
};
}
macro_rules! handle_events_timeout {
( $env:expr, $d:expr, $( $x:expr ),* ) => { ( $env:expr, $d:expr, $( $x:expr ),* ) => {
{ $env.handle_events($d, vec![$( $x, )*] ) } { $env.handle_events_timeout($d, vec![$( $x, )*] ) }
}; };
} }
impl Environment { impl Environment {
pub fn handle_events(&mut self, pub fn handle_events(&mut self,
mut handlers: Vec<EventHandler>)
-> EventsResult {
for ev in self.events.clone() {
match self.handle_event(&mut handlers, ev) {
Some(result) => return result,
_ => {},
}
}
return EventsResult::ShuttingDown;
}
pub fn handle_events_timeout(&mut self,
d: Duration, d: Duration,
mut handlers: Vec<EventHandler>) mut handlers: Vec<EventHandler>)
-> EventsResult { -> EventsResult {
...@@ -40,12 +60,9 @@ impl Environment { ...@@ -40,12 +60,9 @@ impl Environment {
match ev { match ev {
None => return EventsResult::ShuttingDown, None => return EventsResult::ShuttingDown,
Some(ev) => { Some(ev) => {
self.record_state(&ev); match self.handle_event(&mut handlers, ev) {
for handler in handlers.iter_mut() { Some(result) => return result,
match handler.run(ev) { _ => {},
HandlerResult::Continue => {},
HandlerResult::QuitLoop => return EventsResult::Quit,
}
} }
} }
} }
...@@ -54,6 +71,17 @@ impl Environment { ...@@ -54,6 +71,17 @@ impl Environment {
} }
unreachable!() unreachable!()
} }
fn handle_event(&mut self, handlers: &mut Vec<EventHandler>, ev: Event) -> Option<EventsResult> {
self.record_state(&ev);
for handler in handlers.iter_mut() {
match handler.run(ev) {
HandlerResult::Continue => {},
HandlerResult::QuitLoop => return Some(EventsResult::Quit),
}
}
return None;
}
fn record_state(&mut self, ev: &Event) { fn record_state(&mut self, ev: &Event) {
match ev { match ev {
......
...@@ -48,7 +48,7 @@ impl State for UnLockingState { ...@@ -48,7 +48,7 @@ impl State for UnLockingState {
lock_actor.act(LockAction::Release); lock_actor.act(LockAction::Release);
lock_actor lock_actor
}); });
let r = handle_events!(env, Duration::from_secs(1), let r = handle_events_timeout!(env, Duration::from_secs(1),
EventHandler::new_safe(user_command_reject_handler), EventHandler::new_safe(user_command_reject_handler),
EventHandler::new(|ev| { EventHandler::new(|ev| {
match ev { match ev {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment