diff --git a/winit-core/src/event_loop.rs b/winit-core/src/event_loop.rs index 0541c33af..7e1204de5 100644 --- a/winit-core/src/event_loop.rs +++ b/winit-core/src/event_loop.rs @@ -2,6 +2,11 @@ use std::sync::atomic::{AtomicU64, Ordering}; +#[cfg(not(web_platform))] +use std::time::{Duration, Instant}; +#[cfg(web_platform)] +use web_time::{Duration, Instant}; + /// A unique identifier of the winit's async request. /// /// This could be used to identify the async request once it's done @@ -26,3 +31,47 @@ impl AsyncRequestSerial { Self { serial } } } + +/// Set through [`EventLoopWindowTarget::set_control_flow()`]. +/// +/// Indicates the desired behavior of the event loop after [`Event::AboutToWait`] is emitted. +/// +/// Defaults to [`Wait`]. +/// +/// [`Wait`]: Self::Wait +#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)] +pub enum ControlFlow { + /// When the current loop iteration finishes, immediately begin a new iteration regardless of + /// whether or not new events are available to process. + Poll, + + /// When the current loop iteration finishes, suspend the thread until another event arrives. + #[default] + Wait, + + /// When the current loop iteration finishes, suspend the thread until either another event + /// arrives or the given time is reached. + /// + /// Useful for implementing efficient timers. Applications which want to render at the display's + /// native refresh rate should instead use [`Poll`] and the VSync functionality of a graphics API + /// to reduce odds of missed frames. + /// + /// [`Poll`]: Self::Poll + WaitUntil(Instant), +} + +impl ControlFlow { + /// Creates a [`ControlFlow`] that waits until a timeout has expired. + /// + /// In most cases, this is set to [`WaitUntil`]. However, if the timeout overflows, it is + /// instead set to [`Wait`]. + /// + /// [`WaitUntil`]: Self::WaitUntil + /// [`Wait`]: Self::Wait + pub fn wait_duration(timeout: Duration) -> Self { + match Instant::now().checked_add(timeout) { + Some(instant) => Self::WaitUntil(instant), + None => Self::Wait, + } + } +} diff --git a/winit/src/event_loop.rs b/winit/src/event_loop.rs index 7cb6a5a24..4eaadc001 100644 --- a/winit/src/event_loop.rs +++ b/winit/src/event_loop.rs @@ -14,16 +14,11 @@ use std::os::unix::io::{AsFd, AsRawFd, BorrowedFd, RawFd}; use std::sync::atomic::{AtomicBool, Ordering}; use std::{error, fmt}; -#[cfg(not(web_platform))] -use std::time::{Duration, Instant}; -#[cfg(web_platform)] -use web_time::{Duration, Instant}; - use crate::error::EventLoopError; use crate::{event::Event, monitor::MonitorHandle, platform_impl}; #[doc(inline)] -pub use winit_core::event_loop::AsyncRequestSerial; +pub use winit_core::event_loop::{AsyncRequestSerial, ControlFlow}; /// Provides a way to retrieve events from the system and from the windows that were registered to /// the events loop. @@ -144,50 +139,6 @@ impl fmt::Debug for EventLoopWindowTarget { } } -/// Set through [`EventLoopWindowTarget::set_control_flow()`]. -/// -/// Indicates the desired behavior of the event loop after [`Event::AboutToWait`] is emitted. -/// -/// Defaults to [`Wait`]. -/// -/// [`Wait`]: Self::Wait -#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)] -pub enum ControlFlow { - /// When the current loop iteration finishes, immediately begin a new iteration regardless of - /// whether or not new events are available to process. - Poll, - - /// When the current loop iteration finishes, suspend the thread until another event arrives. - #[default] - Wait, - - /// When the current loop iteration finishes, suspend the thread until either another event - /// arrives or the given time is reached. - /// - /// Useful for implementing efficient timers. Applications which want to render at the display's - /// native refresh rate should instead use [`Poll`] and the VSync functionality of a graphics API - /// to reduce odds of missed frames. - /// - /// [`Poll`]: Self::Poll - WaitUntil(Instant), -} - -impl ControlFlow { - /// Creates a [`ControlFlow`] that waits until a timeout has expired. - /// - /// In most cases, this is set to [`WaitUntil`]. However, if the timeout overflows, it is - /// instead set to [`Wait`]. - /// - /// [`WaitUntil`]: Self::WaitUntil - /// [`Wait`]: Self::Wait - pub fn wait_duration(timeout: Duration) -> Self { - match Instant::now().checked_add(timeout) { - Some(instant) => Self::WaitUntil(instant), - None => Self::Wait, - } - } -} - impl EventLoop<()> { /// Create the event loop. /// diff --git a/winit/src/keyboard.rs b/winit/src/keyboard.rs deleted file mode 100644 index 7a743b2a9..000000000 --- a/winit/src/keyboard.rs +++ /dev/null @@ -1,4 +0,0 @@ -//! Types related to the keyboard. - -#[doc(inline)] -pub use winit_core::keyboard::*; diff --git a/winit/src/lib.rs b/winit/src/lib.rs index 079661a5d..3753a5b4d 100644 --- a/winit/src/lib.rs +++ b/winit/src/lib.rs @@ -165,7 +165,7 @@ pub use rwh_06 as raw_window_handle; #[doc(inline)] -pub use winit_core::dpi; +pub use winit_core::{dpi, keyboard}; #[macro_use] pub mod error; @@ -173,7 +173,6 @@ mod cursor; pub mod event; pub mod event_loop; mod icon; -pub mod keyboard; pub mod monitor; mod platform_impl; pub mod window;