From ab1dfaaaa53a3acd206bf494ac90e3fe130dc609 Mon Sep 17 00:00:00 2001 From: Hal Gentz Date: Tue, 23 Apr 2019 21:52:17 -0600 Subject: [PATCH] Minor Signed-off-by: Hal Gentz --- src/platform/unix.rs | 6 +-- src/platform_impl/linux/x11/mod.rs | 64 +++++++++++------------------- 2 files changed, 27 insertions(+), 43 deletions(-) diff --git a/src/platform/unix.rs b/src/platform/unix.rs index acc998c047b..df7d0100e15 100644 --- a/src/platform/unix.rs +++ b/src/platform/unix.rs @@ -112,11 +112,11 @@ pub trait EventLoopExtUnix { #[doc(hidden)] fn get_xlib_xconnection(&self) -> Option>; - /// Returns a pointer to the `wl_display` object of wayland that is used by this `EventsLoop`. + /// Returns a pointer to the `wl_display` object of wayland that is used by this `EventLoop`. /// - /// Returns `None` if the `EventsLoop` doesn't use wayland (if it uses xlib for example). + /// Returns `None` if the `EventLoop` doesn't use wayland (if it uses xlib for example). /// - /// The pointer will become invalid when the glutin `EventsLoop` is destroyed. + /// The pointer will become invalid when the glutin `EventLoop` is destroyed. fn get_wayland_display(&self) -> Option<*mut raw::c_void>; } diff --git a/src/platform_impl/linux/x11/mod.rs b/src/platform_impl/linux/x11/mod.rs index 45485d19de8..ea57996a3de 100644 --- a/src/platform_impl/linux/x11/mod.rs +++ b/src/platform_impl/linux/x11/mod.rs @@ -221,10 +221,24 @@ impl EventLoop { &self.target } + fn sticky_callback(&self, evt: Event, control_flow: &mut ControlFlow, dummy: &mut ControlFlow, mut callback: F) + where F: FnMut(Event, &RootELW, &mut ControlFlow) { + let cf = if *control_flow == ControlFlow::Exit { + dummy + } else { + control_flow + }; + callback(evt, &self.target, cf); + } + pub fn run_return(&mut self, mut callback: F) where F: FnMut(Event, &RootELW, &mut ControlFlow) { let mut control_flow = ControlFlow::default(); + // make ControlFlow::Exit sticky by providing a dummy + // control flow reference if it is already Exit. + let mut dummy = ControlFlow::Exit; + let wt = get_xtarget(&self.target); loop { @@ -232,64 +246,34 @@ impl EventLoop { { let mut guard = self.pending_events.borrow_mut(); for evt in guard.drain(..) { - // make ControlFlow::Exit sticky by providing a dummy - // control flow reference if it is already Exit. - let mut dummy = ControlFlow::Exit; - let cf = if control_flow == ControlFlow::Exit { - &mut dummy - } else { - &mut control_flow - }; - // user callback - callback(evt, &self.target, cf); + self.sticky_callback(evt, &mut control_flow, &mut dummy, &mut callback); } } + // Empty the user event buffer { let mut guard = self.pending_user_events.borrow_mut(); for evt in guard.drain(..) { - // make ControlFlow::Exit sticky - let mut dummy = ControlFlow::Exit; - let cf = if control_flow == ControlFlow::Exit { - &mut dummy - } else { - &mut control_flow - }; - // user callback - callback(::event::Event::UserEvent(evt), &self.target, cf); + self.sticky_callback(::event::Event::UserEvent(evt), &mut control_flow, &mut dummy, &mut callback); } } + // send Events cleared - { - // make ControlFlow::Exit sticky - let mut dummy = ControlFlow::Exit; - let cf = if control_flow == ControlFlow::Exit { - &mut dummy - } else { - &mut control_flow - }; - // user callback - callback(::event::Event::EventsCleared, &self.target, cf); - } + self.sticky_callback(::event::Event::EventsCleared, &mut control_flow, &mut dummy, &mut callback); + // Empty the redraw requests { let mut guard = wt.pending_redraws.lock().unwrap(); for wid in guard.drain(..) { - // make ControlFlow::Exit sticky - let mut dummy = ControlFlow::Exit; - let cf = if control_flow == ControlFlow::Exit { - &mut dummy - } else { - &mut control_flow - }; // user callback - callback( + self.sticky_callback( Event::WindowEvent { window_id: ::window::WindowId(super::WindowId::X(wid)), event: WindowEvent::RedrawRequested }, - &self.target, - cf + &mut control_flow, + &mut dummy, + &mut callback, ); } }