diff --git a/examples/window.rs b/examples/window.rs index 5578dab1..c6d9327a 100644 --- a/examples/window.rs +++ b/examples/window.rs @@ -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(), _ => () } }); diff --git a/src/api_transition.rs b/src/api_transition.rs index 48968376..09788296 100644 --- a/src/api_transition.rs +++ b/src/api_transition.rs @@ -9,15 +9,21 @@ macro_rules! gen_api_transition { () => { pub struct EventsLoop { windows: ::std::sync::Mutex>>, + 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(&self, mut callback: F) where F: FnMut(::Event) { @@ -35,10 +41,15 @@ macro_rules! gen_api_transition { pub fn run_forever(&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; + } } } } diff --git a/src/lib.rs b/src/lib.rs index b28ea0e3..0797891f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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.