From ae9b02e09756916aa7ba7ede2cb4edcd6095ba20 Mon Sep 17 00:00:00 2001 From: Robert Bragg Date: Mon, 24 Jul 2023 00:37:51 +0100 Subject: [PATCH] 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. --- examples/window_pump_events.rs | 7 +++++-- src/platform/pump_events.rs | 11 +++++++---- src/platform_impl/android/mod.rs | 15 ++------------- src/platform_impl/linux/mod.rs | 4 ++-- src/platform_impl/linux/wayland/event_loop/mod.rs | 15 ++------------- src/platform_impl/linux/x11/mod.rs | 15 ++------------- src/platform_impl/macos/app_state.rs | 2 +- src/platform_impl/macos/event_loop.rs | 9 +-------- src/platform_impl/windows/event_loop.rs | 13 +------------ 9 files changed, 23 insertions(+), 68 deletions(-) diff --git a/examples/window_pump_events.rs b/examples/window_pump_events.rs index 937b217d93..be887bfa0e 100644 --- a/examples/window_pump_events.rs +++ b/examples/window_pump_events.rs @@ -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, }; @@ -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:?}"); diff --git a/src/platform/pump_events.rs b/src/platform/pump_events.rs index e7ca85edeb..71f49de7e4 100644 --- a/src/platform/pump_events.rs +++ b/src/platform/pump_events.rs @@ -1,3 +1,5 @@ +use std::time::Duration; + use crate::{ event::Event, event_loop::{ControlFlow, EventLoop, EventLoopWindowTarget}, @@ -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:?}"); @@ -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(&mut self, event_handler: F) -> PumpStatus + fn pump_events(&mut self, timeout: Option, event_handler: F) -> PumpStatus where F: FnMut( Event<'_, Self::UserEvent>, @@ -181,7 +184,7 @@ pub trait EventLoopExtPumpEvents { impl EventLoopExtPumpEvents for EventLoop { type UserEvent = T; - fn pump_events(&mut self, event_handler: F) -> PumpStatus + fn pump_events(&mut self, timeout: Option, event_handler: F) -> PumpStatus where F: FnMut( Event<'_, Self::UserEvent>, @@ -189,6 +192,6 @@ impl EventLoopExtPumpEvents for EventLoop { &mut ControlFlow, ), { - self.event_loop.pump_events(event_handler) + self.event_loop.pump_events(timeout, event_handler) } } diff --git a/src/platform_impl/android/mod.rs b/src/platform_impl/android/mod.rs index 3d449bdb25..e9c2506f33 100644 --- a/src/platform_impl/android/mod.rs +++ b/src/platform_impl/android/mod.rs @@ -543,7 +543,7 @@ impl EventLoop { } loop { - match self.pump_events_with_timeout(None, &mut event_handler) { + match self.pump_events(None, &mut event_handler) { PumpStatus::Exit(0) => { break Ok(()); } @@ -557,18 +557,7 @@ impl EventLoop { } } - pub fn pump_events(&mut self, event_handler: F) -> PumpStatus - where - F: FnMut(event::Event<'_, T>, &RootELW, &mut ControlFlow), - { - self.pump_events_with_timeout(Some(Duration::ZERO), event_handler) - } - - fn pump_events_with_timeout( - &mut self, - timeout: Option, - mut callback: F, - ) -> PumpStatus + pub fn pump_events(&mut self, timeout: Option, mut callback: F) -> PumpStatus where F: FnMut(event::Event<'_, T>, &RootELW, &mut ControlFlow), { diff --git a/src/platform_impl/linux/mod.rs b/src/platform_impl/linux/mod.rs index 35cead75e5..4c2a43c7c5 100644 --- a/src/platform_impl/linux/mod.rs +++ b/src/platform_impl/linux/mod.rs @@ -844,11 +844,11 @@ impl EventLoop { x11_or_wayland!(match self; EventLoop(evlp) => evlp.run_ondemand(callback)) } - pub fn pump_events(&mut self, callback: F) -> PumpStatus + pub fn pump_events(&mut self, timeout: Option, callback: F) -> PumpStatus where F: FnMut(crate::event::Event<'_, T>, &RootELW, &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 { diff --git a/src/platform_impl/linux/wayland/event_loop/mod.rs b/src/platform_impl/linux/wayland/event_loop/mod.rs index 2a00afa626..053b0f26a9 100644 --- a/src/platform_impl/linux/wayland/event_loop/mod.rs +++ b/src/platform_impl/linux/wayland/event_loop/mod.rs @@ -154,7 +154,7 @@ impl EventLoop { } 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(()); } @@ -176,18 +176,7 @@ impl EventLoop { exit } - pub fn pump_events(&mut self, event_handler: F) -> PumpStatus - where - F: FnMut(Event<'_, T>, &RootEventLoopWindowTarget, &mut ControlFlow), - { - self.pump_events_with_timeout(Some(Duration::ZERO), event_handler) - } - - fn pump_events_with_timeout( - &mut self, - timeout: Option, - mut callback: F, - ) -> PumpStatus + pub fn pump_events(&mut self, timeout: Option, mut callback: F) -> PumpStatus where F: FnMut(Event<'_, T>, &RootEventLoopWindowTarget, &mut ControlFlow), { diff --git a/src/platform_impl/linux/x11/mod.rs b/src/platform_impl/linux/x11/mod.rs index 897fded395..be7c442f86 100644 --- a/src/platform_impl/linux/x11/mod.rs +++ b/src/platform_impl/linux/x11/mod.rs @@ -441,7 +441,7 @@ impl EventLoop { } 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(()); } @@ -466,18 +466,7 @@ impl EventLoop { exit } - pub fn pump_events(&mut self, event_handler: F) -> PumpStatus - where - F: FnMut(Event<'_, T>, &RootELW, &mut ControlFlow), - { - self.pump_events_with_timeout(Some(Duration::ZERO), event_handler) - } - - fn pump_events_with_timeout( - &mut self, - timeout: Option, - mut callback: F, - ) -> PumpStatus + pub fn pump_events(&mut self, timeout: Option, mut callback: F) -> PumpStatus where F: FnMut(Event<'_, T>, &RootELW, &mut ControlFlow), { diff --git a/src/platform_impl/macos/app_state.rs b/src/platform_impl/macos/app_state.rs index c75bfc6a96..18f727314a 100644 --- a/src/platform_impl/macos/app_state.rs +++ b/src/platform_impl/macos/app_state.rs @@ -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()), diff --git a/src/platform_impl/macos/event_loop.rs b/src/platform_impl/macos/event_loop.rs index 2623b3e45a..176c90e759 100644 --- a/src/platform_impl/macos/event_loop.rs +++ b/src/platform_impl/macos/event_loop.rs @@ -291,14 +291,7 @@ impl EventLoop { } } - pub fn pump_events(&mut self, callback: F) -> PumpStatus - where - F: FnMut(Event<'_, T>, &RootWindowTarget, &mut ControlFlow), - { - self.pump_events_with_timeout(Some(Duration::ZERO), callback) - } - - fn pump_events_with_timeout(&mut self, timeout: Option, callback: F) -> PumpStatus + pub fn pump_events(&mut self, timeout: Option, callback: F) -> PumpStatus where F: FnMut(Event<'_, T>, &RootWindowTarget, &mut ControlFlow), { diff --git a/src/platform_impl/windows/event_loop.rs b/src/platform_impl/windows/event_loop.rs index 09103b818f..2b0de9236f 100644 --- a/src/platform_impl/windows/event_loop.rs +++ b/src/platform_impl/windows/event_loop.rs @@ -296,18 +296,7 @@ impl EventLoop { } } - pub fn pump_events(&mut self, event_handler: F) -> PumpStatus - where - F: FnMut(Event<'_, T>, &RootELW, &mut ControlFlow), - { - self.pump_events_with_timeout(Some(Duration::ZERO), event_handler) - } - - fn pump_events_with_timeout( - &mut self, - timeout: Option, - mut event_handler: F, - ) -> PumpStatus + pub fn pump_events(&mut self, timeout: Option, mut event_handler: F) -> PumpStatus where F: FnMut(Event<'_, T>, &RootELW, &mut ControlFlow), {