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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
use std::cell::RefCell;
use std::rc::Rc;
use std::sync::mpsc::{Receiver, TryIter};
use crate::event::WindowEvent;
pub struct Event<'a> {
pub value: WindowEvent,
pub inhibited: bool,
inhibitor: &'a RefCell<Vec<WindowEvent>>,
}
impl<'a> Drop for Event<'a> {
#[inline]
fn drop(&mut self) {
if !self.inhibited {
self.inhibitor.borrow_mut().push(self.value)
}
}
}
impl<'a> Event<'a> {
#[inline]
fn new(value: WindowEvent, inhibitor: &RefCell<Vec<WindowEvent>>) -> Event {
Event {
value,
inhibited: false,
inhibitor,
}
}
}
pub struct Events<'a> {
stream: TryIter<'a, WindowEvent>,
inhibitor: &'a RefCell<Vec<WindowEvent>>,
}
impl<'a> Events<'a> {
#[inline]
fn new(
stream: TryIter<'a, WindowEvent>,
inhibitor: &'a RefCell<Vec<WindowEvent>>,
) -> Events<'a> {
Events { stream, inhibitor }
}
}
impl<'a> Iterator for Events<'a> {
type Item = Event<'a>;
#[inline]
fn next(&mut self) -> Option<Event<'a>> {
match self.stream.next() {
None => None,
Some(e) => Some(Event::new(e, self.inhibitor)),
}
}
}
pub struct EventManager {
events: Rc<Receiver<WindowEvent>>,
inhibitor: Rc<RefCell<Vec<WindowEvent>>>,
}
impl EventManager {
#[inline]
pub fn new(
events: Rc<Receiver<WindowEvent>>,
inhibitor: Rc<RefCell<Vec<WindowEvent>>>,
) -> EventManager {
EventManager { events, inhibitor }
}
#[inline]
pub fn iter(&mut self) -> Events {
Events::new(self.events.try_iter(), &*self.inhibitor)
}
}