From 027011d5a577cfa7a5771785c9104385121803d7 Mon Sep 17 00:00:00 2001 From: Robert Bragg Date: Sun, 18 Jun 2023 12:59:53 +0100 Subject: [PATCH] Remove EventLoopExtRunReturn --- CHANGELOG.md | 1 + examples/window_run_return.rs | 71 ------------------- src/event_loop.rs | 2 +- src/lib.rs | 4 +- src/platform/mod.rs | 11 --- src/platform/run_return.rs | 53 -------------- src/platform_impl/android/mod.rs | 11 --- src/platform_impl/linux/mod.rs | 7 -- .../linux/wayland/event_loop/mod.rs | 11 --- src/platform_impl/linux/x11/mod.rs | 11 --- src/platform_impl/macos/event_loop.rs | 11 --- src/platform_impl/orbital/event_loop.rs | 16 ++--- src/platform_impl/windows/event_loop.rs | 11 --- 13 files changed, 8 insertions(+), 212 deletions(-) delete mode 100644 examples/window_run_return.rs delete mode 100644 src/platform/run_return.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index d8abaaeadcf..74f544076b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ And please only add new entries to the top of this list, right below the `# Unre # Unreleased +- **Breaking** Removed `EventLoopExtRunReturn` (replaced by `EventLoopExtPumpEvents` and `EventLoopExtRunOnDemand`) - On Web, allow event loops to be recreated with `spawn`. - **Breaking:** Rename `Window::set_ime_position` to `Window::set_ime_cursor_area` adding a way to set exclusive zone. - On Android, changed default behavior of Android to ignore volume keys letting the operating system handle them. diff --git a/examples/window_run_return.rs b/examples/window_run_return.rs deleted file mode 100644 index 2a2758d0dee..00000000000 --- a/examples/window_run_return.rs +++ /dev/null @@ -1,71 +0,0 @@ -#![allow(clippy::single_match)] - -// Limit this example to only compatible platforms. -#[cfg(any( - windows_platform, - macos_platform, - x11_platform, - wayland_platform, - android_platform, - orbital_platform, -))] -fn main() { - use std::{thread::sleep, time::Duration}; - - use simple_logger::SimpleLogger; - use winit::{ - event::{Event, WindowEvent}, - event_loop::EventLoop, - platform::run_return::EventLoopExtRunReturn, - window::WindowBuilder, - }; - - #[path = "util/fill.rs"] - mod fill; - - let mut event_loop = EventLoop::new(); - - SimpleLogger::new().init().unwrap(); - let window = WindowBuilder::new() - .with_title("A fantastic window!") - .build(&event_loop) - .unwrap(); - - let mut quit = false; - - while !quit { - event_loop.run_return(|event, _, control_flow| { - control_flow.set_wait(); - - if let Event::WindowEvent { event, .. } = &event { - // Print only Window events to reduce noise - println!("{event:?}"); - } - - match event { - Event::WindowEvent { - event: WindowEvent::CloseRequested, - .. - } => { - quit = true; - } - Event::MainEventsCleared => { - control_flow.set_exit(); - } - Event::RedrawRequested(_) => { - fill::fill_window(&window); - } - _ => (), - } - }); - - // Sleep for 1/60 second to simulate rendering - println!("rendering"); - sleep(Duration::from_millis(16)); - } -} - -#[cfg(any(ios_platform, wasm_platform))] -fn main() { - println!("This platform doesn't support run_return."); -} diff --git a/src/event_loop.rs b/src/event_loop.rs index 9e59b37b910..6c2b7c76b7c 100644 --- a/src/event_loop.rs +++ b/src/event_loop.rs @@ -156,7 +156,7 @@ impl fmt::Debug for EventLoopWindowTarget { /// /// Almost every change is persistent between multiple calls to the event loop closure within a /// given run loop. The only exception to this is [`ExitWithCode`] which, once set, cannot be unset. -/// Changes are **not** persistent between multiple calls to `run_return` - issuing a new call will +/// Changes are **not** persistent between multiple calls to `run_ondemand` - issuing a new call will /// reset the control flow to [`Poll`]. /// /// [`ExitWithCode`]: Self::ExitWithCode diff --git a/src/lib.rs b/src/lib.rs index 9d2a93b82f5..ee08ec8caca 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -37,7 +37,7 @@ //! Winit no longer uses a `EventLoop::poll_events() -> impl Iterator`-based event loop //! model, since that can't be implemented properly on some platforms (e.g web, iOS) and works poorly on //! most other platforms. However, this model can be re-implemented to an extent with -//! [`EventLoopExtRunReturn::run_return`]. See that method's documentation for more reasons about why +//! [`EventLoopExtPumpEvents::pump_events`]. See that method's documentation for more reasons about why //! it's discouraged, beyond compatibility reasons. //! //! @@ -109,7 +109,7 @@ //! window visible only once you're ready to render into it. //! //! [`EventLoop`]: event_loop::EventLoop -//! [`EventLoopExtRunReturn::run_return`]: ./platform/run_return/trait.EventLoopExtRunReturn.html#tymethod.run_return +//! [`EventLoopExtPumpEvents::pump_events`]: ./platform/pump_events/trait.EventLoopExtPumpEvents.html#tymethod.pump_events //! [`EventLoop::new()`]: event_loop::EventLoop::new //! [event_loop_run]: event_loop::EventLoop::run //! [`ControlFlow`]: event_loop::ControlFlow diff --git a/src/platform/mod.rs b/src/platform/mod.rs index eb5f781b046..317ef5280bb 100644 --- a/src/platform/mod.rs +++ b/src/platform/mod.rs @@ -13,7 +13,6 @@ //! //! - `run_ondemand` (available on `windows`, `unix`, `macos`, `android`) //! - `pump_events` (available on `windows`, `unix`, `macos`, `android`) -//! - `run_return` (available on `windows`, `unix`, `macos`, and `android`) //! //! However only the module corresponding to the platform you're compiling to will be available. @@ -52,15 +51,5 @@ pub mod run_ondemand; ))] pub mod pump_events; -#[cfg(any( - windows_platform, - macos_platform, - android_platform, - x11_platform, - wayland_platform, - orbital_platform -))] -pub mod run_return; - pub mod modifier_supplement; pub mod scancode; diff --git a/src/platform/run_return.rs b/src/platform/run_return.rs deleted file mode 100644 index 750b0416184..00000000000 --- a/src/platform/run_return.rs +++ /dev/null @@ -1,53 +0,0 @@ -use crate::{ - event::Event, - event_loop::{ControlFlow, EventLoop, EventLoopWindowTarget}, -}; - -/// Additional methods on [`EventLoop`] to return control flow to the caller. -pub trait EventLoopExtRunReturn { - /// A type provided by the user that can be passed through [`Event::UserEvent`]. - type UserEvent; - - /// Initializes the `winit` event loop. - /// - /// Unlike [`EventLoop::run`], this function accepts non-`'static` (i.e. non-`move`) closures - /// and returns control flow to the caller when `control_flow` is set to [`ControlFlow::Exit`]. - /// - /// # Caveats - /// - /// Despite its appearance at first glance, this is *not* a perfect replacement for - /// `poll_events`. For example, this function will not return on Windows or macOS while a - /// window is getting resized, resulting in all application logic outside of the - /// `event_handler` closure not running until the resize operation ends. Other OS operations - /// may also result in such freezes. This behavior is caused by fundamental limitations in the - /// underlying OS APIs, which cannot be hidden by `winit` without severe stability repercussions. - /// - /// You are strongly encouraged to use `run`, unless the use of this is absolutely necessary. - /// - /// ## Platform-specific - /// - /// - **X11 / Wayland:** This function returns `1` upon disconnection from - /// the display server. - fn run_return(&mut self, event_handler: F) -> i32 - where - F: FnMut( - Event<'_, Self::UserEvent>, - &EventLoopWindowTarget, - &mut ControlFlow, - ); -} - -impl EventLoopExtRunReturn for EventLoop { - type UserEvent = T; - - fn run_return(&mut self, event_handler: F) -> i32 - where - F: FnMut( - Event<'_, Self::UserEvent>, - &EventLoopWindowTarget, - &mut ControlFlow, - ), - { - self.event_loop.run_return(event_handler) - } -} diff --git a/src/platform_impl/android/mod.rs b/src/platform_impl/android/mod.rs index 575af803f11..dc470ff77e0 100644 --- a/src/platform_impl/android/mod.rs +++ b/src/platform_impl/android/mod.rs @@ -539,17 +539,6 @@ impl EventLoop { ::std::process::exit(exit_code); } - pub fn run_return(&mut self, callback: F) -> i32 - where - F: FnMut(event::Event<'_, T>, &RootELW, &mut ControlFlow), - { - match self.run_ondemand(callback) { - Err(RunLoopError::ExitFailure(code)) => code, - Err(_err) => 1, - Ok(_) => 0, - } - } - pub fn run_ondemand(&mut self, mut event_handler: F) -> Result<(), RunLoopError> where F: FnMut(event::Event<'_, T>, &event_loop::EventLoopWindowTarget, &mut ControlFlow), diff --git a/src/platform_impl/linux/mod.rs b/src/platform_impl/linux/mod.rs index 363f70b336c..cfc3d0178cd 100644 --- a/src/platform_impl/linux/mod.rs +++ b/src/platform_impl/linux/mod.rs @@ -820,13 +820,6 @@ impl EventLoop { x11_or_wayland!(match self; EventLoop(evlp) => evlp.create_proxy(); as EventLoopProxy) } - pub fn run_return(&mut self, callback: F) -> i32 - where - F: FnMut(crate::event::Event<'_, T>, &RootELW, &mut ControlFlow), - { - x11_or_wayland!(match self; EventLoop(evlp) => evlp.run_return(callback)) - } - pub fn run(self, callback: F) -> ! where F: 'static + FnMut(crate::event::Event<'_, T>, &RootELW, &mut ControlFlow), diff --git a/src/platform_impl/linux/wayland/event_loop/mod.rs b/src/platform_impl/linux/wayland/event_loop/mod.rs index ede009404cf..0b19817609b 100644 --- a/src/platform_impl/linux/wayland/event_loop/mod.rs +++ b/src/platform_impl/linux/wayland/event_loop/mod.rs @@ -157,17 +157,6 @@ impl EventLoop { ::std::process::exit(exit_code) } - pub fn run_return(&mut self, callback: F) -> i32 - where - F: FnMut(Event<'_, T>, &RootEventLoopWindowTarget, &mut ControlFlow), - { - match self.run_ondemand(callback) { - Err(RunLoopError::ExitFailure(code)) => code, - Err(_err) => 1, - Ok(_) => 0, - } - } - pub fn run_ondemand(&mut self, mut event_handler: F) -> Result<(), RunLoopError> 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 6570dc6f59e..37c72e9c2ce 100644 --- a/src/platform_impl/linux/x11/mod.rs +++ b/src/platform_impl/linux/x11/mod.rs @@ -397,17 +397,6 @@ impl EventLoop { ::std::process::exit(exit_code) } - pub fn run_return(&mut self, callback: F) -> i32 - where - F: FnMut(Event<'_, T>, &RootELW, &mut ControlFlow), - { - match self.run_ondemand(callback) { - Err(RunLoopError::ExitFailure(code)) => code, - Err(_err) => 1, - Ok(_) => 0, - } - } - pub fn run_ondemand(&mut self, mut event_handler: F) -> Result<(), RunLoopError> where F: FnMut(Event<'_, T>, &RootELW, &mut ControlFlow), diff --git a/src/platform_impl/macos/event_loop.rs b/src/platform_impl/macos/event_loop.rs index 6b3fe09af92..7dd42cd576f 100644 --- a/src/platform_impl/macos/event_loop.rs +++ b/src/platform_impl/macos/event_loop.rs @@ -196,17 +196,6 @@ impl EventLoop { process::exit(exit_code); } - pub fn run_return(&mut self, callback: F) -> i32 - where - F: FnMut(Event<'_, T>, &RootWindowTarget, &mut ControlFlow), - { - match self.run_ondemand(callback) { - Err(RunLoopError::ExitFailure(code)) => code, - Err(_err) => 1, - Ok(_) => 0, - } - } - // NB: we don't base this on `pump_events` because for `MacOs` we can't support // `pump_events` elegantly (we just ask to run the loop for a "short" amount of // time and so a layered implementation would end up using a lot of CPU due to diff --git a/src/platform_impl/orbital/event_loop.rs b/src/platform_impl/orbital/event_loop.rs index 16cf6f41374..0b23e5d5bb3 100644 --- a/src/platform_impl/orbital/event_loop.rs +++ b/src/platform_impl/orbital/event_loop.rs @@ -301,15 +301,6 @@ impl EventLoop { } } - pub fn run(mut self, event_handler: F) -> ! - where - F: 'static - + FnMut(event::Event<'_, T>, &event_loop::EventLoopWindowTarget, &mut ControlFlow), - { - let exit_code = self.run_return(event_handler); - ::std::process::exit(exit_code); - } - fn process_event( window_id: WindowId, event_option: EventOption, @@ -451,9 +442,10 @@ impl EventLoop { } } - pub fn run_return(&mut self, mut event_handler_inner: F) -> i32 + pub fn run(mut self, event_handler: F) -> ! where - F: FnMut(event::Event<'_, T>, &event_loop::EventLoopWindowTarget, &mut ControlFlow), + F: 'static + + FnMut(event::Event<'_, T>, &event_loop::EventLoopWindowTarget, &mut ControlFlow), { // Wrapper for event handler function that prevents ExitWithCode from being unset. let mut event_handler = @@ -696,7 +688,7 @@ impl EventLoop { &mut control_flow, ); - code + ::std::process::exit(code); } pub fn window_target(&self) -> &event_loop::EventLoopWindowTarget { diff --git a/src/platform_impl/windows/event_loop.rs b/src/platform_impl/windows/event_loop.rs index b582aed17b3..b837555c73d 100644 --- a/src/platform_impl/windows/event_loop.rs +++ b/src/platform_impl/windows/event_loop.rs @@ -255,17 +255,6 @@ impl EventLoop { ::std::process::exit(exit_code); } - pub fn run_return(&mut self, event_handler: F) -> i32 - where - F: FnMut(Event<'_, T>, &RootELW, &mut ControlFlow), - { - match self.run_ondemand(event_handler) { - Err(RunLoopError::ExitFailure(code)) => code, - Err(_err) => 1, - Ok(_) => 0, - } - } - pub fn run_ondemand(&mut self, mut event_handler: F) -> Result<(), RunLoopError> where F: FnMut(Event<'_, T>, &RootELW, &mut ControlFlow),