From 056421546a793cf1129f0fb222881e5f27dd2316 Mon Sep 17 00:00:00 2001 From: Kirill Chibisov Date: Sat, 3 May 2025 20:02:59 +0900 Subject: [PATCH] winit-core: move `ActiveEventLoop` --- src/event_loop.rs | 121 ---------------------------------- src/lib.rs | 1 - winit-core/src/event_loop.rs | 123 +++++++++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+), 122 deletions(-) diff --git a/src/event_loop.rs b/src/event_loop.rs index d093f86d..6c0c3477 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::fmt; use std::marker::PhantomData; #[cfg(any(x11_platform, wayland_platform))] use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, RawFd}; @@ -20,10 +19,7 @@ pub use winit_core::event_loop::*; use crate::application::ApplicationHandler; use crate::cursor::{CustomCursor, CustomCursorSource}; use crate::error::{EventLoopError, RequestError}; -use crate::monitor::MonitorHandle; use crate::platform_impl; -use crate::utils::{impl_dyn_casting, AsAny}; -use crate::window::{Theme, Window, WindowAttributes}; /// Provides a way to retrieve events from the system and from the windows that were registered to /// the events loop. @@ -296,120 +292,3 @@ impl AsRawFd for EventLoop { self.event_loop.as_raw_fd() } } - -pub trait ActiveEventLoop: AsAny + fmt::Debug { - /// Creates an [`EventLoopProxy`] that can be used to dispatch user events - /// to the main event loop, possibly from another thread. - fn create_proxy(&self) -> EventLoopProxy; - - /// Create the window. - /// - /// Possible causes of error include denied permission, incompatible system, and lack of memory. - /// - /// ## Platform-specific - /// - /// - **Web:** The window is created but not inserted into the Web page automatically. Please - /// see the Web platform module for more information. - fn create_window( - &self, - window_attributes: WindowAttributes, - ) -> Result, RequestError>; - - /// Create custom cursor. - /// - /// ## Platform-specific - /// - /// **iOS / Android / Orbital:** Unsupported. - fn create_custom_cursor( - &self, - custom_cursor: CustomCursorSource, - ) -> Result; - - /// Returns the list of all the monitors available on the system. - /// - /// ## Platform-specific - /// - /// **Web:** Only returns the current monitor without - #[cfg_attr( - web_platform, - doc = "[detailed monitor permissions][crate::platform::web::ActiveEventLoopExtWeb::request_detailed_monitor_permission]." - )] - #[cfg_attr(not(web_platform), doc = "detailed monitor permissions.")] - fn available_monitors(&self) -> Box>; - - /// Returns the primary monitor of the system. - /// - /// Returns `None` if it can't identify any monitor as a primary one. - /// - /// ## Platform-specific - /// - /// - **Wayland:** Always returns `None`. - /// - **Web:** Always returns `None` without - #[cfg_attr( - web_platform, - doc = " [detailed monitor permissions][crate::platform::web::ActiveEventLoopExtWeb::request_detailed_monitor_permission]." - )] - #[cfg_attr(not(web_platform), doc = " detailed monitor permissions.")] - fn primary_monitor(&self) -> Option; - - /// 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 - fn listen_device_events(&self, allowed: DeviceEvents); - - /// Returns the current system theme. - /// - /// Returns `None` if it cannot be determined on the current platform. - /// - /// ## Platform-specific - /// - /// - **iOS / Android / Wayland / x11 / Orbital:** Unsupported. - fn system_theme(&self) -> Option; - - /// Sets the [`ControlFlow`]. - fn set_control_flow(&self, control_flow: ControlFlow); - - /// Gets the current [`ControlFlow`]. - fn control_flow(&self) -> ControlFlow; - - /// Stop the event loop. - /// - /// ## Platform-specific - /// - /// ### iOS - /// - /// It is not possible to programmatically exit/quit an application on iOS, so this function is - /// a no-op there. See also [this technical Q&A][qa1561]. - /// - /// [qa1561]: https://developer.apple.com/library/archive/qa/qa1561/_index.html - fn exit(&self); - - /// Returns whether the [`EventLoop`] is about to stop. - /// - /// Set by [`exit()`][Self::exit]. - fn exiting(&self) -> bool; - - /// Gets a persistent reference to the underlying platform display. - /// - /// See the [`OwnedDisplayHandle`] type for more information. - fn owned_display_handle(&self) -> OwnedDisplayHandle; - - /// Get the raw-window-handle handle. - fn rwh_06_handle(&self) -> &dyn HasDisplayHandle; -} - -impl HasDisplayHandle for dyn ActiveEventLoop + '_ { - fn display_handle(&self) -> Result, HandleError> { - self.rwh_06_handle().display_handle() - } -} - -impl_dyn_casting!(ActiveEventLoop); diff --git a/src/lib.rs b/src/lib.rs index 940a1a4f..7f4c134c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -306,6 +306,5 @@ pub use winit_core::{error, icon, keyboard, monitor, window}; #[macro_use] mod os_error; mod platform_impl; -use winit_core::as_any as utils; pub mod platform; diff --git a/winit-core/src/event_loop.rs b/winit-core/src/event_loop.rs index a4d03089..6efd7e8d 100644 --- a/winit-core/src/event_loop.rs +++ b/winit-core/src/event_loop.rs @@ -8,6 +8,129 @@ use rwh_06::{DisplayHandle, HandleError, HasDisplayHandle}; #[cfg(web_platform)] use web_time::{Duration, Instant}; +use crate::as_any::AsAny; +use crate::cursor::{CustomCursor, CustomCursorSource}; +use crate::error::RequestError; +use crate::monitor::MonitorHandle; +use crate::window::{Theme, Window, WindowAttributes}; + +pub trait ActiveEventLoop: AsAny + fmt::Debug { + /// Creates an [`EventLoopProxy`] that can be used to dispatch user events + /// to the main event loop, possibly from another thread. + fn create_proxy(&self) -> EventLoopProxy; + + /// Create the window. + /// + /// Possible causes of error include denied permission, incompatible system, and lack of memory. + /// + /// ## Platform-specific + /// + /// - **Web:** The window is created but not inserted into the Web page automatically. Please + /// see the Web platform module for more information. + fn create_window( + &self, + window_attributes: WindowAttributes, + ) -> Result, RequestError>; + + /// Create custom cursor. + /// + /// ## Platform-specific + /// + /// **iOS / Android / Orbital:** Unsupported. + fn create_custom_cursor( + &self, + custom_cursor: CustomCursorSource, + ) -> Result; + + /// Returns the list of all the monitors available on the system. + /// + /// ## Platform-specific + /// + /// **Web:** Only returns the current monitor without + #[cfg_attr( + web_platform, + doc = "[detailed monitor permissions][crate::platform::web::ActiveEventLoopExtWeb::request_detailed_monitor_permission]." + )] + #[cfg_attr(not(web_platform), doc = "detailed monitor permissions.")] + fn available_monitors(&self) -> Box>; + + /// Returns the primary monitor of the system. + /// + /// Returns `None` if it can't identify any monitor as a primary one. + /// + /// ## Platform-specific + /// + /// - **Wayland:** Always returns `None`. + /// - **Web:** Always returns `None` without + #[cfg_attr( + web_platform, + doc = " [detailed monitor permissions][crate::platform::web::ActiveEventLoopExtWeb::request_detailed_monitor_permission]." + )] + #[cfg_attr(not(web_platform), doc = " detailed monitor permissions.")] + fn primary_monitor(&self) -> Option; + + /// 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 + fn listen_device_events(&self, allowed: DeviceEvents); + + /// Returns the current system theme. + /// + /// Returns `None` if it cannot be determined on the current platform. + /// + /// ## Platform-specific + /// + /// - **iOS / Android / Wayland / x11 / Orbital:** Unsupported. + fn system_theme(&self) -> Option; + + /// Sets the [`ControlFlow`]. + fn set_control_flow(&self, control_flow: ControlFlow); + + /// Gets the current [`ControlFlow`]. + fn control_flow(&self) -> ControlFlow; + + /// Stop the event loop. + /// + /// ## Platform-specific + /// + /// ### iOS + /// + /// It is not possible to programmatically exit/quit an application on iOS, so this function is + /// a no-op there. See also [this technical Q&A][qa1561]. + /// + /// [qa1561]: https://developer.apple.com/library/archive/qa/qa1561/_index.html + fn exit(&self); + + /// Returns whether the [`EventLoop`] is about to stop. + /// + /// Set by [`exit()`][Self::exit]. + fn exiting(&self) -> bool; + + /// Gets a persistent reference to the underlying platform display. + /// + /// See the [`OwnedDisplayHandle`] type for more information. + fn owned_display_handle(&self) -> OwnedDisplayHandle; + + /// Get the raw-window-handle handle. + fn rwh_06_handle(&self) -> &dyn HasDisplayHandle; +} + +impl HasDisplayHandle for dyn ActiveEventLoop + '_ { + fn display_handle(&self) -> Result, HandleError> { + self.rwh_06_handle().display_handle() + } +} + +impl_dyn_casting!(ActiveEventLoop); + /// Control the [`EventLoop`], possibly from a different thread, without referencing it directly. #[derive(Clone, Debug)] pub struct EventLoopProxy {