1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use processors::{Listeners, ListenerProcessor};
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq)]
pub struct Event<'a> {
pub type_str: Option<&'a str>,
pub target_value: Option<&'a str>,
pub client_x: i32,
pub client_y: i32,
pub offset_x: i32,
pub offset_y: i32,
pub which_keycode: i32,
pub shift_key: bool,
pub alt_key: bool,
pub ctrl_key: bool,
pub meta_key: bool,
}
pub trait Listener<Message> {
fn event_type_handled(&self) -> &'static str;
fn handle_event(&self, Event) -> Message;
}
pub struct FnListener<M, F: Fn(Event) -> M> {
event_type_handled: &'static str,
f: F,
}
impl<M, F: Fn(Event) -> M> Listeners<M> for FnListener<M, F> {
fn process_all<'a, P: ListenerProcessor<'a, M>>(&'a self, acc: &mut P::Acc) -> Result<(), P::Error> {
P::get_processor()(acc, self)
}
}
impl<M, F: Fn(Event) -> M> Listener<M> for FnListener<M, F> {
fn event_type_handled(&self) -> &'static str {
self.event_type_handled
}
fn handle_event(&self, event: Event) -> M {
(self.f)(event)
}
}
pub fn on<M, F: Fn(Event) -> M>(event_type: &'static str, f: F) -> FnListener<M, F>
{
FnListener { event_type_handled: event_type, f: f }
}