From 8862ce016307e48c2fcf1a9b10e810a9bad61f9f Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Wed, 30 Aug 2023 10:54:46 +0200 Subject: [PATCH] Add EventLoop::builder, which replaces EventLoopBuilder::new Similarly for EventLoop::with_user_event and EventLoopBuilder::with_user_event --- CHANGELOG.md | 3 +++ examples/custom_events.rs | 6 ++---- src/event_loop.rs | 45 ++++++++++++++++++++++----------------- 3 files changed, 31 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d986cf3..1f63cd2b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -111,7 +111,10 @@ Unreleased` header. - **Breaking:** Add `Event::MemoryWarning`; implemented on iOS/Android. - **Breaking:** Bump `ndk` version to `0.8.0`, ndk-sys to `0.5.0`, `android-activity` to `0.5.0`. - **Breaking:** Change default `ControlFlow` from `Poll` to `Wait`. +- Added `EventLoop::builder`, which is intended to replace the (now deprecated) `EventLoopBuilder::new`. - Added `Window::builder`, which is intended to replace the (now deprecated) `WindowBuilder::new`. +- **Breaking:** Changed the signature of `EventLoop::with_user_event` to return a builder. +- **Breaking:** Removed `EventLoopBuilder::with_user_event`, the functionality is now available in `EventLoop::with_user_event`. - Make iOS `MonitorHandle` and `VideoMode` usable from other threads. - Fix window size sometimes being invalid when resizing on macOS. - On Web, `ControlFlow::Poll` and `ControlFlow::WaitUntil` are now using the Prioritized Task Scheduling API. `setTimeout()` with a trick to circumvent throttling to 4ms is used as a fallback. diff --git a/examples/custom_events.rs b/examples/custom_events.rs index d6d426ef..90e8e41d 100644 --- a/examples/custom_events.rs +++ b/examples/custom_events.rs @@ -5,7 +5,7 @@ fn main() -> Result<(), impl std::error::Error> { use simple_logger::SimpleLogger; use winit::{ event::{Event, WindowEvent}, - event_loop::EventLoopBuilder, + event_loop::EventLoop, window::Window, }; @@ -18,9 +18,7 @@ fn main() -> Result<(), impl std::error::Error> { } SimpleLogger::new().init().unwrap(); - let event_loop = EventLoopBuilder::::with_user_event() - .build() - .unwrap(); + let event_loop = EventLoop::::with_user_event().build().unwrap(); let window = Window::builder() .with_title("A fantastic window!") diff --git a/src/event_loop.rs b/src/event_loop.rs index 93392efc..7f16dbba 100644 --- a/src/event_loop.rs +++ b/src/event_loop.rs @@ -57,33 +57,26 @@ pub struct EventLoopWindowTarget { /// /// This is used to make specifying options that affect the whole application /// easier. But note that constructing multiple event loops is not supported. +/// +/// This can be created using [`EventLoop::new`] or [`EventLoop::with_user_event`]. #[derive(Default)] pub struct EventLoopBuilder { pub(crate) platform_specific: platform_impl::PlatformSpecificEventLoopAttributes, _p: PhantomData, } +static EVENT_LOOP_CREATED: AtomicBool = AtomicBool::new(false); + impl EventLoopBuilder<()> { /// Start building a new event loop. #[inline] + #[deprecated = "use `EventLoop::builder` instead"] pub fn new() -> Self { - Self::with_user_event() + EventLoop::builder() } } -static EVENT_LOOP_CREATED: AtomicBool = AtomicBool::new(false); - impl EventLoopBuilder { - /// Start building a new event loop, with the given type as the user event - /// type. - #[inline] - pub fn with_user_event() -> Self { - Self { - platform_specific: Default::default(), - _p: PhantomData, - } - } - /// Builds a new event loop. /// /// ***For cross-platform compatibility, the [`EventLoop`] must be created on the main thread, @@ -193,19 +186,33 @@ impl ControlFlow { } impl EventLoop<()> { - /// Alias for [`EventLoopBuilder::new().build()`]. + /// Create the event loop. /// - /// [`EventLoopBuilder::new().build()`]: EventLoopBuilder::build + /// This is an alias of `EventLoop::builder().build()`. #[inline] pub fn new() -> Result, EventLoopError> { - EventLoopBuilder::new().build() + Self::builder().build() + } + + /// Start building a new event loop. + /// + /// This returns an [`EventLoopBuilder`], to allow configuring the event loop before creation. + /// + /// To get the actual event loop, call [`build`][EventLoopBuilder::build] on that. + #[inline] + pub fn builder() -> EventLoopBuilder<()> { + Self::with_user_event() } } impl EventLoop { - #[deprecated = "Use `EventLoopBuilder::::with_user_event().build()` instead."] - pub fn with_user_event() -> Result, EventLoopError> { - EventLoopBuilder::::with_user_event().build() + /// Start building a new event loop, with the given type as the user event + /// type. + pub fn with_user_event() -> EventLoopBuilder { + EventLoopBuilder { + platform_specific: Default::default(), + _p: PhantomData, + } } /// Runs the event loop in the calling thread and calls the given `event_handler` closure