Add timeout argument to pump_events

This renames all internal implementations of pump_events_with_timeout
to pump_events and makes them public.

Since all platforms that support pump_events support timeouts there's
no need to have a separate API.
This commit is contained in:
Robert Bragg 2023-07-24 00:37:51 +01:00 committed by Kirill Chibisov
parent e6c7cc297d
commit ae9b02e097
9 changed files with 23 additions and 68 deletions

View file

@ -14,7 +14,7 @@ fn main() -> std::process::ExitCode {
use simple_logger::SimpleLogger; use simple_logger::SimpleLogger;
use winit::{ use winit::{
event::{Event, WindowEvent}, event::{Event, WindowEvent},
event_loop::EventLoop, event_loop::{ControlFlow, EventLoop},
platform::pump_events::{EventLoopExtPumpEvents, PumpStatus}, platform::pump_events::{EventLoopExtPumpEvents, PumpStatus},
window::WindowBuilder, window::WindowBuilder,
}; };
@ -31,7 +31,10 @@ fn main() -> std::process::ExitCode {
.unwrap(); .unwrap();
'main: loop { 'main: loop {
let status = event_loop.pump_events(|event, _, control_flow| { let timeout = Some(Duration::ZERO);
let status = event_loop.pump_events(timeout, |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
if let Event::WindowEvent { event, .. } = &event { if let Event::WindowEvent { event, .. } = &event {
// Print only Window events to reduce noise // Print only Window events to reduce noise
println!("{event:?}"); println!("{event:?}");

View file

@ -1,3 +1,5 @@
use std::time::Duration;
use crate::{ use crate::{
event::Event, event::Event,
event_loop::{ControlFlow, EventLoop, EventLoopWindowTarget}, event_loop::{ControlFlow, EventLoop, EventLoopWindowTarget},
@ -60,7 +62,8 @@ pub trait EventLoopExtPumpEvents {
/// .unwrap(); /// .unwrap();
/// ///
/// 'main: loop { /// 'main: loop {
/// let status = event_loop.pump_events(|event, _, control_flow| { /// let timeout = Some(Duration::ZERO);
/// let status = event_loop.pump_events(timeout, |event, _, control_flow| {
/// # if let Event::WindowEvent { event, .. } = &event { /// # if let Event::WindowEvent { event, .. } = &event {
/// # // Print only Window events to reduce noise /// # // Print only Window events to reduce noise
/// # println!("{event:?}"); /// # println!("{event:?}");
@ -169,7 +172,7 @@ pub trait EventLoopExtPumpEvents {
/// If you render outside of Winit you are likely to see window resizing artifacts /// If you render outside of Winit you are likely to see window resizing artifacts
/// since MacOS expects applications to render synchronously during any `drawRect` /// since MacOS expects applications to render synchronously during any `drawRect`
/// callback. /// callback.
fn pump_events<F>(&mut self, event_handler: F) -> PumpStatus fn pump_events<F>(&mut self, timeout: Option<Duration>, event_handler: F) -> PumpStatus
where where
F: FnMut( F: FnMut(
Event<'_, Self::UserEvent>, Event<'_, Self::UserEvent>,
@ -181,7 +184,7 @@ pub trait EventLoopExtPumpEvents {
impl<T> EventLoopExtPumpEvents for EventLoop<T> { impl<T> EventLoopExtPumpEvents for EventLoop<T> {
type UserEvent = T; type UserEvent = T;
fn pump_events<F>(&mut self, event_handler: F) -> PumpStatus fn pump_events<F>(&mut self, timeout: Option<Duration>, event_handler: F) -> PumpStatus
where where
F: FnMut( F: FnMut(
Event<'_, Self::UserEvent>, Event<'_, Self::UserEvent>,
@ -189,6 +192,6 @@ impl<T> EventLoopExtPumpEvents for EventLoop<T> {
&mut ControlFlow, &mut ControlFlow,
), ),
{ {
self.event_loop.pump_events(event_handler) self.event_loop.pump_events(timeout, event_handler)
} }
} }

View file

@ -543,7 +543,7 @@ impl<T: 'static> EventLoop<T> {
} }
loop { loop {
match self.pump_events_with_timeout(None, &mut event_handler) { match self.pump_events(None, &mut event_handler) {
PumpStatus::Exit(0) => { PumpStatus::Exit(0) => {
break Ok(()); break Ok(());
} }
@ -557,18 +557,7 @@ impl<T: 'static> EventLoop<T> {
} }
} }
pub fn pump_events<F>(&mut self, event_handler: F) -> PumpStatus pub fn pump_events<F>(&mut self, timeout: Option<Duration>, mut callback: F) -> PumpStatus
where
F: FnMut(event::Event<'_, T>, &RootELW<T>, &mut ControlFlow),
{
self.pump_events_with_timeout(Some(Duration::ZERO), event_handler)
}
fn pump_events_with_timeout<F>(
&mut self,
timeout: Option<Duration>,
mut callback: F,
) -> PumpStatus
where where
F: FnMut(event::Event<'_, T>, &RootELW<T>, &mut ControlFlow), F: FnMut(event::Event<'_, T>, &RootELW<T>, &mut ControlFlow),
{ {

View file

@ -844,11 +844,11 @@ impl<T: 'static> EventLoop<T> {
x11_or_wayland!(match self; EventLoop(evlp) => evlp.run_ondemand(callback)) x11_or_wayland!(match self; EventLoop(evlp) => evlp.run_ondemand(callback))
} }
pub fn pump_events<F>(&mut self, callback: F) -> PumpStatus pub fn pump_events<F>(&mut self, timeout: Option<Duration>, callback: F) -> PumpStatus
where where
F: FnMut(crate::event::Event<'_, T>, &RootELW<T>, &mut ControlFlow), F: FnMut(crate::event::Event<'_, T>, &RootELW<T>, &mut ControlFlow),
{ {
x11_or_wayland!(match self; EventLoop(evlp) => evlp.pump_events(callback)) x11_or_wayland!(match self; EventLoop(evlp) => evlp.pump_events(timeout, callback))
} }
pub fn window_target(&self) -> &crate::event_loop::EventLoopWindowTarget<T> { pub fn window_target(&self) -> &crate::event_loop::EventLoopWindowTarget<T> {

View file

@ -154,7 +154,7 @@ impl<T: 'static> EventLoop<T> {
} }
let exit = loop { let exit = loop {
match self.pump_events_with_timeout(None, &mut event_handler) { match self.pump_events(None, &mut event_handler) {
PumpStatus::Exit(0) => { PumpStatus::Exit(0) => {
break Ok(()); break Ok(());
} }
@ -176,18 +176,7 @@ impl<T: 'static> EventLoop<T> {
exit exit
} }
pub fn pump_events<F>(&mut self, event_handler: F) -> PumpStatus pub fn pump_events<F>(&mut self, timeout: Option<Duration>, mut callback: F) -> PumpStatus
where
F: FnMut(Event<'_, T>, &RootEventLoopWindowTarget<T>, &mut ControlFlow),
{
self.pump_events_with_timeout(Some(Duration::ZERO), event_handler)
}
fn pump_events_with_timeout<F>(
&mut self,
timeout: Option<Duration>,
mut callback: F,
) -> PumpStatus
where where
F: FnMut(Event<'_, T>, &RootEventLoopWindowTarget<T>, &mut ControlFlow), F: FnMut(Event<'_, T>, &RootEventLoopWindowTarget<T>, &mut ControlFlow),
{ {

View file

@ -441,7 +441,7 @@ impl<T: 'static> EventLoop<T> {
} }
let exit = loop { let exit = loop {
match self.pump_events_with_timeout(None, &mut event_handler) { match self.pump_events(None, &mut event_handler) {
PumpStatus::Exit(0) => { PumpStatus::Exit(0) => {
break Ok(()); break Ok(());
} }
@ -466,18 +466,7 @@ impl<T: 'static> EventLoop<T> {
exit exit
} }
pub fn pump_events<F>(&mut self, event_handler: F) -> PumpStatus pub fn pump_events<F>(&mut self, timeout: Option<Duration>, mut callback: F) -> PumpStatus
where
F: FnMut(Event<'_, T>, &RootELW<T>, &mut ControlFlow),
{
self.pump_events_with_timeout(Some(Duration::ZERO), event_handler)
}
fn pump_events_with_timeout<F>(
&mut self,
timeout: Option<Duration>,
mut callback: F,
) -> PumpStatus
where where
F: FnMut(Event<'_, T>, &RootELW<T>, &mut ControlFlow), F: FnMut(Event<'_, T>, &RootELW<T>, &mut ControlFlow),
{ {

View file

@ -632,7 +632,7 @@ impl AppState {
Self::stop(); Self::stop();
} }
HANDLER.update_start_time(); HANDLER.update_start_time();
let wait_timeout = HANDLER.wait_timeout(); // configured by pump_events_with_timeout let wait_timeout = HANDLER.wait_timeout(); // configured by pump_events
let app_timeout = match HANDLER.control_flow() { let app_timeout = match HANDLER.control_flow() {
ControlFlow::Wait => None, ControlFlow::Wait => None,
ControlFlow::Poll | ControlFlow::ExitWithCode(_) => Some(Instant::now()), ControlFlow::Poll | ControlFlow::ExitWithCode(_) => Some(Instant::now()),

View file

@ -291,14 +291,7 @@ impl<T> EventLoop<T> {
} }
} }
pub fn pump_events<F>(&mut self, callback: F) -> PumpStatus pub fn pump_events<F>(&mut self, timeout: Option<Duration>, callback: F) -> PumpStatus
where
F: FnMut(Event<'_, T>, &RootWindowTarget<T>, &mut ControlFlow),
{
self.pump_events_with_timeout(Some(Duration::ZERO), callback)
}
fn pump_events_with_timeout<F>(&mut self, timeout: Option<Duration>, callback: F) -> PumpStatus
where where
F: FnMut(Event<'_, T>, &RootWindowTarget<T>, &mut ControlFlow), F: FnMut(Event<'_, T>, &RootWindowTarget<T>, &mut ControlFlow),
{ {

View file

@ -296,18 +296,7 @@ impl<T: 'static> EventLoop<T> {
} }
} }
pub fn pump_events<F>(&mut self, event_handler: F) -> PumpStatus pub fn pump_events<F>(&mut self, timeout: Option<Duration>, mut event_handler: F) -> PumpStatus
where
F: FnMut(Event<'_, T>, &RootELW<T>, &mut ControlFlow),
{
self.pump_events_with_timeout(Some(Duration::ZERO), event_handler)
}
fn pump_events_with_timeout<F>(
&mut self,
timeout: Option<Duration>,
mut event_handler: F,
) -> PumpStatus
where where
F: FnMut(Event<'_, T>, &RootELW<T>, &mut ControlFlow), F: FnMut(Event<'_, T>, &RootELW<T>, &mut ControlFlow),
{ {