Add interrupting the events loop

This commit is contained in:
Pierre Krieger 2017-01-28 15:09:01 +01:00
parent e7d43174e7
commit 0242daa242
3 changed files with 17 additions and 1 deletions

View file

@ -12,7 +12,7 @@ fn main() {
println!("{:?}", event);
match event {
winit::Event::WindowEvent { event: winit::WindowEvent::Closed, .. } => return,
winit::Event::WindowEvent { event: winit::WindowEvent::Closed, .. } => events_loop.interrupt(),
_ => ()
}
});

View file

@ -9,15 +9,21 @@ macro_rules! gen_api_transition {
() => {
pub struct EventsLoop {
windows: ::std::sync::Mutex<Vec<::std::sync::Arc<Window>>>,
interrupted: ::std::sync::atomic::AtomicBool,
}
impl EventsLoop {
pub fn new() -> EventsLoop {
EventsLoop {
windows: ::std::sync::Mutex::new(vec![]),
interrupted: ::std::sync::atomic::AtomicBool::new(false),
}
}
pub fn interrupt(&self) {
self.interrupted.store(true, ::std::sync::atomic::Ordering::Relaxed);
}
pub fn poll_events<F>(&self, mut callback: F)
where F: FnMut(::Event)
{
@ -35,10 +41,15 @@ macro_rules! gen_api_transition {
pub fn run_forever<F>(&self, mut callback: F)
where F: FnMut(::Event)
{
self.interrupted.store(false, ::std::sync::atomic::Ordering::Relaxed);
// Yeah that's a very bad implementation.
loop {
self.poll_events(|e| callback(e));
::std::thread::sleep_ms(5);
if self.interrupted.load(::std::sync::atomic::Ordering::Relaxed) {
break;
}
}
}
}

View file

@ -127,6 +127,11 @@ impl EventsLoop {
{
self.events_loop.run_forever(callback)
}
#[inline]
pub fn interrupt(&self) {
self.events_loop.interrupt()
}
}
/// Object that allows you to build windows.