Skip to content

Commit ae9b02e

Browse files
ribkchibisov
authored andcommitted
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.
1 parent e6c7cc2 commit ae9b02e

File tree

9 files changed

+23
-68
lines changed

9 files changed

+23
-68
lines changed

examples/window_pump_events.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ fn main() -> std::process::ExitCode {
1414
use simple_logger::SimpleLogger;
1515
use winit::{
1616
event::{Event, WindowEvent},
17-
event_loop::EventLoop,
17+
event_loop::{ControlFlow, EventLoop},
1818
platform::pump_events::{EventLoopExtPumpEvents, PumpStatus},
1919
window::WindowBuilder,
2020
};
@@ -31,7 +31,10 @@ fn main() -> std::process::ExitCode {
3131
.unwrap();
3232

3333
'main: loop {
34-
let status = event_loop.pump_events(|event, _, control_flow| {
34+
let timeout = Some(Duration::ZERO);
35+
let status = event_loop.pump_events(timeout, |event, _, control_flow| {
36+
*control_flow = ControlFlow::Wait;
37+
3538
if let Event::WindowEvent { event, .. } = &event {
3639
// Print only Window events to reduce noise
3740
println!("{event:?}");

src/platform/pump_events.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::time::Duration;
2+
13
use crate::{
24
event::Event,
35
event_loop::{ControlFlow, EventLoop, EventLoopWindowTarget},
@@ -60,7 +62,8 @@ pub trait EventLoopExtPumpEvents {
6062
/// .unwrap();
6163
///
6264
/// 'main: loop {
63-
/// let status = event_loop.pump_events(|event, _, control_flow| {
65+
/// let timeout = Some(Duration::ZERO);
66+
/// let status = event_loop.pump_events(timeout, |event, _, control_flow| {
6467
/// # if let Event::WindowEvent { event, .. } = &event {
6568
/// # // Print only Window events to reduce noise
6669
/// # println!("{event:?}");
@@ -169,7 +172,7 @@ pub trait EventLoopExtPumpEvents {
169172
/// If you render outside of Winit you are likely to see window resizing artifacts
170173
/// since MacOS expects applications to render synchronously during any `drawRect`
171174
/// callback.
172-
fn pump_events<F>(&mut self, event_handler: F) -> PumpStatus
175+
fn pump_events<F>(&mut self, timeout: Option<Duration>, event_handler: F) -> PumpStatus
173176
where
174177
F: FnMut(
175178
Event<'_, Self::UserEvent>,
@@ -181,14 +184,14 @@ pub trait EventLoopExtPumpEvents {
181184
impl<T> EventLoopExtPumpEvents for EventLoop<T> {
182185
type UserEvent = T;
183186

184-
fn pump_events<F>(&mut self, event_handler: F) -> PumpStatus
187+
fn pump_events<F>(&mut self, timeout: Option<Duration>, event_handler: F) -> PumpStatus
185188
where
186189
F: FnMut(
187190
Event<'_, Self::UserEvent>,
188191
&EventLoopWindowTarget<Self::UserEvent>,
189192
&mut ControlFlow,
190193
),
191194
{
192-
self.event_loop.pump_events(event_handler)
195+
self.event_loop.pump_events(timeout, event_handler)
193196
}
194197
}

src/platform_impl/android/mod.rs

+2-13
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ impl<T: 'static> EventLoop<T> {
543543
}
544544

545545
loop {
546-
match self.pump_events_with_timeout(None, &mut event_handler) {
546+
match self.pump_events(None, &mut event_handler) {
547547
PumpStatus::Exit(0) => {
548548
break Ok(());
549549
}
@@ -557,18 +557,7 @@ impl<T: 'static> EventLoop<T> {
557557
}
558558
}
559559

560-
pub fn pump_events<F>(&mut self, event_handler: F) -> PumpStatus
561-
where
562-
F: FnMut(event::Event<'_, T>, &RootELW<T>, &mut ControlFlow),
563-
{
564-
self.pump_events_with_timeout(Some(Duration::ZERO), event_handler)
565-
}
566-
567-
fn pump_events_with_timeout<F>(
568-
&mut self,
569-
timeout: Option<Duration>,
570-
mut callback: F,
571-
) -> PumpStatus
560+
pub fn pump_events<F>(&mut self, timeout: Option<Duration>, mut callback: F) -> PumpStatus
572561
where
573562
F: FnMut(event::Event<'_, T>, &RootELW<T>, &mut ControlFlow),
574563
{

src/platform_impl/linux/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -844,11 +844,11 @@ impl<T: 'static> EventLoop<T> {
844844
x11_or_wayland!(match self; EventLoop(evlp) => evlp.run_ondemand(callback))
845845
}
846846

847-
pub fn pump_events<F>(&mut self, callback: F) -> PumpStatus
847+
pub fn pump_events<F>(&mut self, timeout: Option<Duration>, callback: F) -> PumpStatus
848848
where
849849
F: FnMut(crate::event::Event<'_, T>, &RootELW<T>, &mut ControlFlow),
850850
{
851-
x11_or_wayland!(match self; EventLoop(evlp) => evlp.pump_events(callback))
851+
x11_or_wayland!(match self; EventLoop(evlp) => evlp.pump_events(timeout, callback))
852852
}
853853

854854
pub fn window_target(&self) -> &crate::event_loop::EventLoopWindowTarget<T> {

src/platform_impl/linux/wayland/event_loop/mod.rs

+2-13
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ impl<T: 'static> EventLoop<T> {
154154
}
155155

156156
let exit = loop {
157-
match self.pump_events_with_timeout(None, &mut event_handler) {
157+
match self.pump_events(None, &mut event_handler) {
158158
PumpStatus::Exit(0) => {
159159
break Ok(());
160160
}
@@ -176,18 +176,7 @@ impl<T: 'static> EventLoop<T> {
176176
exit
177177
}
178178

179-
pub fn pump_events<F>(&mut self, event_handler: F) -> PumpStatus
180-
where
181-
F: FnMut(Event<'_, T>, &RootEventLoopWindowTarget<T>, &mut ControlFlow),
182-
{
183-
self.pump_events_with_timeout(Some(Duration::ZERO), event_handler)
184-
}
185-
186-
fn pump_events_with_timeout<F>(
187-
&mut self,
188-
timeout: Option<Duration>,
189-
mut callback: F,
190-
) -> PumpStatus
179+
pub fn pump_events<F>(&mut self, timeout: Option<Duration>, mut callback: F) -> PumpStatus
191180
where
192181
F: FnMut(Event<'_, T>, &RootEventLoopWindowTarget<T>, &mut ControlFlow),
193182
{

src/platform_impl/linux/x11/mod.rs

+2-13
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ impl<T: 'static> EventLoop<T> {
441441
}
442442

443443
let exit = loop {
444-
match self.pump_events_with_timeout(None, &mut event_handler) {
444+
match self.pump_events(None, &mut event_handler) {
445445
PumpStatus::Exit(0) => {
446446
break Ok(());
447447
}
@@ -466,18 +466,7 @@ impl<T: 'static> EventLoop<T> {
466466
exit
467467
}
468468

469-
pub fn pump_events<F>(&mut self, event_handler: F) -> PumpStatus
470-
where
471-
F: FnMut(Event<'_, T>, &RootELW<T>, &mut ControlFlow),
472-
{
473-
self.pump_events_with_timeout(Some(Duration::ZERO), event_handler)
474-
}
475-
476-
fn pump_events_with_timeout<F>(
477-
&mut self,
478-
timeout: Option<Duration>,
479-
mut callback: F,
480-
) -> PumpStatus
469+
pub fn pump_events<F>(&mut self, timeout: Option<Duration>, mut callback: F) -> PumpStatus
481470
where
482471
F: FnMut(Event<'_, T>, &RootELW<T>, &mut ControlFlow),
483472
{

src/platform_impl/macos/app_state.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ impl AppState {
632632
Self::stop();
633633
}
634634
HANDLER.update_start_time();
635-
let wait_timeout = HANDLER.wait_timeout(); // configured by pump_events_with_timeout
635+
let wait_timeout = HANDLER.wait_timeout(); // configured by pump_events
636636
let app_timeout = match HANDLER.control_flow() {
637637
ControlFlow::Wait => None,
638638
ControlFlow::Poll | ControlFlow::ExitWithCode(_) => Some(Instant::now()),

src/platform_impl/macos/event_loop.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -291,14 +291,7 @@ impl<T> EventLoop<T> {
291291
}
292292
}
293293

294-
pub fn pump_events<F>(&mut self, callback: F) -> PumpStatus
295-
where
296-
F: FnMut(Event<'_, T>, &RootWindowTarget<T>, &mut ControlFlow),
297-
{
298-
self.pump_events_with_timeout(Some(Duration::ZERO), callback)
299-
}
300-
301-
fn pump_events_with_timeout<F>(&mut self, timeout: Option<Duration>, callback: F) -> PumpStatus
294+
pub fn pump_events<F>(&mut self, timeout: Option<Duration>, callback: F) -> PumpStatus
302295
where
303296
F: FnMut(Event<'_, T>, &RootWindowTarget<T>, &mut ControlFlow),
304297
{

src/platform_impl/windows/event_loop.rs

+1-12
Original file line numberDiff line numberDiff line change
@@ -296,18 +296,7 @@ impl<T: 'static> EventLoop<T> {
296296
}
297297
}
298298

299-
pub fn pump_events<F>(&mut self, event_handler: F) -> PumpStatus
300-
where
301-
F: FnMut(Event<'_, T>, &RootELW<T>, &mut ControlFlow),
302-
{
303-
self.pump_events_with_timeout(Some(Duration::ZERO), event_handler)
304-
}
305-
306-
fn pump_events_with_timeout<F>(
307-
&mut self,
308-
timeout: Option<Duration>,
309-
mut event_handler: F,
310-
) -> PumpStatus
299+
pub fn pump_events<F>(&mut self, timeout: Option<Duration>, mut event_handler: F) -> PumpStatus
311300
where
312301
F: FnMut(Event<'_, T>, &RootELW<T>, &mut ControlFlow),
313302
{

0 commit comments

Comments
 (0)