mirror of
https://github.com/italicsjenga/winit-sonoma-fix.git
synced 2024-12-23 22:01:31 +11:00
Add interrupting the events loop
This commit is contained in:
parent
e7d43174e7
commit
0242daa242
|
@ -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(),
|
||||||
_ => ()
|
_ => ()
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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.
|
||||||
|
|
Loading…
Reference in a new issue