Skip to content

Commit

Permalink
Add timeout argument to pump_events
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rib committed Jul 25, 2023
1 parent 6ad761e commit de9dd42
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 68 deletions.
7 changes: 5 additions & 2 deletions examples/window_pump_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn main() -> std::process::ExitCode {
use simple_logger::SimpleLogger;
use winit::{
event::{Event, WindowEvent},
event_loop::EventLoop,
event_loop::{ControlFlow, EventLoop},
platform::pump_events::{EventLoopExtPumpEvents, PumpStatus},
window::WindowBuilder,
};
Expand All @@ -31,7 +31,10 @@ fn main() -> std::process::ExitCode {
.unwrap();

'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 {
// Print only Window events to reduce noise
println!("{event:?}");
Expand Down
11 changes: 7 additions & 4 deletions src/platform/pump_events.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::time::Duration;

use crate::{
event::Event,
event_loop::{ControlFlow, EventLoop, EventLoopWindowTarget},
Expand Down Expand Up @@ -60,7 +62,8 @@ pub trait EventLoopExtPumpEvents {
/// .unwrap();
///
/// '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 {
/// # // Print only Window events to reduce noise
/// # println!("{event:?}");
Expand Down Expand Up @@ -169,7 +172,7 @@ pub trait EventLoopExtPumpEvents {
/// If you render outside of Winit you are likely to see window resizing artifacts
/// since MacOS expects applications to render synchronously during any `drawRect`
/// 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
F: FnMut(
Event<'_, Self::UserEvent>,
Expand All @@ -181,14 +184,14 @@ pub trait EventLoopExtPumpEvents {
impl<T> EventLoopExtPumpEvents for EventLoop<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
F: FnMut(
Event<'_, Self::UserEvent>,
&EventLoopWindowTarget<Self::UserEvent>,
&mut ControlFlow,
),
{
self.event_loop.pump_events(event_handler)
self.event_loop.pump_events(timeout, event_handler)
}
}
15 changes: 2 additions & 13 deletions src/platform_impl/android/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ impl<T: 'static> EventLoop<T> {
}

loop {
match self.pump_events_with_timeout(None, &mut event_handler) {
match self.pump_events(None, &mut event_handler) {
PumpStatus::Exit(0) => {
break Ok(());
}
Expand All @@ -557,18 +557,7 @@ impl<T: 'static> EventLoop<T> {
}
}

pub fn pump_events<F>(&mut self, event_handler: 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
pub fn pump_events<F>(&mut self, timeout: Option<Duration>, mut callback: F) -> PumpStatus
where
F: FnMut(event::Event<'_, T>, &RootELW<T>, &mut ControlFlow),
{
Expand Down
4 changes: 2 additions & 2 deletions src/platform_impl/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -844,11 +844,11 @@ impl<T: 'static> EventLoop<T> {
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
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> {
Expand Down
15 changes: 2 additions & 13 deletions src/platform_impl/linux/wayland/event_loop/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl<T: 'static> EventLoop<T> {
}

let exit = loop {
match self.pump_events_with_timeout(None, &mut event_handler) {
match self.pump_events(None, &mut event_handler) {
PumpStatus::Exit(0) => {
break Ok(());
}
Expand All @@ -176,18 +176,7 @@ impl<T: 'static> EventLoop<T> {
exit
}

pub fn pump_events<F>(&mut self, event_handler: 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
pub fn pump_events<F>(&mut self, timeout: Option<Duration>, mut callback: F) -> PumpStatus
where
F: FnMut(Event<'_, T>, &RootEventLoopWindowTarget<T>, &mut ControlFlow),
{
Expand Down
15 changes: 2 additions & 13 deletions src/platform_impl/linux/x11/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ impl<T: 'static> EventLoop<T> {
}

let exit = loop {
match self.pump_events_with_timeout(None, &mut event_handler) {
match self.pump_events(None, &mut event_handler) {
PumpStatus::Exit(0) => {
break Ok(());
}
Expand All @@ -466,18 +466,7 @@ impl<T: 'static> EventLoop<T> {
exit
}

pub fn pump_events<F>(&mut self, 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 callback: 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),
{
Expand Down
2 changes: 1 addition & 1 deletion src/platform_impl/macos/app_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ impl AppState {
Self::stop();
}
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() {
ControlFlow::Wait => None,
ControlFlow::Poll | ControlFlow::ExitWithCode(_) => Some(Instant::now()),
Expand Down
9 changes: 1 addition & 8 deletions src/platform_impl/macos/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,14 +291,7 @@ impl<T> EventLoop<T> {
}
}

pub fn pump_events<F>(&mut self, 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
pub fn pump_events<F>(&mut self, timeout: Option<Duration>, callback: F) -> PumpStatus
where
F: FnMut(Event<'_, T>, &RootWindowTarget<T>, &mut ControlFlow),
{
Expand Down
13 changes: 1 addition & 12 deletions src/platform_impl/windows/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,18 +296,7 @@ impl<T: 'static> EventLoop<T> {
}
}

pub fn pump_events<F>(&mut self, 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
pub fn pump_events<F>(&mut self, timeout: Option<Duration>, mut event_handler: F) -> PumpStatus
where
F: FnMut(Event<'_, T>, &RootELW<T>, &mut ControlFlow),
{
Expand Down

0 comments on commit de9dd42

Please sign in to comment.