Handle ControlFlow::Exit

This commit is contained in:
Ryan Goldstein 2019-03-18 22:13:30 -04:00
parent 85446d81f3
commit b09629f1d4

View file

@ -67,6 +67,7 @@ pub struct ELRShared<T> {
struct EventLoopRunner<T> {
control: ControlFlow,
handling: bool,
event_handler: Box<dyn FnMut(Event<T>, &mut ControlFlow)>,
}
@ -89,11 +90,7 @@ impl<T> EventLoop<T> {
}
pub fn run<F>(self, mut event_handler: F) -> !
where F: 'static + FnMut(Event<T>, &RootELW<T>, &mut ControlFlow)
{
// TODO: how to handle request redraw?
// TODO: onclose (stdweb PR)
// TODO: file dropping, PathBuf isn't useful for web
where F: 'static + FnMut(Event<T>, &RootELW<T>, &mut ControlFlow) {
let runner = self.elw.p.runner;
let relw = RootELW {
@ -130,7 +127,6 @@ impl<T> EventLoop<T> {
elrs.send_event(Event::WindowEvent {
window_id: RootWI(WindowId),
event: WindowEvent::KeyboardInput {
// TODO: is there a way to get keyboard device?
device_id: RootDI(unsafe { DeviceId::dummy() }),
input: KeyboardInput {
scancode: scancode(&event),
@ -145,7 +141,6 @@ impl<T> EventLoop<T> {
elrs.send_event(Event::WindowEvent {
window_id: RootWI(WindowId),
event: WindowEvent::KeyboardInput {
// TODO: is there a way to get keyboard device?
device_id: RootDI(unsafe { DeviceId::dummy() }),
input: KeyboardInput {
scancode: scancode(&event),
@ -248,6 +243,7 @@ impl<T> ELRShared<T> {
fn set_listener(&self, event_handler: Box<dyn FnMut(Event<T>, &mut ControlFlow)>) {
*self.runner.borrow_mut() = Some(EventLoopRunner {
control: ControlFlow::Poll,
handling: false
event_handler
});
}
@ -256,11 +252,17 @@ impl<T> ELRShared<T> {
// TODO: handle event buffer
pub fn send_event(&self, event: Event<T>) {
match *self.runner.borrow_mut() {
Some(ref mut runner) => {
Some(ref mut runner) if !runner.handling => {
runner.handling = true;
let closed = runner.control == ControlFlow::Exit;
// TODO: bracket this in control flow events?
(runner.event_handler)(event, &mut runner.control);
if closed {
runner.control = ControlFlow::Exit;
}
None => ()
runner.handling = false;
}
_ => self.events.borrow_mut().push_back(event)
}
}