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); println!("{:?}", event);
match 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 { pub struct EventsLoop {
windows: ::std::sync::Mutex<Vec<::std::sync::Arc<Window>>>, windows: ::std::sync::Mutex<Vec<::std::sync::Arc<Window>>>,
interrupted: ::std::sync::atomic::AtomicBool,
} }
impl EventsLoop { impl EventsLoop {
pub fn new() -> EventsLoop { pub fn new() -> EventsLoop {
EventsLoop { EventsLoop {
windows: ::std::sync::Mutex::new(vec![]), 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) pub fn poll_events<F>(&self, mut callback: F)
where F: FnMut(::Event) where F: FnMut(::Event)
{ {
@ -35,10 +41,15 @@ macro_rules! gen_api_transition {
pub fn run_forever<F>(&self, mut callback: F) pub fn run_forever<F>(&self, mut callback: F)
where F: FnMut(::Event) where F: FnMut(::Event)
{ {
self.interrupted.store(false, ::std::sync::atomic::Ordering::Relaxed);
// Yeah that's a very bad implementation. // Yeah that's a very bad implementation.
loop { loop {
self.poll_events(|e| callback(e)); self.poll_events(|e| callback(e));
::std::thread::sleep_ms(5); ::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) self.events_loop.run_forever(callback)
} }
#[inline]
pub fn interrupt(&self) {
self.events_loop.interrupt()
}
} }
/// Object that allows you to build windows. /// Object that allows you to build windows.