From 8de68e14f393fad02d32b6f0a93f8216d94ac2c8 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Sun, 24 Dec 2023 03:53:20 +0100 Subject: [PATCH] Replace EventLoopWindowTarget with ActiveEventLoop --- examples/child_window.rs | 4 +- examples/custom_cursors.rs | 7 +- src/cursor.rs | 6 +- src/event_loop.rs | 276 ++++++++++++------ src/lib.rs | 2 +- src/monitor.rs | 3 +- src/platform/android.rs | 7 +- src/platform/macos.rs | 16 +- src/platform/pump_events.rs | 12 +- src/platform/run_on_demand.rs | 15 +- src/platform/startup_notify.rs | 25 +- src/platform/wayland.rs | 25 +- src/platform/web.rs | 20 +- src/platform/x11.rs | 25 +- src/platform_impl/android/mod.rs | 39 ++- src/platform_impl/ios/event_loop.rs | 23 +- src/platform_impl/ios/mod.rs | 3 +- src/platform_impl/linux/mod.rs | 15 +- .../linux/wayland/event_loop/mod.rs | 34 +-- .../linux/x11/event_processor.rs | 4 +- src/platform_impl/linux/x11/mod.rs | 33 +-- src/platform_impl/macos/app_state.rs | 15 +- src/platform_impl/macos/event_loop.rs | 28 +- src/platform_impl/macos/mod.rs | 3 +- src/platform_impl/orbital/event_loop.rs | 51 ++-- src/platform_impl/orbital/mod.rs | 2 +- src/platform_impl/web/event_loop/mod.rs | 29 +- src/platform_impl/web/event_loop/runner.rs | 2 +- .../web/event_loop/window_target.rs | 2 +- src/platform_impl/web/mod.rs | 3 +- src/platform_impl/windows/event_loop.rs | 42 +-- src/platform_impl/windows/mod.rs | 3 +- src/window.rs | 16 +- 33 files changed, 448 insertions(+), 342 deletions(-) diff --git a/examples/child_window.rs b/examples/child_window.rs index 2693dbdc08..61f6539a68 100644 --- a/examples/child_window.rs +++ b/examples/child_window.rs @@ -16,14 +16,14 @@ fn main() -> Result<(), impl std::error::Error> { use winit::{ dpi::{LogicalPosition, LogicalSize, Position}, event::{ElementState, Event, KeyEvent, WindowEvent}, - event_loop::{EventLoop, EventLoopWindowTarget}, + event_loop::{ActiveEventLoop, EventLoop}, raw_window_handle::HasRawWindowHandle, window::{Window, WindowBuilder, WindowId}, }; fn spawn_child_window( parent: &Window, - event_loop: &EventLoopWindowTarget, + event_loop: ActiveEventLoop<'_>, windows: &mut HashMap, ) { let parent = parent.raw_window_handle().unwrap(); diff --git a/examples/custom_cursors.rs b/examples/custom_cursors.rs index 4ffac80ae2..5a77979496 100644 --- a/examples/custom_cursors.rs +++ b/examples/custom_cursors.rs @@ -4,19 +4,18 @@ use simple_logger::SimpleLogger; use winit::{ event::{ElementState, Event, KeyEvent, WindowEvent}, - event_loop::{EventLoop, EventLoopWindowTarget}, + event_loop::EventLoop, keyboard::Key, window::{CustomCursor, WindowBuilder}, }; -fn decode_cursor(bytes: &[u8], window_target: &EventLoopWindowTarget) -> CustomCursor { +fn decode_cursor(bytes: &[u8], event_loop: &EventLoop<()>) -> CustomCursor { let img = image::load_from_memory(bytes).unwrap().to_rgba8(); let samples = img.into_flat_samples(); let (_, w, h) = samples.extents(); let (w, h) = (w as u16, h as u16); let builder = CustomCursor::from_rgba(samples.samples, w, h, w / 2, h / 2).unwrap(); - - builder.build(window_target) + builder.build(event_loop) } #[cfg(not(wasm_platform))] diff --git a/src/cursor.rs b/src/cursor.rs index a821486485..1c27d83df6 100644 --- a/src/cursor.rs +++ b/src/cursor.rs @@ -3,7 +3,7 @@ use std::hash::Hasher; use std::sync::Arc; use std::{error::Error, hash::Hash}; -use crate::event_loop::EventLoopWindowTarget; +use crate::event_loop::MaybeActiveEventLoop; use crate::platform_impl::{self, PlatformCustomCursor, PlatformCustomCursorBuilder}; /// The maximum width and height for a cursor when using [`CustomCursor::from_rgba`]. @@ -87,9 +87,9 @@ pub struct CustomCursorBuilder { } impl CustomCursorBuilder { - pub fn build(self, window_target: &EventLoopWindowTarget) -> CustomCursor { + pub fn build<'a>(self, event_loop: impl MaybeActiveEventLoop<'a>) -> CustomCursor { CustomCursor { - inner: PlatformCustomCursor::build(self.inner, &window_target.p), + inner: PlatformCustomCursor::build(self.inner, event_loop.__inner()), } } } diff --git a/src/event_loop.rs b/src/event_loop.rs index 15bf05c84b..386f0ddf4d 100644 --- a/src/event_loop.rs +++ b/src/event_loop.rs @@ -8,7 +8,6 @@ //! See the root-level documentation for information on how to create and use an event loop to //! handle events. use std::marker::PhantomData; -use std::ops::Deref; #[cfg(any(x11_platform, wayland_platform))] use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, RawFd}; use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; @@ -37,19 +36,90 @@ use crate::{event::Event, monitor::MonitorHandle, platform_impl}; /// [`EventLoopProxy`] allows you to wake up an `EventLoop` from another thread. /// /// [`Window`]: crate::window::Window +// TODO: Don't allow window creation from this, since that is broken on macOS/iOS. pub struct EventLoop { pub(crate) event_loop: platform_impl::EventLoop, pub(crate) _marker: PhantomData<*mut ()>, // Not Send nor Sync } -/// Target that associates windows with an [`EventLoop`]. +/// An active event loop. /// -/// This type exists to allow you to create new windows while Winit executes -/// your callback. [`EventLoop`] will coerce into this type (`impl Deref for -/// EventLoop`), so functions that take this as a parameter can also take -/// `&EventLoop`. -pub struct EventLoopWindowTarget { - pub(crate) p: platform_impl::EventLoopWindowTarget, +/// This type exists to differentiate between functionality available when Winit +/// is executing your callback, and outside of it. +#[derive(Copy, Clone)] +pub struct ActiveEventLoop<'a> { + pub(crate) inner: platform_impl::ActiveEventLoop<'a>, +} + +impl fmt::Debug for ActiveEventLoop<'_> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.pad("ActiveEventLoop { .. }") + } +} + +impl ActiveEventLoop<'_> { + /// Sets the [`ControlFlow`]. + pub fn set_control_flow(&self, control_flow: ControlFlow) { + self.inner.set_control_flow(control_flow) + } + + /// Gets the current [`ControlFlow`]. + pub fn control_flow(&self) -> ControlFlow { + self.inner.control_flow() + } + + /// This exits the event loop. + /// + /// See [`LoopExiting`](Event::LoopExiting). + pub fn exit(&self) { + self.inner.exit() + } + + /// Returns if the [`EventLoop`] is about to stop. + /// + /// See [`exit()`](Self::exit). + pub fn exiting(&self) -> bool { + self.inner.exiting() + } + + /// Returns the primary monitor of the system. + /// + /// Returns `None` if it can't identify any monitor as a primary one. + /// + /// ## Platform-specific + /// + /// **Wayland / Web:** Always returns `None`. + #[inline] + pub fn primary_monitor(&self) -> Option { + self.inner + .primary_monitor() + .map(|inner| MonitorHandle { inner }) + } + + /// Returns the list of all the monitors available on the system. + #[inline] + pub fn available_monitors(&self) -> impl Iterator { + #[allow(clippy::useless_conversion)] // false positive on some platforms + self.inner + .available_monitors() + .into_iter() + .map(|inner| MonitorHandle { inner }) + } + + /// Change if or when [`DeviceEvent`]s are captured. + /// + /// Since the [`DeviceEvent`] capture can lead to high CPU usage for unfocused windows, winit + /// will ignore them by default for unfocused windows on Linux/BSD. This method allows changing + /// this at runtime to explicitly capture them again. + /// + /// ## Platform-specific + /// + /// - **Wayland / macOS / iOS / Android / Orbital:** Unsupported. + /// + /// [`DeviceEvent`]: crate::event::DeviceEvent + pub fn listen_device_events(&self, allowed: DeviceEvents) { + self.inner.listen_device_events(allowed); + } } /// Object that allows building the event loop. @@ -141,13 +211,7 @@ impl fmt::Debug for EventLoop { } } -impl fmt::Debug for EventLoopWindowTarget { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.pad("EventLoopWindowTarget { .. }") - } -} - -/// Set through [`EventLoopWindowTarget::set_control_flow()`]. +/// Set through [`ActiveEventLoop::set_control_flow()`]. /// /// Indicates the desired behavior of the event loop after [`Event::AboutToWait`] is emitted. /// @@ -236,16 +300,67 @@ impl EventLoop { /// /// This function won't be available with `target_feature = "exception-handling"`. /// - /// [`set_control_flow()`]: EventLoopWindowTarget::set_control_flow() + /// [`set_control_flow()`]: ActiveEventLoop::set_control_flow() /// [`run()`]: Self::run() /// [^1]: `EventLoopExtWebSys::spawn()` is only available on WASM. #[inline] #[cfg(not(all(wasm_platform, target_feature = "exception-handling")))] - pub fn run(self, event_handler: F) -> Result<(), EventLoopError> + pub fn run(self, mut event_handler: F) -> Result<(), EventLoopError> where - F: FnMut(Event, &EventLoopWindowTarget), + F: FnMut(Event, ActiveEventLoop<'_>), { - self.event_loop.run(event_handler) + self.event_loop + .run(move |event, inner| event_handler(event, ActiveEventLoop { inner })) + } + + /// Set the initial [`ControlFlow`]. + pub fn set_control_flow(&self, control_flow: ControlFlow) { + self.event_loop + .window_target() + .set_control_flow(control_flow) + } + + /// Returns the primary monitor of the system. + /// + /// Returns `None` if it can't identify any monitor as a primary one. + /// + /// ## Platform-specific + /// + /// **Wayland / Web:** Always returns `None`. + #[inline] + pub fn primary_monitor(&self) -> Option { + self.event_loop + .window_target() + .primary_monitor() + .map(|inner| MonitorHandle { inner }) + } + + /// Returns the list of all the monitors available on the system. + #[inline] + pub fn available_monitors(&self) -> impl Iterator { + #[allow(clippy::useless_conversion)] // false positive on some platforms + self.event_loop + .window_target() + .available_monitors() + .into_iter() + .map(|inner| MonitorHandle { inner }) + } + + /// Change if or when [`DeviceEvent`]s are captured. + /// + /// Since the [`DeviceEvent`] capture can lead to high CPU usage for unfocused windows, winit + /// will ignore them by default for unfocused windows on Linux/BSD. This method allows changing + /// this at runtime to explicitly capture them again. + /// + /// ## Platform-specific + /// + /// - **Wayland / macOS / iOS / Android / Orbital:** Unsupported. + /// + /// [`DeviceEvent`]: crate::event::DeviceEvent + pub fn listen_device_events(&self, allowed: DeviceEvents) { + self.event_loop + .window_target() + .listen_device_events(allowed); } /// Creates an [`EventLoopProxy`] that can be used to dispatch user events to the main event loop. @@ -259,7 +374,12 @@ impl EventLoop { #[cfg(feature = "rwh_06")] impl rwh_06::HasDisplayHandle for EventLoop { fn display_handle(&self) -> Result, rwh_06::HandleError> { - rwh_06::HasDisplayHandle::display_handle(&**self) + let raw = self + .event_loop + .window_target() + .raw_display_handle_rwh_06()?; + // SAFETY: The display will never be deallocated while the event loop is alive. + Ok(unsafe { rwh_06::DisplayHandle::borrow_raw(raw) }) } } @@ -267,7 +387,7 @@ impl rwh_06::HasDisplayHandle for EventLoop { unsafe impl rwh_05::HasRawDisplayHandle for EventLoop { /// Returns a [`rwh_05::RawDisplayHandle`] for the event loop. fn raw_display_handle(&self) -> rwh_05::RawDisplayHandle { - rwh_05::HasRawDisplayHandle::raw_display_handle(&**self) + self.event_loop.window_target().raw_display_handle_rwh_05() } } @@ -299,92 +419,44 @@ impl AsRawFd for EventLoop { } } -impl Deref for EventLoop { - type Target = EventLoopWindowTarget; - fn deref(&self) -> &EventLoopWindowTarget { - self.event_loop.window_target() +#[cfg(feature = "rwh_06")] +impl rwh_06::HasDisplayHandle for ActiveEventLoop<'_> { + fn display_handle(&self) -> Result, rwh_06::HandleError> { + let raw = self.inner.raw_display_handle_rwh_06()?; + // SAFETY: The display will never be deallocated while the event loop is alive. + Ok(unsafe { rwh_06::DisplayHandle::borrow_raw(raw) }) } } -impl EventLoopWindowTarget { - /// Returns the list of all the monitors available on the system. - #[inline] - pub fn available_monitors(&self) -> impl Iterator { - #[allow(clippy::useless_conversion)] // false positive on some platforms - self.p - .available_monitors() - .into_iter() - .map(|inner| MonitorHandle { inner }) - } - - /// Returns the primary monitor of the system. - /// - /// Returns `None` if it can't identify any monitor as a primary one. - /// - /// ## Platform-specific - /// - /// **Wayland / Web:** Always returns `None`. - #[inline] - pub fn primary_monitor(&self) -> Option { - self.p - .primary_monitor() - .map(|inner| MonitorHandle { inner }) - } - - /// Change if or when [`DeviceEvent`]s are captured. - /// - /// Since the [`DeviceEvent`] capture can lead to high CPU usage for unfocused windows, winit - /// will ignore them by default for unfocused windows on Linux/BSD. This method allows changing - /// this at runtime to explicitly capture them again. - /// - /// ## Platform-specific - /// - /// - **Wayland / macOS / iOS / Android / Orbital:** Unsupported. - /// - /// [`DeviceEvent`]: crate::event::DeviceEvent - pub fn listen_device_events(&self, allowed: DeviceEvents) { - self.p.listen_device_events(allowed); - } - - /// Sets the [`ControlFlow`]. - pub fn set_control_flow(&self, control_flow: ControlFlow) { - self.p.set_control_flow(control_flow) - } - - /// Gets the current [`ControlFlow`]. - pub fn control_flow(&self) -> ControlFlow { - self.p.control_flow() +#[cfg(feature = "rwh_05")] +unsafe impl rwh_05::HasRawDisplayHandle for ActiveEventLoop<'_> { + /// Returns a [`rwh_05::RawDisplayHandle`] for the event loop. + fn raw_display_handle(&self) -> rwh_05::RawDisplayHandle { + self.inner.raw_display_handle_rwh_05() } +} - /// This exits the event loop. - /// - /// See [`LoopExiting`](Event::LoopExiting). - pub fn exit(&self) { - self.p.exit() - } +mod private { + pub trait Sealed {} +} - /// Returns if the [`EventLoop`] is about to stop. - /// - /// See [`exit()`](Self::exit). - pub fn exiting(&self) -> bool { - self.p.exiting() - } +/// Trait to allow functions to be generic over [`EventLoop`] and [`ActiveEventLoop`]. +pub trait MaybeActiveEventLoop<'a>: private::Sealed { + #[doc(hidden)] + fn __inner(self) -> &'a platform_impl::EventLoopWindowTarget; } -#[cfg(feature = "rwh_06")] -impl rwh_06::HasDisplayHandle for EventLoopWindowTarget { - fn display_handle(&self) -> Result, rwh_06::HandleError> { - let raw = self.p.raw_display_handle_rwh_06()?; - // SAFETY: The display will never be deallocated while the event loop is alive. - Ok(unsafe { rwh_06::DisplayHandle::borrow_raw(raw) }) +impl private::Sealed for &EventLoop {} +impl<'a, T: 'static> MaybeActiveEventLoop<'a> for &'a EventLoop { + fn __inner(self) -> &'a platform_impl::EventLoopWindowTarget { + self.event_loop.window_target() } } -#[cfg(feature = "rwh_05")] -unsafe impl rwh_05::HasRawDisplayHandle for EventLoopWindowTarget { - /// Returns a [`rwh_05::RawDisplayHandle`] for the event loop. - fn raw_display_handle(&self) -> rwh_05::RawDisplayHandle { - self.p.raw_display_handle_rwh_05() +impl private::Sealed for ActiveEventLoop<'_> {} +impl<'a> MaybeActiveEventLoop<'a> for ActiveEventLoop<'a> { + fn __inner(self) -> &'a platform_impl::EventLoopWindowTarget { + self.inner } } @@ -472,3 +544,15 @@ impl AsyncRequestSerial { Self { serial } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[allow(unused)] + fn assert_active_event_loop_covariance<'b>( + event_loop: ActiveEventLoop<'static>, + ) -> ActiveEventLoop<'b> { + event_loop + } +} diff --git a/src/lib.rs b/src/lib.rs index ce26ef0e9d..60450ced0d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -132,7 +132,7 @@ //! [`EventLoop`]: event_loop::EventLoop //! [`EventLoop::new()`]: event_loop::EventLoop::new //! [`EventLoop::run()`]: event_loop::EventLoop::run -//! [`exit()`]: event_loop::EventLoopWindowTarget::exit +//! [`exit()`]: event_loop::ActiveEventLoop::exit //! [`Window`]: window::Window //! [`WindowId`]: window::WindowId //! [`WindowBuilder`]: window::WindowBuilder diff --git a/src/monitor.rs b/src/monitor.rs index 45c355e529..d7421b50b9 100644 --- a/src/monitor.rs +++ b/src/monitor.rs @@ -3,7 +3,8 @@ //! If you want to get basic information about a monitor, you can use the //! [`MonitorHandle`] type. This is retrieved from one of the following //! methods, which return an iterator of [`MonitorHandle`]: -//! - [`EventLoopWindowTarget::available_monitors`](crate::event_loop::EventLoopWindowTarget::available_monitors). +//! - [`EventLoop::available_monitors`](crate::event_loop::EventLoop::available_monitors). +//! - [`ActiveEventLoop::available_monitors`](crate::event_loop::ActiveEventLoop::available_monitors). //! - [`Window::available_monitors`](crate::window::Window::available_monitors). use crate::{ dpi::{PhysicalPosition, PhysicalSize}, diff --git a/src/platform/android.rs b/src/platform/android.rs index c80fbeac29..a00a60fe94 100644 --- a/src/platform/android.rs +++ b/src/platform/android.rs @@ -1,5 +1,5 @@ use crate::{ - event_loop::{EventLoop, EventLoopBuilder, EventLoopWindowTarget}, + event_loop::{EventLoop, EventLoopBuilder}, window::{Window, WindowBuilder}, }; @@ -10,9 +10,6 @@ pub trait EventLoopExtAndroid {} impl EventLoopExtAndroid for EventLoop {} -/// Additional methods on [`EventLoopWindowTarget`] that are specific to Android. -pub trait EventLoopWindowTargetExtAndroid {} - /// Additional methods on [`Window`] that are specific to Android. pub trait WindowExtAndroid { fn content_rect(&self) -> Rect; @@ -30,8 +27,6 @@ impl WindowExtAndroid for Window { } } -impl EventLoopWindowTargetExtAndroid for EventLoopWindowTarget {} - /// Additional methods on [`WindowBuilder`] that are specific to Android. pub trait WindowBuilderExtAndroid {} diff --git a/src/platform/macos.rs b/src/platform/macos.rs index ac7b0a6fb4..b1ea1ff1db 100644 --- a/src/platform/macos.rs +++ b/src/platform/macos.rs @@ -4,7 +4,7 @@ use icrate::Foundation::MainThreadMarker; use objc2::rc::Id; use crate::{ - event_loop::{EventLoopBuilder, EventLoopWindowTarget}, + event_loop::{ActiveEventLoop, EventLoopBuilder}, monitor::MonitorHandle, window::{Window, WindowBuilder}, }; @@ -372,8 +372,8 @@ impl MonitorHandleExtMacOS for MonitorHandle { } } -/// Additional methods on [`EventLoopWindowTarget`] that are specific to macOS. -pub trait EventLoopWindowTargetExtMacOS { +/// Additional methods on [`ActiveEventLoop`] that are specific to macOS. +pub trait ActiveEventLoopExtMacOS { /// Hide the entire application. In most applications this is typically triggered with Command-H. fn hide_application(&self); /// Hide the other applications. In most applications this is typically triggered with Command+Option-H. @@ -386,21 +386,21 @@ pub trait EventLoopWindowTargetExtMacOS { fn allows_automatic_window_tabbing(&self) -> bool; } -impl EventLoopWindowTargetExtMacOS for EventLoopWindowTarget { +impl ActiveEventLoopExtMacOS for ActiveEventLoop<'_> { fn hide_application(&self) { - self.p.hide_application() + self.inner.hide_application() } fn hide_other_applications(&self) { - self.p.hide_other_applications() + self.inner.hide_other_applications() } fn set_allows_automatic_window_tabbing(&self, enabled: bool) { - self.p.set_allows_automatic_window_tabbing(enabled); + self.inner.set_allows_automatic_window_tabbing(enabled); } fn allows_automatic_window_tabbing(&self) -> bool { - self.p.allows_automatic_window_tabbing() + self.inner.allows_automatic_window_tabbing() } } diff --git a/src/platform/pump_events.rs b/src/platform/pump_events.rs index 403c52d978..bbcfa82835 100644 --- a/src/platform/pump_events.rs +++ b/src/platform/pump_events.rs @@ -2,7 +2,7 @@ use std::time::Duration; use crate::{ event::Event, - event_loop::{EventLoop, EventLoopWindowTarget}, + event_loop::{ActiveEventLoop, EventLoop}, }; /// The return status for `pump_events` @@ -174,16 +174,18 @@ pub trait EventLoopExtPumpEvents { /// callback. fn pump_events(&mut self, timeout: Option, event_handler: F) -> PumpStatus where - F: FnMut(Event, &EventLoopWindowTarget); + F: FnMut(Event, ActiveEventLoop<'_>); } impl EventLoopExtPumpEvents for EventLoop { type UserEvent = T; - fn pump_events(&mut self, timeout: Option, event_handler: F) -> PumpStatus + fn pump_events(&mut self, timeout: Option, mut event_handler: F) -> PumpStatus where - F: FnMut(Event, &EventLoopWindowTarget), + F: FnMut(Event, ActiveEventLoop<'_>), { - self.event_loop.pump_events(timeout, event_handler) + self.event_loop.pump_events(timeout, move |event, inner| { + event_handler(event, ActiveEventLoop { inner }) + }) } } diff --git a/src/platform/run_on_demand.rs b/src/platform/run_on_demand.rs index 03e49a961a..89c9923618 100644 --- a/src/platform/run_on_demand.rs +++ b/src/platform/run_on_demand.rs @@ -1,7 +1,7 @@ use crate::{ error::EventLoopError, event::Event, - event_loop::{EventLoop, EventLoopWindowTarget}, + event_loop::{ActiveEventLoop, EventLoop}, }; #[cfg(doc)] @@ -62,20 +62,21 @@ pub trait EventLoopExtRunOnDemand { doc = "[^1]: `spawn()` is only available on `wasm` platforms." )] /// - /// [`exit()`]: EventLoopWindowTarget::exit() - /// [`set_control_flow()`]: EventLoopWindowTarget::set_control_flow() + /// [`exit()`]: ActiveEventLoop::exit() + /// [`set_control_flow()`]: ActiveEventLoop::set_control_flow() fn run_on_demand(&mut self, event_handler: F) -> Result<(), EventLoopError> where - F: FnMut(Event, &EventLoopWindowTarget); + F: FnMut(Event, ActiveEventLoop<'_>); } impl EventLoopExtRunOnDemand for EventLoop { type UserEvent = T; - fn run_on_demand(&mut self, event_handler: F) -> Result<(), EventLoopError> + fn run_on_demand(&mut self, mut event_handler: F) -> Result<(), EventLoopError> where - F: FnMut(Event, &EventLoopWindowTarget), + F: FnMut(Event, ActiveEventLoop<'_>), { - self.event_loop.run_on_demand(event_handler) + self.event_loop + .run_on_demand(move |event, inner| event_handler(event, ActiveEventLoop { inner })) } } diff --git a/src/platform/startup_notify.rs b/src/platform/startup_notify.rs index 207aff86cb..df6aa3317f 100644 --- a/src/platform/startup_notify.rs +++ b/src/platform/startup_notify.rs @@ -24,7 +24,7 @@ use std::env; use crate::error::NotSupportedError; -use crate::event_loop::{AsyncRequestSerial, EventLoopWindowTarget}; +use crate::event_loop::{ActiveEventLoop, AsyncRequestSerial, EventLoop}; use crate::window::{ActivationToken, Window, WindowBuilder}; /// The variable which is used mostly on X11. @@ -55,13 +55,24 @@ pub trait WindowBuilderExtStartupNotify { fn with_activation_token(self, token: ActivationToken) -> Self; } -impl EventLoopExtStartupNotify for EventLoopWindowTarget { +impl EventLoopExtStartupNotify for EventLoop { fn read_token_from_env(&self) -> Option { - match self.p { - #[cfg(wayland_platform)] - crate::platform_impl::EventLoopWindowTarget::Wayland(_) => env::var(WAYLAND_VAR), - #[cfg(x11_platform)] - crate::platform_impl::EventLoopWindowTarget::X(_) => env::var(X11_VAR), + if self.event_loop.window_target().is_wayland() { + env::var(WAYLAND_VAR) + } else { + env::var(X11_VAR) + } + .ok() + .map(ActivationToken::_new) + } +} + +impl EventLoopExtStartupNotify for ActiveEventLoop<'_> { + fn read_token_from_env(&self) -> Option { + if self.inner.is_wayland() { + env::var(WAYLAND_VAR) + } else { + env::var(X11_VAR) } .ok() .map(ActivationToken::_new) diff --git a/src/platform/wayland.rs b/src/platform/wayland.rs index 99ae6b295a..32b98c295b 100644 --- a/src/platform/wayland.rs +++ b/src/platform/wayland.rs @@ -1,5 +1,5 @@ use crate::{ - event_loop::{EventLoopBuilder, EventLoopWindowTarget}, + event_loop::{ActiveEventLoop, EventLoop, EventLoopBuilder}, monitor::MonitorHandle, window::{Window, WindowBuilder}, }; @@ -8,16 +8,29 @@ use crate::platform_impl::{ApplicationName, Backend}; pub use crate::window::Theme; -/// Additional methods on [`EventLoopWindowTarget`] that are specific to Wayland. -pub trait EventLoopWindowTargetExtWayland { - /// True if the [`EventLoopWindowTarget`] uses Wayland. +/// Additional methods on [`EventLoop`] that are specific to Wayland. +pub trait EventLoopExtWayland { + /// True if the [`EventLoop`] uses Wayland. fn is_wayland(&self) -> bool; } -impl EventLoopWindowTargetExtWayland for EventLoopWindowTarget { +impl EventLoopExtWayland for EventLoop { #[inline] fn is_wayland(&self) -> bool { - self.p.is_wayland() + self.event_loop.window_target().is_wayland() + } +} + +/// Additional methods on [`ActiveEventLoop`] that are specific to Wayland. +pub trait ActiveEventLoopExtWayland { + /// True if the [`ActiveEventLoop`] uses Wayland. + fn is_wayland(&self) -> bool; +} + +impl ActiveEventLoopExtWayland for ActiveEventLoop<'_> { + #[inline] + fn is_wayland(&self) -> bool { + self.inner.is_wayland() } } diff --git a/src/platform/web.rs b/src/platform/web.rs index f0f8d2e9ac..4a067c1391 100644 --- a/src/platform/web.rs +++ b/src/platform/web.rs @@ -29,8 +29,7 @@ use crate::cursor::CustomCursorBuilder; use crate::event::Event; -use crate::event_loop::EventLoop; -use crate::event_loop::EventLoopWindowTarget; +use crate::event_loop::{ActiveEventLoop, EventLoop}; use crate::platform_impl::PlatformCustomCursorBuilder; use crate::window::CustomCursor; use crate::window::{Window, WindowBuilder}; @@ -136,21 +135,22 @@ pub trait EventLoopExtWebSys { /// [^1]: `run()` is _not_ available on WASM when the target supports `exception-handling`. fn spawn(self, event_handler: F) where - F: 'static + FnMut(Event, &EventLoopWindowTarget); + F: 'static + FnMut(Event, ActiveEventLoop<'_>); } impl EventLoopExtWebSys for EventLoop { type UserEvent = T; - fn spawn(self, event_handler: F) + fn spawn(self, mut event_handler: F) where - F: 'static + FnMut(Event, &EventLoopWindowTarget), + F: 'static + FnMut(Event, ActiveEventLoop<'_>), { - self.event_loop.spawn(event_handler) + self.event_loop + .spawn(move |event, inner| event_handler(event, ActiveEventLoop { inner })) } } -pub trait EventLoopWindowTargetExtWebSys { +pub trait ActiveEventLoopExtWebSys { /// Sets the strategy for [`ControlFlow::Poll`]. /// /// See [`PollStrategy`]. @@ -166,15 +166,15 @@ pub trait EventLoopWindowTargetExtWebSys { fn poll_strategy(&self) -> PollStrategy; } -impl EventLoopWindowTargetExtWebSys for EventLoopWindowTarget { +impl ActiveEventLoopExtWebSys for ActiveEventLoop<'_> { #[inline] fn set_poll_strategy(&self, strategy: PollStrategy) { - self.p.set_poll_strategy(strategy); + self.inner.set_poll_strategy(strategy); } #[inline] fn poll_strategy(&self) -> PollStrategy { - self.p.poll_strategy() + self.inner.poll_strategy() } } diff --git a/src/platform/x11.rs b/src/platform/x11.rs index 194f02145f..7cb7f765bf 100644 --- a/src/platform/x11.rs +++ b/src/platform/x11.rs @@ -1,5 +1,5 @@ use crate::{ - event_loop::{EventLoopBuilder, EventLoopWindowTarget}, + event_loop::{ActiveEventLoop, EventLoop, EventLoopBuilder}, monitor::MonitorHandle, window::{Window, WindowBuilder}, }; @@ -42,16 +42,29 @@ pub fn register_xlib_error_hook(hook: XlibErrorHook) { } } -/// Additional methods on [`EventLoopWindowTarget`] that are specific to X11. -pub trait EventLoopWindowTargetExtX11 { - /// True if the [`EventLoopWindowTarget`] uses X11. +/// Additional methods on [`EventLoop`] that are specific to X11. +pub trait EventLoopExtX11 { + /// True if the [`EventLoop`] uses X11. fn is_x11(&self) -> bool; } -impl EventLoopWindowTargetExtX11 for EventLoopWindowTarget { +impl EventLoopExtX11 for EventLoop { #[inline] fn is_x11(&self) -> bool { - !self.p.is_wayland() + !self.event_loop.window_target().is_wayland() + } +} + +/// Additional methods on [`ActiveEventLoop`] that are specific to X11. +pub trait ActiveEventLoopExtX11 { + /// True if the [`ActiveEventLoop`] uses X11. + fn is_x11(&self) -> bool; +} + +impl ActiveEventLoopExtX11 for ActiveEventLoop<'_> { + #[inline] + fn is_x11(&self) -> bool { + !self.inner.is_wayland() } } diff --git a/src/platform_impl/android/mod.rs b/src/platform_impl/android/mod.rs index 159ab8448c..4bfb27b153 100644 --- a/src/platform_impl/android/mod.rs +++ b/src/platform_impl/android/mod.rs @@ -21,7 +21,7 @@ use crate::{ dpi::{PhysicalPosition, PhysicalSize, Position, Size}, error, event::{self, Force, InnerSizeWriter, StartCause}, - event_loop::{self, ControlFlow, DeviceEvents, EventLoopWindowTarget as RootELW}, + event_loop::{self, ControlFlow, DeviceEvents}, platform::pump_events::PumpStatus, window::{ self, CursorGrabMode, ImePurpose, ResizeDirection, Theme, WindowButtons, WindowLevel, @@ -138,7 +138,7 @@ pub struct KeyEventExtra {} pub struct EventLoop { android_app: AndroidApp, - window_target: event_loop::EventLoopWindowTarget, + window_target: EventLoopWindowTarget, redraw_flag: SharedFlag, user_events_sender: mpsc::Sender, user_events_receiver: PeekableReceiver, //must wake looper whenever something gets sent @@ -176,16 +176,11 @@ impl EventLoop { Ok(Self { android_app: android_app.clone(), - window_target: event_loop::EventLoopWindowTarget { - p: EventLoopWindowTarget { - app: android_app.clone(), - control_flow: Cell::new(ControlFlow::default()), - exit: Cell::new(false), - redraw_requester: RedrawRequester::new( - &redraw_flag, - android_app.create_waker(), - ), - }, + window_target: EventLoopWindowTarget { + app: android_app.clone(), + control_flow: Cell::new(ControlFlow::default()), + exit: Cell::new(false), + redraw_requester: RedrawRequester::new(&redraw_flag, android_app.create_waker()), }, redraw_flag, user_events_sender, @@ -201,7 +196,7 @@ impl EventLoop { fn single_iteration(&mut self, main_event: Option>, callback: &mut F) where - F: FnMut(event::Event, &RootELW), + F: FnMut(event::Event, &EventLoopWindowTarget), { trace!("Mainloop iteration"); @@ -373,7 +368,7 @@ impl EventLoop { callback: &mut F, ) -> InputStatus where - F: FnMut(event::Event, &RootELW), + F: FnMut(event::Event, &EventLoopWindowTarget), { let mut input_status = InputStatus::Handled; match event { @@ -478,14 +473,14 @@ impl EventLoop { pub fn run(mut self, event_handler: F) -> Result<(), EventLoopError> where - F: FnMut(event::Event, &event_loop::EventLoopWindowTarget), + F: FnMut(event::Event, &EventLoopWindowTarget), { self.run_on_demand(event_handler) } pub fn run_on_demand(&mut self, mut event_handler: F) -> Result<(), EventLoopError> where - F: FnMut(event::Event, &event_loop::EventLoopWindowTarget), + F: FnMut(event::Event, &EventLoopWindowTarget), { if self.loop_running { return Err(EventLoopError::AlreadyRunning); @@ -508,7 +503,7 @@ impl EventLoop { pub fn pump_events(&mut self, timeout: Option, mut callback: F) -> PumpStatus where - F: FnMut(event::Event, &RootELW), + F: FnMut(event::Event, &EventLoopWindowTarget), { if !self.loop_running { self.loop_running = true; @@ -541,7 +536,7 @@ impl EventLoop { fn poll_events_with_timeout(&mut self, mut timeout: Option, mut callback: F) where - F: FnMut(event::Event, &RootELW), + F: FnMut(event::Event, &EventLoopWindowTarget), { let start = Instant::now(); @@ -617,7 +612,7 @@ impl EventLoop { }); } - pub fn window_target(&self) -> &event_loop::EventLoopWindowTarget { + pub fn window_target(&self) -> &EventLoopWindowTarget { &self.window_target } @@ -629,11 +624,11 @@ impl EventLoop { } fn control_flow(&self) -> ControlFlow { - self.window_target.p.control_flow() + self.window_target.control_flow() } fn exiting(&self) -> bool { - self.window_target.p.exiting() + self.window_target.exiting() } } @@ -661,6 +656,8 @@ impl EventLoopProxy { } } +pub type ActiveEventLoop<'a> = &'a EventLoopWindowTarget; + pub struct EventLoopWindowTarget { app: AndroidApp, control_flow: Cell, diff --git a/src/platform_impl/ios/event_loop.rs b/src/platform_impl/ios/event_loop.rs index 029f2200e2..05a1535dbe 100644 --- a/src/platform_impl/ios/event_loop.rs +++ b/src/platform_impl/ios/event_loop.rs @@ -19,10 +19,7 @@ use objc2::ClassType; use crate::{ error::EventLoopError, event::Event, - event_loop::{ - ControlFlow, DeviceEvents, EventLoopClosed, - EventLoopWindowTarget as RootEventLoopWindowTarget, - }, + event_loop::{ControlFlow, DeviceEvents, EventLoopClosed}, platform::ios::Idiom, }; @@ -32,6 +29,8 @@ use super::{ uikit::{UIApplication, UIApplicationMain, UIDevice, UIScreen}, }; +pub type ActiveEventLoop<'a> = &'a EventLoopWindowTarget; + #[derive(Debug)] pub struct EventLoopWindowTarget { pub(super) mtm: MainThreadMarker, @@ -88,7 +87,7 @@ pub struct EventLoop { mtm: MainThreadMarker, sender: Sender, receiver: Receiver, - window_target: RootEventLoopWindowTarget, + window_target: EventLoopWindowTarget, } #[derive(Default, Debug, Copy, Clone, PartialEq, Eq, Hash)] @@ -120,15 +119,13 @@ impl EventLoop { mtm, sender, receiver, - window_target: RootEventLoopWindowTarget { - p: EventLoopWindowTarget { mtm }, - }, + window_target: EventLoopWindowTarget { mtm }, }) } pub fn run(self, event_handler: F) -> ! where - F: FnMut(Event, &RootEventLoopWindowTarget), + F: FnMut(Event, &EventLoopWindowTarget), { unsafe { let application = UIApplication::shared(self.mtm); @@ -140,7 +137,7 @@ impl EventLoop { ); let event_handler = std::mem::transmute::< - Box, &RootEventLoopWindowTarget)>, + Box, &EventLoopWindowTarget)>, Box>, >(Box::new(event_handler)); @@ -169,7 +166,7 @@ impl EventLoop { EventLoopProxy::new(self.sender.clone()) } - pub fn window_target(&self) -> &RootEventLoopWindowTarget { + pub fn window_target(&self) -> &EventLoopWindowTarget { &self.window_target } } @@ -340,7 +337,7 @@ fn setup_control_flow_observers() { #[derive(Debug)] pub enum Never {} -type EventHandlerCallback = dyn FnMut(Event, &RootEventLoopWindowTarget) + 'static; +type EventHandlerCallback = dyn FnMut(Event, &EventLoopWindowTarget) + 'static; pub trait EventHandler: Debug { fn handle_nonuser_event(&mut self, event: Event); @@ -350,7 +347,7 @@ pub trait EventHandler: Debug { struct EventLoopHandler { f: Box>, receiver: Receiver, - event_loop: RootEventLoopWindowTarget, + event_loop: EventLoopWindowTarget, } impl Debug for EventLoopHandler { diff --git a/src/platform_impl/ios/mod.rs b/src/platform_impl/ios/mod.rs index d4d5a77f39..996c455736 100644 --- a/src/platform_impl/ios/mod.rs +++ b/src/platform_impl/ios/mod.rs @@ -70,7 +70,8 @@ use std::fmt; pub(crate) use self::{ event_loop::{ - EventLoop, EventLoopProxy, EventLoopWindowTarget, PlatformSpecificEventLoopAttributes, + ActiveEventLoop, EventLoop, EventLoopProxy, EventLoopWindowTarget, + PlatformSpecificEventLoopAttributes, }, monitor::{MonitorHandle, VideoMode}, window::{PlatformSpecificWindowBuilderAttributes, Window, WindowId}, diff --git a/src/platform_impl/linux/mod.rs b/src/platform_impl/linux/mod.rs index 98f32595c3..dc84f752c1 100644 --- a/src/platform_impl/linux/mod.rs +++ b/src/platform_impl/linux/mod.rs @@ -20,10 +20,7 @@ use crate::{ dpi::{PhysicalPosition, PhysicalSize, Position, Size}, error::{EventLoopError, ExternalError, NotSupportedError, OsError as RootOsError}, event::KeyEvent, - event_loop::{ - AsyncRequestSerial, ControlFlow, DeviceEvents, EventLoopClosed, - EventLoopWindowTarget as RootELW, - }, + event_loop::{AsyncRequestSerial, ControlFlow, DeviceEvents, EventLoopClosed}, icon::Icon, keyboard::{Key, PhysicalKey}, platform::{ @@ -816,26 +813,26 @@ impl EventLoop { pub fn run(mut self, callback: F) -> Result<(), EventLoopError> where - F: FnMut(crate::event::Event, &RootELW), + F: FnMut(crate::event::Event, &EventLoopWindowTarget), { self.run_on_demand(callback) } pub fn run_on_demand(&mut self, callback: F) -> Result<(), EventLoopError> where - F: FnMut(crate::event::Event, &RootELW), + F: FnMut(crate::event::Event, &EventLoopWindowTarget), { x11_or_wayland!(match self; EventLoop(evlp) => evlp.run_on_demand(callback)) } pub fn pump_events(&mut self, timeout: Option, callback: F) -> PumpStatus where - F: FnMut(crate::event::Event, &RootELW), + F: FnMut(crate::event::Event, &EventLoopWindowTarget), { x11_or_wayland!(match self; EventLoop(evlp) => evlp.pump_events(timeout, callback)) } - pub fn window_target(&self) -> &crate::event_loop::EventLoopWindowTarget { + pub fn window_target(&self) -> &EventLoopWindowTarget { x11_or_wayland!(match self; EventLoop(evlp) => evlp.window_target()) } } @@ -858,6 +855,8 @@ impl EventLoopProxy { } } +pub type ActiveEventLoop<'a> = &'a EventLoopWindowTarget; + pub enum EventLoopWindowTarget { #[cfg(wayland_platform)] Wayland(wayland::EventLoopWindowTarget), diff --git a/src/platform_impl/linux/wayland/event_loop/mod.rs b/src/platform_impl/linux/wayland/event_loop/mod.rs index 099f176ed4..a60cc5507c 100644 --- a/src/platform_impl/linux/wayland/event_loop/mod.rs +++ b/src/platform_impl/linux/wayland/event_loop/mod.rs @@ -18,9 +18,7 @@ use sctk::reexports::client::{Connection, QueueHandle}; use crate::dpi::{LogicalSize, PhysicalSize}; use crate::error::{EventLoopError, OsError as RootOsError}; use crate::event::{Event, InnerSizeWriter, StartCause, WindowEvent}; -use crate::event_loop::{ - ControlFlow, DeviceEvents, EventLoopWindowTarget as RootEventLoopWindowTarget, -}; +use crate::event_loop::{ControlFlow, DeviceEvents}; use crate::platform::pump_events::PumpStatus; use crate::platform_impl::platform::min_timeout; use crate::platform_impl::{EventLoopWindowTarget as PlatformEventLoopWindowTarget, OsError}; @@ -62,7 +60,7 @@ pub struct EventLoop { connection: Connection, /// Event loop window target. - window_target: RootEventLoopWindowTarget, + window_target: PlatformEventLoopWindowTarget, // XXX drop after everything else, just to be safe. /// Calloop's event loop. @@ -178,9 +176,7 @@ impl EventLoop { user_events_sender, pending_user_events, event_loop, - window_target: RootEventLoopWindowTarget { - p: PlatformEventLoopWindowTarget::Wayland(window_target), - }, + window_target: PlatformEventLoopWindowTarget::Wayland(window_target), }; Ok(event_loop) @@ -188,7 +184,7 @@ impl EventLoop { pub fn run_on_demand(&mut self, mut event_handler: F) -> Result<(), EventLoopError> where - F: FnMut(Event, &RootEventLoopWindowTarget), + F: FnMut(Event, &PlatformEventLoopWindowTarget), { if self.loop_running { return Err(EventLoopError::AlreadyRunning); @@ -219,7 +215,7 @@ impl EventLoop { pub fn pump_events(&mut self, timeout: Option, mut callback: F) -> PumpStatus where - F: FnMut(Event, &RootEventLoopWindowTarget), + F: FnMut(Event, &PlatformEventLoopWindowTarget), { if !self.loop_running { self.loop_running = true; @@ -246,7 +242,7 @@ impl EventLoop { pub fn poll_events_with_timeout(&mut self, mut timeout: Option, mut callback: F) where - F: FnMut(Event, &RootEventLoopWindowTarget), + F: FnMut(Event, &PlatformEventLoopWindowTarget), { let cause = loop { let start = Instant::now(); @@ -322,7 +318,7 @@ impl EventLoop { fn single_iteration(&mut self, callback: &mut F, cause: StartCause) where - F: FnMut(Event, &RootEventLoopWindowTarget), + F: FnMut(Event, &PlatformEventLoopWindowTarget), { // NOTE currently just indented to simplify the diff @@ -527,12 +523,12 @@ impl EventLoop { } #[inline] - pub fn window_target(&self) -> &RootEventLoopWindowTarget { + pub fn window_target(&self) -> &PlatformEventLoopWindowTarget { &self.window_target } fn with_state<'a, U: 'a, F: FnOnce(&'a mut WinitState) -> U>(&'a mut self, callback: F) -> U { - let state = match &mut self.window_target.p { + let state = match &mut self.window_target { PlatformEventLoopWindowTarget::Wayland(window_target) => window_target.state.get_mut(), #[cfg(x11_platform)] _ => unreachable!(), @@ -542,7 +538,7 @@ impl EventLoop { } fn loop_dispatch>>(&mut self, timeout: D) -> IOResult<()> { - let state = match &mut self.window_target.p { + let state = match &mut self.window_target { PlatformEventLoopWindowTarget::Wayland(window_target) => window_target.state.get_mut(), #[cfg(feature = "x11")] _ => unreachable!(), @@ -555,7 +551,7 @@ impl EventLoop { } fn roundtrip(&mut self) -> Result { - let state = match &mut self.window_target.p { + let state = match &mut self.window_target { PlatformEventLoopWindowTarget::Wayland(window_target) => window_target.state.get_mut(), #[cfg(feature = "x11")] _ => unreachable!(), @@ -571,19 +567,19 @@ impl EventLoop { } fn control_flow(&self) -> ControlFlow { - self.window_target.p.control_flow() + self.window_target.control_flow() } fn exiting(&self) -> bool { - self.window_target.p.exiting() + self.window_target.exiting() } fn set_exit_code(&self, code: i32) { - self.window_target.p.set_exit_code(code) + self.window_target.set_exit_code(code) } fn exit_code(&self) -> Option { - self.window_target.p.exit_code() + self.window_target.exit_code() } } diff --git a/src/platform_impl/linux/x11/event_processor.rs b/src/platform_impl/linux/x11/event_processor.rs index 6702422580..19e065d965 100644 --- a/src/platform_impl/linux/x11/event_processor.rs +++ b/src/platform_impl/linux/x11/event_processor.rs @@ -21,10 +21,10 @@ use super::{ Dnd, DndState, GenericEventCookie, ImeReceiver, ScrollOrientation, UnownedWindow, WindowId, }; +use crate::platform_impl::EventLoopWindowTarget as PlatformEventLoopWindowTarget; use crate::{ dpi::{PhysicalPosition, PhysicalSize}, event::{DeviceEvent, ElementState, Event, Ime, RawKeyEvent, TouchPhase, WindowEvent}, - event_loop::EventLoopWindowTarget as RootELW, keyboard::ModifiersState, platform_impl::platform::common::{keymap, xkb_state::KbdState}, }; @@ -44,7 +44,7 @@ pub(super) struct EventProcessor { pub(super) devices: RefCell>, pub(super) xi2ext: ExtensionInformation, pub(super) xkbext: ExtensionInformation, - pub(super) target: Rc, + pub(super) target: Rc, pub(super) kb_state: KbdState, // Number of touch events currently in progress pub(super) num_touch: u32, diff --git a/src/platform_impl/linux/x11/mod.rs b/src/platform_impl/linux/x11/mod.rs index f1d9ae2cea..07ee82a92d 100644 --- a/src/platform_impl/linux/x11/mod.rs +++ b/src/platform_impl/linux/x11/mod.rs @@ -69,10 +69,11 @@ use super::{common::xkb_state::KbdState, ControlFlow, OsError}; use crate::{ error::{EventLoopError, OsError as RootOsError}, event::{Event, StartCause, WindowEvent}, - event_loop::{DeviceEvents, EventLoopClosed, EventLoopWindowTarget as RootELW}, + event_loop::{DeviceEvents, EventLoopClosed}, platform::pump_events::PumpStatus, platform_impl::{ platform::{min_timeout, WindowId}, + EventLoopWindowTarget as PlatformEventLoopWindowTarget, PlatformSpecificWindowBuilderAttributes, }, window::WindowAttributes, @@ -167,7 +168,7 @@ pub struct EventLoop { user_receiver: PeekableReceiver, activation_receiver: PeekableReceiver, user_sender: Sender, - target: Rc, + target: Rc, /// The current state of the event loop. state: EventLoopState, @@ -325,9 +326,7 @@ impl EventLoop { // Set initial device event filter. window_target.update_listen_device_events(true); - let target = Rc::new(RootELW { - p: super::EventLoopWindowTarget::X(window_target), - }); + let target = Rc::new(PlatformEventLoopWindowTarget::X(window_target)); let event_processor = EventProcessor { target: target.clone(), @@ -392,13 +391,13 @@ impl EventLoop { } } - pub(crate) fn window_target(&self) -> &RootELW { + pub(crate) fn window_target(&self) -> &PlatformEventLoopWindowTarget { &self.target } pub fn run_on_demand(&mut self, mut event_handler: F) -> Result<(), EventLoopError> where - F: FnMut(Event, &RootELW), + F: FnMut(Event, &PlatformEventLoopWindowTarget), { if self.loop_running { return Err(EventLoopError::AlreadyRunning); @@ -432,7 +431,7 @@ impl EventLoop { pub fn pump_events(&mut self, timeout: Option, mut callback: F) -> PumpStatus where - F: FnMut(Event, &RootELW), + F: FnMut(Event, &PlatformEventLoopWindowTarget), { if !self.loop_running { self.loop_running = true; @@ -465,7 +464,7 @@ impl EventLoop { pub fn poll_events_with_timeout(&mut self, mut timeout: Option, mut callback: F) where - F: FnMut(Event, &RootELW), + F: FnMut(Event, &PlatformEventLoopWindowTarget), { let start = Instant::now(); @@ -543,7 +542,7 @@ impl EventLoop { fn single_iteration(&mut self, callback: &mut F, cause: StartCause) where - F: FnMut(Event, &RootELW), + F: FnMut(Event, &PlatformEventLoopWindowTarget), { callback(crate::event::Event::NewEvents(cause), &self.target); @@ -617,7 +616,7 @@ impl EventLoop { fn drain_events(&mut self, callback: &mut F) where - F: FnMut(Event, &RootELW), + F: FnMut(Event, &PlatformEventLoopWindowTarget), { let target = &self.target; let mut xev = MaybeUninit::uninit(); @@ -640,19 +639,19 @@ impl EventLoop { } fn control_flow(&self) -> ControlFlow { - self.target.p.control_flow() + self.target.control_flow() } fn exiting(&self) -> bool { - self.target.p.exiting() + self.target.exiting() } fn set_exit_code(&self, code: i32) { - self.target.p.set_exit_code(code) + self.target.set_exit_code(code) } fn exit_code(&self) -> Option { - self.target.p.exit_code() + self.target.exit_code() } } @@ -668,8 +667,8 @@ impl AsRawFd for EventLoop { } } -pub(crate) fn get_xtarget(target: &RootELW) -> &EventLoopWindowTarget { - match target.p { +pub(crate) fn get_xtarget(target: &PlatformEventLoopWindowTarget) -> &EventLoopWindowTarget { + match target { super::EventLoopWindowTarget::X(ref target) => target, #[cfg(wayland_platform)] _ => unreachable!(), diff --git a/src/platform_impl/macos/app_state.rs b/src/platform_impl/macos/app_state.rs index 75e37b6d71..3921928a47 100644 --- a/src/platform_impl/macos/app_state.rs +++ b/src/platform_impl/macos/app_state.rs @@ -19,12 +19,12 @@ use once_cell::sync::Lazy; use super::{ event::dummy_event, event_loop::PanicInfo, menu, observer::EventLoopWaker, util::Never, - window::WinitWindow, + window::WinitWindow, EventLoopWindowTarget, }; use crate::{ dpi::PhysicalSize, event::{Event, InnerSizeWriter, StartCause, WindowEvent}, - event_loop::{ControlFlow, EventLoopWindowTarget as RootWindowTarget}, + event_loop::ControlFlow, window::WindowId, }; @@ -45,18 +45,21 @@ pub trait EventHandler: Debug { fn handle_user_events(&mut self); } -pub(crate) type Callback = RefCell, &RootWindowTarget)>; +pub(crate) type Callback = RefCell, &EventLoopWindowTarget)>; struct EventLoopHandler { callback: Weak>, - window_target: Rc, + window_target: Rc, receiver: Rc>, } impl EventLoopHandler { fn with_callback(&mut self, f: F) where - F: FnOnce(&mut EventLoopHandler, RefMut<'_, dyn FnMut(Event, &RootWindowTarget)>), + F: FnOnce( + &mut EventLoopHandler, + RefMut<'_, dyn FnMut(Event, &EventLoopWindowTarget)>, + ), { // `NSApplication` and our `HANDLER` are global state and so it's possible // that we could get a delegate callback after the application has exit an @@ -370,7 +373,7 @@ impl AppState { /// a call to `clear_callback` before returning to avoid undefined behaviour. pub unsafe fn set_callback( callback: Weak>, - window_target: Rc, + window_target: Rc, receiver: Rc>, ) { *HANDLER.callback.lock().unwrap() = Some(Box::new(EventLoopHandler { diff --git a/src/platform_impl/macos/event_loop.rs b/src/platform_impl/macos/event_loop.rs index 08597a3d4f..cd1e9042f9 100644 --- a/src/platform_impl/macos/event_loop.rs +++ b/src/platform_impl/macos/event_loop.rs @@ -31,9 +31,7 @@ use super::event::dummy_event; use crate::{ error::EventLoopError, event::Event, - event_loop::{ - ControlFlow, DeviceEvents, EventLoopClosed, EventLoopWindowTarget as RootWindowTarget, - }, + event_loop::{ControlFlow, DeviceEvents, EventLoopClosed}, platform::{macos::ActivationPolicy, pump_events::PumpStatus}, platform_impl::platform::{ app::WinitApplication, @@ -77,6 +75,8 @@ pub struct EventLoopWindowTarget { mtm: MainThreadMarker, } +pub type ActiveEventLoop<'a> = &'a EventLoopWindowTarget; + impl EventLoopWindowTarget { #[inline] pub fn available_monitors(&self) -> VecDeque { @@ -157,7 +157,7 @@ pub struct EventLoop { sender: mpsc::Sender, receiver: Rc>, - window_target: Rc, + window_target: Rc, panic_info: Rc, /// We make sure that the callback closure is dropped during a panic @@ -225,21 +225,19 @@ impl EventLoop { _delegate: delegate, sender, receiver: Rc::new(receiver), - window_target: Rc::new(RootWindowTarget { - p: EventLoopWindowTarget { mtm }, - }), + window_target: Rc::new(EventLoopWindowTarget { mtm }), panic_info, _callback: None, }) } - pub fn window_target(&self) -> &RootWindowTarget { + pub fn window_target(&self) -> &EventLoopWindowTarget { &self.window_target } pub fn run(mut self, callback: F) -> Result<(), EventLoopError> where - F: FnMut(Event, &RootWindowTarget), + F: FnMut(Event, &EventLoopWindowTarget), { self.run_on_demand(callback) } @@ -250,7 +248,7 @@ impl EventLoop { // redundant wake ups. pub fn run_on_demand(&mut self, callback: F) -> Result<(), EventLoopError> where - F: FnMut(Event, &RootWindowTarget), + F: FnMut(Event, &EventLoopWindowTarget), { if AppState::is_running() { return Err(EventLoopError::AlreadyRunning); @@ -267,8 +265,8 @@ impl EventLoop { let callback = unsafe { mem::transmute::< - Rc, &RootWindowTarget)>>, - Rc, &RootWindowTarget)>>, + Rc, &EventLoopWindowTarget)>>, + Rc, &EventLoopWindowTarget)>>, >(Rc::new(RefCell::new(callback))) }; @@ -334,7 +332,7 @@ impl EventLoop { pub fn pump_events(&mut self, timeout: Option, callback: F) -> PumpStatus where - F: FnMut(Event, &RootWindowTarget), + F: FnMut(Event, &EventLoopWindowTarget), { // # Safety // We are erasing the lifetime of the application callback here so that we @@ -347,8 +345,8 @@ impl EventLoop { let callback = unsafe { mem::transmute::< - Rc, &RootWindowTarget)>>, - Rc, &RootWindowTarget)>>, + Rc, &EventLoopWindowTarget)>>, + Rc, &EventLoopWindowTarget)>>, >(Rc::new(RefCell::new(callback))) }; diff --git a/src/platform_impl/macos/mod.rs b/src/platform_impl/macos/mod.rs index 04aae45c69..d67207a264 100644 --- a/src/platform_impl/macos/mod.rs +++ b/src/platform_impl/macos/mod.rs @@ -20,7 +20,8 @@ use std::fmt; pub(crate) use self::{ event::KeyEventExtra, event_loop::{ - EventLoop, EventLoopProxy, EventLoopWindowTarget, PlatformSpecificEventLoopAttributes, + ActiveEventLoop, EventLoop, EventLoopProxy, EventLoopWindowTarget, + PlatformSpecificEventLoopAttributes, }, monitor::{MonitorHandle, VideoMode}, window::{PlatformSpecificWindowBuilderAttributes, WindowId}, diff --git a/src/platform_impl/orbital/event_loop.rs b/src/platform_impl/orbital/event_loop.rs index 86b796eb53..8dacaa7c23 100644 --- a/src/platform_impl/orbital/event_loop.rs +++ b/src/platform_impl/orbital/event_loop.rs @@ -14,7 +14,7 @@ use orbclient::{ use crate::{ error::EventLoopError, event::{self, Ime, Modifiers, StartCause}, - event_loop::{self, ControlFlow, DeviceEvents}, + event_loop::{ControlFlow, DeviceEvents, EventLoopClosed}, keyboard::{ Key, KeyCode, KeyLocation, ModifiersKeys, ModifiersState, NativeKey, NativeKeyCode, PhysicalKey, @@ -272,7 +272,7 @@ impl EventState { pub struct EventLoop { windows: Vec<(Arc, EventState)>, - window_target: event_loop::EventLoopWindowTarget, + window_target: EventLoopWindowTarget, user_events_sender: mpsc::Sender, user_events_receiver: mpsc::Receiver, } @@ -304,16 +304,14 @@ impl EventLoop { Ok(Self { windows: Vec::new(), - window_target: event_loop::EventLoopWindowTarget { - p: EventLoopWindowTarget { - control_flow: Cell::new(ControlFlow::default()), - exit: Cell::new(false), - creates: Mutex::new(VecDeque::new()), - redraws: Arc::new(Mutex::new(VecDeque::new())), - destroys: Arc::new(Mutex::new(VecDeque::new())), - event_socket, - wake_socket, - }, + window_target: EventLoopWindowTarget { + control_flow: Cell::new(ControlFlow::default()), + exit: Cell::new(false), + creates: Mutex::new(VecDeque::new()), + redraws: Arc::new(Mutex::new(VecDeque::new())), + destroys: Arc::new(Mutex::new(VecDeque::new())), + event_socket, + wake_socket, }, user_events_sender, user_events_receiver, @@ -463,10 +461,10 @@ impl EventLoop { pub fn run(mut self, mut event_handler_inner: F) -> Result<(), EventLoopError> where - F: FnMut(event::Event, &event_loop::EventLoopWindowTarget), + F: FnMut(event::Event, &EventLoopWindowTarget), { let mut event_handler = - move |event: event::Event, window_target: &event_loop::EventLoopWindowTarget| { + move |event: event::Event, window_target: &EventLoopWindowTarget| { event_handler_inner(event, window_target); }; @@ -481,7 +479,7 @@ impl EventLoop { // Handle window creates. while let Some(window) = { - let mut creates = self.window_target.p.creates.lock().unwrap(); + let mut creates = self.window_target.creates.lock().unwrap(); creates.pop_front() } { let window_id = WindowId { @@ -515,7 +513,7 @@ impl EventLoop { // Handle window destroys. while let Some(destroy_id) = { - let mut destroys = self.window_target.p.destroys.lock().unwrap(); + let mut destroys = self.window_target.destroys.lock().unwrap(); destroys.pop_front() } { event_handler( @@ -570,7 +568,7 @@ impl EventLoop { .expect("failed to acknowledge resize"); // Require redraw after resize. - let mut redraws = self.window_target.p.redraws.lock().unwrap(); + let mut redraws = self.window_target.redraws.lock().unwrap(); if !redraws.contains(&window_id) { redraws.push_back(window_id); } @@ -586,7 +584,7 @@ impl EventLoop { // To avoid deadlocks the redraws lock is not held during event processing. while let Some(window_id) = { - let mut redraws = self.window_target.p.redraws.lock().unwrap(); + let mut redraws = self.window_target.redraws.lock().unwrap(); redraws.pop_front() } { event_handler( @@ -600,11 +598,11 @@ impl EventLoop { event_handler(event::Event::AboutToWait, &self.window_target); - if self.window_target.p.exiting() { + if self.window_target.exiting() { break; } - let requested_resume = match self.window_target.p.control_flow() { + let requested_resume = match self.window_target.control_flow() { ControlFlow::Poll => { start_cause = StartCause::Poll; continue; @@ -618,7 +616,6 @@ impl EventLoop { let timeout_socket = TimeSocket::open().unwrap(); self.window_target - .p .event_socket .write(&syscall::Event { id: timeout_socket.0.fd, @@ -646,7 +643,7 @@ impl EventLoop { // Wait for event if needed. let mut event = syscall::Event::default(); - self.window_target.p.event_socket.read(&mut event).unwrap(); + self.window_target.event_socket.read(&mut event).unwrap(); // TODO: handle spurious wakeups (redraw caused wakeup but redraw already handled) match requested_resume { @@ -673,14 +670,14 @@ impl EventLoop { Ok(()) } - pub fn window_target(&self) -> &event_loop::EventLoopWindowTarget { + pub fn window_target(&self) -> &EventLoopWindowTarget { &self.window_target } pub fn create_proxy(&self) -> EventLoopProxy { EventLoopProxy { user_events_sender: self.user_events_sender.clone(), - wake_socket: self.window_target.p.wake_socket.clone(), + wake_socket: self.window_target.wake_socket.clone(), } } } @@ -691,10 +688,10 @@ pub struct EventLoopProxy { } impl EventLoopProxy { - pub fn send_event(&self, event: T) -> Result<(), event_loop::EventLoopClosed> { + pub fn send_event(&self, event: T) -> Result<(), EventLoopClosed> { self.user_events_sender .send(event) - .map_err(|mpsc::SendError(x)| event_loop::EventLoopClosed(x))?; + .map_err(|mpsc::SendError(x)| EventLoopClosed(x))?; self.wake_socket.wake().unwrap(); @@ -713,6 +710,8 @@ impl Clone for EventLoopProxy { impl Unpin for EventLoopProxy {} +pub type ActiveEventLoop<'a> = &'a EventLoopWindowTarget; + pub struct EventLoopWindowTarget { control_flow: Cell, exit: Cell, diff --git a/src/platform_impl/orbital/mod.rs b/src/platform_impl/orbital/mod.rs index e2ad1ce08c..dedecc32fc 100644 --- a/src/platform_impl/orbital/mod.rs +++ b/src/platform_impl/orbital/mod.rs @@ -6,7 +6,7 @@ use std::sync::Arc; use crate::dpi::{PhysicalPosition, PhysicalSize}; -pub use self::event_loop::{EventLoop, EventLoopProxy, EventLoopWindowTarget}; +pub use self::event_loop::{ActiveEventLoop, EventLoop, EventLoopProxy, EventLoopWindowTarget}; mod event_loop; pub use self::window::Window; diff --git a/src/platform_impl/web/event_loop/mod.rs b/src/platform_impl/web/event_loop/mod.rs index a1db32f9ee..0ecb9e5fbd 100644 --- a/src/platform_impl/web/event_loop/mod.rs +++ b/src/platform_impl/web/event_loop/mod.rs @@ -2,7 +2,6 @@ use std::sync::mpsc::{self, Receiver, Sender}; use crate::error::EventLoopError; use crate::event::Event; -use crate::event_loop::EventLoopWindowTarget as RootEventLoopWindowTarget; use super::{backend, device, window}; @@ -14,8 +13,10 @@ mod window_target; pub use proxy::EventLoopProxy; pub use window_target::EventLoopWindowTarget; +pub type ActiveEventLoop<'a> = &'a EventLoopWindowTarget; + pub struct EventLoop { - elw: RootEventLoopWindowTarget, + elw: EventLoopWindowTarget, user_event_sender: Sender, user_event_receiver: Receiver, } @@ -26,9 +27,7 @@ pub(crate) struct PlatformSpecificEventLoopAttributes {} impl EventLoop { pub(crate) fn new(_: &PlatformSpecificEventLoopAttributes) -> Result { let (user_event_sender, user_event_receiver) = mpsc::channel(); - let elw = RootEventLoopWindowTarget { - p: EventLoopWindowTarget::new(), - }; + let elw = EventLoopWindowTarget::new(); Ok(EventLoop { elw, user_event_sender, @@ -38,11 +37,9 @@ impl EventLoop { pub fn run(self, mut event_handler: F) -> ! where - F: FnMut(Event, &RootEventLoopWindowTarget), + F: FnMut(Event, &EventLoopWindowTarget), { - let target = RootEventLoopWindowTarget { - p: self.elw.p.clone(), - }; + let target = self.elw.clone(); // SAFETY: Don't use `move` to make sure we leak the `event_handler` and `target`. let handler: Box)> = Box::new(|event| { @@ -61,7 +58,7 @@ impl EventLoop { // because this function will never return and all resources not cleaned up by the point we // `throw` will leak, making this actually `'static`. let handler = unsafe { std::mem::transmute(handler) }; - self.elw.p.run(handler, false); + self.elw.run(handler, false); // Throw an exception to break out of Rust execution and use unreachable to tell the // compiler this function won't return, giving it a return type of '!' @@ -74,13 +71,11 @@ impl EventLoop { pub fn spawn(self, mut event_handler: F) where - F: 'static + FnMut(Event, &RootEventLoopWindowTarget), + F: 'static + FnMut(Event, &EventLoopWindowTarget), { - let target = RootEventLoopWindowTarget { - p: self.elw.p.clone(), - }; + let target = self.elw.clone(); - self.elw.p.run( + self.elw.run( Box::new(move |event| { let event = match event.map_nonuser_event() { Ok(event) => event, @@ -98,10 +93,10 @@ impl EventLoop { } pub fn create_proxy(&self) -> EventLoopProxy { - EventLoopProxy::new(self.elw.p.waker(), self.user_event_sender.clone()) + EventLoopProxy::new(self.elw.waker(), self.user_event_sender.clone()) } - pub fn window_target(&self) -> &RootEventLoopWindowTarget { + pub fn window_target(&self) -> &EventLoopWindowTarget { &self.elw } } diff --git a/src/platform_impl/web/event_loop/runner.rs b/src/platform_impl/web/event_loop/runner.rs index b029ab516f..e6ce280e0e 100644 --- a/src/platform_impl/web/event_loop/runner.rs +++ b/src/platform_impl/web/event_loop/runner.rs @@ -207,7 +207,7 @@ impl Shared { // Set the event callback to use for the event loop runner // This the event callback is a fairly thin layer over the user-provided callback that closes - // over a RootEventLoopWindowTarget reference + // over a EventLoopWindowTarget reference pub fn set_listener(&self, event_handler: Box) { { let mut runner = self.0.runner.borrow_mut(); diff --git a/src/platform_impl/web/event_loop/window_target.rs b/src/platform_impl/web/event_loop/window_target.rs index e29c03df2b..09c3e6fcf8 100644 --- a/src/platform_impl/web/event_loop/window_target.rs +++ b/src/platform_impl/web/event_loop/window_target.rs @@ -47,7 +47,7 @@ pub struct EventLoopWindowTarget { } impl EventLoopWindowTarget { - pub fn new() -> Self { + pub(crate) fn new() -> Self { Self { runner: runner::Shared::new(), modifiers: ModifiersShared::default(), diff --git a/src/platform_impl/web/mod.rs b/src/platform_impl/web/mod.rs index 56ca0eaf47..b20f2ab576 100644 --- a/src/platform_impl/web/mod.rs +++ b/src/platform_impl/web/mod.rs @@ -32,7 +32,8 @@ mod backend; pub use self::device::DeviceId; pub use self::error::OsError; pub(crate) use self::event_loop::{ - EventLoop, EventLoopProxy, EventLoopWindowTarget, PlatformSpecificEventLoopAttributes, + ActiveEventLoop, EventLoop, EventLoopProxy, EventLoopWindowTarget, + PlatformSpecificEventLoopAttributes, }; pub use self::monitor::{MonitorHandle, VideoMode}; pub use self::window::{PlatformSpecificWindowBuilderAttributes, Window, WindowId}; diff --git a/src/platform_impl/windows/event_loop.rs b/src/platform_impl/windows/event_loop.rs index 0ab23bb95a..423f971cd0 100644 --- a/src/platform_impl/windows/event_loop.rs +++ b/src/platform_impl/windows/event_loop.rs @@ -73,7 +73,7 @@ use crate::{ DeviceEvent, Event, Force, Ime, InnerSizeWriter, RawKeyEvent, Touch, TouchPhase, WindowEvent, }, - event_loop::{ControlFlow, DeviceEvents, EventLoopClosed, EventLoopWindowTarget as RootELW}, + event_loop::{ControlFlow, DeviceEvents, EventLoopClosed}, keyboard::ModifiersState, platform::pump_events::PumpStatus, platform_impl::platform::{ @@ -157,7 +157,7 @@ pub(crate) enum ProcResult { pub struct EventLoop { user_event_sender: Sender, user_event_receiver: Receiver, - window_target: RootELW, + window_target: EventLoopWindowTarget, msg_hook: Option bool + 'static>>, } @@ -177,6 +177,8 @@ impl Default for PlatformSpecificEventLoopAttributes { } } +pub type ActiveEventLoop<'a> = &'a EventLoopWindowTarget; + pub struct EventLoopWindowTarget { thread_id: u32, thread_msg_target: HWND, @@ -216,34 +218,32 @@ impl EventLoop { Ok(EventLoop { user_event_sender, user_event_receiver, - window_target: RootELW { - p: EventLoopWindowTarget { - thread_id, - thread_msg_target, - runner_shared, - }, + window_target: EventLoopWindowTarget { + thread_id, + thread_msg_target, + runner_shared, }, msg_hook: attributes.msg_hook.take(), }) } - pub fn window_target(&self) -> &RootELW { + pub fn window_target(&self) -> &EventLoopWindowTarget { &self.window_target } pub fn run(mut self, event_handler: F) -> Result<(), EventLoopError> where - F: FnMut(Event, &RootELW), + F: FnMut(Event, &EventLoopWindowTarget), { self.run_on_demand(event_handler) } pub fn run_on_demand(&mut self, mut event_handler: F) -> Result<(), EventLoopError> where - F: FnMut(Event, &RootELW), + F: FnMut(Event, &EventLoopWindowTarget), { { - let runner = &self.window_target.p.runner_shared; + let runner = &self.window_target.runner_shared; if runner.state() != RunnerState::Uninitialized { return Err(EventLoopError::AlreadyRunning); } @@ -288,7 +288,7 @@ impl EventLoop { } }; - let runner = &self.window_target.p.runner_shared; + let runner = &self.window_target.runner_shared; runner.loop_destroyed(); // # Safety @@ -305,10 +305,10 @@ impl EventLoop { pub fn pump_events(&mut self, timeout: Option, mut event_handler: F) -> PumpStatus where - F: FnMut(Event, &RootELW), + F: FnMut(Event, &EventLoopWindowTarget), { { - let runner = &self.window_target.p.runner_shared; + let runner = &self.window_target.runner_shared; let event_loop_windows_ref = &self.window_target; let user_event_receiver = &self.user_event_receiver; @@ -341,7 +341,7 @@ impl EventLoop { self.dispatch_peeked_messages(); } - let runner = &self.window_target.p.runner_shared; + let runner = &self.window_target.runner_shared; let status = if let Some(code) = runner.exit_code() { runner.loop_destroyed(); @@ -403,7 +403,7 @@ impl EventLoop { } } - let runner = &self.window_target.p.runner_shared; + let runner = &self.window_target.runner_shared; // We aim to be consistent with the MacOS backend which has a RunLoop // observer that will dispatch AboutToWait when about to wait for @@ -465,7 +465,7 @@ impl EventLoop { /// Dispatch all queued messages via `PeekMessageW` fn dispatch_peeked_messages(&mut self) { - let runner = &self.window_target.p.runner_shared; + let runner = &self.window_target.runner_shared; // We generally want to continue dispatching all pending messages // but we also allow dispatching to be interrupted as a means to @@ -515,13 +515,13 @@ impl EventLoop { pub fn create_proxy(&self) -> EventLoopProxy { EventLoopProxy { - target_window: self.window_target.p.thread_msg_target, + target_window: self.window_target.thread_msg_target, event_send: self.user_event_sender.clone(), } } fn exit_code(&self) -> Option { - self.window_target.p.exit_code() + self.window_target.exit_code() } } @@ -665,7 +665,7 @@ fn dur2timeout(dur: Duration) -> u32 { impl Drop for EventLoop { fn drop(&mut self) { unsafe { - DestroyWindow(self.window_target.p.thread_msg_target); + DestroyWindow(self.window_target.thread_msg_target); } } } diff --git a/src/platform_impl/windows/mod.rs b/src/platform_impl/windows/mod.rs index 13f4ecc6e0..6413ba0d70 100644 --- a/src/platform_impl/windows/mod.rs +++ b/src/platform_impl/windows/mod.rs @@ -8,7 +8,8 @@ use windows_sys::Win32::{ pub(crate) use self::{ event_loop::{ - EventLoop, EventLoopProxy, EventLoopWindowTarget, PlatformSpecificEventLoopAttributes, + ActiveEventLoop, EventLoop, EventLoopProxy, EventLoopWindowTarget, + PlatformSpecificEventLoopAttributes, }, icon::{SelectedCursor, WinIcon}, monitor::{MonitorHandle, VideoMode}, diff --git a/src/window.rs b/src/window.rs index 77f95dece9..fb20226551 100644 --- a/src/window.rs +++ b/src/window.rs @@ -4,7 +4,7 @@ use std::fmt; use crate::{ dpi::{PhysicalPosition, PhysicalSize, Position, Size}, error::{ExternalError, NotSupportedError, OsError}, - event_loop::EventLoopWindowTarget, + event_loop::MaybeActiveEventLoop, monitor::{MonitorHandle, VideoMode}, platform_impl, SendSyncWrapper, }; @@ -506,9 +506,9 @@ impl WindowBuilder { /// - **Web:** The window is created but not inserted into the web page automatically. Please /// see the web platform module for more information. #[inline] - pub fn build(self, window_target: &EventLoopWindowTarget) -> Result { + pub fn build<'a>(self, event_loop: impl MaybeActiveEventLoop<'a>) -> Result { let window = - platform_impl::Window::new(&window_target.p, self.window, self.platform_specific)?; + platform_impl::Window::new(event_loop.__inner(), self.window, self.platform_specific)?; window.maybe_queue_on_main(|w| w.request_redraw()); Ok(Window { window }) } @@ -530,7 +530,7 @@ impl Window { /// /// [`WindowBuilder::new().build(event_loop)`]: WindowBuilder::build #[inline] - pub fn new(event_loop: &EventLoopWindowTarget) -> Result { + pub fn new<'a>(event_loop: impl MaybeActiveEventLoop<'a>) -> Result { let builder = WindowBuilder::new(); builder.build(event_loop) } @@ -1494,9 +1494,9 @@ impl Window { /// Returns the list of all the monitors available on the system. /// - /// This is the same as [`EventLoopWindowTarget::available_monitors`], and is provided for convenience. + /// This is the same as [`ActiveEventLoop::available_monitors`], and is provided for convenience. /// - /// [`EventLoopWindowTarget::available_monitors`]: crate::event_loop::EventLoopWindowTarget::available_monitors + /// [`ActiveEventLoop::available_monitors`]: crate::event_loop::ActiveEventLoop::available_monitors #[inline] pub fn available_monitors(&self) -> impl Iterator { self.window.maybe_wait_on_main(|w| { @@ -1510,13 +1510,13 @@ impl Window { /// /// Returns `None` if it can't identify any monitor as a primary one. /// - /// This is the same as [`EventLoopWindowTarget::primary_monitor`], and is provided for convenience. + /// This is the same as [`ActiveEventLoop::primary_monitor`], and is provided for convenience. /// /// ## Platform-specific /// /// **Wayland / Web:** Always returns `None`. /// - /// [`EventLoopWindowTarget::primary_monitor`]: crate::event_loop::EventLoopWindowTarget::primary_monitor + /// [`ActiveEventLoop::primary_monitor`]: crate::event_loop::ActiveEventLoop::primary_monitor #[inline] pub fn primary_monitor(&self) -> Option { self.window